kmatrix.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <iostream>
3 #include <complex>
4 
5 #include <kfilter/kmatrix.hpp>
6 
7 using namespace std;
8 using namespace Kalman;
9 
10 template <typename T, K_UINT_32 BEG, bool DBG>
11 void test() {
12 
14  T values[10] = {9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0};
15 
16  // Constructor tests.
17  Matrix M0;
18  Matrix M1(0,4);
19  Matrix M2(2,3);
20  Matrix M3(0, 2, T(1.0));
21  Matrix M4(4, 1, T(2.0));
22  Matrix M5(3, 0, values);
23  Matrix M6(3, 1, values);
24  Matrix M7(M5);
25  Matrix M8(M6);
26  Matrix M9(4,0);
27 
28  cout << "M0 : " << M0 << endl;
29  cout << "M1 : " << M1 << endl;
30  cout << "M2 : " << M2 << endl;
31  cout << "M3 : " << M3 << endl;
32  cout << "M4 : " << M4 << endl;
33  cout << "M5 : " << M5 << endl;
34  cout << "M6 : " << M6 << endl;
35  cout << "M7 : " << M7 << endl;
36  cout << "M8 : " << M8 << endl;
37  cout << "M9 : " << M9 << endl << endl;
38 
39  // Tests for operator=(const T&) and resize().
40  M4.resize(0, 2);
41  M4 = T(-1.0);
42  M5 = T(6.0);
43  M6 = T(7.0);
44  M7.resize(2, 1);
45  M7 = T(8.0);
46  M8.resize(4, 2);
47  M8 = T(9.0);
48 
49  cout << "M4 : " << M4 << endl;
50  cout << "M5 : " << M5 << endl;
51  cout << "M6 : " << M6 << endl;
52  cout << "M7 : " << M7 << endl;
53  cout << "M8 : " << M8 << endl << endl;
54 
55  // Tests for assignement operator and assign().
56  // Tests swap() at the same time, under the covers.
57  M5 = M7;
58  M6 = M8;
59  M7 = M0;
60  M0.assign(0, 2, values);
61  M1.assign(2, 1, values);
62  M2.assign(3, 0, values);
63  M4.assign(3, 2, values);
64 
65  cout << "M0 : " << M0 << endl;
66  cout << "M1 : " << M1 << endl;
67  cout << "M2 : " << M2 << endl;
68  cout << "M3 : " << M3 << endl;
69  cout << "M4 : " << M4 << endl;
70  cout << "M5 : " << M5 << endl;
71  cout << "M6 : " << M6 << endl;
72  cout << "M7 : " << M7 << endl;
73  cout << "M8 : " << M8 << endl << endl;
74 
75  // Tests member access functions.
76  // Tests exceptions at the same time.
77  const Matrix& ref = M4;
78 
79  cout << "M4(1,1) = " << M4(1,1) << endl;
80  cout << "ref(1,1) = " << ref(1,1) << endl;
81 
82  M4(1,1) = T(3.0);
83  // ref(1,1) = T(2.0); error !!!
84 
85  cout << "M4(1,1) = " << M4(1,1) << endl;
86  cout << "ref(1,1) = " << ref(1,1) << endl;
87 
88  if (DBG) {
89 
90  try {
91  M4(100,100) = T(7.0);
92  } catch(OutOfBoundError& e) {
93  cout << e.what() << endl;
94  }
95 
96  try {
97  cout << ref(100,100) << endl;
98  } catch(OutOfBoundError& e) {
99  cout << e.what() << endl;
100  }
101 
102  }
103 
104  cout << endl;
105 
106  // Output has already been tested extensively.
107  // Let's test input.
108  // This also tests get() and put() under the covers.
109  cout << "M0 is of size : " << M0.nrow() << " by " << M0.ncol() << endl;
110  cin >> M0;
111  cout << "M0 : " << M0 << endl;
112 
113  cout << "M1 is of size : " << M1.nrow() << " by " << M1.ncol() << endl;
114  cin >> M1;
115  cout << "M1 : " << M1 << endl;
116 
117  cout << "----------------------------" << endl << endl;
118 
119 }
120 
121 int main() {
122 // test<float, 0, true>();
123 // test<float, 0, false>();
124  test<float, 1, true>();
125  test<float, 1, false>();
126 // test<double, 0, true>();
127 // test<double, 0, false>();
128  test<double, 1, true>();
129  test<double, 1, false>();
130 // test<complex<double>, 0, true>();
131 // test<complex<double>, 0, false>();
132  test<complex<double>, 1, true>();
133  test<complex<double>, 1, false>();
134  return 0;
135 }
K_UINT_32 nrow() const
Returns m_, the number of rows of the matrix.
int main()
Definition: kmatrix.cpp:121
Contains the interface of the KMatrix template class.
void test()
Definition: kmatrix.cpp:11
void assign(K_UINT_32 m, K_UINT_32 n, const T *v)
Copies a C-style array of instances of T in an m by n matrix.
void resize(K_UINT_32 m, K_UINT_32 n)
Resizes the matrix. Resulting matrix contents are undefined.
Minimalist matrix template class.
Definition: kmatrix.hpp:72
K_UINT_32 ncol() const
Returns n_, the number of columns of the matrix.
Exception class for access to out-of-bound elements.
Definition: ktypes.hpp:85
cPlaneEKF::Matrix Matrix
Definition: plane.h:27


kfilter
Author(s): Jorge Almeida, Vincent Zalzal, Sylvain Marleau, Richard Gourdeau
autogenerated on Mon Mar 2 2015 01:31:45