File: igraph_complex.c

package info (click to toggle)
igraph 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,180 kB
  • ctags: 16,601
  • sloc: ansic: 188,037; sh: 26,731; cpp: 18,275; yacc: 1,164; makefile: 959; lex: 484; xml: 378
file content (183 lines) | stat: -rw-r--r-- 5,573 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* -*- mode: C -*-  */
/* 
   IGraph library.
   Copyright (C) 2010-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard street, Cambridge, MA 02139 USA
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
   02110-1301 USA

*/

#include <igraph.h>
#include <math.h>

#define ARE 4
#define AIM 5
#define BRE 6
#define BIM 2

int main() {

  igraph_complex_t a = igraph_complex(ARE, AIM);
  igraph_complex_t b = igraph_complex(BRE, BIM);
  igraph_complex_t c, d, e;

  /* polar, mod, arg */
  c=igraph_complex_polar(igraph_complex_mod(a), igraph_complex_arg(a));
  if (!igraph_complex_eq_tol(a,c,1e-14)) { return 1; }
  
  /* add */
  c=igraph_complex_add(a,b);
  if (IGRAPH_REAL(c) != ARE+BRE || IGRAPH_IMAG(c) != AIM+BIM) { return 2; }
  
  /* sub */
  c=igraph_complex_sub(a,b);
  if (IGRAPH_REAL(c) != ARE-BRE || IGRAPH_IMAG(c) != AIM-BIM) { return 3; }

  /* mul */
  c=igraph_complex_mul(a,b);
  if (IGRAPH_REAL(c) != ARE*BRE-AIM*BIM || 
      IGRAPH_IMAG(c) != ARE*BIM+AIM*BRE) { return 4; }
  
  /* div */
  c=igraph_complex_div(a,b);
  c=igraph_complex_mul(c,b);
  if (!igraph_complex_eq_tol(a,c,1e-14)) { return 5; }

  /* add_real */
  c=igraph_complex_add_real(a,IGRAPH_REAL(b));
  if (IGRAPH_REAL(c) != IGRAPH_REAL(a) + IGRAPH_REAL(b)) { return 6; }
  if (IGRAPH_IMAG(c) != IGRAPH_IMAG(a)) { return 7; }

  /* add_imag */
  c=igraph_complex_add_imag(a,IGRAPH_IMAG(b));
  if (IGRAPH_REAL(c) != IGRAPH_REAL(a)) { return 8; }
  if (IGRAPH_IMAG(c) != IGRAPH_IMAG(a) + IGRAPH_IMAG(b)) { return 9; }

  /* sub_real */
  c=igraph_complex_sub_real(a,IGRAPH_REAL(b));
  if (IGRAPH_REAL(c) != IGRAPH_REAL(a) - IGRAPH_REAL(b)) { return 10; }
  if (IGRAPH_IMAG(c) != IGRAPH_IMAG(a)) { return 11; }  

  /* sub_imag */
  c=igraph_complex_sub_imag(a,IGRAPH_IMAG(b));
  if (IGRAPH_REAL(c) != IGRAPH_REAL(a)) { return 12; }
  if (IGRAPH_IMAG(c) != IGRAPH_IMAG(a) - IGRAPH_IMAG(b)) { return 13; }

  /* mul_real */
  c=igraph_complex_mul_real(a, IGRAPH_REAL(b));
  if (IGRAPH_REAL(c) != IGRAPH_REAL(a) * IGRAPH_REAL(b)) { return 14; }
  if (IGRAPH_IMAG(c) != IGRAPH_IMAG(a) * IGRAPH_REAL(b)) { return 15; }

  /* mul_imag */
  c=igraph_complex_mul_imag(a, IGRAPH_REAL(b));
  if (IGRAPH_REAL(c) != - IGRAPH_IMAG(a) * IGRAPH_REAL(b)) { return 14; }
  if (IGRAPH_IMAG(c) != IGRAPH_REAL(a) * IGRAPH_REAL(b)) { return 15; }  

  /* div_real */
  c=igraph_complex_div_real(a, IGRAPH_REAL(b));
  if (fabs(IGRAPH_REAL(c) - IGRAPH_REAL(a) / IGRAPH_REAL(b)) > 1e-15) { 
    return 16; 
  }
  if (fabs(IGRAPH_IMAG(c) - IGRAPH_IMAG(a) / IGRAPH_REAL(b)) > 1e-15) { 
    return 17; 
  }

  /* div_imag */
  c=igraph_complex_div_imag(a, IGRAPH_IMAG(b));
  if (IGRAPH_REAL(c) != IGRAPH_IMAG(a) / IGRAPH_IMAG(b)) { return 18; }
  if (IGRAPH_IMAG(c) != - IGRAPH_REAL(a) / IGRAPH_IMAG(b)) { return 19; }

  /* conj */
  c=igraph_complex_conj(a);
  if (IGRAPH_REAL(c) != ARE || IGRAPH_IMAG(c) != -AIM) { return 20; }

  /* neg */
  c=igraph_complex_neg(a);
  if (IGRAPH_REAL(c) != - IGRAPH_REAL(a) ||
      IGRAPH_IMAG(c) != - IGRAPH_IMAG(a)) { return 21; }

  /* inv */
  c=igraph_complex_inv(a);
  d=igraph_complex(1.0, 0.0);
  e=igraph_complex_div(d, a);
  if (!igraph_complex_eq_tol(c, e, 1e-14)) { return 22; }

  /* abs */
  if (igraph_complex_abs(a) != igraph_complex_mod(a)) { return 23; }

  /* logabs */

  /* sqrt */
  c=igraph_complex_sqrt(a);
  d=igraph_complex_mul(c,c);
  if (!igraph_complex_eq_tol(a, d, 1e-14)) { return 24; }

  /* sqrt_real */
  c=igraph_complex_sqrt(igraph_complex(-1.0, 0.0));
  d=igraph_complex_sqrt_real(-1.0);
  if (!igraph_complex_eq_tol(c, d, 1e-14)) { return 25; }

  /* exp */
  c=igraph_complex_exp(igraph_complex(0.0, M_PI));
  if (!igraph_complex_eq_tol(c, igraph_complex(-1.0, 0.0), 1e-14)) { 
    return 26;
  }

  /* pow */  
  c=igraph_complex_pow(igraph_complex(M_E, 0.0), igraph_complex(0.0, M_PI));
  if (!igraph_complex_eq_tol(c, igraph_complex(-1.0, 0.0), 1e-14)) { 
    return 27;
  }

  /* pow_real */
  c=igraph_complex_pow_real(a, 2.0);
  d=igraph_complex_mul(a,a);
  if (!igraph_complex_eq_tol(c, d, 1e-12)) { return 28; }  

  /* log */
  c=igraph_complex_exp(igraph_complex_log(a));
  if (!igraph_complex_eq_tol(a, c, 1e-14)) { return 29; }

  /* log10 */
  c=igraph_complex_pow(igraph_complex(10.0, 0), igraph_complex_log10(a));
  if (!igraph_complex_eq_tol(a, c, 1e-14)) { return 30; }
  
  /* log_b */
  c=igraph_complex_pow(b, igraph_complex_log_b(a, b));
  if (!igraph_complex_eq_tol(a, c, 1e-14)) { return 31; }

  /* sin, cos */
  c=igraph_complex_sin(a);
  d=igraph_complex_cos(a);
  e=igraph_complex_add(igraph_complex_mul(c,c), igraph_complex_mul(d,d));
  if (!igraph_complex_eq_tol(e, igraph_complex(1.0, 0.0), 1e-11)) { 
    return 32;
  }

  /* tan */
  c=igraph_complex_tan(a);
  d=igraph_complex_div(igraph_complex_sin(a), igraph_complex_cos(a));
  if (!igraph_complex_eq_tol(c, d, 1e-14)) { return 33; }

  /* sec */

  /* csc */

  /* cot */

  return 0;
}