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