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 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. [Ignore("Not working")]
  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, TF_DataType.TF_COMPLEX128);
  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.AreEqual(d_real_result, d_real);
  31. Assert.AreEqual(d_imag_result, d_imag);
  32. }
  33. [TestMethod]
  34. public void complex64_basic()
  35. {
  36. tf.init_scope();
  37. float[] d_real = new float[] { 1.0f, 2.0f, 3.0f, 4.0f };
  38. float[] d_imag = new float[] { -1.0f, -3.0f, 5.0f, 7.0f };
  39. Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_FLOAT);
  40. Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_FLOAT);
  41. Tensor t_complex = tf.complex(t_real, t_imag, TF_DataType.TF_COMPLEX64);
  42. Tensor t_real_result = tf.math.real(t_complex);
  43. Tensor t_imag_result = tf.math.imag(t_complex);
  44. // Convert the EagerTensors to NumPy arrays directly
  45. float[] d_real_result = t_real_result.numpy().ToArray<float>();
  46. float[] d_imag_result = t_imag_result.numpy().ToArray<float>();
  47. Assert.IsTrue(base.Equal(d_real_result, d_real));
  48. Assert.IsTrue(base.Equal(d_imag_result, d_imag));
  49. }
  50. [TestMethod]
  51. public void complex64_abs()
  52. {
  53. tf.enable_eager_execution();
  54. float[] d_real = new float[] { -3.0f, -5.0f, 8.0f, 7.0f };
  55. float[] d_imag = new float[] { -4.0f, 12.0f, -15.0f, 24.0f };
  56. float[] d_abs = new float[] { 5.0f, 13.0f, 17.0f, 25.0f };
  57. Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_FLOAT);
  58. Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_FLOAT);
  59. Tensor t_complex = tf.complex(t_real, t_imag, TF_DataType.TF_COMPLEX64);
  60. Tensor t_abs_result = tf.abs(t_complex);
  61. NDArray n_abs_result = t_abs_result.numpy();
  62. float[] d_abs_result = n_abs_result.ToArray<float>();
  63. Assert.IsTrue(base.Equal(d_abs_result, d_abs));
  64. }
  65. [TestMethod]
  66. public void complex64_conj()
  67. {
  68. float[] d_real = new float[] { -3.0f, -5.0f, 8.0f, 7.0f };
  69. float[] d_imag = new float[] { -4.0f, 12.0f, -15.0f, 24.0f };
  70. float[] d_real_expected = new float[] { -3.0f, -5.0f, 8.0f, 7.0f };
  71. float[] d_imag_expected = new float[] { 4.0f, -12.0f, 15.0f, -24.0f };
  72. Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_FLOAT);
  73. Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_FLOAT);
  74. Tensor t_complex = tf.complex(t_real, t_imag, TF_DataType.TF_COMPLEX64);
  75. Tensor t_result = tf.math.conj(t_complex);
  76. NDArray n_real_result = tf.math.real(t_result).numpy();
  77. NDArray n_imag_result = tf.math.imag(t_result).numpy();
  78. float[] d_real_result = n_real_result.ToArray<float>();
  79. float[] d_imag_result = n_imag_result.ToArray<float>();
  80. Assert.IsTrue(base.Equal(d_real_result, d_real_expected));
  81. Assert.IsTrue(base.Equal(d_imag_result, d_imag_expected));
  82. }
  83. [TestMethod]
  84. public void complex64_angle()
  85. {
  86. float[] d_real = new float[] { 0.0f, 1.0f, -1.0f, 0.0f };
  87. float[] d_imag = new float[] { 1.0f, 0.0f, -2.0f, -3.0f };
  88. float[] d_expected = new float[] { 1.5707964f, 0f, -2.0344439f, -1.5707964f };
  89. Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_FLOAT);
  90. Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_FLOAT);
  91. Tensor t_complex = tf.complex(t_real, t_imag, TF_DataType.TF_COMPLEX64);
  92. Tensor t_result = tf.math.angle(t_complex);
  93. NDArray n_result = t_result.numpy();
  94. float[] d_result = n_result.ToArray<float>();
  95. Assert.IsTrue(base.Equal(d_result, d_expected));
  96. }
  97. }
  98. }