use from Ernesto Palacios
Dependents: RoboticArm DXL_SDK_Porting_Test
Fork of Matrix by
Operators.cpp@9:f19d5d6c3468, 2017-03-31 (annotated)
- Committer:
- stanley1228
- Date:
- Fri Mar 31 09:10:27 2017 +0800
- Revision:
- 9:f19d5d6c3468
- Parent:
- 8:1774aa06ab99
no
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
stanley1228 | 7:7a564cf28ab6 | 1 | |
stanley1228 | 7:7a564cf28ab6 | 2 | //* @brief Source Code for the Operator of Matrix Class. |
stanley1228 | 7:7a564cf28ab6 | 3 | //* @file Operators.cpp |
stanley1228 | 7:7a564cf28ab6 | 4 | //* @author Ernesto Palacios |
stanley1228 | 7:7a564cf28ab6 | 5 | //* |
stanley1228 | 7:7a564cf28ab6 | 6 | //* Created on September 2011. |
stanley1228 | 7:7a564cf28ab6 | 7 | //* |
stanley1228 | 7:7a564cf28ab6 | 8 | //* Develop Under GPL v3.0 License |
stanley1228 | 7:7a564cf28ab6 | 9 | //* http://www.gnu.org/licenses/gpl-3.0.html |
stanley1228 | 7:7a564cf28ab6 | 10 | //* |
stanley1228 | 7:7a564cf28ab6 | 11 | //*/ |
Yo_Robot | 3:589fb80932b5 | 12 | #include "mbed.h" |
stanley1228 | 7:7a564cf28ab6 | 13 | |
Yo_Robot | 3:589fb80932b5 | 14 | #include "Matrix.h" |
Yo_Robot | 3:589fb80932b5 | 15 | |
stanley1228 | 8:1774aa06ab99 | 16 | |
stanley1228 | 8:1774aa06ab99 | 17 | |
stanley1228 | 8:1774aa06ab99 | 18 | extern Serial pc; |
stanley1228 | 8:1774aa06ab99 | 19 | |
stanley1228 | 8:1774aa06ab99 | 20 | |
stanley1228 | 8:1774aa06ab99 | 21 | #if (DEBUG) |
stanley1228 | 8:1774aa06ab99 | 22 | #define DBGMSG(x) pc.printf x; |
stanley1228 | 8:1774aa06ab99 | 23 | #else |
stanley1228 | 8:1774aa06ab99 | 24 | #define DBGMSG(x) |
stanley1228 | 8:1774aa06ab99 | 25 | #endif |
stanley1228 | 8:1774aa06ab99 | 26 | |
Yo_Robot | 5:a4014ab0a8cf | 27 | /// Subindex in Matrix left side |
Yo_Robot | 5:a4014ab0a8cf | 28 | float& Matrix::operator ()(int row, int col) |
Yo_Robot | 5:a4014ab0a8cf | 29 | { |
Yo_Robot | 5:a4014ab0a8cf | 30 | --row; --col; |
Yo_Robot | 5:a4014ab0a8cf | 31 | |
Yo_Robot | 5:a4014ab0a8cf | 32 | if( row >= _nRows || col >= _nCols) |
Yo_Robot | 5:a4014ab0a8cf | 33 | { |
stanley1228 | 8:1774aa06ab99 | 34 | DBGMSG(("\n\nError:\nOut of limits @ Matrix::operator()\n")) |
stanley1228 | 7:7a564cf28ab6 | 35 | |
stanley1228 | 7:7a564cf28ab6 | 36 | return _matrix[0][0];//暫時這樣stanley |
stanley1228 | 7:7a564cf28ab6 | 37 | } |
stanley1228 | 7:7a564cf28ab6 | 38 | else |
stanley1228 | 7:7a564cf28ab6 | 39 | { |
Yo_Robot | 5:a4014ab0a8cf | 40 | return _matrix[row][col]; |
Yo_Robot | 5:a4014ab0a8cf | 41 | } |
Yo_Robot | 5:a4014ab0a8cf | 42 | } |
Yo_Robot | 5:a4014ab0a8cf | 43 | |
Yo_Robot | 5:a4014ab0a8cf | 44 | /// Subindex in Matrix right side |
Yo_Robot | 5:a4014ab0a8cf | 45 | float Matrix::operator ()(int row, int col) const |
Yo_Robot | 5:a4014ab0a8cf | 46 | { |
Yo_Robot | 5:a4014ab0a8cf | 47 | --row; --col; |
Yo_Robot | 5:a4014ab0a8cf | 48 | |
Yo_Robot | 5:a4014ab0a8cf | 49 | if( row >= _nRows || col >= _nCols) |
Yo_Robot | 5:a4014ab0a8cf | 50 | { |
stanley1228 | 8:1774aa06ab99 | 51 | DBGMSG(("\n\nError:\nOut of limits @ Matrix::operator()\n")) |
stanley1228 | 7:7a564cf28ab6 | 52 | return _matrix[0][0];//暫時這樣stanley |
Yo_Robot | 5:a4014ab0a8cf | 53 | }else{ |
Yo_Robot | 5:a4014ab0a8cf | 54 | return _matrix[row][col]; |
Yo_Robot | 5:a4014ab0a8cf | 55 | } |
Yo_Robot | 5:a4014ab0a8cf | 56 | } |
Yo_Robot | 5:a4014ab0a8cf | 57 | |
Yo_Robot | 5:a4014ab0a8cf | 58 | |
Yo_Robot | 3:589fb80932b5 | 59 | /// Overloaded Asign Operator. Resizes Matrix |
Yo_Robot | 3:589fb80932b5 | 60 | Matrix& Matrix::operator = ( const Matrix& rightM ) |
Yo_Robot | 3:589fb80932b5 | 61 | { |
Yo_Robot | 3:589fb80932b5 | 62 | if (this != &rightM ) |
Yo_Robot | 3:589fb80932b5 | 63 | { |
Yo_Robot | 3:589fb80932b5 | 64 | |
Yo_Robot | 3:589fb80932b5 | 65 | _nRows = rightM._nRows; |
Yo_Robot | 3:589fb80932b5 | 66 | _nCols = rightM._nCols; |
Yo_Robot | 3:589fb80932b5 | 67 | |
Yo_Robot | 3:589fb80932b5 | 68 | _matrix.resize( rightM._nRows ); |
Yo_Robot | 3:589fb80932b5 | 69 | for( int i = 0; i < rightM._nRows; i++ ) |
Yo_Robot | 3:589fb80932b5 | 70 | _matrix [i].resize(rightM._nCols); |
Yo_Robot | 3:589fb80932b5 | 71 | |
Yo_Robot | 3:589fb80932b5 | 72 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 3:589fb80932b5 | 73 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 3:589fb80932b5 | 74 | _matrix[i][j] = rightM._matrix[i][j]; |
Yo_Robot | 3:589fb80932b5 | 75 | } |
Yo_Robot | 3:589fb80932b5 | 76 | return *this; |
Yo_Robot | 3:589fb80932b5 | 77 | |
Yo_Robot | 3:589fb80932b5 | 78 | } |
Yo_Robot | 3:589fb80932b5 | 79 | |
Yo_Robot | 3:589fb80932b5 | 80 | |
Yo_Robot | 3:589fb80932b5 | 81 | const Matrix Matrix::operator -() |
Yo_Robot | 3:589fb80932b5 | 82 | { |
Yo_Robot | 3:589fb80932b5 | 83 | Matrix result( _nRows, _nCols ); |
Yo_Robot | 3:589fb80932b5 | 84 | |
Yo_Robot | 3:589fb80932b5 | 85 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 3:589fb80932b5 | 86 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 3:589fb80932b5 | 87 | result._matrix[i][j] = _matrix[i][j] * -1; |
Yo_Robot | 3:589fb80932b5 | 88 | |
Yo_Robot | 3:589fb80932b5 | 89 | return result; |
Yo_Robot | 3:589fb80932b5 | 90 | } |
Yo_Robot | 3:589fb80932b5 | 91 | |
Yo_Robot | 5:a4014ab0a8cf | 92 | |
Yo_Robot | 5:a4014ab0a8cf | 93 | /// Comapre element by element |
Yo_Robot | 5:a4014ab0a8cf | 94 | bool operator == ( const Matrix& leftM, const Matrix& rightM ) |
Yo_Robot | 5:a4014ab0a8cf | 95 | { |
Yo_Robot | 5:a4014ab0a8cf | 96 | if( leftM._nRows == rightM._nRows && leftM._nCols == rightM._nCols ) |
Yo_Robot | 5:a4014ab0a8cf | 97 | { |
Yo_Robot | 5:a4014ab0a8cf | 98 | bool equal = false; |
Yo_Robot | 5:a4014ab0a8cf | 99 | |
Yo_Robot | 5:a4014ab0a8cf | 100 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 5:a4014ab0a8cf | 101 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 5:a4014ab0a8cf | 102 | if( leftM._matrix[i][j] != rightM._matrix[i][j] ) |
Yo_Robot | 5:a4014ab0a8cf | 103 | equal = equal || true; |
Yo_Robot | 5:a4014ab0a8cf | 104 | |
Yo_Robot | 5:a4014ab0a8cf | 105 | return !equal; |
Yo_Robot | 5:a4014ab0a8cf | 106 | |
Yo_Robot | 5:a4014ab0a8cf | 107 | }else{ return false; } |
Yo_Robot | 5:a4014ab0a8cf | 108 | } |
Yo_Robot | 5:a4014ab0a8cf | 109 | |
Yo_Robot | 5:a4014ab0a8cf | 110 | |
Yo_Robot | 5:a4014ab0a8cf | 111 | /// Calls for '==' operator |
Yo_Robot | 5:a4014ab0a8cf | 112 | bool operator != ( const Matrix& leftM, const Matrix& rightM ) |
Yo_Robot | 5:a4014ab0a8cf | 113 | { |
Yo_Robot | 5:a4014ab0a8cf | 114 | return !( leftM == rightM ); |
Yo_Robot | 5:a4014ab0a8cf | 115 | } |
Yo_Robot | 5:a4014ab0a8cf | 116 | |
Yo_Robot | 5:a4014ab0a8cf | 117 | |
Yo_Robot | 5:a4014ab0a8cf | 118 | /// Matrices must be same size. |
Yo_Robot | 5:a4014ab0a8cf | 119 | /// Element by element adition. |
Yo_Robot | 5:a4014ab0a8cf | 120 | Matrix& operator +=( Matrix& leftM, const Matrix& rightM ) |
Yo_Robot | 5:a4014ab0a8cf | 121 | { |
Yo_Robot | 5:a4014ab0a8cf | 122 | if( leftM._nRows == rightM._nRows && leftM._nCols == rightM._nCols ) |
Yo_Robot | 5:a4014ab0a8cf | 123 | { |
Yo_Robot | 5:a4014ab0a8cf | 124 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 5:a4014ab0a8cf | 125 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 5:a4014ab0a8cf | 126 | leftM._matrix[i][j] += rightM._matrix[i][j]; |
Yo_Robot | 5:a4014ab0a8cf | 127 | |
Yo_Robot | 5:a4014ab0a8cf | 128 | return leftM; |
Yo_Robot | 5:a4014ab0a8cf | 129 | |
stanley1228 | 7:7a564cf28ab6 | 130 | }else{ |
stanley1228 | 8:1774aa06ab99 | 131 | DBGMSG(( "\n\nERROR:\nDiferent Dimensions @ += operator\n" )) |
stanley1228 | 7:7a564cf28ab6 | 132 | return leftM;//暫時這樣stanley |
stanley1228 | 7:7a564cf28ab6 | 133 | |
stanley1228 | 7:7a564cf28ab6 | 134 | |
stanley1228 | 7:7a564cf28ab6 | 135 | } |
Yo_Robot | 5:a4014ab0a8cf | 136 | } |
Yo_Robot | 5:a4014ab0a8cf | 137 | |
Yo_Robot | 5:a4014ab0a8cf | 138 | |
Yo_Robot | 5:a4014ab0a8cf | 139 | /// Matrices must be same size. |
Yo_Robot | 5:a4014ab0a8cf | 140 | /// Element by element Substraction |
Yo_Robot | 5:a4014ab0a8cf | 141 | Matrix& operator -=( Matrix& leftM, const Matrix& rightM ) |
Yo_Robot | 5:a4014ab0a8cf | 142 | { |
Yo_Robot | 5:a4014ab0a8cf | 143 | if( leftM._nRows == rightM._nRows && leftM._nCols == rightM._nCols ) |
Yo_Robot | 5:a4014ab0a8cf | 144 | { |
Yo_Robot | 5:a4014ab0a8cf | 145 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 5:a4014ab0a8cf | 146 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 5:a4014ab0a8cf | 147 | leftM._matrix[i][j] -= rightM._matrix[i][j]; |
Yo_Robot | 5:a4014ab0a8cf | 148 | |
Yo_Robot | 5:a4014ab0a8cf | 149 | return leftM; |
Yo_Robot | 5:a4014ab0a8cf | 150 | |
Yo_Robot | 5:a4014ab0a8cf | 151 | }else{ |
stanley1228 | 8:1774aa06ab99 | 152 | DBGMSG(( "\n\nERROR:\nDiferent Dimensions @ -= operator\n" )) |
stanley1228 | 7:7a564cf28ab6 | 153 | |
stanley1228 | 7:7a564cf28ab6 | 154 | return leftM;//暫時這樣stanley |
stanley1228 | 7:7a564cf28ab6 | 155 | |
Yo_Robot | 5:a4014ab0a8cf | 156 | } |
Yo_Robot | 5:a4014ab0a8cf | 157 | } |
Yo_Robot | 5:a4014ab0a8cf | 158 | |
stanley1228 | 7:7a564cf28ab6 | 159 | //Original |
stanley1228 | 7:7a564cf28ab6 | 160 | //Matrix& operator *=( Matrix& leftM, const Matrix& rightM ) |
stanley1228 | 7:7a564cf28ab6 | 161 | //{ |
stanley1228 | 7:7a564cf28ab6 | 162 | // if( leftM._nCols == rightM._nRows ) |
stanley1228 | 7:7a564cf28ab6 | 163 | // { |
stanley1228 | 7:7a564cf28ab6 | 164 | // Matrix resultM ( leftM._nRows, rightM._nCols ); |
stanley1228 | 7:7a564cf28ab6 | 165 | // |
stanley1228 | 7:7a564cf28ab6 | 166 | // for( int i = 0; i < resultM._nRows; i++ ) |
stanley1228 | 7:7a564cf28ab6 | 167 | // for( int j = 0; j < resultM._nCols; j++ ) |
stanley1228 | 7:7a564cf28ab6 | 168 | // for( int m = 0; m < rightM._nRows; m++ ) |
stanley1228 | 7:7a564cf28ab6 | 169 | // resultM._matrix[i][j] += leftM._matrix[i][m] * rightM._matrix[m][j]; |
stanley1228 | 7:7a564cf28ab6 | 170 | // |
stanley1228 | 7:7a564cf28ab6 | 171 | // return resultM; |
stanley1228 | 7:7a564cf28ab6 | 172 | // }else{ |
stanley1228 | 7:7a564cf28ab6 | 173 | // printf( "\n\nERROR:\nDiferent Dimensions @ *= operator\n" ); |
stanley1228 | 7:7a564cf28ab6 | 174 | // } |
stanley1228 | 7:7a564cf28ab6 | 175 | //} |
Yo_Robot | 5:a4014ab0a8cf | 176 | |
stanley1228 | 7:7a564cf28ab6 | 177 | //stanley |
Yo_Robot | 5:a4014ab0a8cf | 178 | Matrix& operator *=( Matrix& leftM, const Matrix& rightM ) |
Yo_Robot | 5:a4014ab0a8cf | 179 | { |
Yo_Robot | 5:a4014ab0a8cf | 180 | if( leftM._nCols == rightM._nRows ) |
Yo_Robot | 5:a4014ab0a8cf | 181 | { |
Yo_Robot | 5:a4014ab0a8cf | 182 | Matrix resultM ( leftM._nRows, rightM._nCols ); |
Yo_Robot | 5:a4014ab0a8cf | 183 | |
Yo_Robot | 5:a4014ab0a8cf | 184 | for( int i = 0; i < resultM._nRows; i++ ) |
Yo_Robot | 5:a4014ab0a8cf | 185 | for( int j = 0; j < resultM._nCols; j++ ) |
Yo_Robot | 5:a4014ab0a8cf | 186 | for( int m = 0; m < rightM._nRows; m++ ) |
Yo_Robot | 5:a4014ab0a8cf | 187 | resultM._matrix[i][j] += leftM._matrix[i][m] * rightM._matrix[m][j]; |
Yo_Robot | 5:a4014ab0a8cf | 188 | |
stanley1228 | 7:7a564cf28ab6 | 189 | leftM=resultM;//stanley |
stanley1228 | 7:7a564cf28ab6 | 190 | |
stanley1228 | 7:7a564cf28ab6 | 191 | // return resultM;//org |
stanley1228 | 7:7a564cf28ab6 | 192 | return leftM;//stanley |
Yo_Robot | 5:a4014ab0a8cf | 193 | }else{ |
stanley1228 | 8:1774aa06ab99 | 194 | DBGMSG(( "\n\nERROR:\nDiferent Dimensions @ *= operator\n" )) |
stanley1228 | 7:7a564cf28ab6 | 195 | |
stanley1228 | 7:7a564cf28ab6 | 196 | return leftM;//暫時這樣stanley |
stanley1228 | 7:7a564cf28ab6 | 197 | |
Yo_Robot | 5:a4014ab0a8cf | 198 | } |
Yo_Robot | 5:a4014ab0a8cf | 199 | } |
Yo_Robot | 5:a4014ab0a8cf | 200 | |
Yo_Robot | 5:a4014ab0a8cf | 201 | Matrix& operator *=( Matrix& leftM, float number ) |
Yo_Robot | 5:a4014ab0a8cf | 202 | { |
Yo_Robot | 5:a4014ab0a8cf | 203 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 5:a4014ab0a8cf | 204 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 5:a4014ab0a8cf | 205 | leftM._matrix[i][j] *= number; |
Yo_Robot | 5:a4014ab0a8cf | 206 | |
Yo_Robot | 5:a4014ab0a8cf | 207 | return leftM; |
Yo_Robot | 5:a4014ab0a8cf | 208 | } |
Yo_Robot | 5:a4014ab0a8cf | 209 | |
Yo_Robot | 5:a4014ab0a8cf | 210 | |
Yo_Robot | 3:589fb80932b5 | 211 | /*****************************************************************************/ |
Yo_Robot | 3:589fb80932b5 | 212 | |
Yo_Robot | 3:589fb80932b5 | 213 | // Overload operators |
Yo_Robot | 3:589fb80932b5 | 214 | |
Yo_Robot | 3:589fb80932b5 | 215 | |
Yo_Robot | 3:589fb80932b5 | 216 | const Matrix operator +=( Matrix& leftM, float number ) |
Yo_Robot | 3:589fb80932b5 | 217 | { |
Yo_Robot | 3:589fb80932b5 | 218 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 3:589fb80932b5 | 219 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 3:589fb80932b5 | 220 | leftM._matrix[i][j] += number; |
Yo_Robot | 3:589fb80932b5 | 221 | return leftM; |
Yo_Robot | 3:589fb80932b5 | 222 | } |
Yo_Robot | 3:589fb80932b5 | 223 | |
Yo_Robot | 3:589fb80932b5 | 224 | |
Yo_Robot | 3:589fb80932b5 | 225 | const Matrix operator -=( Matrix& leftM, float number ) |
Yo_Robot | 3:589fb80932b5 | 226 | { |
Yo_Robot | 3:589fb80932b5 | 227 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 3:589fb80932b5 | 228 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 3:589fb80932b5 | 229 | leftM._matrix[i][j] -= number; |
Yo_Robot | 3:589fb80932b5 | 230 | return leftM; |
Yo_Robot | 3:589fb80932b5 | 231 | } |
Yo_Robot | 3:589fb80932b5 | 232 | |
Yo_Robot | 3:589fb80932b5 | 233 | |
Yo_Robot | 3:589fb80932b5 | 234 | const Matrix operator +( const Matrix& leftM, const Matrix& rightM) |
Yo_Robot | 3:589fb80932b5 | 235 | { |
Yo_Robot | 3:589fb80932b5 | 236 | if( leftM._nRows == rightM._nRows && leftM._nCols == rightM._nCols ) |
Yo_Robot | 3:589fb80932b5 | 237 | { |
Yo_Robot | 3:589fb80932b5 | 238 | Matrix result( leftM._nRows, leftM._nCols ); |
Yo_Robot | 3:589fb80932b5 | 239 | |
Yo_Robot | 3:589fb80932b5 | 240 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 3:589fb80932b5 | 241 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 3:589fb80932b5 | 242 | result._matrix[i][j] = leftM._matrix[i][j] + rightM._matrix[i][j]; |
Yo_Robot | 3:589fb80932b5 | 243 | |
Yo_Robot | 3:589fb80932b5 | 244 | return result; |
Yo_Robot | 3:589fb80932b5 | 245 | |
Yo_Robot | 3:589fb80932b5 | 246 | }else{ |
stanley1228 | 8:1774aa06ab99 | 247 | DBGMSG(( "\n\nERROR\nDiferent Dimensions @ + operator \n" )) |
stanley1228 | 7:7a564cf28ab6 | 248 | |
stanley1228 | 7:7a564cf28ab6 | 249 | Matrix NULL_M( 1, 1 );//暫時這樣stanley |
stanley1228 | 7:7a564cf28ab6 | 250 | return NULL_M; |
Yo_Robot | 3:589fb80932b5 | 251 | //Matrix error(4); |
Yo_Robot | 3:589fb80932b5 | 252 | //error.Clear(); |
Yo_Robot | 3:589fb80932b5 | 253 | //return error; |
Yo_Robot | 3:589fb80932b5 | 254 | } |
Yo_Robot | 3:589fb80932b5 | 255 | } |
Yo_Robot | 3:589fb80932b5 | 256 | |
Yo_Robot | 3:589fb80932b5 | 257 | |
Yo_Robot | 3:589fb80932b5 | 258 | const Matrix operator +( const Matrix& leftM, float number ) |
Yo_Robot | 3:589fb80932b5 | 259 | { |
Yo_Robot | 3:589fb80932b5 | 260 | Matrix result( leftM._nRows, leftM._nCols ); |
Yo_Robot | 3:589fb80932b5 | 261 | |
Yo_Robot | 3:589fb80932b5 | 262 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 3:589fb80932b5 | 263 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 3:589fb80932b5 | 264 | result._matrix[i][j] = leftM._matrix[i][j] + number; |
Yo_Robot | 3:589fb80932b5 | 265 | |
Yo_Robot | 3:589fb80932b5 | 266 | return result; |
Yo_Robot | 3:589fb80932b5 | 267 | } |
Yo_Robot | 3:589fb80932b5 | 268 | |
Yo_Robot | 3:589fb80932b5 | 269 | |
Yo_Robot | 3:589fb80932b5 | 270 | const Matrix operator +( float number, const Matrix& leftM ) |
Yo_Robot | 3:589fb80932b5 | 271 | { |
Yo_Robot | 3:589fb80932b5 | 272 | return ( leftM + number ); |
Yo_Robot | 3:589fb80932b5 | 273 | } |
Yo_Robot | 3:589fb80932b5 | 274 | |
Yo_Robot | 3:589fb80932b5 | 275 | |
Yo_Robot | 3:589fb80932b5 | 276 | const Matrix operator -( const Matrix& leftM, const Matrix& rightM ) |
Yo_Robot | 3:589fb80932b5 | 277 | { |
Yo_Robot | 3:589fb80932b5 | 278 | if( leftM._nRows == rightM._nRows && leftM._nCols == rightM._nCols ) |
Yo_Robot | 3:589fb80932b5 | 279 | { |
Yo_Robot | 3:589fb80932b5 | 280 | Matrix result( leftM._nRows, leftM._nCols ); |
Yo_Robot | 3:589fb80932b5 | 281 | |
Yo_Robot | 3:589fb80932b5 | 282 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 3:589fb80932b5 | 283 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 3:589fb80932b5 | 284 | result._matrix[i][j] = leftM._matrix[i][j] - rightM._matrix[i][j]; |
Yo_Robot | 3:589fb80932b5 | 285 | |
Yo_Robot | 3:589fb80932b5 | 286 | return result; |
Yo_Robot | 3:589fb80932b5 | 287 | |
Yo_Robot | 3:589fb80932b5 | 288 | }else{ |
stanley1228 | 8:1774aa06ab99 | 289 | DBGMSG(( "\n\nERROR:\nDiferent Dimensions @ + operator \n" )) |
stanley1228 | 7:7a564cf28ab6 | 290 | |
stanley1228 | 7:7a564cf28ab6 | 291 | Matrix NULL_M( 1, 1 );//暫時這樣stanley |
stanley1228 | 7:7a564cf28ab6 | 292 | return NULL_M; |
Yo_Robot | 3:589fb80932b5 | 293 | } |
Yo_Robot | 3:589fb80932b5 | 294 | } |
Yo_Robot | 3:589fb80932b5 | 295 | |
Yo_Robot | 3:589fb80932b5 | 296 | |
Yo_Robot | 3:589fb80932b5 | 297 | const Matrix operator -( const Matrix& leftM, float number ) |
Yo_Robot | 3:589fb80932b5 | 298 | { |
Yo_Robot | 3:589fb80932b5 | 299 | Matrix result( leftM._nRows, leftM._nCols ); |
Yo_Robot | 3:589fb80932b5 | 300 | |
Yo_Robot | 3:589fb80932b5 | 301 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 3:589fb80932b5 | 302 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 3:589fb80932b5 | 303 | result._matrix[i][j] = leftM._matrix[i][j] - number; |
Yo_Robot | 3:589fb80932b5 | 304 | |
Yo_Robot | 3:589fb80932b5 | 305 | return result; |
Yo_Robot | 3:589fb80932b5 | 306 | } |
Yo_Robot | 3:589fb80932b5 | 307 | |
Yo_Robot | 3:589fb80932b5 | 308 | |
Yo_Robot | 3:589fb80932b5 | 309 | const Matrix operator -( float number, const Matrix& leftM ) |
Yo_Robot | 3:589fb80932b5 | 310 | { |
Yo_Robot | 3:589fb80932b5 | 311 | return ( leftM - number ); |
Yo_Robot | 3:589fb80932b5 | 312 | } |
Yo_Robot | 3:589fb80932b5 | 313 | |
Yo_Robot | 3:589fb80932b5 | 314 | |
Yo_Robot | 3:589fb80932b5 | 315 | const Matrix operator *( const Matrix& leftM, const Matrix& rightM ) |
Yo_Robot | 3:589fb80932b5 | 316 | { |
Yo_Robot | 3:589fb80932b5 | 317 | if( leftM._nCols == rightM._nRows ) |
Yo_Robot | 3:589fb80932b5 | 318 | { |
Yo_Robot | 3:589fb80932b5 | 319 | Matrix resultM ( leftM._nRows, rightM._nCols ); |
Yo_Robot | 3:589fb80932b5 | 320 | resultM.Clear(); |
Yo_Robot | 3:589fb80932b5 | 321 | |
Yo_Robot | 3:589fb80932b5 | 322 | for( int i = 0; i < resultM._nRows; i++ ) |
Yo_Robot | 3:589fb80932b5 | 323 | for( int j = 0; j < resultM._nCols; j++ ) |
Yo_Robot | 3:589fb80932b5 | 324 | for( int m = 0; m < rightM._nRows; m++ ) |
Yo_Robot | 3:589fb80932b5 | 325 | resultM._matrix[i][j] += leftM._matrix[i][m] * rightM._matrix[m][j]; |
Yo_Robot | 3:589fb80932b5 | 326 | |
Yo_Robot | 3:589fb80932b5 | 327 | return resultM; |
Yo_Robot | 3:589fb80932b5 | 328 | |
Yo_Robot | 3:589fb80932b5 | 329 | } else { |
Yo_Robot | 3:589fb80932b5 | 330 | |
stanley1228 | 8:1774aa06ab99 | 331 | DBGMSG(("\n\nERROR:\nDiferent Dimension matrices @ * operator")) |
stanley1228 | 7:7a564cf28ab6 | 332 | |
stanley1228 | 7:7a564cf28ab6 | 333 | Matrix NULL_M( 1, 1 );//暫時這樣stanley |
stanley1228 | 7:7a564cf28ab6 | 334 | return NULL_M; |
Yo_Robot | 3:589fb80932b5 | 335 | } |
Yo_Robot | 3:589fb80932b5 | 336 | |
Yo_Robot | 3:589fb80932b5 | 337 | } |
Yo_Robot | 3:589fb80932b5 | 338 | |
Yo_Robot | 3:589fb80932b5 | 339 | |
Yo_Robot | 3:589fb80932b5 | 340 | const Matrix operator *( const Matrix& leftM, float number ) |
Yo_Robot | 3:589fb80932b5 | 341 | { |
Yo_Robot | 3:589fb80932b5 | 342 | Matrix result( leftM._nRows, leftM._nCols ); |
Yo_Robot | 3:589fb80932b5 | 343 | |
Yo_Robot | 3:589fb80932b5 | 344 | for( int i = 0; i < leftM._nRows; i++ ) |
Yo_Robot | 3:589fb80932b5 | 345 | for( int j = 0; j < leftM._nCols; j++ ) |
Yo_Robot | 3:589fb80932b5 | 346 | result._matrix[i][j] = leftM._matrix[i][j] * number; |
Yo_Robot | 3:589fb80932b5 | 347 | |
Yo_Robot | 3:589fb80932b5 | 348 | return result; |
Yo_Robot | 3:589fb80932b5 | 349 | } |
Yo_Robot | 3:589fb80932b5 | 350 | |
Yo_Robot | 3:589fb80932b5 | 351 | const Matrix operator *( float number, const Matrix& leftM ) |
Yo_Robot | 3:589fb80932b5 | 352 | { |
Yo_Robot | 3:589fb80932b5 | 353 | return ( leftM * number ); |
Yo_Robot | 3:589fb80932b5 | 354 | } |
Yo_Robot | 3:589fb80932b5 | 355 | |
Yo_Robot | 3:589fb80932b5 | 356 | |
Yo_Robot | 3:589fb80932b5 | 357 | Matrix& operator <<( Matrix& leftM, float number ) |
Yo_Robot | 3:589fb80932b5 | 358 | { |
Yo_Robot | 3:589fb80932b5 | 359 | if( leftM._pCol == leftM._nCols ) //end of Row |
Yo_Robot | 3:589fb80932b5 | 360 | { |
Yo_Robot | 3:589fb80932b5 | 361 | leftM._pCol = 0; |
Yo_Robot | 3:589fb80932b5 | 362 | leftM._pRow++; |
Yo_Robot | 3:589fb80932b5 | 363 | } |
Yo_Robot | 3:589fb80932b5 | 364 | if( leftM._pRow > leftM._nRows ) |
Yo_Robot | 3:589fb80932b5 | 365 | { |
stanley1228 | 8:1774aa06ab99 | 366 | DBGMSG(("\n\nERROR:\nAssignment out of limits @ << operator")) |
Yo_Robot | 3:589fb80932b5 | 367 | return leftM; |
Yo_Robot | 3:589fb80932b5 | 368 | |
Yo_Robot | 3:589fb80932b5 | 369 | }else{ |
Yo_Robot | 3:589fb80932b5 | 370 | |
Yo_Robot | 3:589fb80932b5 | 371 | leftM._matrix[ leftM._pRow ][ leftM._pCol ] = number; |
Yo_Robot | 3:589fb80932b5 | 372 | leftM._pCol++; |
Yo_Robot | 3:589fb80932b5 | 373 | |
Yo_Robot | 3:589fb80932b5 | 374 | return leftM; |
Yo_Robot | 3:589fb80932b5 | 375 | } |
stanley1228 | 7:7a564cf28ab6 | 376 | } |