-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscalar1.hh
209 lines (179 loc) · 4.28 KB
/
scalar1.hh
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
#include "scalar.hh"
class Scalar1 : public CxxTest::TestSuite {
public:
/* scalar{} */
void testDefaultConstructor () {
hx::scalar<1> x;
assert_values(x, {0, 0});
}
/* scalar{double, ...} */
void testCoeffConstructor () {
hx::scalar<1> x{3, 4};
assert_values(x, {3, 4});
}
/* scalar{scalar} */
void testCopyConstructor () {
hx::scalar<1> x{3, 4}, y{x};
assert_values(x, y);
}
/* scalar{subscalar} */
void testPromotionConstructor () {
hx::scalar<0> r = 3.14;
hx::scalar<1> x{r};
assert_values(x, {3.14, 0});
}
/* scalar{subscalar, subscalar} */
void testRealImagConstructor () {
hx::scalar<0> re = 2, im = 3;
hx::scalar<1> x{re, im};
assert_values(x, {2, 3});
}
/* scalar = scalar */
void testCopyAssignment () {
hx::scalar<1> x{3, 4}, y;
y = x;
assert_values(x, y);
}
/* scalar += scalar
* scalar += double
*/
void testPlusAssignment () {
hx::scalar<1> x{2, 1}, y{1, 3};
x += y; assert_values(x, {3, 4});
x += 2; assert_values(x, {5, 4});
}
/* scalar -= scalar
* scalar -= double
*/
void testMinusAssignment () {
hx::scalar<1> x{2, 1}, y{1, 3};
x -= y; assert_values(x, {1, -2});
x -= 2; assert_values(x, {-1, -2});
}
/* scalar *= double */
void testMultiplyAssignment () {
hx::scalar<1> x{2, 3};
x *= 0.5;
assert_values(x, {1, 1.5});
}
/* scalar /= double */
void testDivideAssignment () {
hx::scalar<1> x{2, 3};
x /= 2;
assert_values(x, {1, 1.5});
}
/* ~scalar */
void testConjugation () {
hx::scalar<1> x{2, 3};
x = ~x;
assert_values(x, {2, -3});
}
/* -scalar */
void testUnaryMinus () {
hx::scalar<1> x{7, 6};
x = -x;
assert_values(x, {-7, -6});
}
/* scalar + scalar
* scalar + double
*/
void testBinaryPlus () {
hx::scalar<1> x{2, 3}, y{5, 7}, z;
z = x + y; assert_values(z, {7, 10});
z = z + 1; assert_values(z, {8, 10});
}
/* scalar - scalar
* scalar - double
*/
void testBinaryMinus () {
hx::scalar<1> x{2, 3}, y{5, 7}, z;
z = x - y; assert_values(z, {-3, -4});
z = z - 1; assert_values(z, {-4, -4});
}
/* scalar * scalar
* scalar * double
*/
void testBinaryMultiply () {
hx::scalar<1> x{2, 3}, y{5, 7}, z;
z = x * y; assert_values(z, {-11, 29});
z = z * 2; assert_values(z, {-22, 58});
}
/* scalar / double */
void testBinaryDivide () {
hx::scalar<1> x{2, 3}, y{x / 2};
assert_values(y, {1, 1.5});
}
/* scalar == scalar */
void testEqualityComparison () {
hx::scalar<1> x{2, 3}, y{x}, z{~x};
TS_ASSERT(x == y);
TS_ASSERT(!(x == z));
}
/* scalar != scalar */
void testInequalityComparison () {
hx::scalar<1> x{2, 3}, y{~x}, z{x};
TS_ASSERT(x != y);
TS_ASSERT(!(x != z));
}
/* scalar < scalar */
void testLessThanComparison () {
hx::scalar<1> x{7, 3}, y{x + 1}, z{x};
TS_ASSERT(x < y);
TS_ASSERT(!(x < z));
}
/* scalar > scalar */
void testGreaterThanComparison () {
hx::scalar<1> x{7, 3}, y{x - 1}, z{x};
TS_ASSERT(x > y);
TS_ASSERT(!(x > z));
}
/* scalar <= scalar */
void testLessThanOrEqualComparison () {
hx::scalar<1> x{5, 9}, y{x + 1}, z{x}, w{x - 1};
TS_ASSERT(x <= y);
TS_ASSERT(x <= z);
TS_ASSERT(!(x <= w));
}
/* scalar >= scalar */
void testGreaterThanOrEqualComparison () {
hx::scalar<1> x{5, 9}, y{x - 1}, z{x}, w{x + 1};
TS_ASSERT(x >= y);
TS_ASSERT(x >= z);
TS_ASSERT(!(x >= w));
}
/* scalar[i] */
void testSubscript () {
hx::scalar<1> x{3, 4};
TS_ASSERT_EQUALS(x.real, x[0]);
TS_ASSERT_EQUALS(x.imag, x[1]);
x[0] = 4;
assert_values(x, {4, 4});
}
/* squaredNorm() */
void testSquaredNorm () {
hx::scalar<1> x{3, 4};
TS_ASSERT_EQUALS(x.squaredNorm(), 25);
}
/* norm() */
void testNorm () {
hx::scalar<1> x{3, 4};
TS_ASSERT_EQUALS(x.norm(), 5);
}
/* inverse() */
void testInverse () {
hx::scalar<1> x{3, 4}, y{x.inverse()}, z{x * y};
assert_values(x, {3, 4});
assert_values(y, {0.12, -0.16});
assert_values(z, {1, 0});
}
/* scalar::R() */
void testR () {
auto x = hx::scalar<1>::R();
assert_values(x, {1, 0});
}
/* scalar::I() */
void testI () {
auto x = hx::scalar<1>::I();
assert_values(x, {0, 1});
}
};