1 /* -*- mode: C -*-  */
2 /*
3    IGraph library.
4    Copyright (C) 2010-2012  Gabor Csardi <csardi.gabor@gmail.com>
5    334 Harvard street, Cambridge, MA 02139 USA
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301 USA
21 
22 */
23 
24 #include <igraph.h>
25 /* This definition ensures math symbols are also available when
26  * compiling with MSVC.
27  */
28 #define _USE_MATH_DEFINES
29 #include <math.h>
30 
31 #include "test_utilities.inc"
32 
33 #define ARE 4
34 #define AIM 5
35 #define BRE 6
36 #define BIM 2
37 
main()38 int main() {
39 
40     igraph_complex_t a = igraph_complex(ARE, AIM);
41     igraph_complex_t b = igraph_complex(BRE, BIM);
42     igraph_complex_t c, d, e;
43 
44     /* polar, mod, arg */
45     c = igraph_complex_polar(igraph_complex_mod(a), igraph_complex_arg(a));
46     IGRAPH_ASSERT(igraph_complex_eq_tol(a, c, 1e-14));
47 
48     /* add */
49     c = igraph_complex_add(a, b);
50     IGRAPH_ASSERT(IGRAPH_REAL(c) == ARE + BRE && IGRAPH_IMAG(c) == AIM + BIM);
51 
52     /* sub */
53     c = igraph_complex_sub(a, b);
54     IGRAPH_ASSERT(IGRAPH_REAL(c) == ARE - BRE && IGRAPH_IMAG(c) == AIM - BIM);
55 
56     /* mul */
57     c = igraph_complex_mul(a, b);
58     IGRAPH_ASSERT(IGRAPH_REAL(c) == ARE * BRE - AIM * BIM);
59     IGRAPH_ASSERT(IGRAPH_IMAG(c) == ARE * BIM + AIM * BRE);
60 
61     /* div */
62     c = igraph_complex_div(a, b);
63     c = igraph_complex_mul(c, b);
64     IGRAPH_ASSERT(igraph_complex_eq_tol(a, c, 1e-14));
65 
66     /* add_real */
67     c = igraph_complex_add_real(a, IGRAPH_REAL(b));
68     IGRAPH_ASSERT(IGRAPH_REAL(c) == IGRAPH_REAL(a) + IGRAPH_REAL(b));
69     IGRAPH_ASSERT(IGRAPH_IMAG(c) == IGRAPH_IMAG(a));
70 
71     /* add_imag */
72     c = igraph_complex_add_imag(a, IGRAPH_IMAG(b));
73     IGRAPH_ASSERT(IGRAPH_REAL(c) == IGRAPH_REAL(a));
74     IGRAPH_ASSERT(IGRAPH_IMAG(c) == IGRAPH_IMAG(a) + IGRAPH_IMAG(b));
75 
76     /* sub_real */
77     c = igraph_complex_sub_real(a, IGRAPH_REAL(b));
78     IGRAPH_ASSERT(IGRAPH_REAL(c) == IGRAPH_REAL(a) - IGRAPH_REAL(b));
79     IGRAPH_ASSERT(IGRAPH_IMAG(c) == IGRAPH_IMAG(a));
80 
81     /* sub_imag */
82     c = igraph_complex_sub_imag(a, IGRAPH_IMAG(b));
83     IGRAPH_ASSERT(IGRAPH_REAL(c) == IGRAPH_REAL(a));
84     IGRAPH_ASSERT(IGRAPH_IMAG(c) == IGRAPH_IMAG(a) - IGRAPH_IMAG(b));
85 
86     /* mul_real */
87     c = igraph_complex_mul_real(a, IGRAPH_REAL(b));
88     IGRAPH_ASSERT(IGRAPH_REAL(c) == IGRAPH_REAL(a) * IGRAPH_REAL(b));
89     IGRAPH_ASSERT(IGRAPH_IMAG(c) == IGRAPH_IMAG(a) * IGRAPH_REAL(b));
90 
91     /* mul_imag */
92     c = igraph_complex_mul_imag(a, IGRAPH_REAL(b));
93     IGRAPH_ASSERT(IGRAPH_REAL(c) == - IGRAPH_IMAG(a) * IGRAPH_REAL(b));
94     IGRAPH_ASSERT(IGRAPH_IMAG(c) == IGRAPH_REAL(a) * IGRAPH_REAL(b));
95 
96     /* div_real */
97     c = igraph_complex_div_real(a, IGRAPH_REAL(b));
98     IGRAPH_ASSERT(fabs(IGRAPH_REAL(c) - IGRAPH_REAL(a) / IGRAPH_REAL(b)) < 1e-15);
99     IGRAPH_ASSERT(fabs(IGRAPH_IMAG(c) - IGRAPH_IMAG(a) / IGRAPH_REAL(b)) < 1e-15);
100 
101     /* div_imag */
102     c = igraph_complex_div_imag(a, IGRAPH_IMAG(b));
103     IGRAPH_ASSERT(IGRAPH_REAL(c) == IGRAPH_IMAG(a) / IGRAPH_IMAG(b));
104     IGRAPH_ASSERT(IGRAPH_IMAG(c) == - IGRAPH_REAL(a) / IGRAPH_IMAG(b));
105 
106     /* conj */
107     c = igraph_complex_conj(a);
108     IGRAPH_ASSERT(IGRAPH_REAL(c) == ARE && IGRAPH_IMAG(c) == -AIM);
109 
110     /* neg */
111     c = igraph_complex_neg(a);
112     IGRAPH_ASSERT(IGRAPH_REAL(c) == - IGRAPH_REAL(a));
113     IGRAPH_ASSERT(IGRAPH_IMAG(c) == - IGRAPH_IMAG(a));
114 
115     /* inv */
116     c = igraph_complex_inv(a);
117     d = igraph_complex(1.0, 0.0);
118     e = igraph_complex_div(d, a);
119     IGRAPH_ASSERT(igraph_complex_eq_tol(c, e, 1e-14));
120 
121     /* abs */
122     IGRAPH_ASSERT(igraph_complex_abs(a) == igraph_complex_mod(a));
123 
124     /* logabs */
125 
126     /* sqrt */
127     c = igraph_complex_sqrt(a);
128     d = igraph_complex_mul(c, c);
129     IGRAPH_ASSERT(igraph_complex_eq_tol(a, d, 1e-14));
130 
131     /* sqrt_real */
132     c = igraph_complex_sqrt(igraph_complex(-1.0, 0.0));
133     d = igraph_complex_sqrt_real(-1.0);
134     IGRAPH_ASSERT(igraph_complex_eq_tol(c, d, 1e-14));
135 
136     /* exp */
137     c = igraph_complex_exp(igraph_complex(0.0, M_PI));
138     IGRAPH_ASSERT(igraph_complex_eq_tol(c, igraph_complex(-1.0, 0.0), 1e-14));
139 
140     /* pow */
141     c = igraph_complex_pow(igraph_complex(M_E, 0.0), igraph_complex(0.0, M_PI));
142     IGRAPH_ASSERT(igraph_complex_eq_tol(c, igraph_complex(-1.0, 0.0), 1e-14));
143 
144     /* pow_real */
145     c = igraph_complex_pow_real(a, 2.0);
146     d = igraph_complex_mul(a, a);
147     IGRAPH_ASSERT(igraph_complex_eq_tol(c, d, 1e-12));
148 
149     /* log */
150     c = igraph_complex_exp(igraph_complex_log(a));
151     IGRAPH_ASSERT(igraph_complex_eq_tol(a, c, 1e-14));
152 
153     /* log10 */
154     c = igraph_complex_pow(igraph_complex(10.0, 0), igraph_complex_log10(a));
155     IGRAPH_ASSERT(igraph_complex_eq_tol(a, c, 1e-14));
156 
157     /* log_b */
158     c = igraph_complex_pow(b, igraph_complex_log_b(a, b));
159     IGRAPH_ASSERT(igraph_complex_eq_tol(a, c, 1e-14));
160 
161     /* sin, cos */
162     c = igraph_complex_sin(a);
163     d = igraph_complex_cos(a);
164     e = igraph_complex_add(igraph_complex_mul(c, c), igraph_complex_mul(d, d));
165     IGRAPH_ASSERT(igraph_complex_eq_tol(e, igraph_complex(1.0, 0.0), 1e-11));
166 
167     /* tan */
168     c = igraph_complex_tan(a);
169     d = igraph_complex_div(igraph_complex_sin(a), igraph_complex_cos(a));
170     IGRAPH_ASSERT(igraph_complex_eq_tol(c, d, 1e-14));
171 
172     /* sec */
173     c = igraph_complex_sec(a);
174     d = igraph_complex_inv(igraph_complex_cos(a));
175     IGRAPH_ASSERT(igraph_complex_eq_tol(c, d, 1e-14));
176 
177     /* csc */
178     c = igraph_complex_csc(a);
179     d = igraph_complex_inv(igraph_complex_sin(a));
180     IGRAPH_ASSERT(igraph_complex_eq_tol(c, d, 1e-14));
181 
182     /* cot */
183     c = igraph_complex_tan(a);
184     d = igraph_complex_div(igraph_complex_sin(a), igraph_complex_cos(a));
185     IGRAPH_ASSERT(igraph_complex_eq_tol(d, c, 1e-14));
186 
187     VERIFY_FINALLY_STACK();
188     return 0;
189 }
190