nanimo kaete nai
Matrix.cpp@0:4ff90c5befd9, 2022-06-14 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
tyuito | 0:4ff90c5befd9 | 1 | /** |
tyuito | 0:4ff90c5befd9 | 2 | * @brief Source Code for the Matrix Class. |
tyuito | 0:4ff90c5befd9 | 3 | * @file Matrix.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 | |
tyuito | 0:4ff90c5befd9 | 13 | #include "mbed.h" |
tyuito | 0:4ff90c5befd9 | 14 | #include "Matrix.h" |
tyuito | 0:4ff90c5befd9 | 15 | |
tyuito | 0:4ff90c5befd9 | 16 | /// Rows by Cols Matrix Constructor |
tyuito | 0:4ff90c5befd9 | 17 | Matrix::Matrix(int Rows, int Cols): _nRows(Rows), _nCols(Cols) |
tyuito | 0:4ff90c5befd9 | 18 | { |
tyuito | 0:4ff90c5befd9 | 19 | _matrix.resize(_nRows); |
tyuito | 0:4ff90c5befd9 | 20 | for( int i = 0; i < _nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 21 | _matrix[i].resize(_nCols); |
tyuito | 0:4ff90c5befd9 | 22 | |
tyuito | 0:4ff90c5befd9 | 23 | _pRow = 0; |
tyuito | 0:4ff90c5befd9 | 24 | _pCol = 0; |
tyuito | 0:4ff90c5befd9 | 25 | |
tyuito | 0:4ff90c5befd9 | 26 | this->Clear(); //Make all elements zero by default. |
tyuito | 0:4ff90c5befd9 | 27 | } |
tyuito | 0:4ff90c5befd9 | 28 | |
tyuito | 0:4ff90c5befd9 | 29 | |
tyuito | 0:4ff90c5befd9 | 30 | /// Copies one matrix into a new one |
tyuito | 0:4ff90c5befd9 | 31 | Matrix::Matrix(const Matrix& base) |
tyuito | 0:4ff90c5befd9 | 32 | { |
tyuito | 0:4ff90c5befd9 | 33 | _nCols = base._nCols; |
tyuito | 0:4ff90c5befd9 | 34 | _nRows = base._nRows; |
tyuito | 0:4ff90c5befd9 | 35 | |
tyuito | 0:4ff90c5befd9 | 36 | _pRow = base._pRow; |
tyuito | 0:4ff90c5befd9 | 37 | _pCol = base._pCol; |
tyuito | 0:4ff90c5befd9 | 38 | |
tyuito | 0:4ff90c5befd9 | 39 | _matrix.resize(_nRows); |
tyuito | 0:4ff90c5befd9 | 40 | for( int i = 0; i < _nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 41 | _matrix[i].resize(_nCols); |
tyuito | 0:4ff90c5befd9 | 42 | |
tyuito | 0:4ff90c5befd9 | 43 | for( int i = 0; i < _nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 44 | for( int j = 0; j < _nCols; j++ ) |
tyuito | 0:4ff90c5befd9 | 45 | _matrix[i][j] = base._matrix[i][j]; |
tyuito | 0:4ff90c5befd9 | 46 | } |
tyuito | 0:4ff90c5befd9 | 47 | |
tyuito | 0:4ff90c5befd9 | 48 | |
tyuito | 0:4ff90c5befd9 | 49 | /// Default Constructor |
tyuito | 0:4ff90c5befd9 | 50 | Matrix::Matrix() |
tyuito | 0:4ff90c5befd9 | 51 | { |
tyuito | 0:4ff90c5befd9 | 52 | _nCols = 0; |
tyuito | 0:4ff90c5befd9 | 53 | _nRows = 0; |
tyuito | 0:4ff90c5befd9 | 54 | |
tyuito | 0:4ff90c5befd9 | 55 | _pRow = 0; |
tyuito | 0:4ff90c5befd9 | 56 | _pCol = 0; |
tyuito | 0:4ff90c5befd9 | 57 | |
tyuito | 0:4ff90c5befd9 | 58 | } |
tyuito | 0:4ff90c5befd9 | 59 | |
tyuito | 0:4ff90c5befd9 | 60 | /***********************************************************************/ |
tyuito | 0:4ff90c5befd9 | 61 | |
tyuito | 0:4ff90c5befd9 | 62 | /// Returns true if matrix is full of zeros |
tyuito | 0:4ff90c5befd9 | 63 | bool Matrix::isZero() const |
tyuito | 0:4ff90c5befd9 | 64 | { |
tyuito | 0:4ff90c5befd9 | 65 | bool zero = false; |
tyuito | 0:4ff90c5befd9 | 66 | for( int i = 0; i < this->_nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 67 | for( int j = 0; j < this->_nCols; j++ ) |
tyuito | 0:4ff90c5befd9 | 68 | if( _matrix[i][j] != 0 ) |
tyuito | 0:4ff90c5befd9 | 69 | zero = zero || true; |
tyuito | 0:4ff90c5befd9 | 70 | return !zero; |
tyuito | 0:4ff90c5befd9 | 71 | } |
tyuito | 0:4ff90c5befd9 | 72 | |
tyuito | 0:4ff90c5befd9 | 73 | |
tyuito | 0:4ff90c5befd9 | 74 | /// Returns true if Matrix is Single Row ot Single Column. |
tyuito | 0:4ff90c5befd9 | 75 | bool Matrix::isVector() const |
tyuito | 0:4ff90c5befd9 | 76 | { |
tyuito | 0:4ff90c5befd9 | 77 | if( _nRows == 1 || _nCols == 1 ) |
tyuito | 0:4ff90c5befd9 | 78 | return true; |
tyuito | 0:4ff90c5befd9 | 79 | else |
tyuito | 0:4ff90c5befd9 | 80 | return false; |
tyuito | 0:4ff90c5befd9 | 81 | } |
tyuito | 0:4ff90c5befd9 | 82 | |
tyuito | 0:4ff90c5befd9 | 83 | /*************************************************************************/ |
tyuito | 0:4ff90c5befd9 | 84 | |
tyuito | 0:4ff90c5befd9 | 85 | /// Returns all elements in Matrix as a single Row vector. |
tyuito | 0:4ff90c5befd9 | 86 | const Matrix Matrix::ToPackedVector( const Matrix& Mat ) |
tyuito | 0:4ff90c5befd9 | 87 | { |
tyuito | 0:4ff90c5befd9 | 88 | |
tyuito | 0:4ff90c5befd9 | 89 | Matrix Crushed( 1, Mat._nRows * Mat._nCols ); |
tyuito | 0:4ff90c5befd9 | 90 | |
tyuito | 0:4ff90c5befd9 | 91 | int cont = 0; |
tyuito | 0:4ff90c5befd9 | 92 | |
tyuito | 0:4ff90c5befd9 | 93 | for( int i = 0; i < Mat._nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 94 | for( int j = 0; j < Mat._nCols; j++ ) |
tyuito | 0:4ff90c5befd9 | 95 | { |
tyuito | 0:4ff90c5befd9 | 96 | Crushed._matrix[0][cont] = Mat._matrix[i][j]; |
tyuito | 0:4ff90c5befd9 | 97 | cont++; |
tyuito | 0:4ff90c5befd9 | 98 | } |
tyuito | 0:4ff90c5befd9 | 99 | |
tyuito | 0:4ff90c5befd9 | 100 | Crushed._pRow = Crushed._nRows; |
tyuito | 0:4ff90c5befd9 | 101 | Crushed._pCol = Crushed._nCols; |
tyuito | 0:4ff90c5befd9 | 102 | |
tyuito | 0:4ff90c5befd9 | 103 | return Crushed; |
tyuito | 0:4ff90c5befd9 | 104 | } |
tyuito | 0:4ff90c5befd9 | 105 | |
tyuito | 0:4ff90c5befd9 | 106 | |
tyuito | 0:4ff90c5befd9 | 107 | |
tyuito | 0:4ff90c5befd9 | 108 | /// To add (Insert) a Single Row to a Matrix. |
tyuito | 0:4ff90c5befd9 | 109 | void Matrix::AddRow(Matrix& Mat, int index) |
tyuito | 0:4ff90c5befd9 | 110 | { |
tyuito | 0:4ff90c5befd9 | 111 | --index; |
tyuito | 0:4ff90c5befd9 | 112 | |
tyuito | 0:4ff90c5befd9 | 113 | if( index > Mat._nRows + 1) |
tyuito | 0:4ff90c5befd9 | 114 | { |
tyuito | 0:4ff90c5befd9 | 115 | printf("\n\nERROR:\nRow out of Limits @ AddRow()\n"); |
tyuito | 0:4ff90c5befd9 | 116 | |
tyuito | 0:4ff90c5befd9 | 117 | }else{ |
tyuito | 0:4ff90c5befd9 | 118 | |
tyuito | 0:4ff90c5befd9 | 119 | Mat._nRows++; |
tyuito | 0:4ff90c5befd9 | 120 | Mat._matrix.resize( Mat._nRows ); |
tyuito | 0:4ff90c5befd9 | 121 | |
tyuito | 0:4ff90c5befd9 | 122 | Mat._matrix[ Mat._nRows - 1 ].resize( Mat._nCols ); |
tyuito | 0:4ff90c5befd9 | 123 | |
tyuito | 0:4ff90c5befd9 | 124 | for( int i = Mat._nRows - 1; i > index; i-- ) |
tyuito | 0:4ff90c5befd9 | 125 | for( int j = 0; j < Mat._nCols; j++ ) |
tyuito | 0:4ff90c5befd9 | 126 | Mat._matrix[i][j] = Mat._matrix[i - 1][j]; |
tyuito | 0:4ff90c5befd9 | 127 | |
tyuito | 0:4ff90c5befd9 | 128 | for( int j = 0; j < Mat._nCols; j++ ) |
tyuito | 0:4ff90c5befd9 | 129 | Mat._matrix[index][j] = 0.0; |
tyuito | 0:4ff90c5befd9 | 130 | } |
tyuito | 0:4ff90c5befd9 | 131 | } |
tyuito | 0:4ff90c5befd9 | 132 | |
tyuito | 0:4ff90c5befd9 | 133 | |
tyuito | 0:4ff90c5befd9 | 134 | void Matrix::AddRow(Matrix& Receip, const Matrix& Row, int index) |
tyuito | 0:4ff90c5befd9 | 135 | { |
tyuito | 0:4ff90c5befd9 | 136 | Matrix::AddRow( Receip, index ); //Make Room |
tyuito | 0:4ff90c5befd9 | 137 | |
tyuito | 0:4ff90c5befd9 | 138 | --index; |
tyuito | 0:4ff90c5befd9 | 139 | for( int i = 0; i < Receip._nCols; i++ ) |
tyuito | 0:4ff90c5befd9 | 140 | Receip._matrix[index][i] = Row._matrix[0][i]; //Copy Data. |
tyuito | 0:4ff90c5befd9 | 141 | |
tyuito | 0:4ff90c5befd9 | 142 | } |
tyuito | 0:4ff90c5befd9 | 143 | |
tyuito | 0:4ff90c5befd9 | 144 | |
tyuito | 0:4ff90c5befd9 | 145 | /// To add (Insert) a single Column to a Matrix |
tyuito | 0:4ff90c5befd9 | 146 | void Matrix::AddCol( Matrix& Mat, int index ) |
tyuito | 0:4ff90c5befd9 | 147 | { |
tyuito | 0:4ff90c5befd9 | 148 | --index; |
tyuito | 0:4ff90c5befd9 | 149 | |
tyuito | 0:4ff90c5befd9 | 150 | if( index > Mat._nCols + 1 ) |
tyuito | 0:4ff90c5befd9 | 151 | { |
tyuito | 0:4ff90c5befd9 | 152 | printf("\n\nERROR:\nRow out of Limits on AddCol()\n"); |
tyuito | 0:4ff90c5befd9 | 153 | |
tyuito | 0:4ff90c5befd9 | 154 | }else{ |
tyuito | 0:4ff90c5befd9 | 155 | |
tyuito | 0:4ff90c5befd9 | 156 | |
tyuito | 0:4ff90c5befd9 | 157 | Mat._nCols++; |
tyuito | 0:4ff90c5befd9 | 158 | for( int i = 0; i < Mat._nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 159 | Mat._matrix[i].resize( Mat._nCols ); |
tyuito | 0:4ff90c5befd9 | 160 | |
tyuito | 0:4ff90c5befd9 | 161 | for( int i = 0; i < Mat._nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 162 | for( int j = Mat._nCols; j > index; j-- ) |
tyuito | 0:4ff90c5befd9 | 163 | Mat._matrix[i][j] = Mat._matrix[i][j - 1]; |
tyuito | 0:4ff90c5befd9 | 164 | |
tyuito | 0:4ff90c5befd9 | 165 | for( int i = 0; i < Mat._nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 166 | Mat._matrix[i][index] = 0.0; |
tyuito | 0:4ff90c5befd9 | 167 | |
tyuito | 0:4ff90c5befd9 | 168 | } |
tyuito | 0:4ff90c5befd9 | 169 | } |
tyuito | 0:4ff90c5befd9 | 170 | |
tyuito | 0:4ff90c5befd9 | 171 | |
tyuito | 0:4ff90c5befd9 | 172 | void Matrix::AddCol(Matrix& Receip, const Matrix& Row, int index) |
tyuito | 0:4ff90c5befd9 | 173 | { |
tyuito | 0:4ff90c5befd9 | 174 | Matrix::AddCol( Receip, index ); // Make Rom |
tyuito | 0:4ff90c5befd9 | 175 | |
tyuito | 0:4ff90c5befd9 | 176 | --index; |
tyuito | 0:4ff90c5befd9 | 177 | for( int i = 0; i < Receip._nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 178 | Receip._matrix[i][index] = Row._matrix[i][0]; //Copy Data. |
tyuito | 0:4ff90c5befd9 | 179 | } |
tyuito | 0:4ff90c5befd9 | 180 | |
tyuito | 0:4ff90c5befd9 | 181 | |
tyuito | 0:4ff90c5befd9 | 182 | /// Delete a Single Column From Matrix. |
tyuito | 0:4ff90c5befd9 | 183 | void Matrix::DeleteCol( Matrix& Mat, int Col) |
tyuito | 0:4ff90c5befd9 | 184 | { |
tyuito | 0:4ff90c5befd9 | 185 | --Col; // Because of Column zero. |
tyuito | 0:4ff90c5befd9 | 186 | |
tyuito | 0:4ff90c5befd9 | 187 | if( Col > Mat._nCols ) |
tyuito | 0:4ff90c5befd9 | 188 | { |
tyuito | 0:4ff90c5befd9 | 189 | printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); |
tyuito | 0:4ff90c5befd9 | 190 | |
tyuito | 0:4ff90c5befd9 | 191 | }else{ |
tyuito | 0:4ff90c5befd9 | 192 | |
tyuito | 0:4ff90c5befd9 | 193 | for( int i = 0; i < Mat._nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 194 | for( int j = Col; j < Mat._nCols; j++ ) |
tyuito | 0:4ff90c5befd9 | 195 | Mat._matrix[i][j] = Mat._matrix[i][j+1]; |
tyuito | 0:4ff90c5befd9 | 196 | |
tyuito | 0:4ff90c5befd9 | 197 | // If adressing last element of Column, |
tyuito | 0:4ff90c5befd9 | 198 | // wich no longer exists |
tyuito | 0:4ff90c5befd9 | 199 | if( Mat._pCol == Mat._nCols ) |
tyuito | 0:4ff90c5befd9 | 200 | Mat._pCol--; |
tyuito | 0:4ff90c5befd9 | 201 | |
tyuito | 0:4ff90c5befd9 | 202 | // Decrease one column |
tyuito | 0:4ff90c5befd9 | 203 | Mat._nCols--; |
tyuito | 0:4ff90c5befd9 | 204 | |
tyuito | 0:4ff90c5befd9 | 205 | //Erase last Column |
tyuito | 0:4ff90c5befd9 | 206 | for( int i = 0; i < Mat._nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 207 | Mat._matrix[i].reserve(Mat._nCols); |
tyuito | 0:4ff90c5befd9 | 208 | |
tyuito | 0:4ff90c5befd9 | 209 | } |
tyuito | 0:4ff90c5befd9 | 210 | } |
tyuito | 0:4ff90c5befd9 | 211 | |
tyuito | 0:4ff90c5befd9 | 212 | |
tyuito | 0:4ff90c5befd9 | 213 | /// Delete a Single Row form Matrix |
tyuito | 0:4ff90c5befd9 | 214 | void Matrix::DeleteRow(Matrix& Mat, int Row) |
tyuito | 0:4ff90c5befd9 | 215 | { |
tyuito | 0:4ff90c5befd9 | 216 | --Row; |
tyuito | 0:4ff90c5befd9 | 217 | |
tyuito | 0:4ff90c5befd9 | 218 | if( Row > Mat._nRows ) |
tyuito | 0:4ff90c5befd9 | 219 | { |
tyuito | 0:4ff90c5befd9 | 220 | printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); |
tyuito | 0:4ff90c5befd9 | 221 | |
tyuito | 0:4ff90c5befd9 | 222 | }else{ |
tyuito | 0:4ff90c5befd9 | 223 | |
tyuito | 0:4ff90c5befd9 | 224 | for( int i = Row; i < Mat._nRows - 1; i++ ) |
tyuito | 0:4ff90c5befd9 | 225 | |
tyuito | 0:4ff90c5befd9 | 226 | for( int j = 0; j < Mat._nCols; j++ ) |
tyuito | 0:4ff90c5befd9 | 227 | Mat._matrix[i][j] = Mat._matrix[i+1][j]; |
tyuito | 0:4ff90c5befd9 | 228 | Mat._nRows--; |
tyuito | 0:4ff90c5befd9 | 229 | Mat._matrix.resize(Mat._nRows); |
tyuito | 0:4ff90c5befd9 | 230 | } |
tyuito | 0:4ff90c5befd9 | 231 | } |
tyuito | 0:4ff90c5befd9 | 232 | |
tyuito | 0:4ff90c5befd9 | 233 | /*****************************************************************************************/ |
tyuito | 0:4ff90c5befd9 | 234 | |
tyuito | 0:4ff90c5befd9 | 235 | /// Extracts a single row form calling matrix and saves it to another matrix. |
tyuito | 0:4ff90c5befd9 | 236 | const Matrix Matrix::ExportRow( const Matrix& Mat, int row ) |
tyuito | 0:4ff90c5befd9 | 237 | { |
tyuito | 0:4ff90c5befd9 | 238 | --row; |
tyuito | 0:4ff90c5befd9 | 239 | |
tyuito | 0:4ff90c5befd9 | 240 | if( row > Mat._nRows ) |
tyuito | 0:4ff90c5befd9 | 241 | { |
tyuito | 0:4ff90c5befd9 | 242 | printf( "\n\nERROR:\nRow out of dimmensions @ GetRow\n" |
tyuito | 0:4ff90c5befd9 | 243 | "Nothing Done.\n\n" ); |
tyuito | 0:4ff90c5befd9 | 244 | |
tyuito | 0:4ff90c5befd9 | 245 | }else{ |
tyuito | 0:4ff90c5befd9 | 246 | |
tyuito | 0:4ff90c5befd9 | 247 | Matrix SingleRow( 1 , Mat._nCols ); |
tyuito | 0:4ff90c5befd9 | 248 | SingleRow.Clear(); |
tyuito | 0:4ff90c5befd9 | 249 | |
tyuito | 0:4ff90c5befd9 | 250 | for( int j = 0; j < Mat._nCols; j++ ) |
tyuito | 0:4ff90c5befd9 | 251 | SingleRow._matrix[0][j] = Mat._matrix[row][j]; |
tyuito | 0:4ff90c5befd9 | 252 | |
tyuito | 0:4ff90c5befd9 | 253 | SingleRow._pCol = SingleRow._nCols; |
tyuito | 0:4ff90c5befd9 | 254 | SingleRow._pRow = 0; |
tyuito | 0:4ff90c5befd9 | 255 | |
tyuito | 0:4ff90c5befd9 | 256 | return SingleRow; |
tyuito | 0:4ff90c5befd9 | 257 | } |
tyuito | 0:4ff90c5befd9 | 258 | } |
tyuito | 0:4ff90c5befd9 | 259 | |
tyuito | 0:4ff90c5befd9 | 260 | |
tyuito | 0:4ff90c5befd9 | 261 | /// Extracts a single column form calling matrix and saves it to another matrix. |
tyuito | 0:4ff90c5befd9 | 262 | const Matrix Matrix::ExportCol( const Matrix& Mat, int col ) |
tyuito | 0:4ff90c5befd9 | 263 | { |
tyuito | 0:4ff90c5befd9 | 264 | --col; |
tyuito | 0:4ff90c5befd9 | 265 | |
tyuito | 0:4ff90c5befd9 | 266 | if( col > Mat._nCols ) |
tyuito | 0:4ff90c5befd9 | 267 | { |
tyuito | 0:4ff90c5befd9 | 268 | printf( "\n\nERROR:\nColumn out of dimmensions.\n" |
tyuito | 0:4ff90c5befd9 | 269 | "Nothing Done.\n\n" ); |
tyuito | 0:4ff90c5befd9 | 270 | }else{ |
tyuito | 0:4ff90c5befd9 | 271 | |
tyuito | 0:4ff90c5befd9 | 272 | Matrix SingleCol( Mat._nRows, 1 ); |
tyuito | 0:4ff90c5befd9 | 273 | for(int i = 0; i < Mat._nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 274 | SingleCol._matrix[i][0] = Mat._matrix[i][col]; |
tyuito | 0:4ff90c5befd9 | 275 | |
tyuito | 0:4ff90c5befd9 | 276 | SingleCol._pCol = 0; |
tyuito | 0:4ff90c5befd9 | 277 | SingleCol._pRow = SingleCol._nRows; |
tyuito | 0:4ff90c5befd9 | 278 | |
tyuito | 0:4ff90c5befd9 | 279 | return SingleCol; |
tyuito | 0:4ff90c5befd9 | 280 | } |
tyuito | 0:4ff90c5befd9 | 281 | } |
tyuito | 0:4ff90c5befd9 | 282 | |
tyuito | 0:4ff90c5befd9 | 283 | |
tyuito | 0:4ff90c5befd9 | 284 | /// Makes matrix Bigger! |
tyuito | 0:4ff90c5befd9 | 285 | void Matrix::Resize( int Rows, int Cols ) |
tyuito | 0:4ff90c5befd9 | 286 | { |
tyuito | 0:4ff90c5befd9 | 287 | _nRows = Rows; //Decreases one because internally |
tyuito | 0:4ff90c5befd9 | 288 | _nCols = Cols; // Index starts at zero. |
tyuito | 0:4ff90c5befd9 | 289 | |
tyuito | 0:4ff90c5befd9 | 290 | _matrix.resize( _nRows ); |
tyuito | 0:4ff90c5befd9 | 291 | |
tyuito | 0:4ff90c5befd9 | 292 | for( int i = 0; i< _nRows ; i++ ) |
tyuito | 0:4ff90c5befd9 | 293 | _matrix[i].resize(_nCols); |
tyuito | 0:4ff90c5befd9 | 294 | |
tyuito | 0:4ff90c5befd9 | 295 | _pRow = 0; // If matrix is resized the << |
tyuito | 0:4ff90c5befd9 | 296 | _pCol = 0; // operator overwrites everything! |
tyuito | 0:4ff90c5befd9 | 297 | } |
tyuito | 0:4ff90c5befd9 | 298 | |
tyuito | 0:4ff90c5befd9 | 299 | |
tyuito | 0:4ff90c5befd9 | 300 | /// Ask user for elemnts in Matrix |
tyuito | 0:4ff90c5befd9 | 301 | void Matrix::FillMatrix() |
tyuito | 0:4ff90c5befd9 | 302 | { |
tyuito | 0:4ff90c5befd9 | 303 | for(int i = 0; i < _nRows; i++) |
tyuito | 0:4ff90c5befd9 | 304 | { |
tyuito | 0:4ff90c5befd9 | 305 | for(int j = 0; j < _nCols; j++) |
tyuito | 0:4ff90c5befd9 | 306 | { |
tyuito | 0:4ff90c5befd9 | 307 | printf( "Position [%u][%u]: ", i, j ); |
tyuito | 0:4ff90c5befd9 | 308 | float numero; |
tyuito | 0:4ff90c5befd9 | 309 | scanf( "%f", &numero ); |
tyuito | 0:4ff90c5befd9 | 310 | printf("%.3f ", numero); |
tyuito | 0:4ff90c5befd9 | 311 | this->_matrix[i][j] = numero; |
tyuito | 0:4ff90c5befd9 | 312 | } |
tyuito | 0:4ff90c5befd9 | 313 | printf("\n"); |
tyuito | 0:4ff90c5befd9 | 314 | } |
tyuito | 0:4ff90c5befd9 | 315 | printf("\n"); |
tyuito | 0:4ff90c5befd9 | 316 | |
tyuito | 0:4ff90c5befd9 | 317 | _pRow = _nRows; |
tyuito | 0:4ff90c5befd9 | 318 | _pCol = _nCols; |
tyuito | 0:4ff90c5befd9 | 319 | } |
tyuito | 0:4ff90c5befd9 | 320 | |
tyuito | 0:4ff90c5befd9 | 321 | |
tyuito | 0:4ff90c5befd9 | 322 | /// Prints out Matrix. |
tyuito | 0:4ff90c5befd9 | 323 | void Matrix::print() const |
tyuito | 0:4ff90c5befd9 | 324 | { |
tyuito | 0:4ff90c5befd9 | 325 | for( int i = 0; i < _nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 326 | { |
tyuito | 0:4ff90c5befd9 | 327 | for( int j = 0; j < _nCols; j++ ) |
tyuito | 0:4ff90c5befd9 | 328 | { |
tyuito | 0:4ff90c5befd9 | 329 | printf( "%.3f, ",_matrix[i][j] ); |
tyuito | 0:4ff90c5befd9 | 330 | |
tyuito | 0:4ff90c5befd9 | 331 | } |
tyuito | 0:4ff90c5befd9 | 332 | printf( "\n" ); |
tyuito | 0:4ff90c5befd9 | 333 | } |
tyuito | 0:4ff90c5befd9 | 334 | } |
tyuito | 0:4ff90c5befd9 | 335 | |
tyuito | 0:4ff90c5befd9 | 336 | |
tyuito | 0:4ff90c5befd9 | 337 | /// Fills matrix with zeros. |
tyuito | 0:4ff90c5befd9 | 338 | void Matrix::Clear() |
tyuito | 0:4ff90c5befd9 | 339 | { |
tyuito | 0:4ff90c5befd9 | 340 | for( int i = 0; i < _nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 341 | for( int j = 0; j < _nCols; j++ ) |
tyuito | 0:4ff90c5befd9 | 342 | _matrix[i][j] = 0; |
tyuito | 0:4ff90c5befd9 | 343 | |
tyuito | 0:4ff90c5befd9 | 344 | _pCol = 0; // New data can be added |
tyuito | 0:4ff90c5befd9 | 345 | _pRow = 0; |
tyuito | 0:4ff90c5befd9 | 346 | } |
tyuito | 0:4ff90c5befd9 | 347 | |
tyuito | 0:4ff90c5befd9 | 348 | /********************************************************************************/ |
tyuito | 0:4ff90c5befd9 | 349 | |
tyuito | 0:4ff90c5befd9 | 350 | |
tyuito | 0:4ff90c5befd9 | 351 | /// Inserts a Single element in a desired Position( Index starts at [1][1] ); |
tyuito | 0:4ff90c5befd9 | 352 | void Matrix::add(int Row, int Col, float number) |
tyuito | 0:4ff90c5befd9 | 353 | { |
tyuito | 0:4ff90c5befd9 | 354 | --Col; --Row; |
tyuito | 0:4ff90c5befd9 | 355 | |
tyuito | 0:4ff90c5befd9 | 356 | if( Row > _nRows || Col > _nCols ) |
tyuito | 0:4ff90c5befd9 | 357 | { |
tyuito | 0:4ff90c5befd9 | 358 | printf("\n\nERROR:\nOut of limits of Matrix @ mat.Add()"); |
tyuito | 0:4ff90c5befd9 | 359 | |
tyuito | 0:4ff90c5befd9 | 360 | }else{ |
tyuito | 0:4ff90c5befd9 | 361 | _matrix[Row][Col] = number; |
tyuito | 0:4ff90c5befd9 | 362 | } |
tyuito | 0:4ff90c5befd9 | 363 | } |
tyuito | 0:4ff90c5befd9 | 364 | |
tyuito | 0:4ff90c5befd9 | 365 | |
tyuito | 0:4ff90c5befd9 | 366 | /// Adds all elements in matrix and returns the answer. |
tyuito | 0:4ff90c5befd9 | 367 | float Matrix::sum() const |
tyuito | 0:4ff90c5befd9 | 368 | { |
tyuito | 0:4ff90c5befd9 | 369 | float total = 0; |
tyuito | 0:4ff90c5befd9 | 370 | |
tyuito | 0:4ff90c5befd9 | 371 | for( int i = 0; i < _nRows; i++ ) |
tyuito | 0:4ff90c5befd9 | 372 | for( int j = 0; j < _nCols; j++ ) |
tyuito | 0:4ff90c5befd9 | 373 | total += _matrix[i][j]; |
tyuito | 0:4ff90c5befd9 | 374 | return total; |
tyuito | 0:4ff90c5befd9 | 375 | } |
tyuito | 0:4ff90c5befd9 | 376 | |
tyuito | 0:4ff90c5befd9 | 377 | |
tyuito | 0:4ff90c5befd9 | 378 | /// Returns the specified element. Index Starts at [1][1]. |
tyuito | 0:4ff90c5befd9 | 379 | float Matrix::getNumber( int Row, int Col ) const |
tyuito | 0:4ff90c5befd9 | 380 | { return this->_matrix[Row -1][Col - 1]; } |
tyuito | 0:4ff90c5befd9 | 381 | |
tyuito | 0:4ff90c5befd9 | 382 | /// Returns the number of Rows in Matrix. |
tyuito | 0:4ff90c5befd9 | 383 | int Matrix::getRows() const{ return this->_nRows; } |
tyuito | 0:4ff90c5befd9 | 384 | |
tyuito | 0:4ff90c5befd9 | 385 | |
tyuito | 0:4ff90c5befd9 | 386 | /// Returns the number of Columns in Matrix. |
tyuito | 0:4ff90c5befd9 | 387 | int Matrix::getCols() const{ return this->_nCols; } |