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