-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcomplex.c
223 lines (183 loc) · 4.51 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/***********************************************************
complex.c -- 複素数
***********************************************************/
#include <stdio.h> /* {\tt sprintf()} */
#include <math.h>
typedef struct { double re, im; } complex; /* 複素数型 */
complex c_conv(double x, double y) /* $x$, $y$ を複素数 $z = x + iy$ に変換 */
{
complex z;
z.re = x; z.im = y;
return z;
}
char *c_string(complex z) /* 複素数 $z = x + iy$ を文字列に変換 */
{
static char s[40];
sprintf(s, "%g%+gi", z.re, z.im);
return s;
}
complex c_conj(complex z) /* 共役複素数 $\overline{z}$ */
{
z.im = - z.im;
return z;
}
double c_abs(complex z) /* 絶対値 $|z|$ */
{
double t;
if (z.re == 0) return fabs(z.im);
if (z.im == 0) return fabs(z.re);
if (fabs(z.im) > fabs(z.re)) {
t = z.re / z.im;
return fabs(z.im) * sqrt(1 + t * t);
} else {
t = z.im / z.re;
return fabs(z.re) * sqrt(1 + t * t);
}
}
double c_arg(complex z) /* 偏角 ($-\pi \le \varphi \le \pi$) */
{
return atan2(z.im, z.re);
}
complex c_add(complex x, complex y) /* 和 $x + y$ */
{
x.re += y.re;
x.im += y.im;
return x;
}
complex c_sub(complex x, complex y) /* 差 $x - y$ */
{
x.re -= y.re;;
x.im -= y.im;
return x;
}
complex c_mul(complex x, complex y) /* 積 $xy$ */
{
complex z;
z.re = x.re * y.re - x.im * y.im;
z.im = x.re * y.im + x.im * y.re;
return z;
}
#if 0
complex c_div(complex x, complex y) /* 商 $x / y$ (単純版) */
{
double r2;
complex z;
r2 = y.re * y.re + y.im * y.im;
z.re = (x.re * y.re + x.im * y.im) / r2;
z.im = (x.im * y.re - x.re * y.im) / r2;
return z;
}
#endif
complex c_div(complex x, complex y) /* 商 $x / y$ (上位桁あふれ対策版) */
{
double w, d;
complex z;
if (fabs(y.re) >= fabs(y.im)) {
w = y.im / y.re; d = y.re + y.im * w;
z.re = (x.re + x.im * w) / d;
z.im = (x.im - x.re * w) / d;
} else {
w = y.re / y.im; d = y.re * w + y.im;
z.re = (x.re * w + x.im) / d;
z.im = (x.im * w - x.re) / d;
}
return z;
}
complex c_exp(complex x) /* 指数関数 $e^x$ */
{
double a;
a = exp(x.re);
x.re = a * cos(x.im);
x.im = a * sin(x.im);
return x;
}
complex c_log(complex x) /* 自然対数 $\log_e x$ */
{
complex z;
z.re = 0.5 * log(x.re * x.re + x.im * x.im);
z.im = atan2(x.im, x.re);
return z;
}
complex c_pow(complex x, complex y) /* 累乗 $x^y$ */
{
return c_exp(c_mul(y, c_log(x)));
}
complex c_sin(complex x) /* 正弦 $\sin x$ */
{
double e, f;
e = exp(x.im); f = 1 / e;
x.im = 0.5 * cos(x.re) * (e - f);
x.re = 0.5 * sin(x.re) * (e + f);
return x;
}
complex c_cos(complex x) /* 余弦 $\cos x$ */
{
double e, f;
e = exp(x.im); f = 1 / e;
x.im = 0.5 * sin(x.re) * (f - e);
x.re = 0.5 * cos(x.re) * (f + e);
return x;
}
complex c_tan(complex x) /* 正接 $\tan x$ */
{
double e, f, d;
e = exp(2 * x.im); f = 1 / e;
d = cos(2 * x.re) + 0.5 * (e + f);
x.re = sin(2 * x.re) / d;
x.im = 0.5 * (e - f) / d;
return x;
}
complex c_sinh(complex x) /* 双曲線正弦 $\sinh x$ */
{
double e, f;
e = exp(x.re); f = 1 / e;
x.re = 0.5 * (e - f) * cos(x.im);
x.im = 0.5 * (e + f) * sin(x.im);
return x;
}
complex c_cosh(complex x) /* 双曲線余弦 $\cosh x$ */
{
double e, f;
e = exp(x.re); f = 1 / e;
x.re = 0.5 * (e + f) * cos(x.im);
x.im = 0.5 * (e - f) * sin(x.im);
return x;
}
complex c_tanh(complex x) /* 双曲線正接 $\tanh x$ */
{
double e, f, d;
e = exp(2 * x.re); f = 1 / e;
d = 0.5 * (e + f) + cos(2 * x.im);
x.re = 0.5 * (e - f) / d;
x.im = sin(2 * x.im) / d;
return x;
}
#define SQRT05 0.707106781186547524 /* $\sqrt{0.5}$ */
complex c_sqrt(complex x) /* 平方根 $\sqrt{x}$ */
{
double r, w;
r = c_abs(x);
w = sqrt(r + fabs(x.re));
if (x.re >= 0) {
x.re = SQRT05 * w;
x.im = SQRT05 * x.im / w;
} else {
x.re = SQRT05 * fabs(x.im) / w;
x.im = (x.im >= 0) ? SQRT05 * w : -SQRT05 * w;
}
return x;
}
#include <stdlib.h>
int main(void) /* テスト (ごく一部) */
{
double x, y;
complex z;
printf("x, y ? "); scanf("%lf%lf", &x, &y);
z = c_conv(x, y);
printf("z = %s\n", c_string(z));
z = c_exp(z);
printf("exp(z) = %s\n", c_string(z));
z = c_log(z);
printf("log(exp(z)) = %s\n", c_string(z));
return 0;
}