Commit c6e119faadaded94f9de78ccd4ead74df2036dcf

Authored by knodlseder
1 parent a9fe3cb2

Add missing return statements and remove range checks for size mismatch verification

src/GMatrix.cpp
@@ -134,6 +134,51 @@ ostream& operator<< (ostream& os, const GMatrix& m) @@ -134,6 +134,51 @@ ostream& operator<< (ostream& os, const GMatrix& m)
134 if (row != m.m_rows-1) 134 if (row != m.m_rows-1)
135 os << endl; 135 os << endl;
136 } 136 }
  137 + return os;
  138 +}
  139 +
  140 +
  141 +/***************************************************************************
  142 + * GMatrix * Gvector multiplication *
  143 + ***************************************************************************/
  144 +GVector GMatrix::operator* (const GVector& v) const
  145 +{
  146 + // Raise an exception if the matrix and vector dimensions are not compatible
  147 + if (m_cols != v.m_num)
  148 + throw vec_mat_mismatch("GMatrix*GVector operator", v.m_num, m_rows, m_cols);
  149 +
  150 + // Perform vector multiplication
  151 + GVector result(m_rows);
  152 + for (unsigned row = 0; row < m_rows; ++row) {
  153 + double sum = 0.0;
  154 + for (unsigned col = 0; col < m_cols; ++col)
  155 + sum += (*this)(row,col) * v(col);
  156 + result(row) = sum;
  157 + }
  158 + return result;
  159 +}
  160 +
  161 +
  162 +/***************************************************************************
  163 + * GMatrix * GMatrix multiplication *
  164 + ***************************************************************************/
  165 +GMatrix GMatrix::operator* (const GMatrix& m) const
  166 +{
  167 + // Raise an exception if the matrix dimensions are not compatible
  168 + if (m_cols != m.m_rows)
  169 + throw GMatrix::dim_mult_mismatch("GMatrix multiplication", m_cols, m.m_rows);
  170 +
  171 + // Perform matrix multiplication
  172 + GMatrix result(m_rows,m.m_cols);
  173 + for (unsigned row = 0; row < m_rows; ++row) {
  174 + for (unsigned col = 0; col < m.m_cols; ++col) {
  175 + double sum = 0.0;
  176 + for (unsigned i = 0; i < m_cols; ++i)
  177 + sum += (*this)(row,i)*m(i,col);
  178 + result(row,col) = sum;
  179 + }
  180 + }
  181 + return result;
137 } 182 }
138 183
139 184
src/GMatrix.hpp
@@ -71,7 +71,7 @@ public: @@ -71,7 +71,7 @@ public:
71 GMatrix operator+ (const GMatrix& m) const; 71 GMatrix operator+ (const GMatrix& m) const;
72 GMatrix operator- (const GMatrix& m) const; 72 GMatrix operator- (const GMatrix& m) const;
73 GMatrix operator* (const GMatrix& m) const; 73 GMatrix operator* (const GMatrix& m) const;
74 - GVector operator* (const GVector& v) const; 74 + virtual GVector operator* (const GVector& v) const;
75 GMatrix operator+ (const double& d) const; 75 GMatrix operator+ (const double& d) const;
76 GMatrix operator- (const double& d) const; 76 GMatrix operator- (const double& d) const;
77 GMatrix operator* (const double& d) const; 77 GMatrix operator* (const double& d) const;
@@ -128,34 +128,14 @@ protected: @@ -128,34 +128,14 @@ protected:
128 128
129 129
130 /*************************************************************************** 130 /***************************************************************************
131 - * Generic inline members (to be herited) * 131 + * Generic inline members (to be inherited) *
132 ***************************************************************************/ 132 ***************************************************************************/
133 -// Matrix/Vector multiplication: GMatrix * GVector  
134 -inline  
135 -GVector GMatrix::operator* (const GVector& v) const  
136 -{  
137 - #if G_RANGE_CHECK  
138 - if (m_cols != v.m_num)  
139 - throw vec_mat_mismatch("GMatrix*GVector operator", v.m_num, m_rows, m_cols);  
140 - #endif  
141 - GVector result(m_rows);  
142 - for (unsigned row = 0; row < m_rows; ++row) {  
143 - double sum = 0.0;  
144 - for (unsigned col = 0; col < m_cols; ++col)  
145 - sum += (*this)(row,col) * v(col);  
146 - result(row) = sum;  
147 - }  
148 - return result;  
149 -}  
150 -  
151 // Matrix unary addition operator: GMatrix += GMatrix 133 // Matrix unary addition operator: GMatrix += GMatrix
152 inline 134 inline
153 GMatrix& GMatrix::operator+= (const GMatrix& m) 135 GMatrix& GMatrix::operator+= (const GMatrix& m)
154 { 136 {
155 - #if G_RANGE_CHECK  
156 if (m_rows != m.m_rows || m_cols != m.m_cols) 137 if (m_rows != m.m_rows || m_cols != m.m_cols)
157 throw dim_add_mismatch("GMatrix += operator", m_rows, m.m_rows, m_cols, m.m_cols); 138 throw dim_add_mismatch("GMatrix += operator", m_rows, m.m_rows, m_cols, m.m_cols);
158 - #endif  
159 for (unsigned i = 0; i < m_elements; ++i) 139 for (unsigned i = 0; i < m_elements; ++i)
160 m_data[i] += m.m_data[i]; 140 m_data[i] += m.m_data[i];
161 return *this; 141 return *this;
@@ -165,10 +145,8 @@ GMatrix&amp; GMatrix::operator+= (const GMatrix&amp; m) @@ -165,10 +145,8 @@ GMatrix&amp; GMatrix::operator+= (const GMatrix&amp; m)
165 inline 145 inline
166 GMatrix& GMatrix::operator-= (const GMatrix& m) 146 GMatrix& GMatrix::operator-= (const GMatrix& m)
167 { 147 {
168 - #if G_RANGE_CHECK  
169 if (m_rows != m.m_rows || m_cols != m.m_cols) 148 if (m_rows != m.m_rows || m_cols != m.m_cols)
170 throw dim_add_mismatch("GMatrix -= operator", m_rows, m.m_rows, m_cols, m.m_cols); 149 throw dim_add_mismatch("GMatrix -= operator", m_rows, m.m_rows, m_cols, m.m_cols);
171 - #endif  
172 for (unsigned i = 0; i < m_elements; ++i) 150 for (unsigned i = 0; i < m_elements; ++i)
173 m_data[i] -= m.m_data[i]; 151 m_data[i] -= m.m_data[i];
174 return *this; 152 return *this;
@@ -313,7 +291,7 @@ GMatrix&amp; GMatrix::operator= (const double&amp; d) @@ -313,7 +291,7 @@ GMatrix&amp; GMatrix::operator= (const double&amp; d)
313 inline 291 inline
314 double& GMatrix::operator() (unsigned row, unsigned col) 292 double& GMatrix::operator() (unsigned row, unsigned col)
315 { 293 {
316 - #if G_RANGE_CHECK 294 + #if defined(G_RANGE_CHECK)
317 if (row >= m_rows || col >= m_cols) 295 if (row >= m_rows || col >= m_cols)
318 throw out_of_range("GMatrix access", row, col, m_rows, m_cols); 296 throw out_of_range("GMatrix access", row, col, m_rows, m_cols);
319 #endif 297 #endif
@@ -324,7 +302,7 @@ double&amp; GMatrix::operator() (unsigned row, unsigned col) @@ -324,7 +302,7 @@ double&amp; GMatrix::operator() (unsigned row, unsigned col)
324 inline 302 inline
325 const double& GMatrix::operator() (unsigned row, unsigned col) const 303 const double& GMatrix::operator() (unsigned row, unsigned col) const
326 { 304 {
327 - #if G_RANGE_CHECK 305 + #if defined(G_RANGE_CHECK)
328 if (row >= m_rows || col >= m_cols) 306 if (row >= m_rows || col >= m_cols)
329 throw out_of_range("GMatrix access", row, col, m_rows, m_cols); 307 throw out_of_range("GMatrix access", row, col, m_rows, m_cols);
330 #endif 308 #endif
@@ -349,26 +327,6 @@ GMatrix GMatrix::operator- (const GMatrix&amp; m) const @@ -349,26 +327,6 @@ GMatrix GMatrix::operator- (const GMatrix&amp; m) const
349 return result; 327 return result;
350 } 328 }
351 329
352 -// Matrix multiplication: GMatrix * GMatrix  
353 -inline  
354 -GMatrix GMatrix::operator* (const GMatrix& m) const  
355 -{  
356 - #if G_RANGE_CHECK  
357 - if (m_cols != m.m_rows)  
358 - throw GMatrix::dim_mult_mismatch("GMatrix multiplication", m_cols, m.m_rows);  
359 - #endif  
360 - GMatrix result(m_rows,m.m_cols);  
361 - for (unsigned row = 0; row < m_rows; ++row) {  
362 - for (unsigned col = 0; col < m.m_cols; ++col) {  
363 - double sum = 0.0;  
364 - for (unsigned i = 0; i < m_cols; ++i)  
365 - sum += (*this)(row,i)*m(i,col);  
366 - result(row,col) = sum;  
367 - }  
368 - }  
369 - return result;  
370 -}  
371 -  
372 // Matrix scaling: GMatrix + double 330 // Matrix scaling: GMatrix + double
373 inline 331 inline
374 GMatrix GMatrix::operator+ (const double& d) const 332 GMatrix GMatrix::operator+ (const double& d) const
src/GSymMatrix.cpp
@@ -114,6 +114,29 @@ GSymMatrix&amp; GSymMatrix::operator= (const GSymMatrix&amp; m) @@ -114,6 +114,29 @@ GSymMatrix&amp; GSymMatrix::operator= (const GSymMatrix&amp; m)
114 114
115 115
116 /*************************************************************************** 116 /***************************************************************************
  117 + * GSymMatrix * GSymMatrix multiplication *
  118 + ***************************************************************************/
  119 +GSymMatrix GSymMatrix::operator* (const GSymMatrix& m) const
  120 +{
  121 + // Raise an exception if the matrix dimensions are not compatible
  122 + if (m_cols != m.m_rows)
  123 + throw GMatrix::dim_mult_mismatch("GSymMatrix multiplication", m_cols, m.m_rows);
  124 +
  125 + // Perform matrix multiplication
  126 + GSymMatrix result(m_rows,m.m_cols);
  127 + for (unsigned row = 0; row < m_rows; ++row) {
  128 + for (unsigned col = 0; col < m.m_cols; ++col) {
  129 + double sum = 0.0;
  130 + for (unsigned i = 0; i < m_cols; ++i)
  131 + sum += (*this)(row,i)*m(i,col);
  132 + result(row,col) = sum;
  133 + }
  134 + }
  135 + return result;
  136 +}
  137 +
  138 +
  139 +/***************************************************************************
117 * GSymMatrix Cholesky decomposition * 140 * GSymMatrix Cholesky decomposition *
118 * ----------------------------------------------------------------------- * 141 * ----------------------------------------------------------------------- *
119 * Inplace Cholesky decomposition inspired by Numerical Recipes algorithm. * 142 * Inplace Cholesky decomposition inspired by Numerical Recipes algorithm. *
src/GSymMatrix.hpp
@@ -111,14 +111,15 @@ private: @@ -111,14 +111,15 @@ private:
111 inline 111 inline
112 GSymMatrix& GSymMatrix::operator= (const double& d) 112 GSymMatrix& GSymMatrix::operator= (const double& d)
113 { 113 {
114 - this->GMatrix::operator=(d); 114 + this->GMatrix::operator=(d);
  115 + return *this;
115 } 116 }
116 117
117 // Matrix element access operator: GSymMatrix(row,col) 118 // Matrix element access operator: GSymMatrix(row,col)
118 inline 119 inline
119 double& GSymMatrix::operator() (unsigned row, unsigned col) 120 double& GSymMatrix::operator() (unsigned row, unsigned col)
120 { 121 {
121 - #if G_RANGE_CHECK 122 + #if defined(G_RANGE_CHECK)
122 if (row >= m_rows || col >= m_cols) 123 if (row >= m_rows || col >= m_cols)
123 throw out_of_range("GSymMatrix access", row, col, m_rows, m_cols); 124 throw out_of_range("GSymMatrix access", row, col, m_rows, m_cols);
124 #endif 125 #endif
@@ -130,7 +131,7 @@ double&amp; GSymMatrix::operator() (unsigned row, unsigned col) @@ -130,7 +131,7 @@ double&amp; GSymMatrix::operator() (unsigned row, unsigned col)
130 inline 131 inline
131 const double& GSymMatrix::operator() (unsigned row, unsigned col) const 132 const double& GSymMatrix::operator() (unsigned row, unsigned col) const
132 { 133 {
133 - #if G_RANGE_CHECK 134 + #if defined(G_RANGE_CHECK)
134 if (row >= m_rows || col >= m_cols) 135 if (row >= m_rows || col >= m_cols)
135 throw out_of_range("GSymMatrix access", row, col, m_rows, m_cols); 136 throw out_of_range("GSymMatrix access", row, col, m_rows, m_cols);
136 #endif 137 #endif
@@ -156,26 +157,6 @@ GSymMatrix GSymMatrix::operator- (const GSymMatrix&amp; m) const @@ -156,26 +157,6 @@ GSymMatrix GSymMatrix::operator- (const GSymMatrix&amp; m) const
156 return result; 157 return result;
157 } 158 }
158 159
159 -// Matrix multiplication: GSymMatrix * GSymMatrix  
160 -inline  
161 -GSymMatrix GSymMatrix::operator* (const GSymMatrix& m) const  
162 -{  
163 - #if G_RANGE_CHECK  
164 - if (m_cols != m.m_rows)  
165 - throw GMatrix::dim_mult_mismatch("GSymMatrix multiplication", m_cols, m.m_rows);  
166 - #endif  
167 - GSymMatrix result(m_rows,m.m_cols);  
168 - for (unsigned row = 0; row < m_rows; ++row) {  
169 - for (unsigned col = 0; col < m.m_cols; ++col) {  
170 - double sum = 0.0;  
171 - for (unsigned i = 0; i < m_cols; ++i)  
172 - sum += (*this)(row,i)*m(i,col);  
173 - result(row,col) = sum;  
174 - }  
175 - }  
176 - return result;  
177 -}  
178 -  
179 // Matrix/Vector multiplication: GSymMatrix * GVector 160 // Matrix/Vector multiplication: GSymMatrix * GVector
180 inline 161 inline
181 GVector GSymMatrix::operator* (const GVector& v) const 162 GVector GSymMatrix::operator* (const GVector& v) const
src/GVector.cpp
@@ -59,6 +59,7 @@ ostream&amp; operator&lt;&lt; (ostream&amp; os, const GVector&amp; v) @@ -59,6 +59,7 @@ ostream&amp; operator&lt;&lt; (ostream&amp; os, const GVector&amp; v)
59 if (i != v.m_num-1) 59 if (i != v.m_num-1)
60 os << ", "; 60 os << ", ";
61 } 61 }
  62 + return os;
62 } 63 }
63 64
64 65
@@ -67,10 +68,8 @@ ostream&amp; operator&lt;&lt; (ostream&amp; os, const GVector&amp; v) @@ -67,10 +68,8 @@ ostream&amp; operator&lt;&lt; (ostream&amp; os, const GVector&amp; v)
67 ***************************************************************************/ 68 ***************************************************************************/
68 GVector cross(const GVector &a, const GVector &b) 69 GVector cross(const GVector &a, const GVector &b)
69 { 70 {
70 - #if G_RANGE_CHECK  
71 if (a.m_num != b.m_num) 71 if (a.m_num != b.m_num)
72 throw GVector::dim_mismatch("GVector cross product", a.m_num, b.m_num); 72 throw GVector::dim_mismatch("GVector cross product", a.m_num, b.m_num);
73 - #endif  
74 if (a.m_num != 3) 73 if (a.m_num != 3)
75 throw GVector::bad_cross_dim(a.m_num); 74 throw GVector::bad_cross_dim(a.m_num);
76 GVector result(3); 75 GVector result(3);
src/GVector.hpp
@@ -146,7 +146,7 @@ GVector::GVector(const GVector&amp; v) @@ -146,7 +146,7 @@ GVector::GVector(const GVector&amp; v)
146 inline 146 inline
147 double& GVector::operator() (unsigned inx) 147 double& GVector::operator() (unsigned inx)
148 { 148 {
149 - #if G_RANGE_CHECK 149 + #if defined(G_RANGE_CHECK)
150 if (inx >= m_num) 150 if (inx >= m_num)
151 throw out_of_range("GVector access", inx, m_num); 151 throw out_of_range("GVector access", inx, m_num);
152 #endif 152 #endif
@@ -157,7 +157,7 @@ double&amp; GVector::operator() (unsigned inx) @@ -157,7 +157,7 @@ double&amp; GVector::operator() (unsigned inx)
157 inline 157 inline
158 const double& GVector::operator() (unsigned inx) const 158 const double& GVector::operator() (unsigned inx) const
159 { 159 {
160 - #if G_RANGE_CHECK 160 + #if defined(G_RANGE_CHECK)
161 if (inx >= m_num) 161 if (inx >= m_num)
162 throw out_of_range("const GVector access", inx, m_num); 162 throw out_of_range("const GVector access", inx, m_num);
163 #endif 163 #endif
@@ -181,10 +181,8 @@ GVector&amp; GVector::operator= (const GVector&amp; v) @@ -181,10 +181,8 @@ GVector&amp; GVector::operator= (const GVector&amp; v)
181 inline 181 inline
182 GVector& GVector::operator+= (const GVector& v) 182 GVector& GVector::operator+= (const GVector& v)
183 { 183 {
184 - #if G_RANGE_CHECK  
185 if (m_num != v.m_num) 184 if (m_num != v.m_num)
186 throw dim_mismatch("GVector += operator", m_num, v.m_num); 185 throw dim_mismatch("GVector += operator", m_num, v.m_num);
187 - #endif  
188 for (unsigned i = 0; i < m_num; ++i) 186 for (unsigned i = 0; i < m_num; ++i)
189 m_data[i] += v.m_data[i]; 187 m_data[i] += v.m_data[i];
190 return *this; 188 return *this;
@@ -194,10 +192,8 @@ GVector&amp; GVector::operator+= (const GVector&amp; v) @@ -194,10 +192,8 @@ GVector&amp; GVector::operator+= (const GVector&amp; v)
194 inline 192 inline
195 GVector& GVector::operator-= (const GVector& v) 193 GVector& GVector::operator-= (const GVector& v)
196 { 194 {
197 - #if G_RANGE_CHECK  
198 if (m_num != v.m_num) 195 if (m_num != v.m_num)
199 throw dim_mismatch("GVector -= operator", m_num, v.m_num); 196 throw dim_mismatch("GVector -= operator", m_num, v.m_num);
200 - #endif  
201 for (unsigned i = 0; i < m_num; ++i) 197 for (unsigned i = 0; i < m_num; ++i)
202 m_data[i] -= v.m_data[i]; 198 m_data[i] -= v.m_data[i];
203 return *this; 199 return *this;
@@ -327,10 +323,8 @@ GVector operator- (const double&amp; a, const GVector&amp; b) @@ -327,10 +323,8 @@ GVector operator- (const double&amp; a, const GVector&amp; b)
327 inline 323 inline
328 double operator* (const GVector& a, const GVector& b) 324 double operator* (const GVector& a, const GVector& b)
329 { 325 {
330 - #if G_RANGE_CHECK  
331 if (a.m_num != b.m_num) 326 if (a.m_num != b.m_num)
332 throw GVector::dim_mismatch("GVector scalar product", a.m_num, b.m_num); 327 throw GVector::dim_mismatch("GVector scalar product", a.m_num, b.m_num);
333 - #endif  
334 double result = 0.0; 328 double result = 0.0;
335 for (unsigned i = 0; i < a.m_num; ++i) 329 for (unsigned i = 0; i < a.m_num; ++i)
336 result += (a.m_data[i] * b.m_data[i]); 330 result += (a.m_data[i] * b.m_data[i]);
test/test_GMatrix.cpp
@@ -304,7 +304,7 @@ int main(void) @@ -304,7 +304,7 @@ int main(void)
304 cout << e.what() << endl; 304 cout << e.what() << endl;
305 throw; 305 throw;
306 } 306 }
307 - #if G_RANGE_CHECK 307 + #if defined(G_RANGE_CHECK)
308 try { 308 try {
309 GMatrix test3(3,3); 309 GMatrix test3(3,3);
310 test3(3,3) = 1.0; 310 test3(3,3) = 1.0;
test/test_GSymMatrix.cpp
@@ -356,7 +356,7 @@ int main(void) @@ -356,7 +356,7 @@ int main(void)
356 cout << e.what() << endl; 356 cout << e.what() << endl;
357 throw; 357 throw;
358 } 358 }
359 - #if G_RANGE_CHECK 359 + #if defined(G_RANGE_CHECK)
360 try { 360 try {
361 GSymMatrix test3(3,3); 361 GSymMatrix test3(3,3);
362 test3(3,3) = 1.0; 362 test3(3,3) = 1.0;
test/test_GVector.cpp
@@ -18,6 +18,12 @@ using namespace std; @@ -18,6 +18,12 @@ using namespace std;
18 18
19 int main(void) 19 int main(void)
20 { 20 {
  21 + // Dump header
  22 + cout << endl;
  23 + cout << "*************************" << endl;
  24 + cout << "* GVector class testing *" << endl;
  25 + cout << "*************************" << endl;
  26 +
21 // Set parameters 27 // Set parameters
22 unsigned num = 5; 28 unsigned num = 5;
23 29
@@ -64,7 +70,7 @@ int main(void) @@ -64,7 +70,7 @@ int main(void)
64 cout << " Size = " << test3.size() << endl; 70 cout << " Size = " << test3.size() << endl;
65 // 71 //
66 cout << " GVector out of range access: "; 72 cout << " GVector out of range access: ";
67 - #if G_RANGE_CHECK 73 + #if defined(G_RANGE_CHECK)
68 cout << test3(3) << endl; 74 cout << test3(3) << endl;
69 #endif 75 #endif
70 } 76 }
@@ -323,4 +329,4 @@ int main(void) @@ -323,4 +329,4 @@ int main(void)
323 cout << e.what() << endl; 329 cout << e.what() << endl;
324 } 330 }
325 331
326 -}  
327 \ No newline at end of file 332 \ No newline at end of file
  333 +}