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+
11+ namespace TensorFlowNET . UnitTest . Basics
12+ {
13+ [ TestClass ]
14+ public class ComplexTest : EagerModeTestBase
15+ {
16+ // Tests for Complex128
17+
18+ [ TestMethod ]
19+ public void complex128_basic ( )
20+ {
21+ double [ ] d_real = new double [ ] { 1.0 , 2.0 , 3.0 , 4.0 } ;
22+ double [ ] d_imag = new double [ ] { - 1.0 , - 3.0 , 5.0 , 7.0 } ;
23+
24+ Tensor t_real = tf . constant ( d_real , dtype : TF_DataType . TF_DOUBLE ) ;
25+ Tensor t_imag = tf . constant ( d_imag , dtype : TF_DataType . TF_DOUBLE ) ;
26+
27+ Tensor t_complex = tf . complex ( t_real , t_imag ) ;
28+
29+ Tensor t_real_result = tf . math . real ( t_complex ) ;
30+ Tensor t_imag_result = tf . math . imag ( t_complex ) ;
31+
32+ NDArray n_real_result = t_real_result . numpy ( ) ;
33+ NDArray n_imag_result = t_imag_result . numpy ( ) ;
34+
35+ double [ ] d_real_result = n_real_result . ToArray < double > ( ) ;
36+ double [ ] d_imag_result = n_imag_result . ToArray < double > ( ) ;
37+
38+ Assert . IsTrue ( base . Equal ( d_real_result , d_real ) ) ;
39+ Assert . IsTrue ( base . Equal ( d_imag_result , d_imag ) ) ;
40+ }
41+ [ TestMethod ]
42+ public void complex128_abs ( )
43+ {
44+ tf . enable_eager_execution ( ) ;
45+
46+ double [ ] d_real = new double [ ] { - 3.0 , - 5.0 , 8.0 , 7.0 } ;
47+ double [ ] d_imag = new double [ ] { - 4.0 , 12.0 , - 15.0 , 24.0 } ;
48+
49+ double [ ] d_abs = new double [ ] { 5.0 , 13.0 , 17.0 , 25.0 } ;
50+
51+ Tensor t_real = tf . constant ( d_real , dtype : TF_DataType . TF_DOUBLE ) ;
52+ Tensor t_imag = tf . constant ( d_imag , dtype : TF_DataType . TF_DOUBLE ) ;
53+
54+ Tensor t_complex = tf . complex ( t_real , t_imag ) ;
55+
56+ Tensor t_abs_result = tf . abs ( t_complex ) ;
57+
58+ double [ ] d_abs_result = t_abs_result . numpy ( ) . ToArray < double > ( ) ;
59+ Assert . IsTrue ( base . Equal ( d_abs_result , d_abs ) ) ;
60+ }
61+ [ TestMethod ]
62+ public void complex128_conj ( )
63+ {
64+ double [ ] d_real = new double [ ] { - 3.0 , - 5.0 , 8.0 , 7.0 } ;
65+ double [ ] d_imag = new double [ ] { - 4.0 , 12.0 , - 15.0 , 24.0 } ;
66+
67+ double [ ] d_real_expected = new double [ ] { - 3.0 , - 5.0 , 8.0 , 7.0 } ;
68+ double [ ] d_imag_expected = new double [ ] { 4.0 , - 12.0 , 15.0 , - 24.0 } ;
69+
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+
73+ Tensor t_complex = tf . complex ( t_real , t_imag , TF_DataType . TF_COMPLEX128 ) ;
74+
75+ Tensor t_result = tf . math . conj ( t_complex ) ;
76+
77+ NDArray n_real_result = tf . math . real ( t_result ) . numpy ( ) ;
78+ NDArray n_imag_result = tf . math . imag ( t_result ) . numpy ( ) ;
79+
80+ double [ ] d_real_result = n_real_result . ToArray < double > ( ) ;
81+ double [ ] d_imag_result = n_imag_result . ToArray < double > ( ) ;
82+
83+ Assert . IsTrue ( base . Equal ( d_real_result , d_real_expected ) ) ;
84+ Assert . IsTrue ( base . Equal ( d_imag_result , d_imag_expected ) ) ;
85+ }
86+ [ TestMethod ]
87+ public void complex128_angle ( )
88+ {
89+ double [ ] d_real = new double [ ] { 0.0 , 1.0 , - 1.0 , 0.0 } ;
90+ double [ ] d_imag = new double [ ] { 1.0 , 0.0 , - 2.0 , - 3.0 } ;
91+
92+ double [ ] d_expected = new double [ ] { 1.5707963267948966 , 0 , - 2.0344439357957027 , - 1.5707963267948966 } ;
93+
94+ Tensor t_real = tf . constant ( d_real , dtype : TF_DataType . TF_DOUBLE ) ;
95+ Tensor t_imag = tf . constant ( d_imag , dtype : TF_DataType . TF_DOUBLE ) ;
96+
97+ Tensor t_complex = tf . complex ( t_real , t_imag , TF_DataType . TF_COMPLEX128 ) ;
98+
99+ Tensor t_result = tf . math . angle ( t_complex ) ;
100+
101+ NDArray n_result = t_result . numpy ( ) ;
102+
103+ double [ ] d_result = n_result . ToArray < double > ( ) ;
104+
105+ Assert . IsTrue ( base . Equal ( d_result , d_expected ) ) ;
106+ }
107+
108+ // Tests for Complex64
109+ [ TestMethod ]
110+ public void complex64_basic ( )
111+ {
112+ tf . init_scope ( ) ;
113+ float [ ] d_real = new float [ ] { 1.0f , 2.0f , 3.0f , 4.0f } ;
114+ float [ ] d_imag = new float [ ] { - 1.0f , - 3.0f , 5.0f , 7.0f } ;
115+
116+ Tensor t_real = tf . constant ( d_real , dtype : TF_DataType . TF_FLOAT ) ;
117+ Tensor t_imag = tf . constant ( d_imag , dtype : TF_DataType . TF_FLOAT ) ;
118+
119+ Tensor t_complex = tf . complex ( t_real , t_imag ) ;
120+
121+ Tensor t_real_result = tf . math . real ( t_complex ) ;
122+ Tensor t_imag_result = tf . math . imag ( t_complex ) ;
123+
124+ // Convert the EagerTensors to NumPy arrays directly
125+ float [ ] d_real_result = t_real_result . numpy ( ) . ToArray < float > ( ) ;
126+ float [ ] d_imag_result = t_imag_result . numpy ( ) . ToArray < float > ( ) ;
127+
128+ Assert . IsTrue ( base . Equal ( d_real_result , d_real ) ) ;
129+ Assert . IsTrue ( base . Equal ( d_imag_result , d_imag ) ) ;
130+ }
131+ [ TestMethod ]
132+ public void complex64_abs ( )
133+ {
134+ tf . enable_eager_execution ( ) ;
135+
136+ float [ ] d_real = new float [ ] { - 3.0f , - 5.0f , 8.0f , 7.0f } ;
137+ float [ ] d_imag = new float [ ] { - 4.0f , 12.0f , - 15.0f , 24.0f } ;
138+
139+ float [ ] d_abs = new float [ ] { 5.0f , 13.0f , 17.0f , 25.0f } ;
140+
141+ Tensor t_real = tf . constant ( d_real , dtype : TF_DataType . TF_FLOAT ) ;
142+ Tensor t_imag = tf . constant ( d_imag , dtype : TF_DataType . TF_FLOAT ) ;
143+
144+ Tensor t_complex = tf . complex ( t_real , t_imag , TF_DataType . TF_COMPLEX64 ) ;
145+
146+ Tensor t_abs_result = tf . abs ( t_complex ) ;
147+
148+ NDArray n_abs_result = t_abs_result . numpy ( ) ;
149+
150+ float [ ] d_abs_result = n_abs_result . ToArray < float > ( ) ;
151+ Assert . IsTrue ( base . Equal ( d_abs_result , d_abs ) ) ;
152+
153+ }
154+ [ TestMethod ]
155+ public void complex64_conj ( )
156+ {
157+ float [ ] d_real = new float [ ] { - 3.0f , - 5.0f , 8.0f , 7.0f } ;
158+ float [ ] d_imag = new float [ ] { - 4.0f , 12.0f , - 15.0f , 24.0f } ;
159+
160+ float [ ] d_real_expected = new float [ ] { - 3.0f , - 5.0f , 8.0f , 7.0f } ;
161+ float [ ] d_imag_expected = new float [ ] { 4.0f , - 12.0f , 15.0f , - 24.0f } ;
162+
163+ Tensor t_real = tf . constant ( d_real , dtype : TF_DataType . TF_FLOAT ) ;
164+ Tensor t_imag = tf . constant ( d_imag , dtype : TF_DataType . TF_FLOAT ) ;
165+
166+ Tensor t_complex = tf . complex ( t_real , t_imag , TF_DataType . TF_COMPLEX64 ) ;
167+
168+ Tensor t_result = tf . math . conj ( t_complex ) ;
169+
170+ NDArray n_real_result = tf . math . real ( t_result ) . numpy ( ) ;
171+ NDArray n_imag_result = tf . math . imag ( t_result ) . numpy ( ) ;
172+
173+ float [ ] d_real_result = n_real_result . ToArray < float > ( ) ;
174+ float [ ] d_imag_result = n_imag_result . ToArray < float > ( ) ;
175+
176+ Assert . IsTrue ( base . Equal ( d_real_result , d_real_expected ) ) ;
177+ Assert . IsTrue ( base . Equal ( d_imag_result , d_imag_expected ) ) ;
178+
179+ }
180+ [ TestMethod ]
181+ public void complex64_angle ( )
182+ {
183+ float [ ] d_real = new float [ ] { 0.0f , 1.0f , - 1.0f , 0.0f } ;
184+ float [ ] d_imag = new float [ ] { 1.0f , 0.0f , - 2.0f , - 3.0f } ;
185+
186+ float [ ] d_expected = new float [ ] { 1.5707964f , 0f , - 2.0344439f , - 1.5707964f } ;
187+
188+ Tensor t_real = tf . constant ( d_real , dtype : TF_DataType . TF_FLOAT ) ;
189+ Tensor t_imag = tf . constant ( d_imag , dtype : TF_DataType . TF_FLOAT ) ;
190+
191+ Tensor t_complex = tf . complex ( t_real , t_imag , TF_DataType . TF_COMPLEX64 ) ;
192+
193+ Tensor t_result = tf . math . angle ( t_complex ) ;
194+
195+ NDArray n_result = t_result . numpy ( ) ;
196+
197+ float [ ] d_result = n_result . ToArray < float > ( ) ;
198+
199+ Assert . IsTrue ( base . Equal ( d_result , d_expected ) ) ;
200+ }
201+ }
202+ }
0 commit comments