nanimo kaete nai

Committer:
tyuito
Date:
Tue Jun 14 07:37:36 2022 +0000
Revision:
0:4ff90c5befd9
nanimo kaete  nai to omou

Who changed what in which revision?

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