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 )
70 //------------------------------------------------------------------------------
72 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
74 DGtal::SimpleMatrix<T ,TM, TN>::SimpleMatrix(const Self& other)
76 for ( DGtal::Dimension i = 0; i < M*N; ++i )
78 myValues[ i ] = other.myValues[i];
79 myCofactorCoefs[ i ] = other.myCofactorCoefs[i];
83 //---------------------------------------------------------------------------
84 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
87 DGtal::SimpleMatrix<T, TM, TN>::clear()
89 for ( DGtal::Dimension i = 0; i < M*N; ++i )
90 myValues[ i ] = NumberTraits<T>::ZERO;
92 //---------------------------------------------------------------------------
93 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
96 DGtal::SimpleMatrix<T, TM, TN>::constant(const T &aSc)
98 for ( DGtal::Dimension i = 0; i < M*N; ++i )
101 //---------------------------------------------------------------------------
102 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
105 DGtal::SimpleMatrix<T, M,N>::identity( )
107 BOOST_STATIC_ASSERT( M == N);
111 for (DGtal::Dimension i=0; i<M; i++)
112 this->setComponent( i, i , NumberTraits<T>::ONE);
114 //---------------------------------------------------------------------------
115 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
117 typename DGtal::SimpleMatrix<T, TM, TN>::RowVector
118 DGtal::SimpleMatrix<T, TM, TN>::row(const DGtal::Dimension i) const
122 for ( DGtal::Dimension j = 0; j < N; ++j )
123 v[ j ] = this->operator()(i,j);
126 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
128 typename DGtal::SimpleMatrix<T, TM, TN>::ColumnVector
129 DGtal::SimpleMatrix<T, TM, TN>::column(const DGtal::Dimension j) const
133 for ( DGtal::Dimension i = 0; i < M; ++i )
134 v[ i ] = this->operator()(i,j);
137 //---------------------------------------------------------------------------
138 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
139 template<typename TC>
141 DGtal::SimpleMatrix<T, TM, TN> &
142 DGtal::SimpleMatrix<T, TM, TN>::operator=(const SimpleMatrix<TC,M,N>& other)
144 for ( DGtal::Dimension i = 0; i < M*N; ++i )
145 myValues[ i ] = static_cast<T>(other.myValues[i]);
149 //------------------------------------------------------------------------------
150 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
152 DGtal::SimpleMatrix<T, TM, TN>
153 DGtal::SimpleMatrix<T, TM, TN>::operator+(const Self& other) const
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];
160 //------------------------------------------------------------------------------
161 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
163 DGtal::SimpleMatrix<T, TM, TN> &
164 DGtal::SimpleMatrix<T, TM, TN>::operator+=(const Self& other)
166 for ( DGtal::Dimension i = 0; i < M*N; ++i )
167 myValues[ i ] += other.myValues[i];
170 //------------------------------------------------------------------------------
171 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
174 DGtal::SimpleMatrix<T, M,N>::cofactor(const DGtal::Dimension i,
175 const DGtal::Dimension j ) const
177 BOOST_STATIC_ASSERT(M == N);
178 return minorDeterminant(i,j)*myCofactorCoefs[i*N+j];
180 //------------------------------------------------------------------------------
181 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
183 DGtal::SimpleMatrix<T, M,N>
184 DGtal::SimpleMatrix<T, M,N>::cofactor( ) const
186 DGtal::SimpleMatrix<T, M,N> mat;
187 BOOST_STATIC_ASSERT(M == N);
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));
195 //------------------------------------------------------------------------------
196 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
199 DGtal::SimpleMatrix<T, M,N>::minorDeterminant(const DGtal::Dimension i,
200 const DGtal::Dimension j) const
202 return SimpleMatrixSpecializations<Self,M,N>::minorDeterminant(*this,i,j);
204 //------------------------------------------------------------------------------
205 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
208 DGtal::SimpleMatrix<T, M,N>::determinant() const
210 return SimpleMatrixSpecializations<Self,M,N>::determinant(*this);
213 //------------------------------------------------------------------------------
214 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
216 typename DGtal::SimpleMatrix<T, TM, TN>
217 DGtal::SimpleMatrix<T, TM, TN>::inverse() const
219 BOOST_STATIC_ASSERT(TM == TN);
221 SimpleMatrix<T,TM,TM> r = cofactor().transpose();
224 T det = determinant();
229 //------------------------------------------------------------------------------
230 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
232 DGtal::SimpleMatrix<T, TM, TN>
233 DGtal::SimpleMatrix<T, TM, TN>::operator-(const Self& other) const
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];
240 //------------------------------------------------------------------------------
241 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
243 DGtal::SimpleMatrix<T, TM, TN> &
244 DGtal::SimpleMatrix<T, TM, TN>::operator-=(const Self& other)
246 for ( DGtal::Dimension i = 0; i < M*N; ++i )
247 myValues[ i ] -= other.myValues[i];
250 //------------------------------------------------------------------------------
251 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
254 DGtal::SimpleMatrix<T, TM, TN>::operator==(const Self& other) const
256 return myValues == other.myValues;
259 //------------------------------------------------------------------------------
260 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
262 typename DGtal::SimpleMatrix<T, TN, TM>
263 DGtal::SimpleMatrix<T, TM, TN>::transpose() const
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));
271 //------------------------------------------------------------------------------
272 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
274 typename DGtal::SimpleMatrix<T,M,N>::ColumnVector
275 DGtal::SimpleMatrix<T, M, N>::operator*(const RowVector& other) const
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];
284 //------------------------------------------------------------------------------
285 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
287 typename DGtal::SimpleMatrix<T,M,M>
288 DGtal::SimpleMatrix<T, M, N>::operator*(const DGtal::SimpleMatrix<T,N,M>& other) const
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++)
295 for (DGtal::Dimension k=0; k<N; k++)
297 e += this->operator()(i, k )*other(k ,j );
300 res.setComponent(i,j,e);
302 e = NumberTraits<T>::ZERO;
306 //------------------------------------------------------------------------------
307 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
309 DGtal::SimpleMatrix<T, M,N> &
310 DGtal::SimpleMatrix<T, M, N>::operator/=(const T& other)
312 for (DGtal::Dimension i=0; i<M*N; i++)
313 this->myValues[i] /= other;
316 }//------------------------------------------------------------------------------
317 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
319 DGtal::SimpleMatrix<T, M,N>
320 DGtal::SimpleMatrix<T, M, N>::operator/(const T& other) const
323 for (DGtal::Dimension i=0; i<M*N; i++)
324 resultat.myValues[i] = myValues[i]/other;
328 //------------------------------------------------------------------------------
329 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
331 DGtal::SimpleMatrix<T, M,N> &
332 DGtal::SimpleMatrix<T, M,N>::operator*=(const T& other)
334 for (DGtal::Dimension i=0; i<M*N; i++)
335 this->myValues[i] *= other;
339 //------------------------------------------------------------------------------
340 template<typename T, DGtal::Dimension M, DGtal::Dimension N>
342 DGtal::SimpleMatrix<T, M,N>
343 DGtal::SimpleMatrix<T, M,N>::operator*(const T& other) const
346 for (DGtal::Dimension i=0; i<M*N; i++)
347 resultat.myValues[i] = other*myValues[i];
352 //------------------------------------------------------------------------------
353 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
356 DGtal::SimpleMatrix<T, TM, TN>::setComponent(const DGtal::Dimension i,
357 const DGtal::Dimension j,
362 myValues[i*N + j] = aValue;
364 //------------------------------------------------------------------------------
365 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
368 DGtal::SimpleMatrix<T, TM, TN>::operator()(const DGtal::Dimension i,
369 const DGtal::Dimension j) const
373 return myValues[i*N + j];
375 //------------------------------------------------------------------------------
376 template<typename T, DGtal::Dimension TM, DGtal::Dimension TN>
379 DGtal::SimpleMatrix<T, TM, TN>::operator()(const DGtal::Dimension i,
380 const DGtal::Dimension j)
384 return myValues[i*N + j];
387 ///////////////////////////////////////////////////////////////////////////////
388 // Interface - public :
391 * Writes/Displays the object on an output stream.
392 * @param out the output stream where the object is written.
394 template <typename T, DGtal::Dimension TM, DGtal::Dimension TN >
397 DGtal::SimpleMatrix<T,TM,TN>::selfDisplay ( std::ostream & out ) const
399 out << "[SimpleMatrix] "<<M<<"x"<<N<< " [";
400 for(DGtal::Dimension i = 0; i < M; ++i)
403 for(DGtal::Dimension j = 0; j < N; ++j)
404 out<< this->operator()(i,j)<<" ";
411 * Checks the validity/consistency of the object.
412 * @return 'true' if the object is valid, 'false' otherwise.
414 template <typename T,DGtal::Dimension M,DGtal::Dimension N>
417 DGtal::SimpleMatrix<T,M,N>::isValid() const
426 ///////////////////////////////////////////////////////////////////////////////
427 // Implementation of inline functions //
429 template <typename T,DGtal::Dimension M,DGtal::Dimension N>
432 DGtal::operator<< ( std::ostream & out,
433 const SimpleMatrix<T,M,N> & object )
435 object.selfDisplay( out );
439 template <typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
441 DGtal::SimpleMatrix<TComponent, TM, TN>
442 operator* ( const TComponent& scalar, const DGtal::SimpleMatrix<TComponent, TM, TN>& matrix)
444 return matrix * scalar;