DGtal  0.9.2
SimpleMatrix.ih
1 /**
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU Lesser General Public License as
4  * published by the Free Software Foundation, either version 3 of the
5  * License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  *
15  **/
16 
17 /**
18  * @file SimpleMatrix.ih
19  * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20  * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21  *
22  * @date 2012/07/10
23  *
24  * Implementation of inline methods defined in SimpleMatrix.h
25  *
26  * This file is part of the DGtal library.
27  */
28 
29 
30 //////////////////////////////////////////////////////////////////////////////
31 #include <cstdlib>
32 //////////////////////////////////////////////////////////////////////////////
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 // IMPLEMENTATION of inline methods.
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // ----------------------- Standard services ------------------------------
40 
41 
42 
43 /**
44  * Destructor.
45  */
46 template <typename T, DGtal::Dimension TM, DGtal::Dimension TN>
47 inline
48 DGtal::SimpleMatrix<T,TM, TN>::~SimpleMatrix()
49 {
50 }
51 
52 
53 /**
54  * Constructor.
55  */
56 template <typename T, DGtal::Dimension TM, DGtal::Dimension TN>
57 inline
58 DGtal::SimpleMatrix<T,TM, TN>::SimpleMatrix()
59 {
60  for ( DGtal::Dimension i = 0; i < TM*TN; ++i )
61  myValues[ i ] = NumberTraits<Component>::ZERO;
62  const Component one = NumberTraits<Component>::ONE;
63  const Component minus_one = -NumberTraits<Component>::ONE;
64  //Cofactor coefs computation
65  for (DGtal::Dimension i=0; i<TM; i++)
66  for (DGtal::Dimension j=0; j<TN; j++)
67  myCofactorCoefs[i*N+j] = ( (i+j) & 1 )
68  ? minus_one : one;
69 }
70 //------------------------------------------------------------------------------
71 
72 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
73 inline
74 DGtal::SimpleMatrix<T ,TM, TN>::SimpleMatrix(const Self& other)
75 {
76  for ( DGtal::Dimension i = 0; i < M*N; ++i )
77  {
78  myValues[ i ] = other.myValues[i];
79  myCofactorCoefs[ i ] = other.myCofactorCoefs[i];
80  }
81 }
82 
83 //---------------------------------------------------------------------------
84 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
85 inline
86 void
87 DGtal::SimpleMatrix<T, TM, TN>::clear()
88 {
89  for ( DGtal::Dimension i = 0; i < M*N; ++i )
90  myValues[ i ] = NumberTraits<T>::ZERO;
91 }
92 //---------------------------------------------------------------------------
93 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
94 inline
95 void
96 DGtal::SimpleMatrix<T, TM, TN>::constant(const T &aSc)
97 {
98  for ( DGtal::Dimension i = 0; i < M*N; ++i )
99  myValues[ i ] = aSc;
100 }
101 //---------------------------------------------------------------------------
102 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
103 inline
104 void
105 DGtal::SimpleMatrix<T, M,N>::identity( )
106 {
107  BOOST_STATIC_ASSERT( M == N);
108 
109  //fast clear
110  this->clear();
111  for (DGtal::Dimension i=0; i<M; i++)
112  this->setComponent( i, i , NumberTraits<T>::ONE);
113 }
114 //---------------------------------------------------------------------------
115 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
116 inline
117 typename DGtal::SimpleMatrix<T, TM, TN>::RowVector
118 DGtal::SimpleMatrix<T, TM, TN>::row(const DGtal::Dimension i) const
119 {
120  ASSERT(i<M);
121  RowVector v;
122  for ( DGtal::Dimension j = 0; j < N; ++j )
123  v[ j ] = this->operator()(i,j);
124  return v;
125 }
126 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
127 inline
128 typename DGtal::SimpleMatrix<T, TM, TN>::ColumnVector
129 DGtal::SimpleMatrix<T, TM, TN>::column(const DGtal::Dimension j) const
130 {
131  ASSERT(j<N);
132  ColumnVector v;
133  for ( DGtal::Dimension i = 0; i < M; ++i )
134  v[ i ] = this->operator()(i,j);
135  return v;
136 }
137 //---------------------------------------------------------------------------
138 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
139 template<typename TC>
140 inline
141 DGtal::SimpleMatrix<T, TM, TN> &
142 DGtal::SimpleMatrix<T, TM, TN>::operator=(const SimpleMatrix<TC,M,N>& other)
143 {
144  for ( DGtal::Dimension i = 0; i < M*N; ++i )
145  myValues[ i ] = static_cast<T>(other.myValues[i]);
146  return *this;
147 }
148 
149 //------------------------------------------------------------------------------
150 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
151 inline
152 DGtal::SimpleMatrix<T, TM, TN>
153 DGtal::SimpleMatrix<T, TM, TN>::operator+(const Self& other) const
154 {
155  SimpleMatrix<T,TM,TN> res;
156  for ( DGtal::Dimension i = 0; i < M*N; ++i )
157  res.myValues[ i ] = this->myValues[i] + other.myValues[i];
158  return res;
159 }
160 //------------------------------------------------------------------------------
161 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
162 inline
163 DGtal::SimpleMatrix<T, TM, TN> &
164 DGtal::SimpleMatrix<T, TM, TN>::operator+=(const Self& other)
165 {
166  for ( DGtal::Dimension i = 0; i < M*N; ++i )
167  myValues[ i ] += other.myValues[i];
168  return *this;
169 }
170 //------------------------------------------------------------------------------
171 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
172 inline
173 T
174 DGtal::SimpleMatrix<T, M,N>::cofactor(const DGtal::Dimension i,
175  const DGtal::Dimension j ) const
176 {
177  BOOST_STATIC_ASSERT(M == N);
178  return minorDeterminant(i,j)*myCofactorCoefs[i*N+j];
179 }
180 //------------------------------------------------------------------------------
181 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
182 inline
183 DGtal::SimpleMatrix<T, M,N>
184 DGtal::SimpleMatrix<T, M,N>::cofactor( ) const
185 {
186  DGtal::SimpleMatrix<T, M,N> mat;
187  BOOST_STATIC_ASSERT(M == N);
188 
189  for (DGtal::Dimension i=0; i<M; i++)
190  for (DGtal::Dimension j=0; j<M; j++)
191  mat.setComponent( i, j , cofactor(i,j));
192 
193  return mat;
194 }
195 //------------------------------------------------------------------------------
196 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
197 inline
198 T
199 DGtal::SimpleMatrix<T, M,N>::minorDeterminant(const DGtal::Dimension i,
200  const DGtal::Dimension j) const
201 {
202  return SimpleMatrixSpecializations<Self,M,N>::minorDeterminant(*this,i,j);
203 }
204 //------------------------------------------------------------------------------
205 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
206 inline
207 T
208 DGtal::SimpleMatrix<T, M,N>::determinant() const
209 {
210  return SimpleMatrixSpecializations<Self,M,N>::determinant(*this);
211 }
212 
213  //------------------------------------------------------------------------------
214 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
215 inline
216 typename DGtal::SimpleMatrix<T, TM, TN>
217 DGtal::SimpleMatrix<T, TM, TN>::inverse() const
218 {
219  BOOST_STATIC_ASSERT(TM == TN);
220 
221  SimpleMatrix<T,TM,TM> r = cofactor().transpose();
222 
223  //determinant
224  T det = determinant();
225  ASSERT(det != 0);
226  return r/det;
227 }
228 
229 //------------------------------------------------------------------------------
230 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
231 inline
232 DGtal::SimpleMatrix<T, TM, TN>
233 DGtal::SimpleMatrix<T, TM, TN>::operator-(const Self& other) const
234 {
235  SimpleMatrix<T,TM,TN> res;
236  for ( DGtal::Dimension i = 0; i < M*N; ++i )
237  res.myValues[ i ] = this->myValues[i] - other.myValues[i];
238  return res;
239 }
240 //------------------------------------------------------------------------------
241 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
242 inline
243 DGtal::SimpleMatrix<T, TM, TN> &
244 DGtal::SimpleMatrix<T, TM, TN>::operator-=(const Self& other)
245 {
246  for ( DGtal::Dimension i = 0; i < M*N; ++i )
247  myValues[ i ] -= other.myValues[i];
248  return *this;
249 }
250 //------------------------------------------------------------------------------
251 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
252 inline
253 bool
254 DGtal::SimpleMatrix<T, TM, TN>::operator==(const Self& other) const
255 {
256  return myValues == other.myValues;
257 }
258 
259 //------------------------------------------------------------------------------
260 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
261 inline
262 typename DGtal::SimpleMatrix<T, TN, TM>
263 DGtal::SimpleMatrix<T, TM, TN>::transpose() const
264 {
265  DGtal::SimpleMatrix<T, TN, TM> res;
266  for (DGtal::Dimension i=0; i<M; i++)
267  for (DGtal::Dimension j=0; j<N; j++)
268  res.setComponent(j,i, this->operator()(i,j));
269  return res;
270 }
271 //------------------------------------------------------------------------------
272 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
273 inline
274 typename DGtal::SimpleMatrix<T,M,N>::ColumnVector
275 DGtal::SimpleMatrix<T, M, N>::operator*(const RowVector& other) const
276 {
277  ColumnVector res;
278  for (DGtal::Dimension i=0; i<M; i++)
279  for (DGtal::Dimension k=0; k<N; k++)
280  res[i] += this->operator()(i, k )*other[k];
281 
282  return res;
283 }
284 //------------------------------------------------------------------------------
285 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
286 inline
287 typename DGtal::SimpleMatrix<T,M,M>
288 DGtal::SimpleMatrix<T, M, N>::operator*(const DGtal::SimpleMatrix<T,N,M>& other) const
289 {
290  SimpleMatrix<T,M,M> res;
291  T e = NumberTraits<T>::ZERO;
292  for (DGtal::Dimension i=0; i<M; i++)
293  for (DGtal::Dimension j=0; j<M; j++)
294  {
295  for (DGtal::Dimension k=0; k<N; k++)
296  {
297  e += this->operator()(i, k )*other(k ,j );
298  }
299 
300  res.setComponent(i,j,e);
301 
302  e = NumberTraits<T>::ZERO;
303  }
304  return res;
305 }
306 //------------------------------------------------------------------------------
307 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
308 inline
309 DGtal::SimpleMatrix<T, M,N> &
310 DGtal::SimpleMatrix<T, M, N>::operator/=(const T& other)
311 {
312  for (DGtal::Dimension i=0; i<M*N; i++)
313  this->myValues[i] /= other;
314 
315  return *this;
316 }//------------------------------------------------------------------------------
317 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
318 inline
319 DGtal::SimpleMatrix<T, M,N>
320 DGtal::SimpleMatrix<T, M, N>::operator/(const T& other) const
321 {
322  Self resultat;
323  for (DGtal::Dimension i=0; i<M*N; i++)
324  resultat.myValues[i] = myValues[i]/other;
325 
326  return resultat;
327 }
328 //------------------------------------------------------------------------------
329 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
330 inline
331 DGtal::SimpleMatrix<T, M,N> &
332 DGtal::SimpleMatrix<T, M,N>::operator*=(const T& other)
333 {
334  for (DGtal::Dimension i=0; i<M*N; i++)
335  this->myValues[i] *= other;
336 
337  return *this;
338 }
339 //------------------------------------------------------------------------------
340 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
341 inline
342 DGtal::SimpleMatrix<T, M,N>
343 DGtal::SimpleMatrix<T, M,N>::operator*(const T& other) const
344 {
345  Self resultat;
346  for (DGtal::Dimension i=0; i<M*N; i++)
347  resultat.myValues[i] = other*myValues[i];
348 
349  return resultat;
350 }
351 
352 //------------------------------------------------------------------------------
353 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
354 inline
355 void
356 DGtal::SimpleMatrix<T, TM, TN>::setComponent(const DGtal::Dimension i,
357  const DGtal::Dimension j,
358  const T &aValue)
359 {
360  ASSERT(i<M);
361  ASSERT(j<N);
362  myValues[i*N + j] = aValue;
363 }
364 //------------------------------------------------------------------------------
365 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
366 inline
367 T
368 DGtal::SimpleMatrix<T, TM, TN>::operator()(const DGtal::Dimension i,
369  const DGtal::Dimension j) const
370 {
371  ASSERT(i<M);
372  ASSERT(j<N);
373  return myValues[i*N + j];
374 }
375 //------------------------------------------------------------------------------
376 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
377 inline
378 T&
379 DGtal::SimpleMatrix<T, TM, TN>::operator()(const DGtal::Dimension i,
380  const DGtal::Dimension j)
381 {
382  ASSERT(i<M);
383  ASSERT(j<N);
384  return myValues[i*N + j];
385 }
386 
387 ///////////////////////////////////////////////////////////////////////////////
388 // Interface - public :
389 
390 /**
391  * Writes/Displays the object on an output stream.
392  * @param out the output stream where the object is written.
393  */
394 template <typename T, DGtal::Dimension TM, DGtal::Dimension TN >
395 inline
396 void
397 DGtal::SimpleMatrix<T,TM,TN>::selfDisplay ( std::ostream & out ) const
398 {
399  out << "[SimpleMatrix] "<<M<<"x"<<N<< " [";
400  for(DGtal::Dimension i = 0; i < M; ++i)
401  {
402  out<<"[";
403  for(DGtal::Dimension j = 0; j < N; ++j)
404  out<< this->operator()(i,j)<<" ";
405  out<<"]";
406  }
407  out<<"]";
408 }
409 
410 /**
411  * Checks the validity/consistency of the object.
412  * @return 'true' if the object is valid, 'false' otherwise.
413  */
414 template <typename T,DGtal::Dimension M,DGtal::Dimension N>
415 inline
416 bool
417 DGtal::SimpleMatrix<T,M,N>::isValid() const
418 {
419  return true;
420 }
421 
422 
423 
424 
425 
426 ///////////////////////////////////////////////////////////////////////////////
427 // Implementation of inline functions //
428 
429 template <typename T,DGtal::Dimension M,DGtal::Dimension N>
430 inline
431 std::ostream&
432 DGtal::operator<< ( std::ostream & out,
433  const SimpleMatrix<T,M,N> & object )
434 {
435  object.selfDisplay( out );
436  return out;
437 }
438 
439 template <typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
440 inline
441 DGtal::SimpleMatrix<TComponent, TM, TN>
442 operator* ( const TComponent& scalar, const DGtal::SimpleMatrix<TComponent, TM, TN>& matrix)
443 {
444  return matrix * scalar;
445 }