Matrix Library. v1.6.4 + some changes
Matrix.cpp@0:3abd8c2d7c34, 2011-09-23 (annotated)
- Committer:
- Yo_Robot
- Date:
- Fri Sep 23 21:12:17 2011 +0000
- Revision:
- 0:3abd8c2d7c34
- Child:
- 1:48f417da268e
Matrix_v1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yo_Robot | 0:3abd8c2d7c34 | 1 | #include "mbed.h" |
Yo_Robot | 0:3abd8c2d7c34 | 2 | #include "Matrix.h" |
Yo_Robot | 0:3abd8c2d7c34 | 3 | |
Yo_Robot | 0:3abd8c2d7c34 | 4 | // Rows by Cols Matrix Constructor |
Yo_Robot | 0:3abd8c2d7c34 | 5 | Matrix::Matrix(int Cols, int Rows): _Cols(Cols), _Rows(Rows) |
Yo_Robot | 0:3abd8c2d7c34 | 6 | { |
Yo_Robot | 0:3abd8c2d7c34 | 7 | _matrix.resize(_Rows); |
Yo_Robot | 0:3abd8c2d7c34 | 8 | for( int i = 0; i < _Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 9 | _matrix[i].resize(_Cols); |
Yo_Robot | 0:3abd8c2d7c34 | 10 | } |
Yo_Robot | 0:3abd8c2d7c34 | 11 | |
Yo_Robot | 0:3abd8c2d7c34 | 12 | |
Yo_Robot | 0:3abd8c2d7c34 | 13 | |
Yo_Robot | 0:3abd8c2d7c34 | 14 | // Defines the same parameters as 'base' Matrix |
Yo_Robot | 0:3abd8c2d7c34 | 15 | Matrix::Matrix(const Matrix &base): _Cols(base._Cols), _Rows(base._Rows) |
Yo_Robot | 0:3abd8c2d7c34 | 16 | { |
Yo_Robot | 0:3abd8c2d7c34 | 17 | _matrix.resize(_Rows); |
Yo_Robot | 0:3abd8c2d7c34 | 18 | for( int i = 0; i < _Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 19 | _matrix[i].resize(_Cols); |
Yo_Robot | 0:3abd8c2d7c34 | 20 | } |
Yo_Robot | 0:3abd8c2d7c34 | 21 | |
Yo_Robot | 0:3abd8c2d7c34 | 22 | |
Yo_Robot | 0:3abd8c2d7c34 | 23 | |
Yo_Robot | 0:3abd8c2d7c34 | 24 | // Square Matrix Constructor |
Yo_Robot | 0:3abd8c2d7c34 | 25 | Matrix::Matrix(int square): _Cols(square), _Rows(square) |
Yo_Robot | 0:3abd8c2d7c34 | 26 | { |
Yo_Robot | 0:3abd8c2d7c34 | 27 | _matrix.resize(_Rows); |
Yo_Robot | 0:3abd8c2d7c34 | 28 | for( int i = 0; i < _Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 29 | _matrix[i].resize(_Cols); |
Yo_Robot | 0:3abd8c2d7c34 | 30 | } |
Yo_Robot | 0:3abd8c2d7c34 | 31 | |
Yo_Robot | 0:3abd8c2d7c34 | 32 | |
Yo_Robot | 0:3abd8c2d7c34 | 33 | |
Yo_Robot | 0:3abd8c2d7c34 | 34 | // Default Constructor |
Yo_Robot | 0:3abd8c2d7c34 | 35 | Matrix::Matrix(){/*Intentionally left Blank*/} |
Yo_Robot | 0:3abd8c2d7c34 | 36 | |
Yo_Robot | 0:3abd8c2d7c34 | 37 | |
Yo_Robot | 0:3abd8c2d7c34 | 38 | //***** OPERATORS ****// |
Yo_Robot | 0:3abd8c2d7c34 | 39 | |
Yo_Robot | 0:3abd8c2d7c34 | 40 | // Overloaded Asign Operator |
Yo_Robot | 0:3abd8c2d7c34 | 41 | Matrix& Matrix::operator = ( const Matrix &rightM ) |
Yo_Robot | 0:3abd8c2d7c34 | 42 | { |
Yo_Robot | 0:3abd8c2d7c34 | 43 | if( *this != rightM ) |
Yo_Robot | 0:3abd8c2d7c34 | 44 | { |
Yo_Robot | 0:3abd8c2d7c34 | 45 | this->_Rows = rightM._Rows; |
Yo_Robot | 0:3abd8c2d7c34 | 46 | this->_Cols = rightM._Cols; |
Yo_Robot | 0:3abd8c2d7c34 | 47 | |
Yo_Robot | 0:3abd8c2d7c34 | 48 | this->_matrix.resize( rightM._Rows ); |
Yo_Robot | 0:3abd8c2d7c34 | 49 | for( int i = 0; i < rightM._Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 50 | this->_matrix [i].resize(rightM._Cols); |
Yo_Robot | 0:3abd8c2d7c34 | 51 | |
Yo_Robot | 0:3abd8c2d7c34 | 52 | for( int i = 0; i < _Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 53 | for( int j = 0; j < _Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 54 | this->_matrix[i][j] = rightM._matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 55 | |
Yo_Robot | 0:3abd8c2d7c34 | 56 | return *this; |
Yo_Robot | 0:3abd8c2d7c34 | 57 | } |
Yo_Robot | 0:3abd8c2d7c34 | 58 | else |
Yo_Robot | 0:3abd8c2d7c34 | 59 | { |
Yo_Robot | 0:3abd8c2d7c34 | 60 | Matrix ret (*this); |
Yo_Robot | 0:3abd8c2d7c34 | 61 | return ret; |
Yo_Robot | 0:3abd8c2d7c34 | 62 | } |
Yo_Robot | 0:3abd8c2d7c34 | 63 | } |
Yo_Robot | 0:3abd8c2d7c34 | 64 | |
Yo_Robot | 0:3abd8c2d7c34 | 65 | |
Yo_Robot | 0:3abd8c2d7c34 | 66 | bool Matrix::operator == ( const Matrix& rightM ) |
Yo_Robot | 0:3abd8c2d7c34 | 67 | { |
Yo_Robot | 0:3abd8c2d7c34 | 68 | if( _Rows == rightM._Rows && _Cols == rightM._Cols ) |
Yo_Robot | 0:3abd8c2d7c34 | 69 | { |
Yo_Robot | 0:3abd8c2d7c34 | 70 | bool equal = false; |
Yo_Robot | 0:3abd8c2d7c34 | 71 | |
Yo_Robot | 0:3abd8c2d7c34 | 72 | for( int i = 0; i < _Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 73 | for( int j = 0; j < _Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 74 | if( _matrix[i][j] != rightM._matrix[i][j] ) |
Yo_Robot | 0:3abd8c2d7c34 | 75 | equal = equal || true; |
Yo_Robot | 0:3abd8c2d7c34 | 76 | |
Yo_Robot | 0:3abd8c2d7c34 | 77 | return !equal; |
Yo_Robot | 0:3abd8c2d7c34 | 78 | |
Yo_Robot | 0:3abd8c2d7c34 | 79 | }else{ return false; } |
Yo_Robot | 0:3abd8c2d7c34 | 80 | } |
Yo_Robot | 0:3abd8c2d7c34 | 81 | |
Yo_Robot | 0:3abd8c2d7c34 | 82 | |
Yo_Robot | 0:3abd8c2d7c34 | 83 | bool Matrix::operator != ( const Matrix &rightM ) |
Yo_Robot | 0:3abd8c2d7c34 | 84 | { |
Yo_Robot | 0:3abd8c2d7c34 | 85 | return !( *this == rightM ); |
Yo_Robot | 0:3abd8c2d7c34 | 86 | } |
Yo_Robot | 0:3abd8c2d7c34 | 87 | |
Yo_Robot | 0:3abd8c2d7c34 | 88 | |
Yo_Robot | 0:3abd8c2d7c34 | 89 | Matrix& Matrix::operator +=(const Matrix& rightM) |
Yo_Robot | 0:3abd8c2d7c34 | 90 | { |
Yo_Robot | 0:3abd8c2d7c34 | 91 | if( _Rows == rightM._Rows && _Cols == rightM._Cols ) |
Yo_Robot | 0:3abd8c2d7c34 | 92 | { |
Yo_Robot | 0:3abd8c2d7c34 | 93 | for( int i = 0; i < _Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 94 | for( int j = 0; j < _Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 95 | _matrix[i][j] += rightM._matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 96 | |
Yo_Robot | 0:3abd8c2d7c34 | 97 | return *this; |
Yo_Robot | 0:3abd8c2d7c34 | 98 | |
Yo_Robot | 0:3abd8c2d7c34 | 99 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 100 | |
Yo_Robot | 0:3abd8c2d7c34 | 101 | printf( "\nERROR\nDiferent Dimensions @ += Assignment\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 102 | |
Yo_Robot | 0:3abd8c2d7c34 | 103 | // Code below doesn't work! I dunno why. But anyway at this point |
Yo_Robot | 0:3abd8c2d7c34 | 104 | // the program should break anyway. |
Yo_Robot | 0:3abd8c2d7c34 | 105 | // |
Yo_Robot | 0:3abd8c2d7c34 | 106 | //Matrix::Matrix error( 4 ); |
Yo_Robot | 0:3abd8c2d7c34 | 107 | //error.Clear(); |
Yo_Robot | 0:3abd8c2d7c34 | 108 | //return error; |
Yo_Robot | 0:3abd8c2d7c34 | 109 | |
Yo_Robot | 0:3abd8c2d7c34 | 110 | } |
Yo_Robot | 0:3abd8c2d7c34 | 111 | } |
Yo_Robot | 0:3abd8c2d7c34 | 112 | |
Yo_Robot | 0:3abd8c2d7c34 | 113 | |
Yo_Robot | 0:3abd8c2d7c34 | 114 | Matrix& Matrix::operator -=(const Matrix& rightM) |
Yo_Robot | 0:3abd8c2d7c34 | 115 | { |
Yo_Robot | 0:3abd8c2d7c34 | 116 | if( _Rows == rightM._Rows && _Cols == rightM._Cols ) |
Yo_Robot | 0:3abd8c2d7c34 | 117 | { |
Yo_Robot | 0:3abd8c2d7c34 | 118 | for( int i = 0; i < _Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 119 | for( int j = 0; j < _Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 120 | _matrix[i][j] -= rightM._matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 121 | |
Yo_Robot | 0:3abd8c2d7c34 | 122 | return *this; |
Yo_Robot | 0:3abd8c2d7c34 | 123 | |
Yo_Robot | 0:3abd8c2d7c34 | 124 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 125 | printf( "\nERROR\nDiferent Dimensions @ -= Assignment\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 126 | |
Yo_Robot | 0:3abd8c2d7c34 | 127 | // return rightM; |
Yo_Robot | 0:3abd8c2d7c34 | 128 | } |
Yo_Robot | 0:3abd8c2d7c34 | 129 | } |
Yo_Robot | 0:3abd8c2d7c34 | 130 | |
Yo_Robot | 0:3abd8c2d7c34 | 131 | const Matrix Matrix::operator +(const Matrix& rightM) |
Yo_Robot | 0:3abd8c2d7c34 | 132 | { |
Yo_Robot | 0:3abd8c2d7c34 | 133 | Matrix result = *this; |
Yo_Robot | 0:3abd8c2d7c34 | 134 | result += rightM; |
Yo_Robot | 0:3abd8c2d7c34 | 135 | return result; |
Yo_Robot | 0:3abd8c2d7c34 | 136 | } |
Yo_Robot | 0:3abd8c2d7c34 | 137 | |
Yo_Robot | 0:3abd8c2d7c34 | 138 | const Matrix Matrix::operator -(const Matrix& rightM) |
Yo_Robot | 0:3abd8c2d7c34 | 139 | { |
Yo_Robot | 0:3abd8c2d7c34 | 140 | Matrix result = *this; |
Yo_Robot | 0:3abd8c2d7c34 | 141 | result -= rightM; |
Yo_Robot | 0:3abd8c2d7c34 | 142 | return result; |
Yo_Robot | 0:3abd8c2d7c34 | 143 | } |
Yo_Robot | 0:3abd8c2d7c34 | 144 | |
Yo_Robot | 0:3abd8c2d7c34 | 145 | //------------------------------------------ |
Yo_Robot | 0:3abd8c2d7c34 | 146 | |
Yo_Robot | 0:3abd8c2d7c34 | 147 | //*** Method that return MATRIX objects ***// |
Yo_Robot | 0:3abd8c2d7c34 | 148 | |
Yo_Robot | 0:3abd8c2d7c34 | 149 | |
Yo_Robot | 0:3abd8c2d7c34 | 150 | |
Yo_Robot | 0:3abd8c2d7c34 | 151 | Matrix Matrix::GetRow(Matrix mat, int Row) |
Yo_Robot | 0:3abd8c2d7c34 | 152 | { |
Yo_Robot | 0:3abd8c2d7c34 | 153 | if( Row > mat._Rows ) |
Yo_Robot | 0:3abd8c2d7c34 | 154 | { |
Yo_Robot | 0:3abd8c2d7c34 | 155 | printf( "WARNING: Row out of dimmensions @ GetRow().\nEmpty Matrix Returned\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 156 | Matrix err( 1 , mat._Cols ); |
Yo_Robot | 0:3abd8c2d7c34 | 157 | err.Clear(); |
Yo_Robot | 0:3abd8c2d7c34 | 158 | return err; |
Yo_Robot | 0:3abd8c2d7c34 | 159 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 160 | |
Yo_Robot | 0:3abd8c2d7c34 | 161 | Matrix SingleRow( 1 , mat._Cols ); |
Yo_Robot | 0:3abd8c2d7c34 | 162 | |
Yo_Robot | 0:3abd8c2d7c34 | 163 | for( int i = 0; i < mat._Cols; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 164 | SingleRow._matrix[0][i] = mat._matrix[Row][i]; |
Yo_Robot | 0:3abd8c2d7c34 | 165 | |
Yo_Robot | 0:3abd8c2d7c34 | 166 | return SingleRow; |
Yo_Robot | 0:3abd8c2d7c34 | 167 | } |
Yo_Robot | 0:3abd8c2d7c34 | 168 | } |
Yo_Robot | 0:3abd8c2d7c34 | 169 | |
Yo_Robot | 0:3abd8c2d7c34 | 170 | |
Yo_Robot | 0:3abd8c2d7c34 | 171 | |
Yo_Robot | 0:3abd8c2d7c34 | 172 | Matrix Matrix::GetCol(Matrix mat, int Col) |
Yo_Robot | 0:3abd8c2d7c34 | 173 | { |
Yo_Robot | 0:3abd8c2d7c34 | 174 | if( Col > mat._Cols ) |
Yo_Robot | 0:3abd8c2d7c34 | 175 | { |
Yo_Robot | 0:3abd8c2d7c34 | 176 | printf( "WARNING: Column out of dimmensions @ GetCol.\nEmpty matrix returned\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 177 | Matrix err( mat._Rows , 1 ); |
Yo_Robot | 0:3abd8c2d7c34 | 178 | err.Clear(); |
Yo_Robot | 0:3abd8c2d7c34 | 179 | return err; |
Yo_Robot | 0:3abd8c2d7c34 | 180 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 181 | |
Yo_Robot | 0:3abd8c2d7c34 | 182 | Matrix SingleCol( mat._Rows, 1 ); |
Yo_Robot | 0:3abd8c2d7c34 | 183 | for(int i = 0; i < mat._Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 184 | SingleCol._matrix[i][0] = mat._matrix[i][Col]; |
Yo_Robot | 0:3abd8c2d7c34 | 185 | return SingleCol; |
Yo_Robot | 0:3abd8c2d7c34 | 186 | } |
Yo_Robot | 0:3abd8c2d7c34 | 187 | } |
Yo_Robot | 0:3abd8c2d7c34 | 188 | |
Yo_Robot | 0:3abd8c2d7c34 | 189 | |
Yo_Robot | 0:3abd8c2d7c34 | 190 | |
Yo_Robot | 0:3abd8c2d7c34 | 191 | Matrix Matrix::ToPackedVector() |
Yo_Robot | 0:3abd8c2d7c34 | 192 | { |
Yo_Robot | 0:3abd8c2d7c34 | 193 | Matrix Shattered( ( this->_Rows * this->_Cols ) , 1 ); |
Yo_Robot | 0:3abd8c2d7c34 | 194 | |
Yo_Robot | 0:3abd8c2d7c34 | 195 | int cont = 0; |
Yo_Robot | 0:3abd8c2d7c34 | 196 | for( int i = 0; i < this->_Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 197 | { |
Yo_Robot | 0:3abd8c2d7c34 | 198 | for( int j = 0; j < this->_Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 199 | { |
Yo_Robot | 0:3abd8c2d7c34 | 200 | Shattered._matrix[0][cont] = this->_matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 201 | cont++; |
Yo_Robot | 0:3abd8c2d7c34 | 202 | } |
Yo_Robot | 0:3abd8c2d7c34 | 203 | } |
Yo_Robot | 0:3abd8c2d7c34 | 204 | |
Yo_Robot | 0:3abd8c2d7c34 | 205 | return Shattered; |
Yo_Robot | 0:3abd8c2d7c34 | 206 | } |
Yo_Robot | 0:3abd8c2d7c34 | 207 | //---------------------------------------------------- |
Yo_Robot | 0:3abd8c2d7c34 | 208 | |
Yo_Robot | 0:3abd8c2d7c34 | 209 | //*** Method returning BOOLEAN ***// |
Yo_Robot | 0:3abd8c2d7c34 | 210 | |
Yo_Robot | 0:3abd8c2d7c34 | 211 | |
Yo_Robot | 0:3abd8c2d7c34 | 212 | |
Yo_Robot | 0:3abd8c2d7c34 | 213 | |
Yo_Robot | 0:3abd8c2d7c34 | 214 | bool Matrix::isZero() |
Yo_Robot | 0:3abd8c2d7c34 | 215 | { |
Yo_Robot | 0:3abd8c2d7c34 | 216 | bool zero = false; |
Yo_Robot | 0:3abd8c2d7c34 | 217 | for( int i = 0; i < this->_Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 218 | for( int j = 0; j < this->_Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 219 | if( this->_matrix[i][j] != 0 ) |
Yo_Robot | 0:3abd8c2d7c34 | 220 | zero = zero || true; |
Yo_Robot | 0:3abd8c2d7c34 | 221 | return !zero; |
Yo_Robot | 0:3abd8c2d7c34 | 222 | } |
Yo_Robot | 0:3abd8c2d7c34 | 223 | |
Yo_Robot | 0:3abd8c2d7c34 | 224 | |
Yo_Robot | 0:3abd8c2d7c34 | 225 | |
Yo_Robot | 0:3abd8c2d7c34 | 226 | bool Matrix::isVector() |
Yo_Robot | 0:3abd8c2d7c34 | 227 | { |
Yo_Robot | 0:3abd8c2d7c34 | 228 | if( this->_Rows == 1 || this->_Cols == 1 ) |
Yo_Robot | 0:3abd8c2d7c34 | 229 | return true; |
Yo_Robot | 0:3abd8c2d7c34 | 230 | else |
Yo_Robot | 0:3abd8c2d7c34 | 231 | return false; |
Yo_Robot | 0:3abd8c2d7c34 | 232 | } |
Yo_Robot | 0:3abd8c2d7c34 | 233 | |
Yo_Robot | 0:3abd8c2d7c34 | 234 | //------------------------------------- |
Yo_Robot | 0:3abd8c2d7c34 | 235 | |
Yo_Robot | 0:3abd8c2d7c34 | 236 | //*** Static Methods ***// |
Yo_Robot | 0:3abd8c2d7c34 | 237 | |
Yo_Robot | 0:3abd8c2d7c34 | 238 | const bool Matrix::Equals( Matrix mat1, Matrix mat2 ) |
Yo_Robot | 0:3abd8c2d7c34 | 239 | { |
Yo_Robot | 0:3abd8c2d7c34 | 240 | if( mat1._Cols == mat2._Cols && mat1._Rows == mat2._Rows ) |
Yo_Robot | 0:3abd8c2d7c34 | 241 | { |
Yo_Robot | 0:3abd8c2d7c34 | 242 | bool equal = false; |
Yo_Robot | 0:3abd8c2d7c34 | 243 | |
Yo_Robot | 0:3abd8c2d7c34 | 244 | for( int i = 0; i < mat1._Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 245 | for( int j = 0; j < mat1._Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 246 | if( mat1._matrix[i][j] != mat2._matrix[i][j] ) |
Yo_Robot | 0:3abd8c2d7c34 | 247 | equal = equal || true; |
Yo_Robot | 0:3abd8c2d7c34 | 248 | |
Yo_Robot | 0:3abd8c2d7c34 | 249 | return !equal; |
Yo_Robot | 0:3abd8c2d7c34 | 250 | |
Yo_Robot | 0:3abd8c2d7c34 | 251 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 252 | printf( "ERROR: Diferent Size Matrices!\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 253 | printf( "mat1._Rows = %u\nmat1._Cols = %u\n",mat1._Rows, mat1._Cols ); |
Yo_Robot | 0:3abd8c2d7c34 | 254 | printf( "mat2._Rows = %u\nmat2._Cols = %u\n",mat2._Rows, mat2._Cols );; |
Yo_Robot | 0:3abd8c2d7c34 | 255 | return false; |
Yo_Robot | 0:3abd8c2d7c34 | 256 | } |
Yo_Robot | 0:3abd8c2d7c34 | 257 | } |
Yo_Robot | 0:3abd8c2d7c34 | 258 | |
Yo_Robot | 0:3abd8c2d7c34 | 259 | |
Yo_Robot | 0:3abd8c2d7c34 | 260 | |
Yo_Robot | 0:3abd8c2d7c34 | 261 | Matrix Matrix::CreateColumnMatrix(int Cols) |
Yo_Robot | 0:3abd8c2d7c34 | 262 | { |
Yo_Robot | 0:3abd8c2d7c34 | 263 | Matrix ColMatrix(1,Cols); |
Yo_Robot | 0:3abd8c2d7c34 | 264 | return ColMatrix; |
Yo_Robot | 0:3abd8c2d7c34 | 265 | } |
Yo_Robot | 0:3abd8c2d7c34 | 266 | |
Yo_Robot | 0:3abd8c2d7c34 | 267 | |
Yo_Robot | 0:3abd8c2d7c34 | 268 | |
Yo_Robot | 0:3abd8c2d7c34 | 269 | Matrix Matrix::CreateRowMatrix(int Rows) |
Yo_Robot | 0:3abd8c2d7c34 | 270 | { |
Yo_Robot | 0:3abd8c2d7c34 | 271 | Matrix RowMatrix( Rows, 1); |
Yo_Robot | 0:3abd8c2d7c34 | 272 | return RowMatrix; |
Yo_Robot | 0:3abd8c2d7c34 | 273 | } |
Yo_Robot | 0:3abd8c2d7c34 | 274 | |
Yo_Robot | 0:3abd8c2d7c34 | 275 | |
Yo_Robot | 0:3abd8c2d7c34 | 276 | |
Yo_Robot | 0:3abd8c2d7c34 | 277 | void Matrix::Clone( const Matrix &Source, Matrix Receip ) |
Yo_Robot | 0:3abd8c2d7c34 | 278 | { |
Yo_Robot | 0:3abd8c2d7c34 | 279 | Receip._Cols = Source._Cols; |
Yo_Robot | 0:3abd8c2d7c34 | 280 | Receip._Rows = Source._Rows; |
Yo_Robot | 0:3abd8c2d7c34 | 281 | |
Yo_Robot | 0:3abd8c2d7c34 | 282 | Receip._matrix.resize(Receip._Rows); |
Yo_Robot | 0:3abd8c2d7c34 | 283 | for( int i = 0; i < Receip._Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 284 | Receip._matrix[i].resize(Receip._Cols); |
Yo_Robot | 0:3abd8c2d7c34 | 285 | |
Yo_Robot | 0:3abd8c2d7c34 | 286 | for( int i = 0; i < Receip._Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 287 | for( int j = 0; j < Receip._Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 288 | Receip._matrix[i][j] = Source._matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 289 | } |
Yo_Robot | 0:3abd8c2d7c34 | 290 | |
Yo_Robot | 0:3abd8c2d7c34 | 291 | |
Yo_Robot | 0:3abd8c2d7c34 | 292 | void Matrix::DeleteRow(Matrix& Mat, int Row) |
Yo_Robot | 0:3abd8c2d7c34 | 293 | { |
Yo_Robot | 0:3abd8c2d7c34 | 294 | --Row; |
Yo_Robot | 0:3abd8c2d7c34 | 295 | |
Yo_Robot | 0:3abd8c2d7c34 | 296 | if( Row > Mat._Rows ) |
Yo_Robot | 0:3abd8c2d7c34 | 297 | { |
Yo_Robot | 0:3abd8c2d7c34 | 298 | printf("\nERROR:\nColumn out of Limits on DeleteCol()\n"); |
Yo_Robot | 0:3abd8c2d7c34 | 299 | |
Yo_Robot | 0:3abd8c2d7c34 | 300 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 301 | |
Yo_Robot | 0:3abd8c2d7c34 | 302 | for( int i = Row; i < Mat._Rows - 1; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 303 | |
Yo_Robot | 0:3abd8c2d7c34 | 304 | for( int j = 0; j < Mat._Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 305 | Mat._matrix[i][j] = Mat._matrix[i+1][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 306 | Mat._Rows--; |
Yo_Robot | 0:3abd8c2d7c34 | 307 | Mat._matrix.resize(Mat._Rows); |
Yo_Robot | 0:3abd8c2d7c34 | 308 | } |
Yo_Robot | 0:3abd8c2d7c34 | 309 | } |
Yo_Robot | 0:3abd8c2d7c34 | 310 | |
Yo_Robot | 0:3abd8c2d7c34 | 311 | |
Yo_Robot | 0:3abd8c2d7c34 | 312 | |
Yo_Robot | 0:3abd8c2d7c34 | 313 | void Matrix::DeleteCol( Matrix &Mat, int Col) |
Yo_Robot | 0:3abd8c2d7c34 | 314 | { |
Yo_Robot | 0:3abd8c2d7c34 | 315 | --Col; // Because of Column zero. |
Yo_Robot | 0:3abd8c2d7c34 | 316 | |
Yo_Robot | 0:3abd8c2d7c34 | 317 | if( Col > Mat._Cols ) |
Yo_Robot | 0:3abd8c2d7c34 | 318 | { |
Yo_Robot | 0:3abd8c2d7c34 | 319 | printf("\nERROR:\nColumn out of Limits on DeleteCol()\n"); |
Yo_Robot | 0:3abd8c2d7c34 | 320 | |
Yo_Robot | 0:3abd8c2d7c34 | 321 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 322 | |
Yo_Robot | 0:3abd8c2d7c34 | 323 | for( int i = 0; i < Mat._Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 324 | for( int j = Col; j < Mat._Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 325 | Mat._matrix[i][j] = Mat._matrix[i][j+1]; |
Yo_Robot | 0:3abd8c2d7c34 | 326 | |
Yo_Robot | 0:3abd8c2d7c34 | 327 | // Decrease one column |
Yo_Robot | 0:3abd8c2d7c34 | 328 | Mat._Cols--; |
Yo_Robot | 0:3abd8c2d7c34 | 329 | |
Yo_Robot | 0:3abd8c2d7c34 | 330 | //Erase last Column |
Yo_Robot | 0:3abd8c2d7c34 | 331 | for( int i = 0; i < Mat._Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 332 | Mat._matrix[i].reserve(Mat._Cols); |
Yo_Robot | 0:3abd8c2d7c34 | 333 | |
Yo_Robot | 0:3abd8c2d7c34 | 334 | } |
Yo_Robot | 0:3abd8c2d7c34 | 335 | } |
Yo_Robot | 0:3abd8c2d7c34 | 336 | |
Yo_Robot | 0:3abd8c2d7c34 | 337 | //--------------------------------------------------------------- |
Yo_Robot | 0:3abd8c2d7c34 | 338 | |
Yo_Robot | 0:3abd8c2d7c34 | 339 | //*** Methods returning nothing VOID ***// |
Yo_Robot | 0:3abd8c2d7c34 | 340 | |
Yo_Robot | 0:3abd8c2d7c34 | 341 | void Matrix::Add(int Row, int Col, float number) |
Yo_Robot | 0:3abd8c2d7c34 | 342 | { |
Yo_Robot | 0:3abd8c2d7c34 | 343 | if( Row > _Rows || Col > _Cols ) |
Yo_Robot | 0:3abd8c2d7c34 | 344 | { |
Yo_Robot | 0:3abd8c2d7c34 | 345 | printf("\nERROR:\n@ Matrix::Add, Out of limits of Matrix\n"); |
Yo_Robot | 0:3abd8c2d7c34 | 346 | |
Yo_Robot | 0:3abd8c2d7c34 | 347 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 348 | _matrix[Row][Col] = number; |
Yo_Robot | 0:3abd8c2d7c34 | 349 | } |
Yo_Robot | 0:3abd8c2d7c34 | 350 | |
Yo_Robot | 0:3abd8c2d7c34 | 351 | |
Yo_Robot | 0:3abd8c2d7c34 | 352 | } |
Yo_Robot | 0:3abd8c2d7c34 | 353 | |
Yo_Robot | 0:3abd8c2d7c34 | 354 | void Matrix::Resize( int Rows, int Cols ) |
Yo_Robot | 0:3abd8c2d7c34 | 355 | { |
Yo_Robot | 0:3abd8c2d7c34 | 356 | this->_Rows = Rows; |
Yo_Robot | 0:3abd8c2d7c34 | 357 | this->_Cols = Cols; |
Yo_Robot | 0:3abd8c2d7c34 | 358 | this->_matrix.resize( _Rows ); |
Yo_Robot | 0:3abd8c2d7c34 | 359 | |
Yo_Robot | 0:3abd8c2d7c34 | 360 | for( int i = 0; i< _Rows ; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 361 | _matrix[i].resize(_Cols); |
Yo_Robot | 0:3abd8c2d7c34 | 362 | } |
Yo_Robot | 0:3abd8c2d7c34 | 363 | |
Yo_Robot | 0:3abd8c2d7c34 | 364 | |
Yo_Robot | 0:3abd8c2d7c34 | 365 | void Matrix::FillMatrix() |
Yo_Robot | 0:3abd8c2d7c34 | 366 | { |
Yo_Robot | 0:3abd8c2d7c34 | 367 | for(int i = 0; i < _Rows; i++) |
Yo_Robot | 0:3abd8c2d7c34 | 368 | { |
Yo_Robot | 0:3abd8c2d7c34 | 369 | for(int j = 0; j < _Cols; j++) |
Yo_Robot | 0:3abd8c2d7c34 | 370 | { |
Yo_Robot | 0:3abd8c2d7c34 | 371 | printf( "Position [%u][%u]: ", i, j ); |
Yo_Robot | 0:3abd8c2d7c34 | 372 | float numero; |
Yo_Robot | 0:3abd8c2d7c34 | 373 | scanf( "%f", &numero ); |
Yo_Robot | 0:3abd8c2d7c34 | 374 | printf("%.3f ", numero); |
Yo_Robot | 0:3abd8c2d7c34 | 375 | this->Add( i, j, numero); |
Yo_Robot | 0:3abd8c2d7c34 | 376 | } |
Yo_Robot | 0:3abd8c2d7c34 | 377 | printf("\n"); |
Yo_Robot | 0:3abd8c2d7c34 | 378 | } |
Yo_Robot | 0:3abd8c2d7c34 | 379 | printf("\n"); |
Yo_Robot | 0:3abd8c2d7c34 | 380 | } |
Yo_Robot | 0:3abd8c2d7c34 | 381 | |
Yo_Robot | 0:3abd8c2d7c34 | 382 | |
Yo_Robot | 0:3abd8c2d7c34 | 383 | void Matrix::Clear() |
Yo_Robot | 0:3abd8c2d7c34 | 384 | { |
Yo_Robot | 0:3abd8c2d7c34 | 385 | for( int i = 0; i < _Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 386 | for( int j = 0; j < _Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 387 | _matrix[i][j] = 0; |
Yo_Robot | 0:3abd8c2d7c34 | 388 | } |
Yo_Robot | 0:3abd8c2d7c34 | 389 | |
Yo_Robot | 0:3abd8c2d7c34 | 390 | void Matrix::print() |
Yo_Robot | 0:3abd8c2d7c34 | 391 | { |
Yo_Robot | 0:3abd8c2d7c34 | 392 | for( int i = 0; i < _Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 393 | { |
Yo_Robot | 0:3abd8c2d7c34 | 394 | for( int j = 0; j < _Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 395 | { |
Yo_Robot | 0:3abd8c2d7c34 | 396 | printf( "%.3f, ",_matrix[i][j] ); |
Yo_Robot | 0:3abd8c2d7c34 | 397 | } |
Yo_Robot | 0:3abd8c2d7c34 | 398 | printf( "\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 399 | } |
Yo_Robot | 0:3abd8c2d7c34 | 400 | } |
Yo_Robot | 0:3abd8c2d7c34 | 401 | |
Yo_Robot | 0:3abd8c2d7c34 | 402 | |
Yo_Robot | 0:3abd8c2d7c34 | 403 | |
Yo_Robot | 0:3abd8c2d7c34 | 404 | float Matrix::Sum() |
Yo_Robot | 0:3abd8c2d7c34 | 405 | { |
Yo_Robot | 0:3abd8c2d7c34 | 406 | float total; |
Yo_Robot | 0:3abd8c2d7c34 | 407 | |
Yo_Robot | 0:3abd8c2d7c34 | 408 | for( int i = 0; i < this->_Rows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 409 | for( int j = 0; j < this->_Cols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 410 | total += this->_matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 411 | return total; |
Yo_Robot | 0:3abd8c2d7c34 | 412 | } |
Yo_Robot | 0:3abd8c2d7c34 | 413 | |
Yo_Robot | 0:3abd8c2d7c34 | 414 | |
Yo_Robot | 0:3abd8c2d7c34 | 415 | |
Yo_Robot | 0:3abd8c2d7c34 | 416 | float Matrix::GetNumber( int Row, int Col ) |
Yo_Robot | 0:3abd8c2d7c34 | 417 | { |
Yo_Robot | 0:3abd8c2d7c34 | 418 | return this->_matrix[Row][Col]; |
Yo_Robot | 0:3abd8c2d7c34 | 419 | } |
Yo_Robot | 0:3abd8c2d7c34 | 420 | |
Yo_Robot | 0:3abd8c2d7c34 | 421 | |
Yo_Robot | 0:3abd8c2d7c34 | 422 | int Matrix::getRows(){ return this->_Rows; } |
Yo_Robot | 0:3abd8c2d7c34 | 423 | |
Yo_Robot | 0:3abd8c2d7c34 | 424 | int Matrix::getCols(){ return this->_Cols; } |
Yo_Robot | 0:3abd8c2d7c34 | 425 | |
Yo_Robot | 0:3abd8c2d7c34 | 426 | |
Yo_Robot | 0:3abd8c2d7c34 | 427 |