DGtal  0.9.4.1
testSimpleMatrix.cpp
Go to the documentation of this file.
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/math/linalg/SimpleMatrix.h"
34 #include "DGtal/math/linalg/CStaticMatrix.h"
35 #include "DGtal/math/linalg/CDenseMatrix.h"
36 #include "DGtal/math/linalg/CStaticVector.h"
37 #include "DGtal/math/linalg/CDenseVector.h"
38 #include "DGtal/math/linalg/CLinearAlgebra.h"
39 #include "DGtal/helpers/StdDefs.h"
41 
42 using namespace std;
43 using namespace DGtal;
44 
46 // Functions for testing class SimpleMatrix.
48 
53 {
54  unsigned int nbok = 0;
55  unsigned int nb = 0;
56 
57  trace.beginBlock ( "Testing create ..." );
58 
59  typedef SimpleMatrix<double,3,4> M34d;
60 
61  M34d m34d;
62  trace.info() << m34d<<std::endl;
63 
64  m34d.setComponent(1,2, 0.5);
65  trace.info() << m34d<<std::endl;
66 
67  nbok += (m34d(1,2) == 0.5) ? 1 : 0;
68  nb++;
69  trace.info() << "(" << nbok << "/" << nb << ") "
70  << "true == true" << std::endl;
71 
72  M34d matrix;
73  bool res=true;
74 
75  matrix.constant(12.3);
76  trace.info() << matrix;
77  for(DGtal::Dimension i = 0; i< 3; ++i)
78  for(DGtal::Dimension j = 0; j< 4; ++j)
79  res = res && (matrix(i,j) == 12.3);
80  nbok += res ? 1 : 0;
81  nb++;
82  trace.info() << "(" << nbok << "/" << nb << ") "
83  << "all equals to 12.3" << std::endl;
84 
85 
86  trace.endBlock();
87 
88  return nbok == nb;
89 }
90 
91 bool testArithm()
92 {
93  unsigned int nbok = 0;
94  unsigned int nb = 0;
95 
96 
97  typedef SimpleMatrix<double,3,4> M34d;
98  typedef SimpleMatrix<double,4,3> M43d;
99  typedef SimpleMatrix<double,3,3> M33d;
100 
101  M34d m34d, two,four;
102  M34d m34dbis, resadd, ressub;
103 
104  two.constant(2);
105  four.constant(4);
106 
107  for(DGtal::Dimension i = 0; i< 3; ++i)
108  for(DGtal::Dimension j = 0; j< 4; ++j)
109  {
110  m34d.setComponent(i,j,i*j);
111  m34dbis.setComponent(i,j,i+j);
112  resadd.setComponent(i,j,i*j+i+j);
113  ressub.setComponent(i,j,i*j-(double)i-(double)j);
114  }
115 
116 
117  trace.info() << m34d <<std::endl;
118  trace.info() << m34dbis<<std::endl;
119 
120  trace.beginBlock ( "Testing add ..." );
121  nbok += ((m34d + m34dbis) == resadd) ? 1 : 0;
122  nb++;
123  trace.info() << "(" << nbok << "/" << nb << ") "
124  << "ok" << std::endl;
125  nbok += ((m34dbis + m34d) == resadd) ? 1 : 0;
126  nb++;
127  trace.info() << "(" << nbok << "/" << nb << ") "
128  << "ok commutative" << std::endl;
129 
130  M34d other;
131  other += m34d;
132  nbok += (other == m34d) ? 1 : 0;
133  nb++;
134  trace.info() << "(" << nbok << "/" << nb << ") "
135  << "ok +=" << std::endl;
136 
137  trace.endBlock();
138 
139  trace.beginBlock ( "Testing substraction ..." );
140  nbok += ((m34d - m34dbis) == ressub) ? 1 : 0;
141  nb++;
142  trace.info()<<ressub<<std::endl;
143  trace.info()<<m34d - m34dbis<<std::endl;
144 
145  trace.info() << "(" << nbok << "/" << nb << ") "
146  << "ok simple" << std::endl;
147  trace.endBlock();
148 
149  trace.beginBlock ( "Testing scalar product/divide ..." );
150  nbok += ( (two*2.0) == four) ? 1 : 0;
151  nb++;
152  trace.info()<<ressub<<std::endl;
153  trace.info() << "(" << nbok << "/" << nb << ") "
154  << " [2]*2 == [4]" << std::endl;
155 
156  nbok += ( two == four/2.0) ? 1 : 0;
157  nb++;
158  trace.info()<<ressub<<std::endl;
159  trace.info() << "(" << nbok << "/" << nb << ") "
160  << " [2]= [4]/2" << std::endl;
161  trace.endBlock();
162 
163 
164  trace.beginBlock ( "Testing transpose ..." );
165  M43d transp = m34d.transpose();
166  nbok += (transp.transpose() == m34d) ? 1 : 0;
167  nb++;
168  trace.info() << "(" << nbok << "/" << nb << ") "
169  << "ok idem potent" << std::endl;
170  trace.endBlock();
171 
172  trace.beginBlock ( "Testing product ..." );
173 
174  M43d one;
175  M33d eight33;
176 
177  one.constant(1);
178  eight33.constant(8);
179  trace.info() << two * one<<std::endl;
180  nbok += (two * one == eight33) ? 1 : 0;
181  nb++;
182  trace.info() << "(" << nbok << "/" << nb << ") "
183  << " [2]*[1] = [8]" << std::endl;
184  trace.endBlock();
185 
186 
187 
188  return nbok == nb;
189 
190 }
191 
193 {
194  unsigned int nbok = 0;
195  unsigned int nb = 0;
196 
198  for(DGtal::Dimension i = 0; i< 3; ++i)
199  for(DGtal::Dimension j = 0; j< 4; ++j)
200  mat.setComponent(i,j,i+j);
201 
202  trace.beginBlock("Get Row");
203  trace.info() << mat <<std::endl;
205  row = mat.row(1);
206  trace.info() << row << std::endl;
207  nbok += (row[1] == 2 ) ? 1 : 0;
208  nb++;
209  trace.info() << "(" << nbok << "/" << nb << ") "
210  << " row value" << std::endl;
211  trace.endBlock();
212 
213  trace.beginBlock("Get Col");
215  col = mat.column(1);
216  trace.info() << row << std::endl;
217  nbok += (col[1] == 2 ) ? 1 : 0;
218  nb++;
219  trace.info() << "(" << nbok << "/" << nb << ") "
220  << " col value" << std::endl;
221  trace.endBlock();
222 
223 
224 
225  trace.beginBlock("Prod Matrix x Row^t");
226  //Row vector is a dim 4 vector
229  c = mat*r;
231 
232  trace.info() << c << std::endl;
233  nbok += (c == expected) ? 1 : 0;
234  nb++;
235  trace.info() << "(" << nbok << "/" << nb << ") "
236  << " mat*row^t" << std::endl;
237  trace.endBlock();
238 
239  return nbok == nb;
240 }
241 
243 {
244  unsigned int nbok = 0;
245  unsigned int nb = 0;
246 
247  typedef DGtal::SimpleMatrix<double,2,2> MAT2;
248  MAT2 mat2;
249  mat2.setComponent(0,0,1);
250  mat2.setComponent(1,1,2);
251 
252  trace.beginBlock("det2x2 tests...");
253  trace.info() << mat2<<std::endl;
254  trace.info() << mat2.determinant() << std::endl;
255  nbok += (mat2.determinant() == 2) ? 1 : 0;
256  nb++;
257  trace.info() << "(" << nbok << "/" << nb << ") "
258  << " 2" << std::endl;
259  trace.endBlock();
260 
262  MAT mat;
263  mat.setComponent(0,0,1);
264  mat.setComponent(1,1,2);
265  mat.setComponent(2,2,4);
266 
267  trace.beginBlock("det3x3 tests...");
268  trace.info() << mat<<std::endl;
269  nbok += (mat.determinant() == 8) ? 1 : 0;
270  nb++;
271  trace.info() << "(" << nbok << "/" << nb << ") "
272  << " 8" << std::endl;
273  trace.endBlock();
274 
275 
276  typedef DGtal::SimpleMatrix<double,4,4> MAT44;
277  MAT44 mat44;
278  mat44.setComponent(0,0,1);
279  mat44.setComponent(1,1,2);
280  mat44.setComponent(2,2,4);
281  mat44.setComponent(3,3,4);
282 
283  trace.beginBlock("det4x4 tests...");
284  trace.info() << mat44 <<std::endl;
285  trace.info() << mat44.determinant() << std::endl;
286  nbok += (mat44.determinant() == 32) ? 1 : 0;
287  nb++;
288  trace.info() << "(" << nbok << "/" << nb << ") "
289  << " 32" << std::endl;
290  trace.endBlock();
291 
292 
293  return nbok == nb;
294 }
295 
297 {
298  trace.beginBlock("Mx1 matrix test");
300  trace.info() << mat<<std::endl;
301  trace.endBlock();
302  return true;
303 }
304 
306 {
307  unsigned int nbok = 0;
308  unsigned int nb = 0;
309 
310  trace.beginBlock("Inverse tests 2X2...");
311 
312  typedef DGtal::SimpleMatrix<double,2,2> MAT2;
313  MAT2 mat2;
314  mat2.setComponent(0,0,1);
315  mat2.setComponent(1,1,2);
316 
317  MAT2 Id2;
318  Id2.identity();
319 
320  trace.info() << mat2<<std::endl;
321  trace.info() << mat2.inverse() << std::endl;
322  nbok += (( mat2 * mat2.inverse() )== Id2 ) ? 1 : 0;
323  nb++;
324  trace.info() << "(" << nbok << "/" << nb << ") "
325  << " M*M^-1=Id" << std::endl;
326 
327  trace.endBlock();
328 
329  trace.beginBlock("Inverse tests 6x6 random...");
330 
331  typedef DGtal::SimpleMatrix<double,6,6> MAT6;
332  MAT6 mat;
333 
334  for(unsigned int i=0; i< 6; i++)
335  for(unsigned int j=0; j< 6; j++)
336  mat.setComponent(i,j, rand() % 10);
337 
338  MAT6 Id6;
339  Id6.identity();
340 
341  trace.info() << "M= "<<mat<<std::endl;
342  trace.info() << "M^-1=" <<mat.inverse() << std::endl;
343  trace.info() << "det(M)= "<<mat.determinant() <<std::endl;
344  trace.info() << "M*M^-1= "<<mat.inverse()*mat << std::endl;
345 
346 
347 
348  trace.endBlock();
349 
350 
351  return nbok == nb;
352 }
353 
355 {
356  unsigned int nbok = 0;
357  unsigned int nb = 0;
358 
359  trace.beginBlock( "Initilizer-list constructor test" );
360  SimpleMatrix<double, 3, 3> mat = {1, 2, 3, 4, 5, 6, 7, 8, 9};
361  trace.info() << mat << std::endl;
362 
363  trace.info() << "Testing values: ";
364  trace.info() << mat( 0, 0 );
365  nbok += ( mat( 0, 0 ) == 1 ) ? 1 : 0;
366  nb++;
367  trace.info() << "(" << nbok << "/" << nb << ") ";
368 
369  trace.info() << mat( 0, 1 );
370  nbok += ( mat( 0, 1 ) == 2 ) ? 1 : 0;
371  nb++;
372  trace.info() << "(" << nbok << "/" << nb << ") ";
373 
374  trace.info() << mat( 2, 2 );
375  nbok += ( mat( 2, 2 ) == 9 ) ? 1 : 0;
376  nb++;
377  trace.info() << "(" << nbok << "/" << nb << ") ";
378 
379  trace.info() << std::endl;
380  trace.endBlock();
381  return nbok == nb;
382 }
383 
385 {
386  typedef DGtal::SimpleMatrix<double,3,3> Matrix;
387  typedef Matrix::ColumnVector Vector;
388 
389  BOOST_CONCEPT_ASSERT(( concepts::CStaticVector<Vector> ));
390  BOOST_CONCEPT_ASSERT(( concepts::CDenseVector<Vector> ));
391  BOOST_CONCEPT_ASSERT(( concepts::CStaticMatrix<Matrix> ));
392  BOOST_CONCEPT_ASSERT(( concepts::CDenseMatrix<Matrix> ));
393  BOOST_CONCEPT_ASSERT(( concepts::CLinearAlgebra<Vector, Matrix> ));
394  BOOST_CONCEPT_ASSERT(( concepts::CStaticVector<DGtal::Z3i::Space::Vector> ));
395  BOOST_CONCEPT_ASSERT(( concepts::CDenseVector<DGtal::Z3i::Space::Vector> ));
396 
397  return true;
398 }
399 
401 // Standard services - public :
402 
403 int main( int argc, char** argv )
404 {
405  trace.beginBlock ( "Testing class SimpleMatrix" );
406  trace.info() << "Args:";
407  for ( int i = 0; i < argc; ++i )
408  trace.info() << " " << argv[ i ];
409  trace.info() << endl;
410 
411  bool res = testSimpleMatrix() && testArithm() && testColRow() &&
414  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
415  trace.endBlock();
416  return res ? 0 : 1;
417 }
418 // //
void beginBlock(const std::string &keyword="")
Aim: Represent any dynamic or static sized matrix having dense representation.
Definition: CDenseVector.h:88
Trace trace
Definition: Common.h:137
DGtal::uint32_t Dimension
Definition: Common.h:120
Aim: Represent any static sized column vector having sparse or dense representation.
Definition: CStaticVector.h:90
STL namespace.
double endBlock()
Aim: Represent any static sized matrix having sparse or dense representation.
Definition: CStaticMatrix.h:89
Aim: Check right multiplication between matrix and vector and internal matrix multiplication. Matrix and vector scalar types should be the same.
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
bool testConstructor()
RowVector row(const DGtal::Dimension i) const
Aim: implements basic MxN Matrix services (M,N>=1).
Definition: SimpleMatrix.h:75
bool testArithm()
bool testM1Matrix()
std::ostream & emphase()
bool testColRow()
void setComponent(const DGtal::Dimension i, const DGtal::Dimension j, const Component &aValue)
Aim: Represent any dynamic or static sized matrix having dense representation.
Definition: CDenseMatrix.h:88
bool testInverse()
bool testDetCofactor()
bool testSimpleMatrix()
int main(int argc, char **argv)
DGtal is the top-level namespace which contains all DGtal functions and types.
void constant(const Component &aScalar)
ColumnVector column(const DGtal::Dimension j) const
std::ostream & info()
FreemanChain< int >::Vector Vector
bool testConcepts()