File: TestVectorMath.cpp

package info (click to toggle)
simbody 3.7%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 72,876 kB
  • sloc: cpp: 248,828; ansic: 18,240; sh: 29; makefile: 24
file content (357 lines) | stat: -rw-r--r-- 15,573 bytes parent folder | download | duplicates (6)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* -------------------------------------------------------------------------- *
 *                       Simbody(tm): SimTKcommon                             *
 * -------------------------------------------------------------------------- *
 * This is part of the SimTK biosimulation toolkit originating from           *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody.  *
 *                                                                            *
 * Portions copyright (c) 2007-12 Stanford University and the Authors.        *
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License. You may obtain a  *
 * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
 *                                                                            *
 * Unless required by applicable law or agreed to in writing, software        *
 * distributed under the License is distributed on an "AS IS" BASIS,          *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 * -------------------------------------------------------------------------- */

#include "SimTKcommon.h"

#include <iostream>

#define ASSERT(cond) {SimTK_ASSERT_ALWAYS((cond), "Assertion failed");}
#define ASSERT_EQUAL(val1, val2) {ASSERT(std::abs((val1)-(val2)) < 1e-10);}

using std::cout;
using std::endl;
using namespace SimTK;
using namespace std;

static Real sqrt(int a) {return std::sqrt(Real(a));}
static Real sin(int a) {return std::sin(Real(a));}
static Real cos(int a) {return std::cos(Real(a));}
static Real tan(int a) {return std::tan(Real(a));}
static Real asin(int a) {return std::asin(Real(a));}
static Real acos(int a) {return std::acos(Real(a));}
static Real atan(int a) {return std::atan(Real(a));}
static Real exp(int a) {return std::exp(Real(a));}
static Real log(int a) {return std::log(Real(a));}
static Real cosh(int a) {return std::cosh(Real(a));}
static Real sinh(int a) {return std::sinh(Real(a));}
static Real tanh(int a) {return std::tanh(Real(a));}

template <class T, int N>
void testVector(const T& value, const Vec<N>& expected) {
    ASSERT(value.size() == N);
    for (int i = 0; i < N; ++i) {
        if (isNaN(expected[i])) {
            ASSERT(isNaN(value[i]));
        }
        else {
            ASSERT_EQUAL(value[i], expected[i]);
        }
    }
}

template <class T, int M, int N>
void testMatrix(const T& value, const Mat<M, N>& expected) {
    ASSERT(value.nrow() == M);
    ASSERT(value.ncol() == N);
    for (int i = 0; i < M; ++i)
        for (int j = 0; j < N; ++j) {
            if (isNaN(expected(i, j))) {
                ASSERT(isNaN(value(i, j)));
            }
            else {
                ASSERT_EQUAL(value(i, j), expected(i, j));
            }
        }
}

template <class T, int N>
void testSymMat(const T& value, const SymMat<N>& expected) {
    ASSERT(value.nrow() == N);
    for (int i = 0; i < N; ++i)
        for (int j = 0; j <= i; ++j) {
            if (isNaN(expected(i, j))) {
                ASSERT(isNaN(value(i, j)));
            }
            else {
                ASSERT_EQUAL(value(i, j), expected(i, j));
            }
        }
}

