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.
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.
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/>.
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
24 * Implementation of inline methods defined in SimpleMatrix.h
26 * This file is part of the DGtal library.
30//////////////////////////////////////////////////////////////////////////////
32//////////////////////////////////////////////////////////////////////////////
34///////////////////////////////////////////////////////////////////////////////
35// IMPLEMENTATION of inline methods.
36///////////////////////////////////////////////////////////////////////////////
38///////////////////////////////////////////////////////////////////////////////
39// ----------------------- Standard services ------------------------------
46template <typename T, DGtal::Dimension TM, DGtal::Dimension TN>
48DGtal::SimpleMatrix<T,TM, TN>::~SimpleMatrix()
56template <typename T, DGtal::Dimension TM, DGtal::Dimension TN>
58DGtal::SimpleMatrix<T,TM, TN>::SimpleMatrix()
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 )
74template <typename T, DGtal::Dimension TM, DGtal::Dimension TN>
76DGtal::SimpleMatrix<T,TM, TN>::SimpleMatrix(std::initializer_list<T> values)
78 DGtal::Dimension i = 0;
79 for ( const T *p = values.begin(); p != values.end() && i < TM*TN; ++p, ++i )
81 for ( ; i < TM*TN; ++i )
82 myValues[ i ] = NumberTraits<Component>::ZERO;
84 const Component one = NumberTraits<Component>::ONE;
85 const Component minus_one = -NumberTraits<Component>::ONE;
86 // Cofactor coefs computation
87 for ( i = 0; i < TM; i++ )
88 for ( DGtal::Dimension j = 0; j < TN; j++ )
89 myCofactorCoefs[ i * N + j ] = ( ( i + j ) & 1 ) ? minus_one : one;
91//------------------------------------------------------------------------------
93template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
95DGtal::SimpleMatrix<T ,TM, TN>::SimpleMatrix(const Self& other)
97 for ( DGtal::Dimension i = 0; i < M*N; ++i )
99 myValues[ i ] = other.myValues[i];
100 myCofactorCoefs[ i ] = other.myCofactorCoefs[i];
104//---------------------------------------------------------------------------
105template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
108DGtal::SimpleMatrix<T, TM, TN>::clear()
110 for ( DGtal::Dimension i = 0; i < M*N; ++i )
111 myValues[ i ] = NumberTraits<T>::ZERO;
113//---------------------------------------------------------------------------
114template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
117DGtal::SimpleMatrix<T, TM, TN>::constant(const T &aSc)
119 for ( DGtal::Dimension i = 0; i < M*N; ++i )
122//---------------------------------------------------------------------------
123template<typename T, DGtal::Dimension M, DGtal::Dimension N>
126DGtal::SimpleMatrix<T, M,N>::identity( )
128 BOOST_STATIC_ASSERT( M == N);
132 for (DGtal::Dimension i=0; i<M; i++)
133 this->setComponent( i, i , NumberTraits<T>::ONE);
135//---------------------------------------------------------------------------
136template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
138typename DGtal::SimpleMatrix<T, TM, TN>::RowVector
139DGtal::SimpleMatrix<T, TM, TN>::row(const DGtal::Dimension i) const
143 for ( DGtal::Dimension j = 0; j < N; ++j )
144 v[ j ] = this->operator()(i,j);
147template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
149typename DGtal::SimpleMatrix<T, TM, TN>::ColumnVector
150DGtal::SimpleMatrix<T, TM, TN>::column(const DGtal::Dimension j) const
154 for ( DGtal::Dimension i = 0; i < M; ++i )
155 v[ i ] = this->operator()(i,j);
158//---------------------------------------------------------------------------
159template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
162DGtal::SimpleMatrix<T, TM, TN> &
163DGtal::SimpleMatrix<T, TM, TN>::operator=(const SimpleMatrix<TC,M,N>& other)
165 for ( DGtal::Dimension i = 0; i < M*N; ++i )
166 myValues[ i ] = static_cast<T>(other.myValues[i]);
170//------------------------------------------------------------------------------
171template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
173DGtal::SimpleMatrix<T, TM, TN>
174DGtal::SimpleMatrix<T, TM, TN>::operator+(const Self& other) const
176 SimpleMatrix<T,TM,TN> res;
177 for ( DGtal::Dimension i = 0; i < M*N; ++i )
178 res.myValues[ i ] = this->myValues[i] + other.myValues[i];
181//------------------------------------------------------------------------------
182template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
184DGtal::SimpleMatrix<T, TM, TN> &
185DGtal::SimpleMatrix<T, TM, TN>::operator+=(const Self& other)
187 for ( DGtal::Dimension i = 0; i < M*N; ++i )
188 myValues[ i ] += other.myValues[i];
191//------------------------------------------------------------------------------
192template<typename T, DGtal::Dimension M, DGtal::Dimension N>
195DGtal::SimpleMatrix<T, M,N>::cofactor(const DGtal::Dimension i,
196 const DGtal::Dimension j ) const
198 BOOST_STATIC_ASSERT(M == N);
199 return minorDeterminant(i,j)*myCofactorCoefs[i*N+j];
201//------------------------------------------------------------------------------
202template<typename T, DGtal::Dimension M, DGtal::Dimension N>
204DGtal::SimpleMatrix<T, M,N>
205DGtal::SimpleMatrix<T, M,N>::cofactor( ) const
207 DGtal::SimpleMatrix<T, M,N> mat;
208 BOOST_STATIC_ASSERT(M == N);
210 for (DGtal::Dimension i=0; i<M; i++)
211 for (DGtal::Dimension j=0; j<M; j++)
212 mat.setComponent( i, j , cofactor(i,j));
216//------------------------------------------------------------------------------
217template<typename T, DGtal::Dimension M, DGtal::Dimension N>
220DGtal::SimpleMatrix<T, M,N>::minorDeterminant(const DGtal::Dimension i,
221 const DGtal::Dimension j) const
223 return SimpleMatrixSpecializations<Self,M,N>::minorDeterminant(*this,i,j);
225//------------------------------------------------------------------------------
226template<typename T, DGtal::Dimension M, DGtal::Dimension N>
229DGtal::SimpleMatrix<T, M,N>::determinant() const
231 return SimpleMatrixSpecializations<Self,M,N>::determinant(*this);
234 //------------------------------------------------------------------------------
235template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
237typename DGtal::SimpleMatrix<T, TM, TN>
238DGtal::SimpleMatrix<T, TM, TN>::inverse() const
240 BOOST_STATIC_ASSERT(TM == TN);
242 SimpleMatrix<T,TM,TM> r = cofactor().transpose();
245 T det = determinant();
250//------------------------------------------------------------------------------
251template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
253DGtal::SimpleMatrix<T, TM, TN>
254DGtal::SimpleMatrix<T, TM, TN>::operator-(const Self& other) const
256 SimpleMatrix<T,TM,TN> res;
257 for ( DGtal::Dimension i = 0; i < M*N; ++i )
258 res.myValues[ i ] = this->myValues[i] - other.myValues[i];
261//------------------------------------------------------------------------------
262template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
264DGtal::SimpleMatrix<T, TM, TN> &
265DGtal::SimpleMatrix<T, TM, TN>::operator-=(const Self& other)
267 for ( DGtal::Dimension i = 0; i < M*N; ++i )
268 myValues[ i ] -= other.myValues[i];
271//------------------------------------------------------------------------------
272template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
275DGtal::SimpleMatrix<T, TM, TN>::operator==(const Self& other) const
277 return myValues == other.myValues;
280//------------------------------------------------------------------------------
281template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
283typename DGtal::SimpleMatrix<T, TN, TM>
284DGtal::SimpleMatrix<T, TM, TN>::transpose() const
286 DGtal::SimpleMatrix<T, TN, TM> res;
287 for (DGtal::Dimension i=0; i<M; i++)
288 for (DGtal::Dimension j=0; j<N; j++)
289 res.setComponent(j,i, this->operator()(i,j));
292//------------------------------------------------------------------------------
293template<typename T, DGtal::Dimension M, DGtal::Dimension N>
295typename DGtal::SimpleMatrix<T,M,N>::ColumnVector
296DGtal::SimpleMatrix<T, M, N>::operator*(const RowVector& other) const
299 for (DGtal::Dimension i=0; i<M; i++)
300 for (DGtal::Dimension k=0; k<N; k++)
301 res[i] += this->operator()(i, k )*other[k];
305//------------------------------------------------------------------------------
306template<typename T, DGtal::Dimension M, DGtal::Dimension N>
308typename DGtal::SimpleMatrix<T,M,M>
309DGtal::SimpleMatrix<T, M, N>::operator*(const DGtal::SimpleMatrix<T,N,M>& other) const
311 SimpleMatrix<T,M,M> res;
312 T e = NumberTraits<T>::ZERO;
313 for (DGtal::Dimension i=0; i<M; i++)
314 for (DGtal::Dimension j=0; j<M; j++)
316 for (DGtal::Dimension k=0; k<N; k++)
318 e += this->operator()(i, k )*other(k ,j );
321 res.setComponent(i,j,e);
323 e = NumberTraits<T>::ZERO;
327//------------------------------------------------------------------------------
328template<typename T, DGtal::Dimension M, DGtal::Dimension N>
330DGtal::SimpleMatrix<T, M,N> &
331DGtal::SimpleMatrix<T, M, N>::operator/=(const T& other)
333 for (DGtal::Dimension i=0; i<M*N; i++)
334 this->myValues[i] /= other;
337}//------------------------------------------------------------------------------
338template<typename T, DGtal::Dimension M, DGtal::Dimension N>
340DGtal::SimpleMatrix<T, M,N>
341DGtal::SimpleMatrix<T, M, N>::operator/(const T& other) const
344 for (DGtal::Dimension i=0; i<M*N; i++)
345 resultat.myValues[i] = myValues[i]/other;
349//------------------------------------------------------------------------------
350template<typename T, DGtal::Dimension M, DGtal::Dimension N>
352DGtal::SimpleMatrix<T, M,N> &
353DGtal::SimpleMatrix<T, M,N>::operator*=(const T& other)
355 for (DGtal::Dimension i=0; i<M*N; i++)
356 this->myValues[i] *= other;
360//------------------------------------------------------------------------------
361template<typename T, DGtal::Dimension M, DGtal::Dimension N>
363DGtal::SimpleMatrix<T, M,N>
364DGtal::SimpleMatrix<T, M,N>::operator*(const T& other) const
367 for (DGtal::Dimension i=0; i<M*N; i++)
368 resultat.myValues[i] = other*myValues[i];
373//------------------------------------------------------------------------------
374template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
377DGtal::SimpleMatrix<T, TM, TN>::setComponent(const DGtal::Dimension i,
378 const DGtal::Dimension j,
383 myValues[i*N + j] = aValue;
385//------------------------------------------------------------------------------
386template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
389DGtal::SimpleMatrix<T, TM, TN>::operator()(const DGtal::Dimension i,
390 const DGtal::Dimension j) const
394 return myValues[i*N + j];
396//------------------------------------------------------------------------------
397template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
400DGtal::SimpleMatrix<T, TM, TN>::operator()(const DGtal::Dimension i,
401 const DGtal::Dimension j)
405 return myValues[i*N + j];
408///////////////////////////////////////////////////////////////////////////////
409// Interface - public :
412 * Writes/Displays the object on an output stream.
413 * @param out the output stream where the object is written.
415template <typename T, DGtal::Dimension TM, DGtal::Dimension TN >
418DGtal::SimpleMatrix<T,TM,TN>::selfDisplay ( std::ostream & out ) const
420 out << "[SimpleMatrix] "<<M<<"x"<<N<< " [";
421 for(DGtal::Dimension i = 0; i < M; ++i)
424 for(DGtal::Dimension j = 0; j < N; ++j)
425 out<< this->operator()(i,j)<<" ";
432 * Checks the validity/consistency of the object.
433 * @return 'true' if the object is valid, 'false' otherwise.
435template <typename T,DGtal::Dimension M,DGtal::Dimension N>
438DGtal::SimpleMatrix<T,M,N>::isValid() const
447///////////////////////////////////////////////////////////////////////////////
448// Implementation of inline functions //
450template <typename T,DGtal::Dimension M,DGtal::Dimension N>
453DGtal::operator<< ( std::ostream & out,
454 const SimpleMatrix<T,M,N> & object )
456 object.selfDisplay( out );
460template <typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
462DGtal::SimpleMatrix<TComponent, TM, TN>
463DGtal::operator* ( const TComponent& scalar, const DGtal::SimpleMatrix<TComponent, TM, TN>& matrix)
465 return matrix * scalar;