wfmath  1.0.3
A math library for the Worldforge system.
oldmatrix_test.cpp
1 // -*-C++-*-
2 // matrix_test.cpp (Matrix<> test functions)
3 //
4 // The WorldForge Project
5 // Copyright (C) 2001 The WorldForge Project
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 //
21 // For information about WorldForge and its authors, please contact
22 // the Worldforge Web Site at http://www.worldforge.org.
23 
24 // Author: Ron Steinke
25 // Created: 2001-12-7
26 
27 #include "vector.h"
28 #include "vector_funcs.h"
29 #include "matrix.h"
30 #include "matrix_funcs.h"
31 #include "const.h"
32 #include <assert.h>
33 #include "stream_funcs.h"
34 #include <iostream>
35 
36 using namespace WF::Math;
37 
38 //TODO tests for non-square matrices
39 
40 template<const int size>
41 void test_matrix(const Matrix<size>& m)
42 {
43  cout << "Testing matrix: " << m << std::endl;
44 
45  cout << "Transpose is: " << m.transpose() << std::endl;
46 
47  Matrix<size> minv = m.inverse();
48 
49  double mdet = m.determinant(), minvdet = minv.determinant();
50 
51  cout << "Inverse is: " << minv << std::endl;
52 
53  assert(fabs(mdet * minvdet - 1) < WFMATH_EPSILON);
54 
55  Matrix<size> nothing;
56 
57  nothing.identity();
58 
59  nothing -= m * minv;
60 
61  cout << "This should be zero: " << nothing << std::endl;
62 
63  for(int i = 0; i < size; ++i)
64  for(int j = 0; j < size; ++j)
65  assert(fabs(nothing.elem(i, j)) < WFMATH_EPSILON);
66 }
67 
68 int main()
69 {
70  Matrix<2> m2;
71  Matrix<3> m3;
72 
73  m2.identity();
74  m2.elem(1, 0) = 1;
75 
76  test_matrix(m2);
77 
78  m3.identity();
79  m3.elem(1, 0) = 1;
80  m3.elem(0, 2) = WFMATH_CONST_SQRT2;
81  m3.elem(2, 0) = WFMATH_CONST_SQRT3;
82 
83  test_matrix(m3);
84 
85  return 0;
86 }