int main() {
    try {
        // Create a bunch of vectors and matrices that will be used for testing.
        
        Vector vector(5);
        vector[0] = -1;
        vector[1] = 2;
        vector[2] = -3;
        vector[3] = 4;
        vector[4] = -5;
        RowVector rowvector = ~vector;
        Vec5 vec = Vec5(-1, 2, -3, 4, -5);
        Row5 row = Row5(-1, 2, -3, 4, -5);
        Matrix matrix(2, 3);
        matrix[0][0] = -1;
        matrix[0][1] = 2;
        matrix[0][2] = -3;
        matrix[1][0] = 4;
        matrix[1][1] = -5;
        matrix[1][2] = 6;
        Mat<2,3> mat = Mat<2,3>(-1,  2, -3, 
                                 4, -5,  6);
        Real temp[] = {-1, 2, -3};
        SymMat<2> symmat = SymMat<2>(temp);
        
        // Test the abs function.
        
        Vec5 expectedVec = Vec5(1, 2, 3, 4, 5);
        Mat<2,3> expectedMat = Mat<2,3>(1, 2, 3, 
                                        4, 5, 6);
        testVector(abs(vector), expectedVec);
        testVector(abs(rowvector), expectedVec);
        testVector(abs(vec), expectedVec);
        testVector(abs(row), expectedVec);
        testMatrix<Matrix,2,3>(abs(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(abs(mat), expectedMat);
        testSymMat(abs(symmat), SymMat<2>(Mat22(1, 2, 
                                                2, 3)));
        
        // Test the exp function.
        
        expectedVec = Vec5(exp(-1), exp(2), exp(-3), exp(4), exp(-5));
        expectedMat = Mat<2,3>(exp(-1), exp( 2), exp(-3), 
                               exp( 4), exp(-5), exp( 6));
        testVector(exp(vector), expectedVec);
        testVector(exp(rowvector), expectedVec);
        testVector(exp(vec), expectedVec);
        testVector(exp(row), expectedVec);
        testMatrix<Matrix,2,3>(exp(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(exp(mat), expectedMat);
        testSymMat(exp(symmat), SymMat<2>(Mat22(exp(-1), exp( 2), 
                                                exp( 2), exp(-3))));
        
        // Test the log function.
        
        expectedVec = Vec5(log(-1), log(2), log(-3), log(4), log(-5));
        expectedMat = Mat<2,3>(log(-1), log(2), log(-3), log(4), log(-5), log(6));
        testVector(log(vector), expectedVec);
        testVector(log(rowvector), expectedVec);
        testVector(log(vec), expectedVec);
        testVector(log(row), expectedVec);
        testMatrix<Matrix,2,3>(log(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(log(mat), expectedMat);
        testSymMat(log(symmat), SymMat<2>(Mat22(log(-1), log(2), 
                                                log(2), log(-3))));
        
        // Test the sqrt function.
        
        expectedVec = Vec5(sqrt(-1), sqrt(2), sqrt(-3), sqrt(4), sqrt(-5));
        expectedMat = Mat<2,3>(sqrt(-1), sqrt(2), sqrt(-3), sqrt(4), sqrt(-5), sqrt(6));
        testVector(sqrt(vector), expectedVec);
        testVector(sqrt(rowvector), expectedVec);
        testVector(sqrt(vec), expectedVec);
        testVector(sqrt(row), expectedVec);
        testMatrix<Matrix,2,3>(sqrt(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(sqrt(mat), expectedMat);
        testSymMat(sqrt(symmat), SymMat<2>(Mat22(sqrt(-1), sqrt(2), 
                                                 sqrt(2), sqrt(-3))));
        
        // Test the sin function.
        
        expectedVec = Vec5(sin(-1), sin(2), sin(-3), sin(4), sin(-5));
        expectedMat = Mat<2,3>(sin(-1), sin(2), sin(-3), sin(4), sin(-5), sin(6));
        testVector(sin(vector), expectedVec);
        testVector(sin(rowvector), expectedVec);
        testVector(sin(vec), expectedVec);
        testVector(sin(row), expectedVec);
        testMatrix<Matrix,2,3>(sin(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(sin(mat), expectedMat);
        testSymMat(sin(symmat), SymMat<2>(Mat22(sin(-1), sin(2), 
                                                sin(2), sin(-3))));
        
        // Test the cos function.
        
        expectedVec = Vec5(cos(-1), cos(2), cos(-3), cos(4), cos(-5));
        expectedMat = Mat<2,3>(cos(-1), cos(2), cos(-3), cos(4), cos(-5), cos(6));
        testVector(cos(vector), expectedVec);
        testVector(cos(rowvector), expectedVec);
        testVector(cos(vec), expectedVec);
        testVector(cos(row), expectedVec);
        testMatrix<Matrix,2,3>(cos(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(cos(mat), expectedMat);
        testSymMat(cos(symmat), SymMat<2>(Mat22(cos(-1), cos(2), 
                                                cos(2), cos(-3))));
        
        // Test the tan function.
        
        expectedVec = Vec5(tan(-1), tan(2), tan(-3), tan(4), tan(-5));
        expectedMat = Mat<2,3>(tan(-1), tan(2), tan(-3), tan(4), tan(-5), tan(6));
        testVector(tan(vector), expectedVec);
        testVector(tan(rowvector), expectedVec);
        testVector(tan(vec), expectedVec);
        testVector(tan(row), expectedVec);
        testMatrix<Matrix,2,3>(tan(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(tan(mat), expectedMat);
        testSymMat(tan(symmat), SymMat<2>(Mat22(tan(-1), tan(2), 
                                                tan(2), tan(-3))));
        
        // Test the asin function.
        
        expectedVec = Vec5(asin(-1), asin(2), asin(-3), asin(4), asin(-5));
        expectedMat = Mat<2,3>(asin(-1), asin(2), asin(-3), asin(4), asin(-5), asin(6));
        testVector(asin(vector), expectedVec);
        testVector(asin(rowvector), expectedVec);
        testVector(asin(vec), expectedVec);
        testVector(asin(row), expectedVec);
        testMatrix<Matrix,2,3>(asin(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(asin(mat), expectedMat);
        testSymMat(asin(symmat), SymMat<2>(Mat22(asin(-1), asin(2), 
                                                 asin(2), asin(-3))));
        
        // Test the asin function.
        
        expectedVec = Vec5(acos(-1), acos(2), acos(-3), acos(4), acos(-5));
        expectedMat = Mat<2,3>(acos(-1), acos(2), acos(-3), acos(4), acos(-5), acos(6));
        testVector(acos(vector), expectedVec);
        testVector(acos(rowvector), expectedVec);
        testVector(acos(vec), expectedVec);
        testVector(acos(row), expectedVec);
        testMatrix<Matrix,2,3>(acos(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(acos(mat), expectedMat);
        testSymMat(acos(symmat), SymMat<2>(Mat22(acos(-1), acos(2), 
                                                 acos(2), acos(-3))));
        
        // Test the atan function.
        
        expectedVec = Vec5(atan(-1), atan(2), atan(-3), atan(4), atan(-5));
        expectedMat = Mat<2,3>(atan(-1), atan(2), atan(-3), atan(4), atan(-5), atan(6));
        testVector(atan(vector), expectedVec);
        testVector(atan(rowvector), expectedVec);
        testVector(atan(vec), expectedVec);
        testVector(atan(row), expectedVec);
        testMatrix<Matrix,2,3>(atan(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(atan(mat), expectedMat);
        testSymMat(atan(symmat), SymMat<2>(Mat22(atan(-1), atan(2), 
                                                 atan(2), atan(-3))));
        
        // Test the sinh function.
        
        expectedVec = Vec5(sinh(-1), sinh(2), sinh(-3), sinh(4), sinh(-5));
        expectedMat = Mat<2,3>(sinh(-1), sinh(2), sinh(-3), sinh(4), sinh(-5), sinh(6));
        testVector(sinh(vector), expectedVec);
        testVector(sinh(rowvector), expectedVec);
        testVector(sinh(vec), expectedVec);
        testVector(sinh(row), expectedVec);
        testMatrix<Matrix,2,3>(sinh(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(sinh(mat), expectedMat);
        testSymMat(sinh(symmat), SymMat<2>(Mat22(sinh(-1), sinh(2), 
                                                 sinh(2), sinh(-3))));
        
        // Test the cosh function.
        
        expectedVec = Vec5(cosh(-1), cosh(2), cosh(-3), cosh(4), cosh(-5));
        expectedMat = Mat<2,3>(cosh(-1), cosh(2), cosh(-3), cosh(4), cosh(-5), cosh(6));
        testVector(cosh(vector), expectedVec);
        testVector(cosh(rowvector), expectedVec);
        testVector(cosh(vec), expectedVec);
        testVector(cosh(row), expectedVec);
        testMatrix<Matrix,2,3>(cosh(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(cosh(mat), expectedMat);
        testSymMat(cosh(symmat), SymMat<2>(Mat22(cosh(-1), cosh(2), 
                                                 cosh(2), cosh(-3))));
        
        // Test the tanh function.
        
        expectedVec = Vec5(tanh(-1), tanh(2), tanh(-3), tanh(4), tanh(-5));
        expectedMat = Mat<2,3>(tanh(-1), tanh(2), tanh(-3), tanh(4), tanh(-5), tanh(6));
        testVector(tanh(vector), expectedVec);
        testVector(tanh(rowvector), expectedVec);
        testVector(tanh(vec), expectedVec);
        testVector(tanh(row), expectedVec);
        testMatrix<Matrix,2,3>(tanh(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(tanh(mat), expectedMat);
        testSymMat(tanh(symmat), SymMat<2>(Mat22(tanh(-1), tanh(2), 
                                                 tanh(2), tanh(-3))));
        
        // Test the sum function.
        
        ASSERT_EQUAL(sum(vector), -3);
        ASSERT_EQUAL(sum(rowvector), -3);
        ASSERT_EQUAL(sum(vec), -3);
        ASSERT_EQUAL(sum(row), -3);
        testVector(sum(matrix), Vec3(3, -3, 3));
        testVector(sum(mat), Vec3(3, -3, 3));
        testVector(sum(symmat), Vec2(1, -1));
        
        // Test the min function.
        
        ASSERT_EQUAL(min(vector), -5);
        ASSERT_EQUAL(min(rowvector), -5);
        ASSERT_EQUAL(min(vec), -5);
        ASSERT_EQUAL(min(row), -5);
        testVector(min(matrix), Vec3(-1, -5, -3));
        testVector(min(mat), Vec3(-1, -5, -3));
        testVector(min(symmat), Vec2(-1, -3));
        
        // Test the max function.
        
        ASSERT_EQUAL(max(vector), 4);
        ASSERT_EQUAL(max(rowvector), 4);
        ASSERT_EQUAL(max(vec), 4);
        ASSERT_EQUAL(max(row), 4);
        testVector(max(matrix), Vec3(4, 2, 6));
        testVector(max(mat), Vec3(4, 2, 6));
        testVector(max(symmat), Vec2(2, 2));
        
        // Test the mean function.
        
        ASSERT_EQUAL(mean(vector), -0.6);
        ASSERT_EQUAL(mean(rowvector), -0.6);
        ASSERT_EQUAL(mean(vec), -0.6);
        ASSERT_EQUAL(mean(row), -0.6);
        testVector(mean(matrix), Vec3(1.5, -1.5, 1.5));
        testVector(mean(mat), Vec3(1.5, -1.5, 1.5));
        testVector(mean(symmat), Vec2(0.5, -0.5));
        
        // Test the sort function.

        expectedVec = Vec5(-5, -3, -1, 2, 4);
        expectedMat = Mat<2,3>(-1, -5, -3, 4, 2, 6);
        testVector(sort(vector), expectedVec);
        testVector(sort(rowvector), expectedVec);
        testVector(sort(vec), expectedVec);
        testVector(sort(row), expectedVec);
        testMatrix<Matrix,2,3>(sort(matrix), expectedMat);
        testMatrix<Mat<2,3>,2,3>(sort(mat), expectedMat);
        testMatrix<Mat<2,2>,2,2>(sort(symmat), Mat22(-1, -3, 
                                                      2,  2));
        
        // Test the median function.
        
        ASSERT_EQUAL(median(vector), -1);
        ASSERT_EQUAL(median(rowvector), -1);
        ASSERT_EQUAL(median(vec), -1);
        ASSERT_EQUAL(median(row), -1);
        testVector(median(matrix), Vec3(1.5, -1.5, 1.5));
        testVector(median(mat), Vec3(1.5, -1.5, 1.5));
        testVector(median(symmat), Vec2(0.5, -0.5));
        ASSERT_EQUAL(median(Vec6(6, 1, 5, 2, 4, 3)), 3.5);
    } catch(const std::exception& e) {
        cout << "exception: " << e.what() << endl;
        return 1;
    }
    cout << "Done" << endl;
    return 0;
}