forked from ITA-Solar/rh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.c
173 lines (114 loc) · 4.01 KB
/
complex.c
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
/* ------- file: -------------------------- complex.c ---------------
Version: rh2.0
Author: Han Uitenbroek ([email protected])
Last modified: Thu Dec 13 11:16:58 2001 --
-------------------------- ----------RH-- */
/* --- Complex operations needed in Voigt generator and FFT -- ------ */
#include <stdlib.h>
#include <math.h>
#include "rh.h"
#include "complex.h"
/* --- Function prototypes -- -------------- */
/* --- Global variables -- -------------- */
/* ------- begin -------------------------- complex.c --------------- */
complex cmplx(double a, double b)
{
complex c;
/* --- Return complex number z = a + bi -- ------------ */
c.r = a;
c.i = b;
return c;
}
/* ------- end ---------------------------- complex.c --------------- */
/* ------- begin -------------------------- cmplx_conj.c ------------ */
complex cmplx_conj(complex a)
{
complex c;
/* --- Return complex conjugate -- ------------ */
c.r = a.r;
c.i = -a.i;
return c;
}
/* ------- end ---------------------------- cmplx_conj.c ------------ */
/* ------- begin -------------------------- cmplx_mult.c ------------ */
complex cmplx_mult(complex a, complex b)
{
complex c;
/* --- Multiply two complex numbers -- ------------ */
c.r = a.r*b.r - a.i*b.i;
c.i = a.r*b.i + a.i*b.r;
return c;
}
/* ------- end ---------------------------- cmplx_mult.c ------------ */
/* ------- begin -------------------------- cmplx_scl.c ------------- */
complex cmplx_sclr(double a, complex b)
{
complex c;
/* --- Multiply a complex number with a scalar -- ------------ */
c.r = a * b.r;
c.i = a * b.i;
return c;
}
/* ------- end ---------------------------- cmplx_scl.c ------------- */
/* ------- begin -------------------------- cmplx_div.c ------------- */
complex cmplx_div(complex a, complex b)
{
complex c;
double d;
/* --- Divide two complex numbers -- ------------ */
d = b.r*b.r + b.i*b.i;
c.r = (a.r*b.r + a.i*b.i) / d;
c.i = (a.i*b.r - a.r*b.i) / d;
return c;
}
/* ------- end ---------------------------- cmplx_div.c ------------- */
/* ------- begin -------------------------- cmplx_exp.c ------------- */
complex cmplx_exp(complex a)
{
/* --- Complex exponential function -- ------------ */
return cmplx_sclr(exp(a.r), cmplx(cos(a.i), sin(a.i)));
}
/* ------- end ---------------------------- cmplx_div.c ------------- */
/* ------- begin -------------------------- cmplx_add.c ------------- */
complex cmplx_add(complex a, complex b)
{
complex c;
/* --- Add two complex numbers -- ------------ */
c.r = a.r + b.r;
c.i = a.i + b.i;
return c;
}
/* ------- end ---------------------------- cmplx_add.c ------------- */
/* ------- begin -------------------------- cmplx_addr.c ------------ */
complex cmplx_addr(complex a, double b)
{
complex c;
/* --- Add real number to complex -- ------------ */
c.r = a.r + b;
c.i = a.i;
return c;
}
/* ------- end ---------------------------- cmplx_addr.c ------------ */
/* ------- begin -------------------------- cmplx_subt.c ------------ */
complex cmplx_subt(complex a, complex b)
{
complex c;
/* --- Subtract two complex numbers -- ------------ */
c.r = a.r - b.r;
c.i = a.i - b.i;
return c;
}
/* ------- end ---------------------------- cmplx_subt.c ------------ */
/* ------- begin -------------------------- matrix_complex.c -------- */
complex **matrix_complex(int Nrow, int Ncol)
{
register int i;
int typeSize = sizeof(complex), pointerSize = sizeof(complex *);
complex *theMatrix, **Matrix;
theMatrix = (complex *) calloc(Nrow * Ncol, typeSize);
Matrix = (complex **) malloc(Nrow * pointerSize);
for (i = 0; i < Nrow; i++, theMatrix += Ncol)
Matrix[i] = theMatrix;
return Matrix;
}
/* ------- end ---------------------------- matrix_complex.c -------- */