Matrix Library. v1.6.4
Dependents: Matrix_class Wizardsneverdie TwoTank mbed_multiplex_matrix ... more
Operators.cpp@2:493402568a5e, 2011-10-20 (annotated)
- Committer:
- Yo_Robot
- Date:
- Thu Oct 20 23:42:13 2011 +0000
- Revision:
- 2:493402568a5e
- Child:
- 3:589fb80932b5
Everything working fine, dropped some useless methods, Matrix Class is Complete, maybe I\ll keep adding stuff but basic usage is covered in my opinion.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yo_Robot | 2:493402568a5e | 1 | /** |
Yo_Robot | 2:493402568a5e | 2 | * |
Yo_Robot | 2:493402568a5e | 3 | */ |
Yo_Robot | 2:493402568a5e | 4 | |
Yo_Robot | 2:493402568a5e | 5 | #include "mbed.h" |
Yo_Robot | 2:493402568a5e | 6 | #include "Matrix.h" |
Yo_Robot | 2:493402568a5e | 7 | |
Yo_Robot | 2:493402568a5e | 8 | /// Overloaded Asign Operator. Resizes Matrix |
Yo_Robot | 2:493402568a5e | 9 | Matrix& Matrix::operator = ( const Matrix& rightM ) |
Yo_Robot | 2:493402568a5e | 10 | { |
Yo_Robot | 2:493402568a5e | 11 | if (this != &rightM ) |
Yo_Robot | 2:493402568a5e | 12 | { |
Yo_Robot | 2:493402568a5e | 13 | |
Yo_Robot | 2:493402568a5e | 14 | _nRows = rightM._nRows; |
Yo_Robot | 2:493402568a5e | 15 | _nCols = rightM._nCols; |
Yo_Robot | 2:493402568a5e | 16 | |
Yo_Robot | 2:493402568a5e | 17 | _matrix.resize( rightM._nRows ); |
Yo_Robot | 2:493402568a5e | 18 | for( int i = 0; i < rightM._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 19 | _matrix [i].resize(rightM._nCols); |
Yo_Robot | 2:493402568a5e | 20 | |
Yo_Robot | 2:493402568a5e | 21 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 22 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 23 | _matrix[i][j] = rightM._matrix[i][j]; |
Yo_Robot | 2:493402568a5e | 24 | } |
Yo_Robot | 2:493402568a5e | 25 | return *this; |
Yo_Robot | 2:493402568a5e | 26 | |
Yo_Robot | 2:493402568a5e | 27 | } |
Yo_Robot | 2:493402568a5e | 28 | |
Yo_Robot | 2:493402568a5e | 29 | /// Comapre element by element |
Yo_Robot | 2:493402568a5e | 30 | bool Matrix::operator == ( const Matrix& rightM ) |
Yo_Robot | 2:493402568a5e | 31 | { |
Yo_Robot | 2:493402568a5e | 32 | if( _nRows == rightM._nRows && _nCols == rightM._nCols ) |
Yo_Robot | 2:493402568a5e | 33 | { |
Yo_Robot | 2:493402568a5e | 34 | bool equal = false; |
Yo_Robot | 2:493402568a5e | 35 | |
Yo_Robot | 2:493402568a5e | 36 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 37 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 38 | if( _matrix[i][j] != rightM._matrix[i][j] ) |
Yo_Robot | 2:493402568a5e | 39 | equal = equal || true; |
Yo_Robot | 2:493402568a5e | 40 | |
Yo_Robot | 2:493402568a5e | 41 | return !equal; |
Yo_Robot | 2:493402568a5e | 42 | |
Yo_Robot | 2:493402568a5e | 43 | }else{ return false; } |
Yo_Robot | 2:493402568a5e | 44 | } |
Yo_Robot | 2:493402568a5e | 45 | |
Yo_Robot | 2:493402568a5e | 46 | |
Yo_Robot | 2:493402568a5e | 47 | /// Calls for '==' operator |
Yo_Robot | 2:493402568a5e | 48 | bool Matrix::operator != ( const Matrix& rightM ) |
Yo_Robot | 2:493402568a5e | 49 | { |
Yo_Robot | 2:493402568a5e | 50 | return !( *this == rightM ); |
Yo_Robot | 2:493402568a5e | 51 | } |
Yo_Robot | 2:493402568a5e | 52 | |
Yo_Robot | 2:493402568a5e | 53 | |
Yo_Robot | 2:493402568a5e | 54 | /// Matrices must be same size. |
Yo_Robot | 2:493402568a5e | 55 | /// Element by element adition. |
Yo_Robot | 2:493402568a5e | 56 | Matrix& Matrix::operator +=(const Matrix& rightM) |
Yo_Robot | 2:493402568a5e | 57 | { |
Yo_Robot | 2:493402568a5e | 58 | if( _nRows == rightM._nRows && _nCols == rightM._nCols ) |
Yo_Robot | 2:493402568a5e | 59 | { |
Yo_Robot | 2:493402568a5e | 60 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 61 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 62 | _matrix[i][j] += rightM._matrix[i][j]; |
Yo_Robot | 2:493402568a5e | 63 | |
Yo_Robot | 2:493402568a5e | 64 | return *this; |
Yo_Robot | 2:493402568a5e | 65 | |
Yo_Robot | 2:493402568a5e | 66 | }else{ |
Yo_Robot | 2:493402568a5e | 67 | printf( "\n\nERROR\nDiferent Dimensions @ += operator\n" ); |
Yo_Robot | 2:493402568a5e | 68 | //Matrix error(4); |
Yo_Robot | 2:493402568a5e | 69 | //error.Clear(); |
Yo_Robot | 2:493402568a5e | 70 | //return error; |
Yo_Robot | 2:493402568a5e | 71 | } |
Yo_Robot | 2:493402568a5e | 72 | } |
Yo_Robot | 2:493402568a5e | 73 | |
Yo_Robot | 2:493402568a5e | 74 | |
Yo_Robot | 2:493402568a5e | 75 | /// Matrices must be same size. |
Yo_Robot | 2:493402568a5e | 76 | /// Element by element Substraction |
Yo_Robot | 2:493402568a5e | 77 | Matrix& Matrix::operator -=(const Matrix& rightM) |
Yo_Robot | 2:493402568a5e | 78 | { |
Yo_Robot | 2:493402568a5e | 79 | if( _nRows == rightM._nRows && _nCols == rightM._nCols ) |
Yo_Robot | 2:493402568a5e | 80 | { |
Yo_Robot | 2:493402568a5e | 81 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 82 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 83 | _matrix[i][j] -= rightM._matrix[i][j]; |
Yo_Robot | 2:493402568a5e | 84 | |
Yo_Robot | 2:493402568a5e | 85 | return *this; |
Yo_Robot | 2:493402568a5e | 86 | |
Yo_Robot | 2:493402568a5e | 87 | }else{ |
Yo_Robot | 2:493402568a5e | 88 | printf( "\n\nERROR\nDiferent Dimensions @ -= operator \n" ); |
Yo_Robot | 2:493402568a5e | 89 | //Matrix error(4); |
Yo_Robot | 2:493402568a5e | 90 | //error.Clear(); |
Yo_Robot | 2:493402568a5e | 91 | //return error; |
Yo_Robot | 2:493402568a5e | 92 | } |
Yo_Robot | 2:493402568a5e | 93 | } |
Yo_Robot | 2:493402568a5e | 94 | |
Yo_Robot | 2:493402568a5e | 95 | |
Yo_Robot | 2:493402568a5e | 96 | Matrix& Matrix::operator *=( const Matrix& rightM ) |
Yo_Robot | 2:493402568a5e | 97 | { |
Yo_Robot | 2:493402568a5e | 98 | if( this->_nCols == rightM._nRows ) |
Yo_Robot | 2:493402568a5e | 99 | { |
Yo_Robot | 2:493402568a5e | 100 | Matrix resultM ( this->_nRows, rightM._nCols ); |
Yo_Robot | 2:493402568a5e | 101 | |
Yo_Robot | 2:493402568a5e | 102 | for( int i = 0; i < resultM._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 103 | for( int j = 0; j < resultM._nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 104 | for( int m = 0; m < rightM._nRows; m++ ) |
Yo_Robot | 2:493402568a5e | 105 | resultM._matrix[i][j] += this->_matrix[i][m] * rightM._matrix[m][j]; |
Yo_Robot | 2:493402568a5e | 106 | |
Yo_Robot | 2:493402568a5e | 107 | *this = resultM; |
Yo_Robot | 2:493402568a5e | 108 | } |
Yo_Robot | 2:493402568a5e | 109 | |
Yo_Robot | 2:493402568a5e | 110 | return *this; |
Yo_Robot | 2:493402568a5e | 111 | } |
Yo_Robot | 2:493402568a5e | 112 | |
Yo_Robot | 2:493402568a5e | 113 | |
Yo_Robot | 2:493402568a5e | 114 | Matrix& Matrix::operator *=(float number) |
Yo_Robot | 2:493402568a5e | 115 | { |
Yo_Robot | 2:493402568a5e | 116 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 117 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 118 | _matrix[i][j] *= number; |
Yo_Robot | 2:493402568a5e | 119 | |
Yo_Robot | 2:493402568a5e | 120 | return *this; |
Yo_Robot | 2:493402568a5e | 121 | } |
Yo_Robot | 2:493402568a5e | 122 | |
Yo_Robot | 2:493402568a5e | 123 | |
Yo_Robot | 2:493402568a5e | 124 | const Matrix Matrix::operator -() |
Yo_Robot | 2:493402568a5e | 125 | { |
Yo_Robot | 2:493402568a5e | 126 | Matrix result( _nRows, _nCols ); |
Yo_Robot | 2:493402568a5e | 127 | |
Yo_Robot | 2:493402568a5e | 128 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 129 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 130 | result._matrix[i][j] = _matrix[i][j] * -1; |
Yo_Robot | 2:493402568a5e | 131 | |
Yo_Robot | 2:493402568a5e | 132 | return result; |
Yo_Robot | 2:493402568a5e | 133 | } |
Yo_Robot | 2:493402568a5e | 134 | |
Yo_Robot | 2:493402568a5e | 135 | /*****************************************************************************/ |
Yo_Robot | 2:493402568a5e | 136 | |
Yo_Robot | 2:493402568a5e | 137 | // Overload operators |
Yo_Robot | 2:493402568a5e | 138 | |
Yo_Robot | 2:493402568a5e | 139 | |
Yo_Robot | 2:493402568a5e | 140 | const Matrix operator +=( Matrix& leftM, float number ) |
Yo_Robot | 2:493402568a5e | 141 | { |
Yo_Robot | 2:493402568a5e | 142 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 143 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 144 | leftM._matrix[i][j] += number; |
Yo_Robot | 2:493402568a5e | 145 | return leftM; |
Yo_Robot | 2:493402568a5e | 146 | } |
Yo_Robot | 2:493402568a5e | 147 | |
Yo_Robot | 2:493402568a5e | 148 | |
Yo_Robot | 2:493402568a5e | 149 | const Matrix operator -=( Matrix& leftM, float number ) |
Yo_Robot | 2:493402568a5e | 150 | { |
Yo_Robot | 2:493402568a5e | 151 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 152 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 153 | leftM._matrix[i][j] -= number; |
Yo_Robot | 2:493402568a5e | 154 | return leftM; |
Yo_Robot | 2:493402568a5e | 155 | } |
Yo_Robot | 2:493402568a5e | 156 | |
Yo_Robot | 2:493402568a5e | 157 | |
Yo_Robot | 2:493402568a5e | 158 | const Matrix operator +( const Matrix& leftM, const Matrix& rightM) |
Yo_Robot | 2:493402568a5e | 159 | { |
Yo_Robot | 2:493402568a5e | 160 | if( leftM._nRows == rightM._nRows && leftM._nCols == rightM._nCols ) |
Yo_Robot | 2:493402568a5e | 161 | { |
Yo_Robot | 2:493402568a5e | 162 | Matrix result( leftM._nRows, leftM._nCols ); |
Yo_Robot | 2:493402568a5e | 163 | |
Yo_Robot | 2:493402568a5e | 164 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 165 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 166 | result._matrix[i][j] = leftM._matrix[i][j] + rightM._matrix[i][j]; |
Yo_Robot | 2:493402568a5e | 167 | |
Yo_Robot | 2:493402568a5e | 168 | return result; |
Yo_Robot | 2:493402568a5e | 169 | |
Yo_Robot | 2:493402568a5e | 170 | }else{ |
Yo_Robot | 2:493402568a5e | 171 | printf( "\n\nERROR\nDiferent Dimensions @ + operator \n" ); |
Yo_Robot | 2:493402568a5e | 172 | //Matrix error(4); |
Yo_Robot | 2:493402568a5e | 173 | //error.Clear(); |
Yo_Robot | 2:493402568a5e | 174 | //return error; |
Yo_Robot | 2:493402568a5e | 175 | } |
Yo_Robot | 2:493402568a5e | 176 | } |
Yo_Robot | 2:493402568a5e | 177 | |
Yo_Robot | 2:493402568a5e | 178 | |
Yo_Robot | 2:493402568a5e | 179 | const Matrix operator +( const Matrix& leftM, float number ) |
Yo_Robot | 2:493402568a5e | 180 | { |
Yo_Robot | 2:493402568a5e | 181 | Matrix result( leftM._nRows, leftM._nCols ); |
Yo_Robot | 2:493402568a5e | 182 | |
Yo_Robot | 2:493402568a5e | 183 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 184 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 185 | result._matrix[i][j] = leftM._matrix[i][j] + number; |
Yo_Robot | 2:493402568a5e | 186 | |
Yo_Robot | 2:493402568a5e | 187 | return result; |
Yo_Robot | 2:493402568a5e | 188 | } |
Yo_Robot | 2:493402568a5e | 189 | |
Yo_Robot | 2:493402568a5e | 190 | |
Yo_Robot | 2:493402568a5e | 191 | const Matrix operator +( float number, const Matrix& leftM ) |
Yo_Robot | 2:493402568a5e | 192 | { |
Yo_Robot | 2:493402568a5e | 193 | return ( leftM + number ); |
Yo_Robot | 2:493402568a5e | 194 | } |
Yo_Robot | 2:493402568a5e | 195 | |
Yo_Robot | 2:493402568a5e | 196 | |
Yo_Robot | 2:493402568a5e | 197 | const Matrix operator -( const Matrix& leftM, const Matrix& rightM ) |
Yo_Robot | 2:493402568a5e | 198 | { |
Yo_Robot | 2:493402568a5e | 199 | if( leftM._nRows == rightM._nRows && leftM._nCols == rightM._nCols ) |
Yo_Robot | 2:493402568a5e | 200 | { |
Yo_Robot | 2:493402568a5e | 201 | Matrix result( leftM._nRows, leftM._nCols ); |
Yo_Robot | 2:493402568a5e | 202 | |
Yo_Robot | 2:493402568a5e | 203 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 204 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 205 | result._matrix[i][j] = leftM._matrix[i][j] - rightM._matrix[i][j]; |
Yo_Robot | 2:493402568a5e | 206 | |
Yo_Robot | 2:493402568a5e | 207 | return result; |
Yo_Robot | 2:493402568a5e | 208 | |
Yo_Robot | 2:493402568a5e | 209 | }else{ |
Yo_Robot | 2:493402568a5e | 210 | printf( "\n\nERROR:\nDiferent Dimensions @ + operator \n" ); |
Yo_Robot | 2:493402568a5e | 211 | |
Yo_Robot | 2:493402568a5e | 212 | } |
Yo_Robot | 2:493402568a5e | 213 | } |
Yo_Robot | 2:493402568a5e | 214 | |
Yo_Robot | 2:493402568a5e | 215 | |
Yo_Robot | 2:493402568a5e | 216 | const Matrix operator -( const Matrix& leftM, float number ) |
Yo_Robot | 2:493402568a5e | 217 | { |
Yo_Robot | 2:493402568a5e | 218 | Matrix result( leftM._nRows, leftM._nCols ); |
Yo_Robot | 2:493402568a5e | 219 | |
Yo_Robot | 2:493402568a5e | 220 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 221 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 222 | result._matrix[i][j] = leftM._matrix[i][j] - number; |
Yo_Robot | 2:493402568a5e | 223 | |
Yo_Robot | 2:493402568a5e | 224 | return result; |
Yo_Robot | 2:493402568a5e | 225 | } |
Yo_Robot | 2:493402568a5e | 226 | |
Yo_Robot | 2:493402568a5e | 227 | |
Yo_Robot | 2:493402568a5e | 228 | const Matrix operator -( float number, const Matrix& leftM ) |
Yo_Robot | 2:493402568a5e | 229 | { |
Yo_Robot | 2:493402568a5e | 230 | return ( leftM - number ); |
Yo_Robot | 2:493402568a5e | 231 | } |
Yo_Robot | 2:493402568a5e | 232 | |
Yo_Robot | 2:493402568a5e | 233 | |
Yo_Robot | 2:493402568a5e | 234 | const Matrix operator *( const Matrix& leftM, const Matrix& rightM ) |
Yo_Robot | 2:493402568a5e | 235 | { |
Yo_Robot | 2:493402568a5e | 236 | if( leftM._nCols == rightM._nRows ) |
Yo_Robot | 2:493402568a5e | 237 | { |
Yo_Robot | 2:493402568a5e | 238 | Matrix resultM ( leftM._nRows, rightM._nCols ); |
Yo_Robot | 2:493402568a5e | 239 | resultM.Clear(); |
Yo_Robot | 2:493402568a5e | 240 | |
Yo_Robot | 2:493402568a5e | 241 | for( int i = 0; i < resultM._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 242 | for( int j = 0; j < resultM._nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 243 | for( int m = 0; m < rightM._nRows; m++ ) |
Yo_Robot | 2:493402568a5e | 244 | resultM._matrix[i][j] += leftM._matrix[i][m] * rightM._matrix[m][j]; |
Yo_Robot | 2:493402568a5e | 245 | |
Yo_Robot | 2:493402568a5e | 246 | return resultM; |
Yo_Robot | 2:493402568a5e | 247 | |
Yo_Robot | 2:493402568a5e | 248 | } else { |
Yo_Robot | 2:493402568a5e | 249 | |
Yo_Robot | 2:493402568a5e | 250 | printf("\n\nERROR:\nDiferent Dimension matrices @ * operator"); |
Yo_Robot | 2:493402568a5e | 251 | } |
Yo_Robot | 2:493402568a5e | 252 | |
Yo_Robot | 2:493402568a5e | 253 | } |
Yo_Robot | 2:493402568a5e | 254 | |
Yo_Robot | 2:493402568a5e | 255 | |
Yo_Robot | 2:493402568a5e | 256 | const Matrix operator *( const Matrix& leftM, float number ) |
Yo_Robot | 2:493402568a5e | 257 | { |
Yo_Robot | 2:493402568a5e | 258 | Matrix result( leftM._nRows, leftM._nCols ); |
Yo_Robot | 2:493402568a5e | 259 | |
Yo_Robot | 2:493402568a5e | 260 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 261 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 262 | result._matrix[i][j] = leftM._matrix[i][j] * number; |
Yo_Robot | 2:493402568a5e | 263 | |
Yo_Robot | 2:493402568a5e | 264 | return result; |
Yo_Robot | 2:493402568a5e | 265 | } |
Yo_Robot | 2:493402568a5e | 266 | |
Yo_Robot | 2:493402568a5e | 267 | const Matrix operator *( float number, const Matrix& leftM ) |
Yo_Robot | 2:493402568a5e | 268 | { |
Yo_Robot | 2:493402568a5e | 269 | return ( leftM * number ); |
Yo_Robot | 2:493402568a5e | 270 | } |
Yo_Robot | 2:493402568a5e | 271 | |
Yo_Robot | 2:493402568a5e | 272 | |
Yo_Robot | 2:493402568a5e | 273 | Matrix& operator <<( Matrix& leftM, float number ) |
Yo_Robot | 2:493402568a5e | 274 | { |
Yo_Robot | 2:493402568a5e | 275 | if( leftM._pCol == leftM._nCols ) //end of Row |
Yo_Robot | 2:493402568a5e | 276 | { |
Yo_Robot | 2:493402568a5e | 277 | leftM._pCol = 0; |
Yo_Robot | 2:493402568a5e | 278 | leftM._pRow++; |
Yo_Robot | 2:493402568a5e | 279 | } |
Yo_Robot | 2:493402568a5e | 280 | if( leftM._pRow > leftM._nRows ) |
Yo_Robot | 2:493402568a5e | 281 | { |
Yo_Robot | 2:493402568a5e | 282 | printf( "\n\nERROR:\nAssignment out of limits @ << operator" ); |
Yo_Robot | 2:493402568a5e | 283 | return leftM; |
Yo_Robot | 2:493402568a5e | 284 | |
Yo_Robot | 2:493402568a5e | 285 | }else{ |
Yo_Robot | 2:493402568a5e | 286 | |
Yo_Robot | 2:493402568a5e | 287 | leftM._matrix[ leftM._pRow ][ leftM._pCol ] = number; |
Yo_Robot | 2:493402568a5e | 288 | leftM._pCol++; |
Yo_Robot | 2:493402568a5e | 289 | |
Yo_Robot | 2:493402568a5e | 290 | return leftM; |
Yo_Robot | 2:493402568a5e | 291 | } |
Yo_Robot | 2:493402568a5e | 292 | } |
Yo_Robot | 2:493402568a5e | 293 |