-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathldcomplex_math_MPI.c
executable file
·201 lines (149 loc) · 4.33 KB
/
ldcomplex_math_MPI.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Written by James Mc Donald 2006
[Antispam: email in reverse] ei.yawlagiun.ti@semaj
Functions to perform ldcomplex maths
Copyright (C) 2006 James Mc Donald,
Computational Astrophysics Laboratory,
National University of Ireland, Galway
This code is covered by the GNU General Public License */
#include "ldcomplex_math_MPI.h"
long double ldcomplex_abs(ldcomplex in){
/* Absolute magnitude
in=a+ib
|in|=sqrt(a^2+b^2) */
long double out;
out=sqrtl(in.dat[0]*in.dat[0]+in.dat[1]*in.dat[1]);
return out;
}
long double ldcomplex_abs2(ldcomplex in){
/* in=a+ib
|in|^2=a^2+b^2 */
long double out;
out=in.dat[0]*in.dat[0]+in.dat[1]*in.dat[1];
return out;
}
ldcomplex ldcomplex_add(ldcomplex in0,ldcomplex in1){
/* Add complex numbers
in0=a+ib
n1=c+id
out=(a+c)+i(b+d) */
ldcomplex out;
out.dat[0]=in0.dat[0]+in1.dat[0];
out.dat[1]=in0.dat[1]+in1.dat[1];
return out;
}
ldcomplex ldcomplex_sub(ldcomplex in0,ldcomplex in1){
/* Subtract complex numbers
in0=a+ib
in1=c+id
out=(a-c)+i(b-d) */
ldcomplex out;
out.dat[0]=in0.dat[0]-in1.dat[0];
out.dat[1]=in0.dat[1]-in1.dat[1];
return out;
}
ldcomplex ldcomplex_mul(ldcomplex in0,ldcomplex in1){
/* Multiply complex numbers
in0=a+ib
in1=c+id
out=(ac-bd)+i(ad-bc) */
ldcomplex out;
out.dat[0]=in0.dat[0]*in1.dat[0]-in0.dat[1]*in1.dat[1];
out.dat[1]=in0.dat[0]*in1.dat[1]+in0.dat[1]*in1.dat[0];
return out;
}
ldcomplex ldcomplex_div(ldcomplex in0,ldcomplex in1){
/* Divide complex numbers
in0=a+ib
in1=c+id
out=[(a+ib)/(c+id)]*[(c-id)/(c-id)]=[(ac+bd)+i(bc-ad)]/[c^2+d^2] */
ldcomplex out;
long double scale;
scale=1.0L/ldcomplex_abs2(in1);
out.dat[0]=(in0.dat[0]*in1.dat[0]+in0.dat[1]*in1.dat[1])*scale;
out.dat[1]=(in0.dat[1]*in1.dat[0]-in0.dat[0]*in1.dat[1])*scale;
return out;
}
ldcomplex ldcomplex_scale(ldcomplex in,long double scale){
/* Scale by a long double
in=(a*scale)+i(b*scale) */
in.dat[0]*=scale;
in.dat[1]*=scale;
return in;
}
ldcomplex ldcomplex_no_r_scale(ldcomplex in,long double scale){
/* Scale by a long double
in=a+i(b*scale) */
in.dat[1]*=scale;
return in;
}
long double ldcomplex_2vectornorm(ldcomplex *in){
/* 2-vector-norm
out=sqrt[sum_{i=0}^{n-1}(|in[i]|^2)] */
long int index0i;
long double out=0.0L,result=0.0L;
for(index0i=0;index0i<parallel.alloc_vector3;index0i++){
out+=ldcomplex_abs2(in[index0i]);
}
MPI_Allreduce(&out,&result,1,MPI_LONG_DOUBLE,MPI_SUM,MPI_COMM_WORLD);
result=sqrtl(result);
return result;
}
ldcomplex ldcomplex_exp(ldcomplex in){
/* Complex exponential
in=a+ib
out=exp(in)=exp(a)*[cos(b)+isin(b)]*/
long double a=expl(in.dat[0]);
long double b=in.dat[1];
ldcomplex out;
out.dat[0]=a*cosl(b);
out.dat[1]=a*sinl(b);
return out;
}
ldcomplex ldcomplex_no_r_exp(ldcomplex in){
/* Complex Exponential where real part of 'in' is zero
in=a+ib=0.0+ib
out=exp(in)=exp(a)*[cos(b)+isin(b)]
=exp(0.0)*[cos(b)+isin(b)]
=1.0*[cos(b)+isin(b)]
=cos(b)+isin(b) */
long double b=in.dat[1];
ldcomplex out;
out.dat[0]=cosl(b);
out.dat[1]=sinl(b);
return out;
}
ldcomplex ldcomplex_conj(ldcomplex in){
/* Complex conjugate of input
in=a+ib
out=a-ib */
in.dat[1]*=-1.0L;
return in;
}
ldcomplex ldcomplex_sqrt(ldcomplex in){
/* sqrt[a+ib]=p+iq=
p=[1/sqrt(2)]sqrt[sqrt(a^2+b^2)+a]
q=[(sign of b)/sqrt(2)]sqrt[sqrt(a^2+b^2)-a]
sqrt[(sqrt{a^2+b^2}+a)/(2)] +/- i*sqrt[(sqrt{a^2+b^2}-a)/(2)]
where the sign is the same as `b' */
ldcomplex out;
long double sign,temp,fa,fb;
fa=fabsl(in.dat[0]);
fb=fabsl(in.dat[1]);
if((fa<LDBL_EPSILON)&&(fb<LDBL_EPSILON)){ /* Check for zeroes */
out.dat[0]=0.0L;
out.dat[1]=0.0L;
}
else if(fb<LDBL_EPSILON){ /* Imaginary part ~zero */
out.dat[0]=sqrtl(in.dat[0]);
out.dat[1]=0.0L;
}
else{
sign=in.dat[1]/fb; /* sign of b */
temp=ldcomplex_abs(in); /* sqrt{a^2+b^2} */
/* sqrt[(sqrt{a^2+b^2}+a)/(2)] */
out.dat[0]=sqrtl((temp+in.dat[0])/2.0L);
/* (sign of b)*sqrt[(sqrt{a^2+b^2}-a)/(2)] */
out.dat[1]=sign*sqrtl((temp-in.dat[0])/2.0L);
}
return out;
}