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 ------------------------------
46 template <typename T, DGtal::Dimension TM, DGtal::Dimension TN>
48 DGtal::SimpleMatrix<T,TM, TN>::~SimpleMatrix()
56 template <typename T, DGtal::Dimension TM, DGtal::Dimension TN>
58 DGtal::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 )
74 template <typename T, DGtal::Dimension TM, DGtal::Dimension TN>
76 DGtal::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 //------------------------------------------------------------------------------
93 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
95 DGtal::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 //---------------------------------------------------------------------------
105 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
108 DGtal::SimpleMatrix<T, TM, TN>::clear()
110 for ( DGtal::Dimension i = 0; i < M*N; ++i )
111 myValues[ i ] = NumberTraits<T>::ZERO;
113 //---------------------------------------------------------------------------
114 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
117 DGtal::SimpleMatrix<T, TM, TN>::constant(const T &aSc)
119 for ( DGtal::Dimension i = 0; i < M*N; ++i )
122 //---------------------------------------------------------------------------
123 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
126 DGtal::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 //---------------------------------------------------------------------------
136 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
138 typename DGtal::SimpleMatrix<T, TM, TN>::RowVector
139 DGtal::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);
147 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
149 typename DGtal::SimpleMatrix<T, TM, TN>::ColumnVector
150 DGtal::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 //---------------------------------------------------------------------------
159 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
160 template<typename TC>
162 DGtal::SimpleMatrix<T, TM, TN> &
163 DGtal::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 //------------------------------------------------------------------------------
171 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
173 DGtal::SimpleMatrix<T, TM, TN>
174 DGtal::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 //------------------------------------------------------------------------------
182 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
184 DGtal::SimpleMatrix<T, TM, TN> &
185 DGtal::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 //------------------------------------------------------------------------------
192 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
195 DGtal::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 //------------------------------------------------------------------------------
202 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
204 DGtal::SimpleMatrix<T, M,N>
205 DGtal::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 //------------------------------------------------------------------------------
217 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
220 DGtal::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 //------------------------------------------------------------------------------
226 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
229 DGtal::SimpleMatrix<T, M,N>::determinant() const
231 return SimpleMatrixSpecializations<Self,M,N>::determinant(*this);
234 //------------------------------------------------------------------------------
235 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
237 typename DGtal::SimpleMatrix<T, TM, TN>
238 DGtal::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 //------------------------------------------------------------------------------
251 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
253 DGtal::SimpleMatrix<T, TM, TN>
254 DGtal::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 //------------------------------------------------------------------------------
262 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
264 DGtal::SimpleMatrix<T, TM, TN> &
265 DGtal::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 //------------------------------------------------------------------------------
272 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
275 DGtal::SimpleMatrix<T, TM, TN>::operator==(const Self& other) const
277 return myValues == other.myValues;
280 //------------------------------------------------------------------------------
281 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
283 typename DGtal::SimpleMatrix<T, TN, TM>
284 DGtal::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 //------------------------------------------------------------------------------
293 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
295 typename DGtal::SimpleMatrix<T,M,N>::ColumnVector
296 DGtal::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 //------------------------------------------------------------------------------
306 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
308 typename DGtal::SimpleMatrix<T,M,M>
309 DGtal::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 //------------------------------------------------------------------------------
328 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
330 DGtal::SimpleMatrix<T, M,N> &
331 DGtal::SimpleMatrix<T, M, N>::operator/=(const T& other)
333 for (DGtal::Dimension i=0; i<M*N; i++)
334 this->myValues[i] /= other;
337 }//------------------------------------------------------------------------------
338 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
340 DGtal::SimpleMatrix<T, M,N>
341 DGtal::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 //------------------------------------------------------------------------------
350 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
352 DGtal::SimpleMatrix<T, M,N> &
353 DGtal::SimpleMatrix<T, M,N>::operator*=(const T& other)
355 for (DGtal::Dimension i=0; i<M*N; i++)
356 this->myValues[i] *= other;
360 //------------------------------------------------------------------------------
361 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
363 DGtal::SimpleMatrix<T, M,N>
364 DGtal::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 //------------------------------------------------------------------------------
374 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
377 DGtal::SimpleMatrix<T, TM, TN>::setComponent(const DGtal::Dimension i,
378 const DGtal::Dimension j,
383 myValues[i*N + j] = aValue;
385 //------------------------------------------------------------------------------
386 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
389 DGtal::SimpleMatrix<T, TM, TN>::operator()(const DGtal::Dimension i,
390 const DGtal::Dimension j) const
394 return myValues[i*N + j];
396 //------------------------------------------------------------------------------
397 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
400 DGtal::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.
415 template <typename T, DGtal::Dimension TM, DGtal::Dimension TN >
418 DGtal::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.
435 template <typename T,DGtal::Dimension M,DGtal::Dimension N>
438 DGtal::SimpleMatrix<T,M,N>::isValid() const
447 ///////////////////////////////////////////////////////////////////////////////
448 // Implementation of inline functions //
450 template <typename T,DGtal::Dimension M,DGtal::Dimension N>
453 DGtal::operator<< ( std::ostream & out,
454 const SimpleMatrix<T,M,N> & object )
456 object.selfDisplay( out );
460 template <typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
462 DGtal::SimpleMatrix<TComponent, TM, TN>
463 DGtal::operator* ( const TComponent& scalar, const DGtal::SimpleMatrix<TComponent, TM, TN>& matrix)
465 return matrix * scalar;