You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComplexTest.cs 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Tensorflow.NumPy;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Tensorflow;
  7. using static Tensorflow.Binding;
  8. using Buffer = Tensorflow.Buffer;
  9. using TensorFlowNET.Keras.UnitTest;
  10. namespace TensorFlowNET.UnitTest.Basics
  11. {
  12. [TestClass]
  13. public class ComplexTest : EagerModeTestBase
  14. {
  15. // Tests for Complex128
  16. [TestMethod]
  17. public void complex128_basic()
  18. {
  19. double[] d_real = new double[] { 1.0, 2.0, 3.0, 4.0 };
  20. double[] d_imag = new double[] { -1.0, -3.0, 5.0, 7.0 };
  21. Tensor t_real = tf.constant(d_real, dtype:TF_DataType.TF_DOUBLE);
  22. Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_DOUBLE);
  23. Tensor t_complex = tf.complex(t_real, t_imag);
  24. Tensor t_real_result = tf.math.real(t_complex);
  25. Tensor t_imag_result = tf.math.imag(t_complex);
  26. NDArray n_real_result = t_real_result.numpy();
  27. NDArray n_imag_result = t_imag_result.numpy();
  28. double[] d_real_result =n_real_result.ToArray<double>();
  29. double[] d_imag_result = n_imag_result.ToArray<double>();
  30. Assert.IsTrue(base.Equal(d_real_result, d_real));
  31. Assert.IsTrue(base.Equal(d_imag_result, d_imag));
  32. }
  33. [TestMethod]
  34. public void complex128_abs()
  35. {
  36. tf.enable_eager_execution();
  37. double[] d_real = new double[] { -3.0, -5.0, 8.0, 7.0 };
  38. double[] d_imag = new double[] { -4.0, 12.0, -15.0, 24.0 };
  39. double[] d_abs = new double[] { 5.0, 13.0, 17.0, 25.0 };
  40. Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_DOUBLE);
  41. Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_DOUBLE);
  42. Tensor t_complex = tf.complex(t_real, t_imag);
  43. Tensor t_abs_result = tf.abs(t_complex);
  44. double[] d_abs_result = t_abs_result.numpy().ToArray<double>();
  45. Assert.IsTrue(base.Equal(d_abs_result, d_abs));
  46. }
  47. [TestMethod]
  48. public void complex128_conj()
  49. {
  50. double[] d_real = new double[] { -3.0, -5.0, 8.0, 7.0 };
  51. double[] d_imag = new double[] { -4.0, 12.0, -15.0, 24.0 };
  52. double[] d_real_expected = new double[] { -3.0, -5.0, 8.0, 7.0 };
  53. double[] d_imag_expected = new double[] { 4.0, -12.0, 15.0, -24.0 };
  54. Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_DOUBLE);
  55. Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_DOUBLE);
  56. Tensor t_complex = tf.complex(t_real, t_imag, TF_DataType.TF_COMPLEX128);
  57. Tensor t_result = tf.math.conj(t_complex);
  58. NDArray n_real_result = tf.math.real(t_result).numpy();
  59. NDArray n_imag_result = tf.math.imag(t_result).numpy();
  60. double[] d_real_result = n_real_result.ToArray<double>();
  61. double[] d_imag_result = n_imag_result.ToArray<double>();
  62. Assert.IsTrue(base.Equal(d_real_result, d_real_expected));
  63. Assert.IsTrue(base.Equal(d_imag_result, d_imag_expected));
  64. }
  65. [TestMethod]
  66. public void complex128_angle()
  67. {
  68. double[] d_real = new double[] { 0.0, 1.0, -1.0, 0.0 };
  69. double[] d_imag = new double[] { 1.0, 0.0, -2.0, -3.0 };
  70. double[] d_expected = new double[] { 1.5707963267948966, 0, -2.0344439357957027, -1.5707963267948966 };
  71. Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_DOUBLE);
  72. Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_DOUBLE);
  73. Tensor t_complex = tf.complex(t_real, t_imag, TF_DataType.TF_COMPLEX128);
  74. Tensor t_result = tf.math.angle(t_complex);
  75. NDArray n_result = t_result.numpy();
  76. double[] d_result = n_result.ToArray<double>();
  77. Assert.IsTrue(base.Equal(d_result, d_expected));
  78. }
  79. // Tests for Complex64
  80. [TestMethod]
  81. public void complex64_basic()
  82. {
  83. tf.init_scope();
  84. float[] d_real = new float[] { 1.0f, 2.0f, 3.0f, 4.0f };
  85. float[] d_imag = new float[] { -1.0f, -3.0f, 5.0f, 7.0f };
  86. Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_FLOAT);
  87. Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_FLOAT);
  88. Tensor t_complex = tf.complex(t_real, t_imag);
  89. Tensor t_real_result = tf.math.real(t_complex);
  90. Tensor t_imag_result = tf.math.imag(t_complex);
  91. // Convert the EagerTensors to NumPy arrays directly
  92. float[] d_real_result = t_real_result.numpy().ToArray<float>();
  93. float[] d_imag_result = t_imag_result.numpy().ToArray<float>();
  94. Assert.IsTrue(base.Equal(d_real_result, d_real));
  95. Assert.IsTrue(base.Equal(d_imag_result, d_imag));
  96. }
  97. [TestMethod]
  98. public void complex64_abs()
  99. {
  100. tf.enable_eager_execution();
  101. float[] d_real = new float[] { -3.0f, -5.0f, 8.0f, 7.0f };
  102. float[] d_imag = new float[] { -4.0f, 12.0f, -15.0f, 24.0f };
  103. float[] d_abs = new float[] { 5.0f, 13.0f, 17.0f, 25.0f };
  104. Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_FLOAT);
  105. Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_FLOAT);
  106. Tensor t_complex = tf.complex(t_real, t_imag, TF_DataType.TF_COMPLEX64);
  107. Tensor t_abs_result = tf.abs(t_complex);
  108. NDArray n_abs_result = t_abs_result.numpy();
  109. float[] d_abs_result = n_abs_result.ToArray<float>();
  110. Assert.IsTrue(base.Equal(d_abs_result, d_abs));
  111. }
  112. [TestMethod]
  113. public void complex64_conj()
  114. {
  115. float[] d_real = new float[] { -3.0f, -5.0f, 8.0f, 7.0f };
  116. float[] d_imag = new float[] { -4.0f, 12.0f, -15.0f, 24.0f };
  117. float[] d_real_expected = new float[] { -3.0f, -5.0f, 8.0f, 7.0f };
  118. float[] d_imag_expected = new float[] { 4.0f, -12.0f, 15.0f, -24.0f };
  119. Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_FLOAT);
  120. Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_FLOAT);
  121. Tensor t_complex = tf.complex(t_real, t_imag, TF_DataType.TF_COMPLEX64);
  122. Tensor t_result = tf.math.conj(t_complex);
  123. NDArray n_real_result = tf.math.real(t_result).numpy();
  124. NDArray n_imag_result = tf.math.imag(t_result).numpy();
  125. float[] d_real_result = n_real_result.ToArray<float>();
  126. float[] d_imag_result = n_imag_result.ToArray<float>();
  127. Assert.IsTrue(base.Equal(d_real_result, d_real_expected));
  128. Assert.IsTrue(base.Equal(d_imag_result, d_imag_expected));
  129. }
  130. [TestMethod]
  131. public void complex64_angle()
  132. {
  133. float[] d_real = new float[] { 0.0f, 1.0f, -1.0f, 0.0f };
  134. float[] d_imag = new float[] { 1.0f, 0.0f, -2.0f, -3.0f };
  135. float[] d_expected = new float[] { 1.5707964f, 0f, -2.0344439f, -1.5707964f };
  136. Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_FLOAT);
  137. Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_FLOAT);
  138. Tensor t_complex = tf.complex(t_real, t_imag, TF_DataType.TF_COMPLEX64);
  139. Tensor t_result = tf.math.angle(t_complex);
  140. NDArray n_result = t_result.numpy();
  141. float[] d_result = n_result.ToArray<float>();
  142. Assert.IsTrue(base.Equal(d_result, d_expected));
  143. }
  144. }
  145. }