DGtal  0.9.3
Functions
testSimpleMatrix.cpp File Reference
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/math/linalg/SimpleMatrix.h"
#include "DGtal/math/linalg/CStaticMatrix.h"
#include "DGtal/math/linalg/CDenseMatrix.h"
#include "DGtal/math/linalg/CStaticVector.h"
#include "DGtal/math/linalg/CDenseVector.h"
#include "DGtal/math/linalg/CLinearAlgebra.h"
#include "DGtal/helpers/StdDefs.h"
Include dependency graph for testSimpleMatrix.cpp:

Go to the source code of this file.

Functions

bool testSimpleMatrix ()
 
bool testArithm ()
 
bool testColRow ()
 
bool testDetCofactor ()
 
bool testM1Matrix ()
 
bool testInverse ()
 
bool testConcepts ()
 
int main (int argc, char **argv)
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
David Coeurjolly (david.nosp@m..coe.nosp@m.urjol.nosp@m.ly@l.nosp@m.iris..nosp@m.cnrs.nosp@m..fr ) Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
Date
2012/07/10

Functions for testing class SimpleMatrix.

This file is part of the DGtal library.

Definition in file testSimpleMatrix.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 373 of file testSimpleMatrix.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::Trace::endBlock(), DGtal::Trace::info(), testArithm(), testColRow(), testConcepts(), testDetCofactor(), testInverse(), testM1Matrix(), testSimpleMatrix(), and DGtal::trace.

374 {
375  trace.beginBlock ( "Testing class SimpleMatrix" );
376  trace.info() << "Args:";
377  for ( int i = 0; i < argc; ++i )
378  trace.info() << " " << argv[ i ];
379  trace.info() << endl;
380 
381  bool res = testSimpleMatrix() && testArithm() && testColRow()
382  && testDetCofactor() && testM1Matrix()
383  && testInverse() && testConcepts();
384  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
385  trace.endBlock();
386  return res ? 0 : 1;
387 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
double endBlock()
bool testArithm()
bool testM1Matrix()
std::ostream & emphase()
bool testColRow()
bool testInverse()
bool testDetCofactor()
bool testSimpleMatrix()
std::ostream & info()
bool testConcepts()

◆ testArithm()

bool testArithm ( )

Definition at line 91 of file testSimpleMatrix.cpp.

References DGtal::Trace::beginBlock(), DGtal::SimpleMatrix< TComponent, TM, TN >::constant(), DGtal::Trace::endBlock(), DGtal::Trace::info(), and DGtal::trace.

Referenced by main().

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
DGtal::uint32_t Dimension
Definition: Common.h:120
double endBlock()
Aim: implements basic MxN Matrix services (M,N>=1).
Definition: SimpleMatrix.h:75
void constant(const Component &aScalar)
std::ostream & info()

◆ testColRow()

bool testColRow ( )

Definition at line 192 of file testSimpleMatrix.cpp.

References DGtal::Trace::beginBlock(), DGtal::SimpleMatrix< TComponent, TM, TN >::column(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::SimpleMatrix< TComponent, TM, TN >::row(), DGtal::SimpleMatrix< TComponent, TM, TN >::setComponent(), and DGtal::trace.

Referenced by main().

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
DGtal::uint32_t Dimension
Definition: Common.h:120
double endBlock()
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:141
RowVector row(const DGtal::Dimension i) const
Aim: implements basic MxN Matrix services (M,N>=1).
Definition: SimpleMatrix.h:75
void setComponent(const DGtal::Dimension i, const DGtal::Dimension j, const Component &aValue)
ColumnVector column(const DGtal::Dimension j) const
std::ostream & info()

◆ testConcepts()

bool testConcepts ( )

Definition at line 354 of file testSimpleMatrix.cpp.

Referenced by main().

355 {
356  typedef DGtal::SimpleMatrix<double,3,3> Matrix;
357  typedef Matrix::ColumnVector Vector;
358 
359  BOOST_CONCEPT_ASSERT(( concepts::CStaticVector<Vector> ));
360  BOOST_CONCEPT_ASSERT(( concepts::CDenseVector<Vector> ));
361  BOOST_CONCEPT_ASSERT(( concepts::CStaticMatrix<Matrix> ));
362  BOOST_CONCEPT_ASSERT(( concepts::CDenseMatrix<Matrix> ));
363  BOOST_CONCEPT_ASSERT(( concepts::CLinearAlgebra<Vector, Matrix> ));
364  BOOST_CONCEPT_ASSERT(( concepts::CStaticVector<DGtal::Z3i::Space::Vector> ));
365  BOOST_CONCEPT_ASSERT(( concepts::CDenseVector<DGtal::Z3i::Space::Vector> ));
366 
367  return true;
368 }
Aim: Represent any dynamic or static sized matrix having dense representation.
Definition: CDenseVector.h:88
Aim: Represent any static sized column vector having sparse or dense representation.
Definition: CStaticVector.h:90
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: Represent any dynamic or static sized matrix having dense representation.
Definition: CDenseMatrix.h:88
FreemanChain< int >::Vector Vector

◆ testDetCofactor()

bool testDetCofactor ( )

Definition at line 242 of file testSimpleMatrix.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::SimpleMatrix< TComponent, TM, TN >::setComponent(), and DGtal::trace.

Referenced by main().

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
double endBlock()
Aim: implements basic MxN Matrix services (M,N>=1).
Definition: SimpleMatrix.h:75
void setComponent(const DGtal::Dimension i, const DGtal::Dimension j, const Component &aValue)
std::ostream & info()

◆ testInverse()

bool testInverse ( )

Definition at line 305 of file testSimpleMatrix.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::SimpleMatrix< TComponent, TM, TN >::setComponent(), and DGtal::trace.

Referenced by main().

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
double endBlock()
Aim: implements basic MxN Matrix services (M,N>=1).
Definition: SimpleMatrix.h:75
void setComponent(const DGtal::Dimension i, const DGtal::Dimension j, const Component &aValue)
std::ostream & info()

◆ testM1Matrix()

bool testM1Matrix ( )

Definition at line 296 of file testSimpleMatrix.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), and DGtal::trace.

Referenced by main().

297 {
298  trace.beginBlock("Mx1 matrix test");
300  trace.info() << mat<<std::endl;
301  trace.endBlock();
302  return true;
303 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
double endBlock()
Aim: implements basic MxN Matrix services (M,N>=1).
Definition: SimpleMatrix.h:75
std::ostream & info()

◆ testSimpleMatrix()

bool testSimpleMatrix ( )

Example of a test. To be completed.

Definition at line 52 of file testSimpleMatrix.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), and DGtal::trace.

Referenced by main().

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
DGtal::uint32_t Dimension
Definition: Common.h:120
double endBlock()
Aim: implements basic MxN Matrix services (M,N>=1).
Definition: SimpleMatrix.h:75
std::ostream & info()