-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.c
183 lines (155 loc) · 3.54 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
#include "complex.h"
com * create_com(long double real, long double imag) {
com *ret;
ret = malloc(sizeof(*ret));
// check for failure
if(ret == NULL) {
ret = NULL;
} else {
ret->r = real;
ret->i = imag;
}
return ret;
}
void add_com(com *a, com *b, com *ret) {
ret->i = a->i + b->i;
ret->r = a->r + b->r;
}
void sub_com(com *a, com *b, com *ret) {
ret->i = a->i - b->i;
ret->r = a->r - b->r;
}
void mul_comsc(com *a, long double b, com *ret) {
ret->i = a->i * b;
ret->r = a->r * b;
}
void mul_comcom(com *a, com *b, com *ret) {
// (r1 + i1)(r2 + i2)
// this way is clearer, and doesn't mangle the input if input and output are the same ptr
long double r1r2 = a->r * b->r;
long double r1i2 = a->r * b->i;
long double i1r2 = a->i * b->r;
long double i1i2 = a->i * b->i * -1;//i^2 = -1
ret->r = r1r2 + i1i2;
ret->i = r1i2 + i1r2;
}
void sqr_com(com *a, com *ret) {
// (r + i)(r + i)
long double rr = a->r * a->r;
long double ri = a->r * a->i;
long double ii = a->i * a->i * -1;//i^2 = -1
//= rr + 2ri - ii
ret->r = rr + ii;
ret->i = ri * 2;
}
long double magnitude_squared_com(com *a) {
return a->i * a->i + a->r * a->r;
}
void test_com_stack() {
com test;
com test2;
com ret;
com ret2;
com ret3;
test.r = 4;
test.i = 7;
test2.r = 3;
test2.i = 8;
add_com(&test, &test2, &ret);
if(ret.r != 7 || ret.i != 15) {
printf("test failed: (4 + 7i) + (3 + 8i) = (7 + 15i). got: ");
println_com(&ret);
} else {
printf("add test passed\n");
}
//(4 + 7i)(4 + 7i)
//16 + 56i - 49
//-33 + 56i
sqr_com(&test, &ret2);
if(ret2.r != -33 || ret2.i != 56) {
printf("test failed: (4 + 7i)^2 = (-33 + 56i). got: ");
println_com(&ret2);
} else {
printf("sqr test passed\n");
}
// (4 + 7i) * (3 + 8i)
// 12 + 32i + 21i + 56ii
// 12 + 53i - 56
// -44 + 53i
mul_comcom(&test, &test2, &ret3);
if(ret3.r != -44 || ret3.i != 53) {
printf("test failed: (4 + 7i) * (3 + 8i) = (-44 + 53i). got: ");
println_com(&ret3);
} else {
printf("mul test passed\n");
}
// magnitude squared
// 4+7i
// (4^2 + 7^2) = 65
int mag_sqr = magnitude_squared_com(&test);
if(mag_sqr != 65) {
printf("test failed: (4^2 + 7i^2) = 65. got: %d\n", mag_sqr);
} else {
printf("mag_sqr test passed\n");
}
}
void test_com_heap() {
com *test;
com *test2;
com *ret;
com *ret2;
com *ret3;
test = create_com(4, 7);
test2 = create_com(3, 8);
ret = create_com(0, 0);
ret2 = create_com(0, 0);
ret3 = create_com(0, 0);
add_com(test, test2, ret);
if(ret->r != 7 || ret->i != 15) {
printf("test failed: (4 + 7i) + (3 + 8i) = (7 + 15i). got: ");
println_com(ret);
} else {
printf("add test passed\n");
}
//(4 + 7i)(4 + 7i)
//16 + 56i - 49
//-33 + 56i
sqr_com(test, ret2);
if(ret2->r != -33 || ret2->i != 56) {
printf("test failed: (4 + 7i)^2 = (-33 + 56i). got: ");
println_com(ret2);
} else {
printf("sqr test passed\n");
}
// (4 + 7i) * (3 + 8i)
// 12 + 32i + 21i + 56ii
// 12 + 53i - 56
// -44 + 53i
mul_comcom(test, test2, ret3);
if(ret3->r != -44 || ret3->i != 53) {
printf("test failed: (4 + 7i) * (3 + 8i) = (-44 + 53i). got: ");
println_com(ret3);
} else {
printf("mul test passed\n");
}
// magnitude squared
// 4+7i
// (4^2 + 7^2) = 65
int mag_sqr = magnitude_squared_com(test);
if(mag_sqr != 65) {
printf("test failed: (4^2 + 7i^2) = 65. got: %d\n", mag_sqr);
} else {
printf("mag_sqr test passed\n");
}
free(test);
free(test2);
free(ret);
free(ret2);
free(ret3);
}
void println_com(com *a) {
printf("%.2Lf + %.2Lfi\n", a->r, a->i);
}
void print_com(com *a) {
printf("%.2Lf + %.2Lfi", a->r, a->i);
}