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

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