DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testSimpleMatrix.cpp
1 
30 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/kernel/SimpleMatrix.h"
35 
36 using namespace std;
37 using namespace DGtal;
38 
40 // Functions for testing class SimpleMatrix.
42 
46 bool testSimpleMatrix()
47 {
48  unsigned int nbok = 0;
49  unsigned int nb = 0;
50 
51  trace.beginBlock ( "Testing create ..." );
52 
53  typedef SimpleMatrix<double,3,4> M34d;
54 
55  M34d m34d;
56  trace.info() << m34d<<std::endl;
57 
58  m34d.setComponent(1,2, 0.5);
59  trace.info() << m34d<<std::endl;
60 
61  nbok += (m34d(1,2) == 0.5) ? 1 : 0;
62  nb++;
63  trace.info() << "(" << nbok << "/" << nb << ") "
64  << "true == true" << std::endl;
65 
66  M34d matrix;
67  bool res=true;
68 
69  matrix.constant(12.3);
70  trace.info() << matrix;
71  for(DGtal::Dimension i = 0; i< 3; ++i)
72  for(DGtal::Dimension j = 0; j< 4; ++j)
73  res = res && (matrix(i,j) == 12.3);
74  nbok += res ? 1 : 0;
75  nb++;
76  trace.info() << "(" << nbok << "/" << nb << ") "
77  << "all equals to 12.3" << std::endl;
78 
79 
80  trace.endBlock();
81 
82  return nbok == nb;
83 }
84 
85 bool testArithm()
86 {
87  unsigned int nbok = 0;
88  unsigned int nb = 0;
89 
90 
91  typedef SimpleMatrix<double,3,4> M34d;
92  typedef SimpleMatrix<double,4,3> M43d;
93  typedef SimpleMatrix<double,3,3> M33d;
94 
95  M34d m34d, two,four;
96  M34d m34dbis, resadd, ressub;
97 
98  two.constant(2);
99  four.constant(4);
100 
101  for(DGtal::Dimension i = 0; i< 3; ++i)
102  for(DGtal::Dimension j = 0; j< 4; ++j)
103  {
104  m34d.setComponent(i,j,i*j);
105  m34dbis.setComponent(i,j,i+j);
106  resadd.setComponent(i,j,i*j+i+j);
107  ressub.setComponent(i,j,i*j-(double)i-(double)j);
108  }
109 
110 
111  trace.info() << m34d <<std::endl;
112  trace.info() << m34dbis<<std::endl;
113 
114  trace.beginBlock ( "Testing add ..." );
115  nbok += ((m34d + m34dbis) == resadd) ? 1 : 0;
116  nb++;
117  trace.info() << "(" << nbok << "/" << nb << ") "
118  << "ok" << std::endl;
119  nbok += ((m34dbis + m34d) == resadd) ? 1 : 0;
120  nb++;
121  trace.info() << "(" << nbok << "/" << nb << ") "
122  << "ok commutative" << std::endl;
123 
124  M34d other;
125  other += m34d;
126  nbok += (other == m34d) ? 1 : 0;
127  nb++;
128  trace.info() << "(" << nbok << "/" << nb << ") "
129  << "ok +=" << std::endl;
130 
131  trace.endBlock();
132 
133  trace.beginBlock ( "Testing substraction ..." );
134  nbok += ((m34d - m34dbis) == ressub) ? 1 : 0;
135  nb++;
136  trace.info()<<ressub<<std::endl;
137  trace.info()<<m34d - m34dbis<<std::endl;
138 
139  trace.info() << "(" << nbok << "/" << nb << ") "
140  << "ok simple" << std::endl;
141  trace.endBlock();
142 
143  trace.beginBlock ( "Testing scalar product/divide ..." );
144  nbok += ( (two*2.0) == four) ? 1 : 0;
145  nb++;
146  trace.info()<<ressub<<std::endl;
147  trace.info() << "(" << nbok << "/" << nb << ") "
148  << " [2]*2 == [4]" << std::endl;
149 
150  nbok += ( two == four/2.0) ? 1 : 0;
151  nb++;
152  trace.info()<<ressub<<std::endl;
153  trace.info() << "(" << nbok << "/" << nb << ") "
154  << " [2]= [4]/2" << std::endl;
155  trace.endBlock();
156 
157 
158  trace.beginBlock ( "Testing transpose ..." );
159  M43d transp = m34d.transpose();
160  nbok += (transp.transpose() == m34d) ? 1 : 0;
161  nb++;
162  trace.info() << "(" << nbok << "/" << nb << ") "
163  << "ok idem potent" << std::endl;
164  trace.endBlock();
165 
166  trace.beginBlock ( "Testing product ..." );
167 
168  M43d one;
169  M33d eight33;
170 
171  one.constant(1);
172  eight33.constant(8);
173  trace.info() << two * one<<std::endl;
174  nbok += (two * one == eight33) ? 1 : 0;
175  nb++;
176  trace.info() << "(" << nbok << "/" << nb << ") "
177  << " [2]*[1] = [8]" << std::endl;
178  trace.endBlock();
179 
180 
181 
182  return nbok == nb;
183 
184 }
185 
186 bool testColRow()
187 {
188  unsigned int nbok = 0;
189  unsigned int nb = 0;
190 
192  for(DGtal::Dimension i = 0; i< 3; ++i)
193  for(DGtal::Dimension j = 0; j< 4; ++j)
194  mat.setComponent(i,j,i+j);
195 
196  trace.beginBlock("Get Row");
197  trace.info() << mat <<std::endl;
199  row = mat.row(1);
200  trace.info() << row << std::endl;
201  nbok += (row[1] == 2 ) ? 1 : 0;
202  nb++;
203  trace.info() << "(" << nbok << "/" << nb << ") "
204  << " row value" << std::endl;
205  trace.endBlock();
206 
207  trace.beginBlock("Get Col");
209  col = mat.column(1);
210  trace.info() << row << std::endl;
211  nbok += (col[1] == 2 ) ? 1 : 0;
212  nb++;
213  trace.info() << "(" << nbok << "/" << nb << ") "
214  << " col value" << std::endl;
215  trace.endBlock();
216 
217 
218 
219  trace.beginBlock("Prod Matrix x Row^t");
220  //Row vector is a dim 4 vector
223  c = mat*r;
225 
226  trace.info() << c << std::endl;
227  nbok += (c == expected) ? 1 : 0;
228  nb++;
229  trace.info() << "(" << nbok << "/" << nb << ") "
230  << " mat*row^t" << std::endl;
231  trace.endBlock();
232 
233  return nbok == nb;
234 }
235 
236 bool testDetCofactor()
237 {
238  unsigned int nbok = 0;
239  unsigned int nb = 0;
240 
241  typedef DGtal::SimpleMatrix<double,2,2> MAT2;
242  MAT2 mat2;
243  mat2.setComponent(0,0,1);
244  mat2.setComponent(1,1,2);
245 
246  trace.beginBlock("det2x2 tests...");
247  trace.info() << mat2<<std::endl;
248  trace.info() << mat2.determinant() << std::endl;
249  nbok += (mat2.determinant() == 2) ? 1 : 0;
250  nb++;
251  trace.info() << "(" << nbok << "/" << nb << ") "
252  << " 2" << std::endl;
253  trace.endBlock();
254 
256  MAT mat;
257  mat.setComponent(0,0,1);
258  mat.setComponent(1,1,2);
259  mat.setComponent(2,2,4);
260 
261  trace.beginBlock("det3x3 tests...");
262  trace.info() << mat<<std::endl;
263  nbok += (mat.determinant() == 8) ? 1 : 0;
264  nb++;
265  trace.info() << "(" << nbok << "/" << nb << ") "
266  << " 8" << std::endl;
267  trace.endBlock();
268 
269 
270  typedef DGtal::SimpleMatrix<double,4,4> MAT44;
271  MAT44 mat44;
272  mat44.setComponent(0,0,1);
273  mat44.setComponent(1,1,2);
274  mat44.setComponent(2,2,4);
275  mat44.setComponent(3,3,4);
276 
277  trace.beginBlock("det4x4 tests...");
278  trace.info() << mat44 <<std::endl;
279  trace.info() << mat44.determinant() << std::endl;
280  nbok += (mat44.determinant() == 32) ? 1 : 0;
281  nb++;
282  trace.info() << "(" << nbok << "/" << nb << ") "
283  << " 32" << std::endl;
284  trace.endBlock();
285 
286 
287  return nbok == nb;
288 }
289 
290 bool testM1Matrix()
291 {
292  trace.beginBlock("Mx1 matrix test");
294  trace.info() << mat<<std::endl;
295  trace.endBlock();
296  return true;
297 }
298 
299 bool testInverse()
300 {
301  unsigned int nbok = 0;
302  unsigned int nb = 0;
303 
304  trace.beginBlock("Inverse tests 2X2...");
305 
306  typedef DGtal::SimpleMatrix<double,2,2> MAT2;
307  MAT2 mat2;
308  mat2.setComponent(0,0,1);
309  mat2.setComponent(1,1,2);
310 
311  MAT2 Id2;
312  Id2.identity();
313 
314  trace.info() << mat2<<std::endl;
315  trace.info() << mat2.inverse() << std::endl;
316  nbok += (( mat2 * mat2.inverse() )== Id2 ) ? 1 : 0;
317  nb++;
318  trace.info() << "(" << nbok << "/" << nb << ") "
319  << " M*M^-1=Id" << std::endl;
320 
321  trace.endBlock();
322 
323  trace.beginBlock("Inverse tests 6x6 random...");
324 
325  typedef DGtal::SimpleMatrix<double,6,6> MAT6;
326  MAT6 mat;
327 
328  for(unsigned int i=0; i< 6; i++)
329  for(unsigned int j=0; j< 6; j++)
330  mat.setComponent(i,j, rand() % 10);
331 
332  MAT6 Id6;
333  Id6.identity();
334 
335  trace.info() << "M= "<<mat<<std::endl;
336  trace.info() << "M^-1=" <<mat.inverse() << std::endl;
337  trace.info() << "det(M)= "<<mat.determinant() <<std::endl;
338  trace.info() << "M*M^-1= "<<mat.inverse()*mat << std::endl;
339 
340 
341 
342  trace.endBlock();
343 
344 
345  return nbok == nb;
346 }
347 
349 // Standard services - public :
350 
351 int main( int argc, char** argv )
352 {
353  trace.beginBlock ( "Testing class SimpleMatrix" );
354  trace.info() << "Args:";
355  for ( int i = 0; i < argc; ++i )
356  trace.info() << " " << argv[ i ];
357  trace.info() << endl;
358 
359  bool res = testSimpleMatrix() && testArithm() && testColRow()
360  && testDetCofactor() && testM1Matrix()
361  && testInverse(); // && ... other tests
362  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
363  trace.endBlock();
364  return res ? 0 : 1;
365 }
366 // //