-
Notifications
You must be signed in to change notification settings - Fork 1
/
complex.c
267 lines (231 loc) · 5.86 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#pragma once
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifndef M_PI
#define M_PI 3.141592653589793
#endif
#ifndef M_E
#define M_E 2.718281828459045
#endif
#define SHORT_CTOA 1
long double real(complex_t num)
{
return num.real;
}
long double imag(complex_t num)
{
return num.imag;
}
char *ctoa(complex_t num)
{
/* This function is very badly written. Contributions to improve it are highly appreciated */
size_t allocsize = sizeof(char) * 2 * (8 * sizeof(long double) + 1);
size_t len = 0;
char *buf = calloc(allocsize, 1);
char sign = '+';
if (num.imag < 0.0L)
{
sign = '-';
}
if(fabs(num.real) < 1e-15)
{
num.real = 0;
}
if(fabs(num.imag) < 1e-15)
{
num.imag = 0;
}
if (SHORT_CTOA)
{
if (num.real != 0)
{
snprintf(buf, allocsize, "%.8Lg", num.real);
len = strlen(buf);
}
if (num.imag != 0)
{
if (num.real != 0 || sign == '-')
{
snprintf(buf + len, allocsize, "%c%.8Lgi", sign, fabs(num.imag));
}
else
{
snprintf(buf + len, allocsize, "%.8Lgi", num.imag);
}
len = strlen(buf);
}
if(buf[0] == '\0')
{
buf[0] = '0';
}
}
else
{
snprintf(buf, allocsize, "%.8Lg%c%.8Lgi", num.real, sign, fabs(num.imag)); // +3 to account for the ',' and 'i'
}
return buf;
}
long double cabs(complex_t num)
{
return sqrt(num.real * num.real + num.imag * num.imag);
}
complex_t cvalue(long double real, long double imag) // Change values of a complex function
{
complex_t result = {real, imag};
return result;
}
long double carg(complex_t num)
{
return atan2(num.imag, num.real); // arg(z) = atan(y/x)
}
complex_t conj(complex_t num)
{
complex_t result = {num.real, -num.imag};
return result;
}
complex_t clog(complex_t num)
{
long double modulus = cabs(num), arg = carg(num);
complex_t c_log = {log(modulus), arg};
return c_log;
}
long double degtorad(long double deg)
{
long double rad = deg * M_PI / 180;
return rad;
}
long double radtodeg(long double rad)
{
long double deg = rad * 180 / M_PI;
return deg;
}
complex_t cadd(complex_t num1, complex_t num2)
{
complex_t result;
result.real = num1.real + num2.real;
result.imag = num1.imag + num2.imag;
return result;
}
complex_t cdiff(complex_t num1, complex_t num2)
{
complex_t result;
result.real = num1.real - num2.real;
result.imag = num1.imag - num2.imag;
return result;
}
complex_t cmultiply(complex_t num1, complex_t num2)
{
complex_t result;
result.real = num1.real * num2.real - num1.imag * num2.imag;
result.imag = num1.real * num2.imag + num1.imag * num2.real;
return result;
}
complex_t cdivide(complex_t num1, complex_t num2)
{
complex_t result;
long double denominator = num2.real * num2.real + num2.imag * num2.imag;
result.real = (num1.real * num2.real + num1.imag * num2.imag) / denominator;
result.imag = (num1.imag * num2.real - num1.real * num2.imag) / denominator;
return result;
}
complex_t cexp(complex_t num)
{
complex_t result;
complex_t tmp = cvalue(num.real, num.imag);
result.real = pow(M_E, num.real) * cos(num.imag);
result.imag = pow(M_E, num.real) * sin(num.imag);
return result;
}
complex_t ccosh(complex_t num)
{
complex_t exp1 = cexp(num);
complex_t exp2 = cexp(cvalue(-num.real, -num.imag));
complex_t result = cdivide(cadd(exp1, exp2), cvalue(2, 0));
return result;
}
complex_t csinh(complex_t num)
{
complex_t exp1 = cexp(num);
complex_t exp2 = cexp(cvalue(-num.real, -num.imag));
complex_t result = cdivide(cdiff(exp1, exp2), cvalue(2, 0));
return result;
}
complex_t ccos(complex_t num)
{
complex_t result;
result.real = cos(num.real) * cosh(num.imag);
result.imag = -sin(num.real) * sinh(num.imag);
return result;
}
complex_t csin(complex_t num)
{
complex_t result;
result.real = sin(num.real) * cosh(num.imag);
result.imag = cos(num.real) * sinh(num.imag);
return result;
}
complex_t ctan(complex_t num)
{
complex_t result = cdivide(csin(num), ccos(num));
return result;
}
complex_t ctanh(complex_t num)
{
complex_t result = cdivide(csinh(num), ccosh(num));
return result;
}
complex_t csec(complex_t num) // Inverse of cosine
{
complex_t result = cdivide(cvalue(1, 0), ccos(num));
return result;
}
complex_t ccsc(complex_t num) // Inverse of sine
{
complex_t result = cdivide(cvalue(1, 0), csin(num));
return result;
}
complex_t ccot(complex_t num)
{
complex_t result = cdivide(cvalue(1, 0), ctan(num));
return result;
}
complex_t csech(complex_t num)
{
complex_t result = cdivide(cvalue(1, 0), ccosh(num));
return result;
}
complex_t ccsch(complex_t num)
{
complex_t result = cdivide(cvalue(1, 0), csinh(num));
return result;
}
complex_t ccoth(complex_t num)
{
complex_t result = cdivide(cvalue(1, 0), ctanh(num));
return result;
}
complex_t cpower(complex_t num, complex_t power)
{
complex_t result;
complex_t exponent = cmultiply(power, clog(num));
result = cexp(exponent);
return result;
}
complex_t clogbase(complex_t base, complex_t num)
{
complex_t result = cdivide(clog(num), clog(base));
return result;
}
complex_t clog10(complex_t num)
{
return clogbase(cvalue(10, 0), num);
}
complex_t csqrt(complex_t num)
{
return cpower(num, cvalue(0.5, 0)); // Convert square root to fraction exponent (1/2)
}
#ifndef j
#define j cvalue
#endif