Matrix Library. v1.6.4 + some changes
Matrix.cpp@1:48f417da268e, 2011-09-26 (annotated)
- Committer:
- Yo_Robot
- Date:
- Mon Sep 26 03:23:35 2011 +0000
- Revision:
- 1:48f417da268e
- Parent:
- 0:3abd8c2d7c34
- Child:
- 2:493402568a5e
Matrix Class v1.02 - Fixed bugs and typos. Later work will add more functions. This is a replacement for the previous version. Use this one not the old one.
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 | 1:48f417da268e | 4 | |
Yo_Robot | 1:48f417da268e | 5 | /// Rows by Cols Matrix Constructor |
Yo_Robot | 1:48f417da268e | 6 | Matrix::Matrix(int Rows, int Cols): _nCols(Cols), _nRows(Rows) |
Yo_Robot | 0:3abd8c2d7c34 | 7 | { |
Yo_Robot | 1:48f417da268e | 8 | _matrix.resize(_nRows); |
Yo_Robot | 1:48f417da268e | 9 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 10 | _matrix[i].resize(_nCols); |
Yo_Robot | 0:3abd8c2d7c34 | 11 | } |
Yo_Robot | 0:3abd8c2d7c34 | 12 | |
Yo_Robot | 0:3abd8c2d7c34 | 13 | |
Yo_Robot | 1:48f417da268e | 14 | /// Defines the same parameters as 'base' Matrix |
Yo_Robot | 1:48f417da268e | 15 | Matrix::Matrix(const Matrix &base): _nCols(base._nCols), _nRows(base._nRows) |
Yo_Robot | 0:3abd8c2d7c34 | 16 | { |
Yo_Robot | 1:48f417da268e | 17 | _matrix.resize(_nRows); |
Yo_Robot | 1:48f417da268e | 18 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 19 | _matrix[i].resize(_nCols); |
Yo_Robot | 0:3abd8c2d7c34 | 20 | } |
Yo_Robot | 0:3abd8c2d7c34 | 21 | |
Yo_Robot | 0:3abd8c2d7c34 | 22 | |
Yo_Robot | 1:48f417da268e | 23 | /// Square Matrix Constructor |
Yo_Robot | 1:48f417da268e | 24 | Matrix::Matrix(int square): _nCols(square), _nRows(square) |
Yo_Robot | 0:3abd8c2d7c34 | 25 | { |
Yo_Robot | 1:48f417da268e | 26 | _matrix.resize(_nRows); |
Yo_Robot | 1:48f417da268e | 27 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 28 | _matrix[i].resize(_nCols); |
Yo_Robot | 0:3abd8c2d7c34 | 29 | } |
Yo_Robot | 0:3abd8c2d7c34 | 30 | |
Yo_Robot | 0:3abd8c2d7c34 | 31 | |
Yo_Robot | 1:48f417da268e | 32 | /// Default Constructor |
Yo_Robot | 0:3abd8c2d7c34 | 33 | Matrix::Matrix(){/*Intentionally left Blank*/} |
Yo_Robot | 0:3abd8c2d7c34 | 34 | |
Yo_Robot | 0:3abd8c2d7c34 | 35 | |
Yo_Robot | 1:48f417da268e | 36 | /************************************************************************/ |
Yo_Robot | 0:3abd8c2d7c34 | 37 | |
Yo_Robot | 1:48f417da268e | 38 | |
Yo_Robot | 1:48f417da268e | 39 | /// Overloaded Asign Operator. Resizes Matrix |
Yo_Robot | 0:3abd8c2d7c34 | 40 | Matrix& Matrix::operator = ( const Matrix &rightM ) |
Yo_Robot | 0:3abd8c2d7c34 | 41 | { |
Yo_Robot | 0:3abd8c2d7c34 | 42 | if( *this != rightM ) |
Yo_Robot | 0:3abd8c2d7c34 | 43 | { |
Yo_Robot | 1:48f417da268e | 44 | this->_nRows = rightM._nRows; |
Yo_Robot | 1:48f417da268e | 45 | this->_nCols = rightM._nCols; |
Yo_Robot | 0:3abd8c2d7c34 | 46 | |
Yo_Robot | 1:48f417da268e | 47 | this->_matrix.resize( rightM._nRows ); |
Yo_Robot | 1:48f417da268e | 48 | for( int i = 0; i < rightM._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 49 | this->_matrix [i].resize(rightM._nCols); |
Yo_Robot | 0:3abd8c2d7c34 | 50 | |
Yo_Robot | 1:48f417da268e | 51 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 52 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 53 | this->_matrix[i][j] = rightM._matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 54 | |
Yo_Robot | 0:3abd8c2d7c34 | 55 | return *this; |
Yo_Robot | 1:48f417da268e | 56 | |
Yo_Robot | 1:48f417da268e | 57 | }else{ |
Yo_Robot | 1:48f417da268e | 58 | |
Yo_Robot | 0:3abd8c2d7c34 | 59 | Matrix ret (*this); |
Yo_Robot | 0:3abd8c2d7c34 | 60 | return ret; |
Yo_Robot | 0:3abd8c2d7c34 | 61 | } |
Yo_Robot | 0:3abd8c2d7c34 | 62 | } |
Yo_Robot | 0:3abd8c2d7c34 | 63 | |
Yo_Robot | 1:48f417da268e | 64 | /// Comapre element by element |
Yo_Robot | 0:3abd8c2d7c34 | 65 | bool Matrix::operator == ( const Matrix& rightM ) |
Yo_Robot | 0:3abd8c2d7c34 | 66 | { |
Yo_Robot | 1:48f417da268e | 67 | if( _nRows == rightM._nRows && _nCols == rightM._nCols ) |
Yo_Robot | 0:3abd8c2d7c34 | 68 | { |
Yo_Robot | 0:3abd8c2d7c34 | 69 | bool equal = false; |
Yo_Robot | 0:3abd8c2d7c34 | 70 | |
Yo_Robot | 1:48f417da268e | 71 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 72 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 73 | if( _matrix[i][j] != rightM._matrix[i][j] ) |
Yo_Robot | 0:3abd8c2d7c34 | 74 | equal = equal || true; |
Yo_Robot | 0:3abd8c2d7c34 | 75 | |
Yo_Robot | 0:3abd8c2d7c34 | 76 | return !equal; |
Yo_Robot | 0:3abd8c2d7c34 | 77 | |
Yo_Robot | 0:3abd8c2d7c34 | 78 | }else{ return false; } |
Yo_Robot | 0:3abd8c2d7c34 | 79 | } |
Yo_Robot | 0:3abd8c2d7c34 | 80 | |
Yo_Robot | 0:3abd8c2d7c34 | 81 | |
Yo_Robot | 1:48f417da268e | 82 | /// Calls for '==' operator |
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 | 1:48f417da268e | 89 | /// Matrices must be same size. |
Yo_Robot | 1:48f417da268e | 90 | /// Element by element adition. |
Yo_Robot | 0:3abd8c2d7c34 | 91 | Matrix& Matrix::operator +=(const Matrix& rightM) |
Yo_Robot | 0:3abd8c2d7c34 | 92 | { |
Yo_Robot | 1:48f417da268e | 93 | if( _nRows == rightM._nRows && _nCols == rightM._nCols ) |
Yo_Robot | 0:3abd8c2d7c34 | 94 | { |
Yo_Robot | 1:48f417da268e | 95 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 96 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 97 | _matrix[i][j] += rightM._matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 98 | |
Yo_Robot | 0:3abd8c2d7c34 | 99 | return *this; |
Yo_Robot | 0:3abd8c2d7c34 | 100 | |
Yo_Robot | 0:3abd8c2d7c34 | 101 | }else{ |
Yo_Robot | 1:48f417da268e | 102 | printf( "\n\nERROR\nDiferent Dimensions @ += \n" ); |
Yo_Robot | 1:48f417da268e | 103 | // Matrix error(4); |
Yo_Robot | 1:48f417da268e | 104 | // error.Clear(); |
Yo_Robot | 1:48f417da268e | 105 | // return error; |
Yo_Robot | 0:3abd8c2d7c34 | 106 | } |
Yo_Robot | 0:3abd8c2d7c34 | 107 | } |
Yo_Robot | 0:3abd8c2d7c34 | 108 | |
Yo_Robot | 0:3abd8c2d7c34 | 109 | |
Yo_Robot | 1:48f417da268e | 110 | /// Matrices must be same size. |
Yo_Robot | 1:48f417da268e | 111 | /// Element by element Substraction |
Yo_Robot | 0:3abd8c2d7c34 | 112 | Matrix& Matrix::operator -=(const Matrix& rightM) |
Yo_Robot | 0:3abd8c2d7c34 | 113 | { |
Yo_Robot | 1:48f417da268e | 114 | if( _nRows == rightM._nRows && _nCols == rightM._nCols ) |
Yo_Robot | 0:3abd8c2d7c34 | 115 | { |
Yo_Robot | 1:48f417da268e | 116 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 117 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 118 | _matrix[i][j] -= rightM._matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 119 | |
Yo_Robot | 0:3abd8c2d7c34 | 120 | return *this; |
Yo_Robot | 0:3abd8c2d7c34 | 121 | |
Yo_Robot | 0:3abd8c2d7c34 | 122 | }else{ |
Yo_Robot | 1:48f417da268e | 123 | printf( "\n\nERROR\nDiferent Dimensions @ -= \n" ); |
Yo_Robot | 1:48f417da268e | 124 | //Matrix error(4); |
Yo_Robot | 1:48f417da268e | 125 | //error.Clear(); |
Yo_Robot | 1:48f417da268e | 126 | //return error; |
Yo_Robot | 0:3abd8c2d7c34 | 127 | } |
Yo_Robot | 0:3abd8c2d7c34 | 128 | } |
Yo_Robot | 0:3abd8c2d7c34 | 129 | |
Yo_Robot | 1:48f417da268e | 130 | |
Yo_Robot | 1:48f417da268e | 131 | /// Element by element Adition. Must be same sizes |
Yo_Robot | 0:3abd8c2d7c34 | 132 | const Matrix Matrix::operator +(const Matrix& rightM) |
Yo_Robot | 0:3abd8c2d7c34 | 133 | { |
Yo_Robot | 0:3abd8c2d7c34 | 134 | Matrix result = *this; |
Yo_Robot | 0:3abd8c2d7c34 | 135 | result += rightM; |
Yo_Robot | 0:3abd8c2d7c34 | 136 | return result; |
Yo_Robot | 0:3abd8c2d7c34 | 137 | } |
Yo_Robot | 0:3abd8c2d7c34 | 138 | |
Yo_Robot | 1:48f417da268e | 139 | |
Yo_Robot | 1:48f417da268e | 140 | /// Element by element Substraction. Must be same sizes |
Yo_Robot | 0:3abd8c2d7c34 | 141 | const Matrix Matrix::operator -(const Matrix& rightM) |
Yo_Robot | 0:3abd8c2d7c34 | 142 | { |
Yo_Robot | 0:3abd8c2d7c34 | 143 | Matrix result = *this; |
Yo_Robot | 0:3abd8c2d7c34 | 144 | result -= rightM; |
Yo_Robot | 0:3abd8c2d7c34 | 145 | return result; |
Yo_Robot | 0:3abd8c2d7c34 | 146 | } |
Yo_Robot | 0:3abd8c2d7c34 | 147 | |
Yo_Robot | 1:48f417da268e | 148 | /************************************************************************/ |
Yo_Robot | 0:3abd8c2d7c34 | 149 | |
Yo_Robot | 0:3abd8c2d7c34 | 150 | |
Yo_Robot | 1:48f417da268e | 151 | /// Returns true if matrix is full of zeros |
Yo_Robot | 1:48f417da268e | 152 | bool Matrix::isZero() |
Yo_Robot | 0:3abd8c2d7c34 | 153 | { |
Yo_Robot | 1:48f417da268e | 154 | bool zero = false; |
Yo_Robot | 1:48f417da268e | 155 | for( int i = 0; i < this->_nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 156 | for( int j = 0; j < this->_nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 157 | if( this->_matrix[i][j] != 0 ) |
Yo_Robot | 1:48f417da268e | 158 | zero = zero || true; |
Yo_Robot | 1:48f417da268e | 159 | return !zero; |
Yo_Robot | 0:3abd8c2d7c34 | 160 | } |
Yo_Robot | 0:3abd8c2d7c34 | 161 | |
Yo_Robot | 0:3abd8c2d7c34 | 162 | |
Yo_Robot | 1:48f417da268e | 163 | /// Returns true if Matrix is Single Row ot Single Column. |
Yo_Robot | 1:48f417da268e | 164 | bool Matrix::isVector() |
Yo_Robot | 0:3abd8c2d7c34 | 165 | { |
Yo_Robot | 1:48f417da268e | 166 | if( this->_nRows == 1 || this->_nCols == 1 ) |
Yo_Robot | 1:48f417da268e | 167 | return true; |
Yo_Robot | 1:48f417da268e | 168 | else |
Yo_Robot | 1:48f417da268e | 169 | return false; |
Yo_Robot | 0:3abd8c2d7c34 | 170 | } |
Yo_Robot | 0:3abd8c2d7c34 | 171 | |
Yo_Robot | 1:48f417da268e | 172 | /*************************************************************************/ |
Yo_Robot | 0:3abd8c2d7c34 | 173 | |
Yo_Robot | 1:48f417da268e | 174 | /// Returns all elements in Matrix as a single Row vector. |
Yo_Robot | 0:3abd8c2d7c34 | 175 | Matrix Matrix::ToPackedVector() |
Yo_Robot | 0:3abd8c2d7c34 | 176 | { |
Yo_Robot | 1:48f417da268e | 177 | Matrix Shattered( ( this->_nRows * this->_nCols ) , 1 ); |
Yo_Robot | 0:3abd8c2d7c34 | 178 | |
Yo_Robot | 0:3abd8c2d7c34 | 179 | int cont = 0; |
Yo_Robot | 1:48f417da268e | 180 | for( int i = 0; i < this->_nRows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 181 | { |
Yo_Robot | 1:48f417da268e | 182 | for( int j = 0; j < this->_nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 183 | { |
Yo_Robot | 0:3abd8c2d7c34 | 184 | Shattered._matrix[0][cont] = this->_matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 185 | cont++; |
Yo_Robot | 0:3abd8c2d7c34 | 186 | } |
Yo_Robot | 0:3abd8c2d7c34 | 187 | } |
Yo_Robot | 0:3abd8c2d7c34 | 188 | |
Yo_Robot | 0:3abd8c2d7c34 | 189 | return Shattered; |
Yo_Robot | 0:3abd8c2d7c34 | 190 | } |
Yo_Robot | 0:3abd8c2d7c34 | 191 | |
Yo_Robot | 1:48f417da268e | 192 | /***************************************************************************/ |
Yo_Robot | 0:3abd8c2d7c34 | 193 | |
Yo_Robot | 1:48f417da268e | 194 | /// Test elment-by-element. Prints detailed error. |
Yo_Robot | 1:48f417da268e | 195 | bool Matrix::Equals( const Matrix mat1, const Matrix mat2 ) |
Yo_Robot | 0:3abd8c2d7c34 | 196 | { |
Yo_Robot | 1:48f417da268e | 197 | if( mat1._nCols == mat2._nCols && mat1._nRows == mat2._nRows ) |
Yo_Robot | 0:3abd8c2d7c34 | 198 | { |
Yo_Robot | 0:3abd8c2d7c34 | 199 | bool equal = false; |
Yo_Robot | 0:3abd8c2d7c34 | 200 | |
Yo_Robot | 1:48f417da268e | 201 | for( int i = 0; i < mat1._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 202 | for( int j = 0; j < mat1._nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 203 | if( mat1._matrix[i][j] != mat2._matrix[i][j] ) |
Yo_Robot | 0:3abd8c2d7c34 | 204 | equal = equal || true; |
Yo_Robot | 0:3abd8c2d7c34 | 205 | |
Yo_Robot | 0:3abd8c2d7c34 | 206 | return !equal; |
Yo_Robot | 0:3abd8c2d7c34 | 207 | |
Yo_Robot | 0:3abd8c2d7c34 | 208 | }else{ |
Yo_Robot | 1:48f417da268e | 209 | printf( "\n\nERROR:\nDiferent Size Matrices!\n" ); |
Yo_Robot | 1:48f417da268e | 210 | printf( "mat1._nRows = %u\nmat1._nCols = %u\n",mat1._nRows, mat1._nCols ); |
Yo_Robot | 1:48f417da268e | 211 | printf( "mat2._nRows = %u\nmat2._nCols = %u\n",mat2._nRows, mat2._nCols ); |
Yo_Robot | 0:3abd8c2d7c34 | 212 | return false; |
Yo_Robot | 0:3abd8c2d7c34 | 213 | } |
Yo_Robot | 0:3abd8c2d7c34 | 214 | } |
Yo_Robot | 0:3abd8c2d7c34 | 215 | |
Yo_Robot | 0:3abd8c2d7c34 | 216 | |
Yo_Robot | 1:48f417da268e | 217 | /// To Implicitly create a Single Column Matrix. |
Yo_Robot | 0:3abd8c2d7c34 | 218 | Matrix Matrix::CreateColumnMatrix(int Cols) |
Yo_Robot | 0:3abd8c2d7c34 | 219 | { |
Yo_Robot | 0:3abd8c2d7c34 | 220 | Matrix ColMatrix(1,Cols); |
Yo_Robot | 0:3abd8c2d7c34 | 221 | return ColMatrix; |
Yo_Robot | 0:3abd8c2d7c34 | 222 | } |
Yo_Robot | 0:3abd8c2d7c34 | 223 | |
Yo_Robot | 0:3abd8c2d7c34 | 224 | |
Yo_Robot | 1:48f417da268e | 225 | /// To Implicitly create a Single Row Matrix. |
Yo_Robot | 0:3abd8c2d7c34 | 226 | Matrix Matrix::CreateRowMatrix(int Rows) |
Yo_Robot | 0:3abd8c2d7c34 | 227 | { |
Yo_Robot | 0:3abd8c2d7c34 | 228 | Matrix RowMatrix( Rows, 1); |
Yo_Robot | 0:3abd8c2d7c34 | 229 | return RowMatrix; |
Yo_Robot | 0:3abd8c2d7c34 | 230 | } |
Yo_Robot | 0:3abd8c2d7c34 | 231 | |
Yo_Robot | 0:3abd8c2d7c34 | 232 | |
Yo_Robot | 1:48f417da268e | 233 | /// To implicitly Clone a Matrix. Although '=' works the same. |
Yo_Robot | 1:48f417da268e | 234 | void Matrix::Clone( const Matrix &Source, Matrix &Receip ) |
Yo_Robot | 0:3abd8c2d7c34 | 235 | { |
Yo_Robot | 1:48f417da268e | 236 | Receip._nCols = Source._nCols; |
Yo_Robot | 1:48f417da268e | 237 | Receip._nRows = Source._nRows; |
Yo_Robot | 0:3abd8c2d7c34 | 238 | |
Yo_Robot | 1:48f417da268e | 239 | Receip._matrix.resize(Receip._nRows); |
Yo_Robot | 1:48f417da268e | 240 | for( int i = 0; i < Receip._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 241 | Receip._matrix[i].resize(Receip._nCols); |
Yo_Robot | 0:3abd8c2d7c34 | 242 | |
Yo_Robot | 1:48f417da268e | 243 | for( int i = 0; i < Receip._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 244 | for( int j = 0; j < Receip._nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 245 | Receip._matrix[i][j] = Source._matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 246 | } |
Yo_Robot | 0:3abd8c2d7c34 | 247 | |
Yo_Robot | 0:3abd8c2d7c34 | 248 | |
Yo_Robot | 1:48f417da268e | 249 | /// To add (Insert) a Single Row to a Matrix. |
Yo_Robot | 1:48f417da268e | 250 | void Matrix::AddRow(Matrix& Mat, int Row) |
Yo_Robot | 0:3abd8c2d7c34 | 251 | { |
Yo_Robot | 0:3abd8c2d7c34 | 252 | --Row; |
Yo_Robot | 0:3abd8c2d7c34 | 253 | |
Yo_Robot | 1:48f417da268e | 254 | if( Row > Mat._nRows + 1) |
Yo_Robot | 0:3abd8c2d7c34 | 255 | { |
Yo_Robot | 1:48f417da268e | 256 | printf("\n\nERROR:\nRow out of Limits @ AddRow()\n"); |
Yo_Robot | 1:48f417da268e | 257 | |
Yo_Robot | 0:3abd8c2d7c34 | 258 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 259 | |
Yo_Robot | 1:48f417da268e | 260 | Mat._nRows++; |
Yo_Robot | 1:48f417da268e | 261 | Mat._matrix.resize( Mat._nRows ); |
Yo_Robot | 1:48f417da268e | 262 | |
Yo_Robot | 1:48f417da268e | 263 | Mat._matrix[ Mat._nRows - 1 ].resize( Mat._nCols ); |
Yo_Robot | 1:48f417da268e | 264 | |
Yo_Robot | 1:48f417da268e | 265 | for( int i = Mat._nRows - 1; i > Row; i-- ) |
Yo_Robot | 1:48f417da268e | 266 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 267 | Mat._matrix[i][j] = Mat._matrix[i - 1][j]; |
Yo_Robot | 1:48f417da268e | 268 | |
Yo_Robot | 1:48f417da268e | 269 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 270 | Mat._matrix[Row][j] = 0.0; |
Yo_Robot | 0:3abd8c2d7c34 | 271 | } |
Yo_Robot | 0:3abd8c2d7c34 | 272 | } |
Yo_Robot | 0:3abd8c2d7c34 | 273 | |
Yo_Robot | 0:3abd8c2d7c34 | 274 | |
Yo_Robot | 1:48f417da268e | 275 | /// To add (Insert) a single Column to a Matrix |
Yo_Robot | 1:48f417da268e | 276 | void Matrix::AddColumn( Matrix &Mat, int Col ) |
Yo_Robot | 1:48f417da268e | 277 | { |
Yo_Robot | 1:48f417da268e | 278 | --Col; |
Yo_Robot | 0:3abd8c2d7c34 | 279 | |
Yo_Robot | 1:48f417da268e | 280 | if( Col > Mat._nCols + 1 ) |
Yo_Robot | 1:48f417da268e | 281 | { |
Yo_Robot | 1:48f417da268e | 282 | printf("\n\nERROR:\nRow out of Limits on AddCol()\n"); |
Yo_Robot | 1:48f417da268e | 283 | |
Yo_Robot | 1:48f417da268e | 284 | }else{ |
Yo_Robot | 1:48f417da268e | 285 | |
Yo_Robot | 1:48f417da268e | 286 | |
Yo_Robot | 1:48f417da268e | 287 | Mat._nCols++; |
Yo_Robot | 1:48f417da268e | 288 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 289 | Mat._matrix[i].resize( Mat._nCols ); |
Yo_Robot | 1:48f417da268e | 290 | |
Yo_Robot | 1:48f417da268e | 291 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 292 | for( int j = Mat._nCols; j > Col; j-- ) |
Yo_Robot | 1:48f417da268e | 293 | Mat._matrix[i][j] = Mat._matrix[i][j - 1]; |
Yo_Robot | 1:48f417da268e | 294 | |
Yo_Robot | 1:48f417da268e | 295 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 296 | Mat._matrix[i][Col] = 0.0; |
Yo_Robot | 1:48f417da268e | 297 | |
Yo_Robot | 1:48f417da268e | 298 | } |
Yo_Robot | 1:48f417da268e | 299 | } |
Yo_Robot | 1:48f417da268e | 300 | |
Yo_Robot | 1:48f417da268e | 301 | |
Yo_Robot | 1:48f417da268e | 302 | /// Delete a Single Column From Matrix. |
Yo_Robot | 0:3abd8c2d7c34 | 303 | void Matrix::DeleteCol( Matrix &Mat, int Col) |
Yo_Robot | 0:3abd8c2d7c34 | 304 | { |
Yo_Robot | 0:3abd8c2d7c34 | 305 | --Col; // Because of Column zero. |
Yo_Robot | 0:3abd8c2d7c34 | 306 | |
Yo_Robot | 1:48f417da268e | 307 | if( Col > Mat._nCols ) |
Yo_Robot | 0:3abd8c2d7c34 | 308 | { |
Yo_Robot | 1:48f417da268e | 309 | printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); |
Yo_Robot | 1:48f417da268e | 310 | |
Yo_Robot | 0:3abd8c2d7c34 | 311 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 312 | |
Yo_Robot | 1:48f417da268e | 313 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 314 | for( int j = Col; j < Mat._nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 315 | Mat._matrix[i][j] = Mat._matrix[i][j+1]; |
Yo_Robot | 0:3abd8c2d7c34 | 316 | |
Yo_Robot | 0:3abd8c2d7c34 | 317 | // Decrease one column |
Yo_Robot | 1:48f417da268e | 318 | Mat._nCols--; |
Yo_Robot | 0:3abd8c2d7c34 | 319 | |
Yo_Robot | 0:3abd8c2d7c34 | 320 | //Erase last Column |
Yo_Robot | 1:48f417da268e | 321 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 322 | Mat._matrix[i].reserve(Mat._nCols); |
Yo_Robot | 0:3abd8c2d7c34 | 323 | |
Yo_Robot | 0:3abd8c2d7c34 | 324 | } |
Yo_Robot | 0:3abd8c2d7c34 | 325 | } |
Yo_Robot | 0:3abd8c2d7c34 | 326 | |
Yo_Robot | 1:48f417da268e | 327 | |
Yo_Robot | 1:48f417da268e | 328 | /// Delete a Single Row form Matrix |
Yo_Robot | 1:48f417da268e | 329 | void Matrix::DeleteRow(Matrix& Mat, int Row) |
Yo_Robot | 1:48f417da268e | 330 | { |
Yo_Robot | 1:48f417da268e | 331 | --Row; |
Yo_Robot | 0:3abd8c2d7c34 | 332 | |
Yo_Robot | 1:48f417da268e | 333 | if( Row > Mat._nRows ) |
Yo_Robot | 1:48f417da268e | 334 | { |
Yo_Robot | 1:48f417da268e | 335 | printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); |
Yo_Robot | 1:48f417da268e | 336 | |
Yo_Robot | 1:48f417da268e | 337 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 338 | |
Yo_Robot | 1:48f417da268e | 339 | for( int i = Row; i < Mat._nRows - 1; i++ ) |
Yo_Robot | 1:48f417da268e | 340 | |
Yo_Robot | 1:48f417da268e | 341 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 342 | Mat._matrix[i][j] = Mat._matrix[i+1][j]; |
Yo_Robot | 1:48f417da268e | 343 | Mat._nRows--; |
Yo_Robot | 1:48f417da268e | 344 | Mat._matrix.resize(Mat._nRows); |
Yo_Robot | 1:48f417da268e | 345 | } |
Yo_Robot | 1:48f417da268e | 346 | } |
Yo_Robot | 1:48f417da268e | 347 | |
Yo_Robot | 1:48f417da268e | 348 | /*****************************************************************************************/ |
Yo_Robot | 1:48f417da268e | 349 | |
Yo_Robot | 1:48f417da268e | 350 | /// Extracts a single row form calling matrix and saves it to another matrix. |
Yo_Robot | 1:48f417da268e | 351 | void Matrix::ExportRow( int Row, Matrix &Mat ) |
Yo_Robot | 0:3abd8c2d7c34 | 352 | { |
Yo_Robot | 1:48f417da268e | 353 | --Row; |
Yo_Robot | 1:48f417da268e | 354 | |
Yo_Robot | 1:48f417da268e | 355 | if( Row > this->_nRows ) |
Yo_Robot | 0:3abd8c2d7c34 | 356 | { |
Yo_Robot | 1:48f417da268e | 357 | printf( "\n\nWARNING: Row out of dimmensions @ GetRow\n" |
Yo_Robot | 1:48f417da268e | 358 | "Nothing Done.\n\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 359 | |
Yo_Robot | 0:3abd8c2d7c34 | 360 | }else{ |
Yo_Robot | 1:48f417da268e | 361 | |
Yo_Robot | 1:48f417da268e | 362 | Matrix SingleRow( 1 , this->_nCols ); |
Yo_Robot | 1:48f417da268e | 363 | SingleRow.Clear(); |
Yo_Robot | 1:48f417da268e | 364 | |
Yo_Robot | 1:48f417da268e | 365 | for( int j = 0; j < this->_nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 366 | SingleRow._matrix[0][j] = this->_matrix[Row][j]; |
Yo_Robot | 1:48f417da268e | 367 | |
Yo_Robot | 1:48f417da268e | 368 | Mat = SingleRow; |
Yo_Robot | 0:3abd8c2d7c34 | 369 | } |
Yo_Robot | 0:3abd8c2d7c34 | 370 | } |
Yo_Robot | 0:3abd8c2d7c34 | 371 | |
Yo_Robot | 0:3abd8c2d7c34 | 372 | |
Yo_Robot | 1:48f417da268e | 373 | /// Extracts a single column form calling matrix and saves it to another matrix. |
Yo_Robot | 1:48f417da268e | 374 | void Matrix::ExportCol( int Col, Matrix &Mat ) |
Yo_Robot | 1:48f417da268e | 375 | { |
Yo_Robot | 1:48f417da268e | 376 | if( Col > this->_nCols ) |
Yo_Robot | 1:48f417da268e | 377 | { |
Yo_Robot | 1:48f417da268e | 378 | printf( "\n\nWARNING:\nCol out of dimmensions.\n" |
Yo_Robot | 1:48f417da268e | 379 | "Nothing Done.\n\n" ); |
Yo_Robot | 1:48f417da268e | 380 | }else{ |
Yo_Robot | 1:48f417da268e | 381 | |
Yo_Robot | 1:48f417da268e | 382 | Matrix SingleCol( this->_nRows, 1 ); |
Yo_Robot | 1:48f417da268e | 383 | for(int i = 0; i < this->_nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 384 | SingleCol._matrix[i][0] = this->_matrix[i][Col]; |
Yo_Robot | 1:48f417da268e | 385 | Mat = SingleCol; |
Yo_Robot | 1:48f417da268e | 386 | } |
Yo_Robot | 1:48f417da268e | 387 | } |
Yo_Robot | 1:48f417da268e | 388 | |
Yo_Robot | 1:48f417da268e | 389 | |
Yo_Robot | 1:48f417da268e | 390 | /// Makes matrix Bigger! |
Yo_Robot | 1:48f417da268e | 391 | void Matrix::Resize( int Rows, int Cols ) |
Yo_Robot | 1:48f417da268e | 392 | { |
Yo_Robot | 1:48f417da268e | 393 | this->_nRows = Rows; |
Yo_Robot | 1:48f417da268e | 394 | this->_nCols = Cols; |
Yo_Robot | 1:48f417da268e | 395 | this->_matrix.resize( _nRows ); |
Yo_Robot | 1:48f417da268e | 396 | |
Yo_Robot | 1:48f417da268e | 397 | for( int i = 0; i< _nRows ; i++ ) |
Yo_Robot | 1:48f417da268e | 398 | _matrix[i].resize(_nCols); |
Yo_Robot | 1:48f417da268e | 399 | } |
Yo_Robot | 1:48f417da268e | 400 | |
Yo_Robot | 1:48f417da268e | 401 | |
Yo_Robot | 1:48f417da268e | 402 | /// Ask user for elemnts in Matrix |
Yo_Robot | 0:3abd8c2d7c34 | 403 | void Matrix::FillMatrix() |
Yo_Robot | 0:3abd8c2d7c34 | 404 | { |
Yo_Robot | 1:48f417da268e | 405 | for(int i = 0; i < _nRows; i++) |
Yo_Robot | 0:3abd8c2d7c34 | 406 | { |
Yo_Robot | 1:48f417da268e | 407 | for(int j = 0; j < _nCols; j++) |
Yo_Robot | 0:3abd8c2d7c34 | 408 | { |
Yo_Robot | 0:3abd8c2d7c34 | 409 | printf( "Position [%u][%u]: ", i, j ); |
Yo_Robot | 0:3abd8c2d7c34 | 410 | float numero; |
Yo_Robot | 0:3abd8c2d7c34 | 411 | scanf( "%f", &numero ); |
Yo_Robot | 0:3abd8c2d7c34 | 412 | printf("%.3f ", numero); |
Yo_Robot | 1:48f417da268e | 413 | this->_matrix[i][j] = numero; |
Yo_Robot | 0:3abd8c2d7c34 | 414 | } |
Yo_Robot | 0:3abd8c2d7c34 | 415 | printf("\n"); |
Yo_Robot | 0:3abd8c2d7c34 | 416 | } |
Yo_Robot | 0:3abd8c2d7c34 | 417 | printf("\n"); |
Yo_Robot | 0:3abd8c2d7c34 | 418 | } |
Yo_Robot | 0:3abd8c2d7c34 | 419 | |
Yo_Robot | 0:3abd8c2d7c34 | 420 | |
Yo_Robot | 1:48f417da268e | 421 | /// Prints out Matrix. |
Yo_Robot | 0:3abd8c2d7c34 | 422 | void Matrix::print() |
Yo_Robot | 0:3abd8c2d7c34 | 423 | { |
Yo_Robot | 1:48f417da268e | 424 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 425 | { |
Yo_Robot | 1:48f417da268e | 426 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 427 | { |
Yo_Robot | 0:3abd8c2d7c34 | 428 | printf( "%.3f, ",_matrix[i][j] ); |
Yo_Robot | 1:48f417da268e | 429 | |
Yo_Robot | 0:3abd8c2d7c34 | 430 | } |
Yo_Robot | 1:48f417da268e | 431 | printf( "\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 432 | } |
Yo_Robot | 0:3abd8c2d7c34 | 433 | } |
Yo_Robot | 0:3abd8c2d7c34 | 434 | |
Yo_Robot | 0:3abd8c2d7c34 | 435 | |
Yo_Robot | 1:48f417da268e | 436 | /// Fills matrix with zeros. |
Yo_Robot | 1:48f417da268e | 437 | void Matrix::Clear() |
Yo_Robot | 1:48f417da268e | 438 | { |
Yo_Robot | 1:48f417da268e | 439 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 440 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 441 | _matrix[i][j] = 0; |
Yo_Robot | 1:48f417da268e | 442 | } |
Yo_Robot | 0:3abd8c2d7c34 | 443 | |
Yo_Robot | 1:48f417da268e | 444 | /********************************************************************************/ |
Yo_Robot | 1:48f417da268e | 445 | |
Yo_Robot | 1:48f417da268e | 446 | /// Inserts a Single element in a desired Position( Index starts at [1][1] ); |
Yo_Robot | 1:48f417da268e | 447 | void Matrix::add(int Row, int Col, float number) |
Yo_Robot | 1:48f417da268e | 448 | { |
Yo_Robot | 1:48f417da268e | 449 | --Col; --Row; |
Yo_Robot | 1:48f417da268e | 450 | |
Yo_Robot | 1:48f417da268e | 451 | if( Row > _nRows || Col > _nCols ) |
Yo_Robot | 1:48f417da268e | 452 | { |
Yo_Robot | 1:48f417da268e | 453 | printf("\n\nERROR:\nOut of limits of Matrix @ mat.Add()"); |
Yo_Robot | 1:48f417da268e | 454 | |
Yo_Robot | 1:48f417da268e | 455 | }else{ |
Yo_Robot | 1:48f417da268e | 456 | _matrix[Row][Col] = number; |
Yo_Robot | 1:48f417da268e | 457 | } |
Yo_Robot | 1:48f417da268e | 458 | } |
Yo_Robot | 1:48f417da268e | 459 | |
Yo_Robot | 1:48f417da268e | 460 | |
Yo_Robot | 1:48f417da268e | 461 | /// Adds all elements in matrix and returns the answer. |
Yo_Robot | 1:48f417da268e | 462 | float Matrix::sum() |
Yo_Robot | 0:3abd8c2d7c34 | 463 | { |
Yo_Robot | 0:3abd8c2d7c34 | 464 | float total; |
Yo_Robot | 0:3abd8c2d7c34 | 465 | |
Yo_Robot | 1:48f417da268e | 466 | for( int i = 0; i < this->_nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 467 | for( int j = 0; j < this->_nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 468 | total += this->_matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 469 | return total; |
Yo_Robot | 0:3abd8c2d7c34 | 470 | } |
Yo_Robot | 0:3abd8c2d7c34 | 471 | |
Yo_Robot | 0:3abd8c2d7c34 | 472 | |
Yo_Robot | 1:48f417da268e | 473 | /// Returns the specified element. Index Starts at [1][1]. |
Yo_Robot | 1:48f417da268e | 474 | float Matrix::getNumber( int Row, int Col ) |
Yo_Robot | 1:48f417da268e | 475 | { return this->_matrix[Row -1][Col - 1]; } |
Yo_Robot | 0:3abd8c2d7c34 | 476 | |
Yo_Robot | 1:48f417da268e | 477 | /// Returns the number of Rows in Matrix. |
Yo_Robot | 1:48f417da268e | 478 | int Matrix::getRows(){ return this->_nRows; } |
Yo_Robot | 0:3abd8c2d7c34 | 479 | |
Yo_Robot | 0:3abd8c2d7c34 | 480 | |
Yo_Robot | 1:48f417da268e | 481 | /// Returns the number of Columns in Matrix. |
Yo_Robot | 1:48f417da268e | 482 | int Matrix::getCols(){ return this->_nCols; } |