1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18 #ifndef itkMatrix_hxx
19 #define itkMatrix_hxx
20
21 #include "itkMatrix.h"
22 #include "itkNumericTraits.h"
23
24 namespace itk
25 {
26 /**
27 * Product by a Vector
28 */
29 template< typename T, unsigned int NRows, unsigned int NColumns >
30 Vector< T, NRows >
31 Matrix< T, NRows, NColumns >
operator *(const Vector<T,NColumns> & vect) const32 ::operator*(const Vector< T, NColumns > & vect) const
33 {
34 Vector< T, NRows > result;
35 for ( unsigned int r = 0; r < NRows; r++ )
36 {
37 T sum = NumericTraits< T >::ZeroValue();
38 for ( unsigned int c = 0; c < NColumns; c++ )
39 {
40 sum += m_Matrix(r, c) * vect[c];
41 }
42 result[r] = sum;
43 }
44 return result;
45 }
46
47 /**
48 * Product by a Point
49 */
50 template< typename T, unsigned int NRows, unsigned int NColumns >
51 Point< T, NRows >
52 Matrix< T, NRows, NColumns >
operator *(const Point<T,NColumns> & pnt) const53 ::operator*(const Point< T, NColumns > & pnt) const
54 {
55 Point< T, NRows > result;
56 for ( unsigned int r = 0; r < NRows; r++ )
57 {
58 T sum = NumericTraits< T >::ZeroValue();
59 for ( unsigned int c = 0; c < NColumns; c++ )
60 {
61 sum += m_Matrix(r, c) * pnt[c];
62 }
63 result[r] = sum;
64 }
65 return result;
66 }
67
68 /**
69 * Product by a vnl_vector_fixed
70 */
71 template< typename T, unsigned int NRows, unsigned int NColumns >
72 vnl_vector_fixed< T, NRows >
73 Matrix< T, NRows, NColumns >
operator *(const vnl_vector_fixed<T,NColumns> & inVNLvect) const74 ::operator*(const vnl_vector_fixed< T, NColumns > & inVNLvect) const
75 {
76 vnl_vector_fixed< T, NRows > result;
77 for ( unsigned int r = 0; r < NRows; r++ )
78 {
79 T sum = NumericTraits< T >::ZeroValue();
80 for ( unsigned int c = 0; c < NColumns; c++ )
81 {
82 sum += m_Matrix(r, c) * inVNLvect[c];
83 }
84 result[r] = sum;
85 }
86 return result;
87 }
88
89 /**
90 * Product by a CovariantVector
91 */
92 template< typename T, unsigned int NRows, unsigned int NColumns >
93 CovariantVector< T, NRows >
94 Matrix< T, NRows, NColumns >
operator *(const CovariantVector<T,NColumns> & covect) const95 ::operator*(const CovariantVector< T, NColumns > & covect) const
96 {
97 CovariantVector< T, NRows > result;
98 for ( unsigned int r = 0; r < NRows; r++ )
99 {
100 T sum = NumericTraits< T >::ZeroValue();
101 for ( unsigned int c = 0; c < NColumns; c++ )
102 {
103 sum += m_Matrix(r, c) * covect[c];
104 }
105 result[r] = sum;
106 }
107 return result;
108 }
109
110 /**
111 * Product by a matrix
112 */
113 template< typename T, unsigned int NRows, unsigned int NColumns >
114 Matrix< T, NRows, NColumns >
115 Matrix< T, NRows, NColumns >
operator *(const CompatibleSquareMatrixType & matrix) const116 ::operator*(const CompatibleSquareMatrixType & matrix) const
117 {
118 const Self result( m_Matrix * matrix.GetVnlMatrix() );
119 return result;
120 }
121
122 /**
123 * Matrix Addition
124 */
125 template< typename T, unsigned int NRows, unsigned int NColumns >
126 Matrix< T, NRows, NColumns >
127 Matrix< T, NRows, NColumns >
operator +(const Self & matrix) const128 ::operator+(const Self & matrix) const
129 {
130 Self result;
131
132 for ( unsigned int r = 0; r < NRows; r++ )
133 {
134 for ( unsigned int c = 0; c < NColumns; c++ )
135 {
136 result.m_Matrix(r, c) = m_Matrix(r, c) + matrix.m_Matrix(r, c);
137 }
138 }
139 return result;
140 }
141
142 /**
143 * Matrix Addition in-place
144 */
145 template< typename T, unsigned int NRows, unsigned int NColumns >
146 const Matrix< T, NRows, NColumns > &
147 Matrix< T, NRows, NColumns >
operator +=(const Self & matrix)148 ::operator+=(const Self & matrix)
149 {
150 for ( unsigned int r = 0; r < NRows; r++ )
151 {
152 for ( unsigned int c = 0; c < NColumns; c++ )
153 {
154 m_Matrix(r, c) += matrix.m_Matrix(r, c);
155 }
156 }
157 return *this;
158 }
159
160 /**
161 * Matrix Subtraction
162 */
163 template< typename T, unsigned int NRows, unsigned int NColumns >
164 Matrix< T, NRows, NColumns >
165 Matrix< T, NRows, NColumns >
operator -(const Self & matrix) const166 ::operator-(const Self & matrix) const
167 {
168 Self result;
169
170 for ( unsigned int r = 0; r < NRows; r++ )
171 {
172 for ( unsigned int c = 0; c < NColumns; c++ )
173 {
174 result.m_Matrix(r, c) = m_Matrix(r, c) - matrix.m_Matrix(r, c);
175 }
176 }
177 return result;
178 }
179
180 /**
181 * Matrix subtraction in-place
182 */
183 template< typename T, unsigned int NRows, unsigned int NColumns >
184 const Matrix< T, NRows, NColumns > &
185 Matrix< T, NRows, NColumns >
operator -=(const Self & matrix)186 ::operator-=(const Self & matrix)
187 {
188 for ( unsigned int r = 0; r < NRows; r++ )
189 {
190 for ( unsigned int c = 0; c < NColumns; c++ )
191 {
192 m_Matrix(r, c) -= matrix.m_Matrix(r, c);
193 }
194 }
195 return *this;
196 }
197
198 /**
199 * Product by a vnl_matrix
200 */
201 template< typename T, unsigned int NRows, unsigned int NColumns >
202 vnl_matrix< T >
203 Matrix< T, NRows, NColumns >
operator *(const vnl_matrix<T> & matrix) const204 ::operator*(const vnl_matrix< T > & matrix) const
205 {
206 return m_Matrix * matrix;
207 }
208
209 /**
210 * Product by a matrix
211 */
212 template< typename T, unsigned int NRows, unsigned int NColumns >
213 void
214 Matrix< T, NRows, NColumns >
operator *=(const CompatibleSquareMatrixType & matrix)215 ::operator*=(const CompatibleSquareMatrixType & matrix)
216 {
217 m_Matrix *= matrix.GetVnlMatrix();
218 }
219
220 /**
221 * Product by a vnl_matrix
222 */
223 template< typename T, unsigned int NRows, unsigned int NColumns >
224 void
225 Matrix< T, NRows, NColumns >
operator *=(const vnl_matrix<T> & matrix)226 ::operator*=(const vnl_matrix< T > & matrix)
227 {
228 m_Matrix *= matrix;
229 }
230
231 /**
232 * Product by a vnl_vector
233 */
234 template< typename T, unsigned int NRows, unsigned int NColumns >
235 vnl_vector< T >
236 Matrix< T, NRows, NColumns >
operator *(const vnl_vector<T> & vc) const237 ::operator*(const vnl_vector< T > & vc) const
238 {
239 return m_Matrix * vc;
240 }
241 } // end namespace itk
242
243 #endif
244