DGtal 1.3.0
Loading...
Searching...
No Matches
SimpleMatrixSpecializations.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 SimpleMatrixSpecializations.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/19
23 *
24 * Implementation of inline methods defined in SimpleMatrixSpecializations.h
25 *
26 * This file is part of the DGtal library.
27 */
28
29
30//////////////////////////////////////////////////////////////////////////////
31#include <cstdlib>
32//////////////////////////////////////////////////////////////////////////////
33///////////////////////////////////////////////////////////////////////////////
34// IMPLEMENTATION of inline methods.
35///////////////////////////////////////////////////////////////////////////////
36
37///////////////////////////////////////////////////////////////////////////////
38// ----------------------- Standard services ------------------------------
39template <typename M, DGtal::Dimension TM, DGtal::Dimension TN>
40inline
41typename DGtal::SimpleMatrixSpecializations<M,TM,TN>::Component
42DGtal::SimpleMatrixSpecializations<M,TM,TN>::minorDeterminant(const Matrix &aM,
43 const DGtal::Dimension ai,
44 const DGtal::Dimension aj)
45{
46 BOOST_STATIC_ASSERT(TM == TN);
47 ASSERT(ai<TM);
48 ASSERT(aj<TN);
49 DGtal::SimpleMatrix<Component,TM-1,TN-1> mat;
50 DGtal::Dimension indexR=0;
51 DGtal::Dimension indexC=0;
52 for (DGtal::Dimension i=0; i<TM; i++)
53 for (DGtal::Dimension j=0; j<TN; j++)
54 {
55 if (i!=ai && j!=aj)
56 {
57 ASSERT(indexR < TM -1);
58 ASSERT(indexC < TN -1);
59 mat.setComponent(indexR,indexC, aM(i,j));
60 indexC++;
61 }
62 if (indexC==TM-1)
63 {
64 indexC=0;
65 indexR++;
66 }
67
68 if (indexR==TM-1)
69 return mat.determinant();
70 }
71
72 return mat.determinant();
73}
74
75template <typename M, DGtal::Dimension TM, DGtal::Dimension TN>
76inline
77typename DGtal::SimpleMatrixSpecializations<M,TM,TN>::Component
78DGtal::SimpleMatrixSpecializations<M,TM,TN>::determinant(const Matrix &aM)
79{
80 BOOST_STATIC_ASSERT(TM == TN);
81
82 Component d = DGtal::NumberTraits<Component>::ZERO;
83 for(DGtal::Dimension i= 0; i< TM; ++i)
84 d += aM(i,0) * aM.cofactor(i,0);
85 return d;
86}
87// ----------------------- Specialization 1x1 ------------------------------
88
89template <typename M>
90inline
91typename DGtal::SimpleMatrixSpecializations<M,1,1>::Component
92DGtal::SimpleMatrixSpecializations<M,1,1>::minorDeterminant(const Matrix &aM,
93 const DGtal::Dimension i,
94 const DGtal::Dimension j)
95{
96 boost::ignore_unused_variable_warning( aM );
97 boost::ignore_unused_variable_warning( i );
98 boost::ignore_unused_variable_warning( j );
99 ASSERT(false && "Not defined for 1x1 matrices");
100 return NumberTraits<Component>::ZERO;
101}
102
103template <typename M>
104inline
105typename DGtal::SimpleMatrixSpecializations<M,1,1>::Component
106DGtal::SimpleMatrixSpecializations<M,1,1>::determinant(const Matrix &aM)
107{
108
109 return aM(0,0);
110}
111// ----------------------- Specialization 2x2 ------------------------------
112
113template <typename M>
114inline
115typename DGtal::SimpleMatrixSpecializations<M,2,2>::Component
116DGtal::SimpleMatrixSpecializations<M,2,2>::minorDeterminant(const Matrix &aM,
117 const DGtal::Dimension i,
118 const DGtal::Dimension j)
119{
120 ASSERT(i<2);
121 ASSERT(j<2);
122 return aM((i+1) % 2,(j+1) % 2);
123}
124
125template <typename M>
126inline
127typename DGtal::SimpleMatrixSpecializations<M,2,2>::Component
128DGtal::SimpleMatrixSpecializations<M,2,2>::determinant(const Matrix &aM)
129{
130
131 return aM(0,0)*aM(1,1) - aM(0,1)*aM(1,0);
132}
133// ----------------------- Specialization 3x3 ------------------------------
134
135template <typename M>
136inline
137typename DGtal::SimpleMatrixSpecializations<M,3,3>::Component
138DGtal::SimpleMatrixSpecializations<M,3,3>::minorDeterminant(const Matrix &aM,
139 const DGtal::Dimension ai,
140 const DGtal::Dimension aj)
141{
142 ASSERT(ai<3);
143 ASSERT(aj<3);
144 DGtal::SimpleMatrix<Component,2,2> mat;
145 DGtal::Dimension indexR=0;
146 DGtal::Dimension indexC=0;
147 for (DGtal::Dimension i=0; i<3; i++)
148 for (DGtal::Dimension j=0; j<3; j++)
149 {
150 if (i!=ai && j!=aj)
151 {
152 ASSERT(indexR < 3 -1);
153 ASSERT(indexC < 3 -1);
154 mat.setComponent(indexR,indexC, aM(i,j));
155 indexC++;
156 }
157 if (indexC==3-1)
158 {
159 indexC=0;
160 indexR++;
161 }
162
163 if (indexR==3-1)
164 return mat.determinant(); //DGtal::SimpleMatrixSpecializations<M,2,2>::determinant(mat);
165 }
166
167 return mat.determinant(); //DGtal::SimpleMatrixSpecializations<M,2,2>::determinant(mat);
168}
169
170template <typename M>
171inline
172typename DGtal::SimpleMatrixSpecializations<M,3,3>::Component
173DGtal::SimpleMatrixSpecializations<M,3,3>::determinant(const Matrix &aM)
174{
175 return aM(0,0) * ( (aM(1,1)*aM(2,2))-
176 (aM(1,2)*aM(2,1)) )-
177 aM(1,0) * ( (aM(0,1)*aM(2,2))-
178 (aM(0,2)*aM(2,1)) )
179 + aM(2,0) * ( (aM(0,1)*aM(1,2))-
180 (aM(0,2)*aM(1,1)) );
181}
182
183// //
184///////////////////////////////////////////////////////////////////////////////
185
186