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 134 if (row != m.m_rows-1)
135 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 71 GMatrix operator+ (const GMatrix& m) const;
72 72 GMatrix operator- (const GMatrix& m) const;
73 73 GMatrix operator* (const GMatrix& m) const;
74   - GVector operator* (const GVector& v) const;
  74 + virtual GVector operator* (const GVector& v) const;
75 75 GMatrix operator+ (const double& d) const;
76 76 GMatrix operator- (const double& d) const;
77 77 GMatrix operator* (const double& d) const;
... ... @@ -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 133 // Matrix unary addition operator: GMatrix += GMatrix
152 134 inline
153 135 GMatrix& GMatrix::operator+= (const GMatrix& m)
154 136 {
155   - #if G_RANGE_CHECK
156 137 if (m_rows != m.m_rows || m_cols != m.m_cols)
157 138 throw dim_add_mismatch("GMatrix += operator", m_rows, m.m_rows, m_cols, m.m_cols);
158   - #endif
159 139 for (unsigned i = 0; i < m_elements; ++i)
160 140 m_data[i] += m.m_data[i];
161 141 return *this;
... ... @@ -165,10 +145,8 @@ GMatrix&amp; GMatrix::operator+= (const GMatrix&amp; m)
165 145 inline
166 146 GMatrix& GMatrix::operator-= (const GMatrix& m)
167 147 {
168   - #if G_RANGE_CHECK
169 148 if (m_rows != m.m_rows || m_cols != m.m_cols)
170 149 throw dim_add_mismatch("GMatrix -= operator", m_rows, m.m_rows, m_cols, m.m_cols);
171   - #endif
172 150 for (unsigned i = 0; i < m_elements; ++i)
173 151 m_data[i] -= m.m_data[i];
174 152 return *this;
... ... @@ -313,7 +291,7 @@ GMatrix&amp; GMatrix::operator= (const double&amp; d)
313 291 inline
314 292 double& GMatrix::operator() (unsigned row, unsigned col)
315 293 {
316   - #if G_RANGE_CHECK
  294 + #if defined(G_RANGE_CHECK)
317 295 if (row >= m_rows || col >= m_cols)
318 296 throw out_of_range("GMatrix access", row, col, m_rows, m_cols);
319 297 #endif
... ... @@ -324,7 +302,7 @@ double&amp; GMatrix::operator() (unsigned row, unsigned col)
324 302 inline
325 303 const double& GMatrix::operator() (unsigned row, unsigned col) const
326 304 {
327   - #if G_RANGE_CHECK
  305 + #if defined(G_RANGE_CHECK)
328 306 if (row >= m_rows || col >= m_cols)
329 307 throw out_of_range("GMatrix access", row, col, m_rows, m_cols);
330 308 #endif
... ... @@ -349,26 +327,6 @@ GMatrix GMatrix::operator- (const GMatrix&amp; m) const
349 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 330 // Matrix scaling: GMatrix + double
373 331 inline
374 332 GMatrix GMatrix::operator+ (const double& d) const
... ...
src/GSymMatrix.cpp
... ... @@ -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 140 * GSymMatrix Cholesky decomposition *
118 141 * ----------------------------------------------------------------------- *
119 142 * Inplace Cholesky decomposition inspired by Numerical Recipes algorithm. *
... ...
src/GSymMatrix.hpp
... ... @@ -111,14 +111,15 @@ private:
111 111 inline
112 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 118 // Matrix element access operator: GSymMatrix(row,col)
118 119 inline
119 120 double& GSymMatrix::operator() (unsigned row, unsigned col)
120 121 {
121   - #if G_RANGE_CHECK
  122 + #if defined(G_RANGE_CHECK)
122 123 if (row >= m_rows || col >= m_cols)
123 124 throw out_of_range("GSymMatrix access", row, col, m_rows, m_cols);
124 125 #endif
... ... @@ -130,7 +131,7 @@ double&amp; GSymMatrix::operator() (unsigned row, unsigned col)
130 131 inline
131 132 const double& GSymMatrix::operator() (unsigned row, unsigned col) const
132 133 {
133   - #if G_RANGE_CHECK
  134 + #if defined(G_RANGE_CHECK)
134 135 if (row >= m_rows || col >= m_cols)
135 136 throw out_of_range("GSymMatrix access", row, col, m_rows, m_cols);
136 137 #endif
... ... @@ -156,26 +157,6 @@ GSymMatrix GSymMatrix::operator- (const GSymMatrix&amp; m) const
156 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 160 // Matrix/Vector multiplication: GSymMatrix * GVector
180 161 inline
181 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 59 if (i != v.m_num-1)
60 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 68 ***************************************************************************/
68 69 GVector cross(const GVector &a, const GVector &b)
69 70 {
70   - #if G_RANGE_CHECK
71 71 if (a.m_num != b.m_num)
72 72 throw GVector::dim_mismatch("GVector cross product", a.m_num, b.m_num);
73   - #endif
74 73 if (a.m_num != 3)
75 74 throw GVector::bad_cross_dim(a.m_num);
76 75 GVector result(3);
... ...
src/GVector.hpp
... ... @@ -146,7 +146,7 @@ GVector::GVector(const GVector&amp; v)
146 146 inline
147 147 double& GVector::operator() (unsigned inx)
148 148 {
149   - #if G_RANGE_CHECK
  149 + #if defined(G_RANGE_CHECK)
150 150 if (inx >= m_num)
151 151 throw out_of_range("GVector access", inx, m_num);
152 152 #endif
... ... @@ -157,7 +157,7 @@ double&amp; GVector::operator() (unsigned inx)
157 157 inline
158 158 const double& GVector::operator() (unsigned inx) const
159 159 {
160   - #if G_RANGE_CHECK
  160 + #if defined(G_RANGE_CHECK)
161 161 if (inx >= m_num)
162 162 throw out_of_range("const GVector access", inx, m_num);
163 163 #endif
... ... @@ -181,10 +181,8 @@ GVector&amp; GVector::operator= (const GVector&amp; v)
181 181 inline
182 182 GVector& GVector::operator+= (const GVector& v)
183 183 {
184   - #if G_RANGE_CHECK
185 184 if (m_num != v.m_num)
186 185 throw dim_mismatch("GVector += operator", m_num, v.m_num);
187   - #endif
188 186 for (unsigned i = 0; i < m_num; ++i)
189 187 m_data[i] += v.m_data[i];
190 188 return *this;
... ... @@ -194,10 +192,8 @@ GVector&amp; GVector::operator+= (const GVector&amp; v)
194 192 inline
195 193 GVector& GVector::operator-= (const GVector& v)
196 194 {
197   - #if G_RANGE_CHECK
198 195 if (m_num != v.m_num)
199 196 throw dim_mismatch("GVector -= operator", m_num, v.m_num);
200   - #endif
201 197 for (unsigned i = 0; i < m_num; ++i)
202 198 m_data[i] -= v.m_data[i];
203 199 return *this;
... ... @@ -327,10 +323,8 @@ GVector operator- (const double&amp; a, const GVector&amp; b)
327 323 inline
328 324 double operator* (const GVector& a, const GVector& b)
329 325 {
330   - #if G_RANGE_CHECK
331 326 if (a.m_num != b.m_num)
332 327 throw GVector::dim_mismatch("GVector scalar product", a.m_num, b.m_num);
333   - #endif
334 328 double result = 0.0;
335 329 for (unsigned i = 0; i < a.m_num; ++i)
336 330 result += (a.m_data[i] * b.m_data[i]);
... ...
test/test_GMatrix.cpp
... ... @@ -304,7 +304,7 @@ int main(void)
304 304 cout << e.what() << endl;
305 305 throw;
306 306 }
307   - #if G_RANGE_CHECK
  307 + #if defined(G_RANGE_CHECK)
308 308 try {
309 309 GMatrix test3(3,3);
310 310 test3(3,3) = 1.0;
... ...
test/test_GSymMatrix.cpp
... ... @@ -356,7 +356,7 @@ int main(void)
356 356 cout << e.what() << endl;
357 357 throw;
358 358 }
359   - #if G_RANGE_CHECK
  359 + #if defined(G_RANGE_CHECK)
360 360 try {
361 361 GSymMatrix test3(3,3);
362 362 test3(3,3) = 1.0;
... ...
test/test_GVector.cpp
... ... @@ -18,6 +18,12 @@ using namespace std;
18 18  
19 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 27 // Set parameters
22 28 unsigned num = 5;
23 29  
... ... @@ -64,7 +70,7 @@ int main(void)
64 70 cout << " Size = " << test3.size() << endl;
65 71 //
66 72 cout << " GVector out of range access: ";
67   - #if G_RANGE_CHECK
  73 + #if defined(G_RANGE_CHECK)
68 74 cout << test3(3) << endl;
69 75 #endif
70 76 }
... ... @@ -323,4 +329,4 @@ int main(void)
323 329 cout << e.what() << endl;
324 330 }
325 331  
326   -}
327 332 \ No newline at end of file
  333 +}
... ...