use from Ernesto Palacios
Dependents: RoboticArm DXL_SDK_Porting_Test
Fork of Matrix by
Matrix.cpp@9:f19d5d6c3468, 2017-03-31 (annotated)
- Committer:
- stanley1228
- Date:
- Fri Mar 31 09:10:27 2017 +0800
- Revision:
- 9:f19d5d6c3468
- Parent:
- 8:1774aa06ab99
no
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yo_Robot | 2:493402568a5e | 1 | /** |
Yo_Robot | 5:a4014ab0a8cf | 2 | * @brief Source Code for the Matrix Class. |
Yo_Robot | 5:a4014ab0a8cf | 3 | * @file Matrix.cpp |
Yo_Robot | 3:589fb80932b5 | 4 | * @author Ernesto Palacios |
Yo_Robot | 2:493402568a5e | 5 | * |
Yo_Robot | 2:493402568a5e | 6 | * Created on September 2011. |
Yo_Robot | 2:493402568a5e | 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 | 2:493402568a5e | 11 | */ |
Yo_Robot | 2:493402568a5e | 12 | |
Yo_Robot | 0:3abd8c2d7c34 | 13 | #include "mbed.h" |
Yo_Robot | 0:3abd8c2d7c34 | 14 | #include "Matrix.h" |
Yo_Robot | 0:3abd8c2d7c34 | 15 | |
stanley1228 | 8:1774aa06ab99 | 16 | |
stanley1228 | 8:1774aa06ab99 | 17 | extern Serial pc; |
stanley1228 | 8:1774aa06ab99 | 18 | |
stanley1228 | 8:1774aa06ab99 | 19 | |
stanley1228 | 8:1774aa06ab99 | 20 | #if (DEBUG) |
stanley1228 | 8:1774aa06ab99 | 21 | #define DBGMSG(x) pc.printf x; |
stanley1228 | 8:1774aa06ab99 | 22 | #else |
stanley1228 | 8:1774aa06ab99 | 23 | #define DBGMSG(x) |
stanley1228 | 8:1774aa06ab99 | 24 | #endif |
stanley1228 | 8:1774aa06ab99 | 25 | |
stanley1228 | 8:1774aa06ab99 | 26 | |
stanley1228 | 8:1774aa06ab99 | 27 | |
stanley1228 | 8:1774aa06ab99 | 28 | |
stanley1228 | 8:1774aa06ab99 | 29 | |
Yo_Robot | 1:48f417da268e | 30 | /// Rows by Cols Matrix Constructor |
Yo_Robot | 2:493402568a5e | 31 | Matrix::Matrix(int Rows, int Cols): _nRows(Rows), _nCols(Cols) |
Yo_Robot | 0:3abd8c2d7c34 | 32 | { |
Yo_Robot | 1:48f417da268e | 33 | _matrix.resize(_nRows); |
Yo_Robot | 1:48f417da268e | 34 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 35 | _matrix[i].resize(_nCols); |
Yo_Robot | 2:493402568a5e | 36 | |
Yo_Robot | 2:493402568a5e | 37 | _pRow = 0; |
Yo_Robot | 2:493402568a5e | 38 | _pCol = 0; |
Yo_Robot | 2:493402568a5e | 39 | |
Yo_Robot | 2:493402568a5e | 40 | this->Clear(); //Make all elements zero by default. |
Yo_Robot | 0:3abd8c2d7c34 | 41 | } |
Yo_Robot | 0:3abd8c2d7c34 | 42 | |
Yo_Robot | 0:3abd8c2d7c34 | 43 | |
Yo_Robot | 2:493402568a5e | 44 | /// Copies one matrix into a new one |
Yo_Robot | 2:493402568a5e | 45 | Matrix::Matrix(const Matrix& base) |
Yo_Robot | 0:3abd8c2d7c34 | 46 | { |
Yo_Robot | 2:493402568a5e | 47 | _nCols = base._nCols; |
Yo_Robot | 2:493402568a5e | 48 | _nRows = base._nRows; |
Yo_Robot | 2:493402568a5e | 49 | |
Yo_Robot | 2:493402568a5e | 50 | _pRow = base._pRow; |
Yo_Robot | 2:493402568a5e | 51 | _pCol = base._pCol; |
Yo_Robot | 2:493402568a5e | 52 | |
Yo_Robot | 1:48f417da268e | 53 | _matrix.resize(_nRows); |
Yo_Robot | 1:48f417da268e | 54 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 55 | _matrix[i].resize(_nCols); |
Yo_Robot | 0:3abd8c2d7c34 | 56 | |
Yo_Robot | 1:48f417da268e | 57 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 58 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 59 | _matrix[i][j] = base._matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 60 | } |
Yo_Robot | 0:3abd8c2d7c34 | 61 | |
Yo_Robot | 0:3abd8c2d7c34 | 62 | |
Yo_Robot | 1:48f417da268e | 63 | /// Default Constructor |
Yo_Robot | 2:493402568a5e | 64 | Matrix::Matrix() |
Yo_Robot | 0:3abd8c2d7c34 | 65 | { |
Yo_Robot | 2:493402568a5e | 66 | _nCols = 0; |
Yo_Robot | 2:493402568a5e | 67 | _nRows = 0; |
Yo_Robot | 1:48f417da268e | 68 | |
Yo_Robot | 2:493402568a5e | 69 | _pRow = 0; |
Yo_Robot | 2:493402568a5e | 70 | _pCol = 0; |
Yo_Robot | 4:c0c8f3edd60e | 71 | |
Yo_Robot | 0:3abd8c2d7c34 | 72 | } |
Yo_Robot | 0:3abd8c2d7c34 | 73 | |
Yo_Robot | 2:493402568a5e | 74 | /***********************************************************************/ |
Yo_Robot | 0:3abd8c2d7c34 | 75 | |
Yo_Robot | 1:48f417da268e | 76 | /// Returns true if matrix is full of zeros |
Yo_Robot | 5:a4014ab0a8cf | 77 | bool Matrix::isZero() const |
Yo_Robot | 0:3abd8c2d7c34 | 78 | { |
Yo_Robot | 1:48f417da268e | 79 | bool zero = false; |
Yo_Robot | 1:48f417da268e | 80 | for( int i = 0; i < this->_nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 81 | for( int j = 0; j < this->_nCols; j++ ) |
Yo_Robot | 2:493402568a5e | 82 | if( _matrix[i][j] != 0 ) |
Yo_Robot | 1:48f417da268e | 83 | zero = zero || true; |
Yo_Robot | 1:48f417da268e | 84 | return !zero; |
Yo_Robot | 0:3abd8c2d7c34 | 85 | } |
Yo_Robot | 0:3abd8c2d7c34 | 86 | |
Yo_Robot | 0:3abd8c2d7c34 | 87 | |
Yo_Robot | 1:48f417da268e | 88 | /// Returns true if Matrix is Single Row ot Single Column. |
Yo_Robot | 5:a4014ab0a8cf | 89 | bool Matrix::isVector() const |
Yo_Robot | 0:3abd8c2d7c34 | 90 | { |
Yo_Robot | 2:493402568a5e | 91 | if( _nRows == 1 || _nCols == 1 ) |
Yo_Robot | 1:48f417da268e | 92 | return true; |
Yo_Robot | 1:48f417da268e | 93 | else |
Yo_Robot | 1:48f417da268e | 94 | return false; |
Yo_Robot | 0:3abd8c2d7c34 | 95 | } |
Yo_Robot | 0:3abd8c2d7c34 | 96 | |
Yo_Robot | 1:48f417da268e | 97 | /*************************************************************************/ |
Yo_Robot | 0:3abd8c2d7c34 | 98 | |
Yo_Robot | 1:48f417da268e | 99 | /// Returns all elements in Matrix as a single Row vector. |
Yo_Robot | 2:493402568a5e | 100 | const Matrix Matrix::ToPackedVector( const Matrix& Mat ) |
Yo_Robot | 0:3abd8c2d7c34 | 101 | { |
Yo_Robot | 4:c0c8f3edd60e | 102 | |
Yo_Robot | 2:493402568a5e | 103 | Matrix Crushed( 1, Mat._nRows * Mat._nCols ); |
Yo_Robot | 0:3abd8c2d7c34 | 104 | |
Yo_Robot | 0:3abd8c2d7c34 | 105 | int cont = 0; |
Yo_Robot | 2:493402568a5e | 106 | |
Yo_Robot | 2:493402568a5e | 107 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 108 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 109 | { |
Yo_Robot | 2:493402568a5e | 110 | Crushed._matrix[0][cont] = Mat._matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 111 | cont++; |
Yo_Robot | 0:3abd8c2d7c34 | 112 | } |
Yo_Robot | 0:3abd8c2d7c34 | 113 | |
Yo_Robot | 2:493402568a5e | 114 | Crushed._pRow = Crushed._nRows; |
Yo_Robot | 2:493402568a5e | 115 | Crushed._pCol = Crushed._nCols; |
Yo_Robot | 2:493402568a5e | 116 | |
Yo_Robot | 2:493402568a5e | 117 | return Crushed; |
Yo_Robot | 0:3abd8c2d7c34 | 118 | } |
Yo_Robot | 0:3abd8c2d7c34 | 119 | |
Yo_Robot | 2:493402568a5e | 120 | |
Yo_Robot | 0:3abd8c2d7c34 | 121 | |
Yo_Robot | 2:493402568a5e | 122 | /// To add (Insert) a Single Row to a Matrix. |
Yo_Robot | 4:c0c8f3edd60e | 123 | void Matrix::AddRow(Matrix& Mat, int index) |
Yo_Robot | 0:3abd8c2d7c34 | 124 | { |
Yo_Robot | 4:c0c8f3edd60e | 125 | --index; |
Yo_Robot | 0:3abd8c2d7c34 | 126 | |
Yo_Robot | 4:c0c8f3edd60e | 127 | if( index > Mat._nRows + 1) |
Yo_Robot | 0:3abd8c2d7c34 | 128 | { |
stanley1228 | 8:1774aa06ab99 | 129 | DBGMSG(printf("\n\nERROR:\nRow out of Limits @ AddRow()\n")) |
Yo_Robot | 2:493402568a5e | 130 | |
Yo_Robot | 0:3abd8c2d7c34 | 131 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 132 | |
Yo_Robot | 1:48f417da268e | 133 | Mat._nRows++; |
Yo_Robot | 1:48f417da268e | 134 | Mat._matrix.resize( Mat._nRows ); |
Yo_Robot | 1:48f417da268e | 135 | |
Yo_Robot | 1:48f417da268e | 136 | Mat._matrix[ Mat._nRows - 1 ].resize( Mat._nCols ); |
Yo_Robot | 1:48f417da268e | 137 | |
Yo_Robot | 4:c0c8f3edd60e | 138 | for( int i = Mat._nRows - 1; i > index; i-- ) |
Yo_Robot | 1:48f417da268e | 139 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 140 | Mat._matrix[i][j] = Mat._matrix[i - 1][j]; |
Yo_Robot | 1:48f417da268e | 141 | |
Yo_Robot | 1:48f417da268e | 142 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 4:c0c8f3edd60e | 143 | Mat._matrix[index][j] = 0.0; |
Yo_Robot | 0:3abd8c2d7c34 | 144 | } |
Yo_Robot | 0:3abd8c2d7c34 | 145 | } |
Yo_Robot | 0:3abd8c2d7c34 | 146 | |
Yo_Robot | 0:3abd8c2d7c34 | 147 | |
Yo_Robot | 4:c0c8f3edd60e | 148 | void Matrix::AddRow(Matrix& Receip, const Matrix& Row, int index) |
Yo_Robot | 4:c0c8f3edd60e | 149 | { |
Yo_Robot | 4:c0c8f3edd60e | 150 | Matrix::AddRow( Receip, index ); //Make Room |
Yo_Robot | 4:c0c8f3edd60e | 151 | |
Yo_Robot | 4:c0c8f3edd60e | 152 | --index; |
Yo_Robot | 5:a4014ab0a8cf | 153 | for( int i = 0; i < Receip._nCols; i++ ) |
Yo_Robot | 4:c0c8f3edd60e | 154 | Receip._matrix[index][i] = Row._matrix[0][i]; //Copy Data. |
Yo_Robot | 4:c0c8f3edd60e | 155 | |
Yo_Robot | 4:c0c8f3edd60e | 156 | } |
Yo_Robot | 4:c0c8f3edd60e | 157 | |
Yo_Robot | 4:c0c8f3edd60e | 158 | |
Yo_Robot | 1:48f417da268e | 159 | /// To add (Insert) a single Column to a Matrix |
Yo_Robot | 4:c0c8f3edd60e | 160 | void Matrix::AddCol( Matrix& Mat, int index ) |
Yo_Robot | 1:48f417da268e | 161 | { |
Yo_Robot | 4:c0c8f3edd60e | 162 | --index; |
Yo_Robot | 0:3abd8c2d7c34 | 163 | |
Yo_Robot | 4:c0c8f3edd60e | 164 | if( index > Mat._nCols + 1 ) |
Yo_Robot | 1:48f417da268e | 165 | { |
Yo_Robot | 1:48f417da268e | 166 | printf("\n\nERROR:\nRow out of Limits on AddCol()\n"); |
Yo_Robot | 2:493402568a5e | 167 | |
Yo_Robot | 1:48f417da268e | 168 | }else{ |
Yo_Robot | 1:48f417da268e | 169 | |
Yo_Robot | 1:48f417da268e | 170 | |
Yo_Robot | 1:48f417da268e | 171 | Mat._nCols++; |
Yo_Robot | 1:48f417da268e | 172 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 173 | Mat._matrix[i].resize( Mat._nCols ); |
Yo_Robot | 1:48f417da268e | 174 | |
Yo_Robot | 1:48f417da268e | 175 | for( int i = 0; i < Mat._nRows; i++ ) |
stanley1228 | 6:3f01dc2d77f1 | 176 | for( int j = Mat._nCols-1; j > index; j-- )//stanley Mat._nCols =>Mat._nCols-1 |
Yo_Robot | 1:48f417da268e | 177 | Mat._matrix[i][j] = Mat._matrix[i][j - 1]; |
Yo_Robot | 1:48f417da268e | 178 | |
Yo_Robot | 1:48f417da268e | 179 | for( int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 4:c0c8f3edd60e | 180 | Mat._matrix[i][index] = 0.0; |
Yo_Robot | 1:48f417da268e | 181 | |
Yo_Robot | 1:48f417da268e | 182 | } |
Yo_Robot | 1:48f417da268e | 183 | } |
Yo_Robot | 1:48f417da268e | 184 | |
Yo_Robot | 1:48f417da268e | 185 | |
Yo_Robot | 4:c0c8f3edd60e | 186 | void Matrix::AddCol(Matrix& Receip, const Matrix& Row, int index) |
Yo_Robot | 4:c0c8f3edd60e | 187 | { |
Yo_Robot | 4:c0c8f3edd60e | 188 | Matrix::AddCol( Receip, index ); // Make Rom |
Yo_Robot | 4:c0c8f3edd60e | 189 | |
Yo_Robot | 4:c0c8f3edd60e | 190 | --index; |
Yo_Robot | 4:c0c8f3edd60e | 191 | for( int i = 0; i < Receip._nRows; i++ ) |
Yo_Robot | 4:c0c8f3edd60e | 192 | Receip._matrix[i][index] = Row._matrix[i][0]; //Copy Data. |
Yo_Robot | 4:c0c8f3edd60e | 193 | } |
Yo_Robot | 4:c0c8f3edd60e | 194 | |
Yo_Robot | 4:c0c8f3edd60e | 195 | |
Yo_Robot | 1:48f417da268e | 196 | /// Delete a Single Column From Matrix. |
Yo_Robot | 2:493402568a5e | 197 | void Matrix::DeleteCol( Matrix& Mat, int Col) |
Yo_Robot | 0:3abd8c2d7c34 | 198 | { |
Yo_Robot | 0:3abd8c2d7c34 | 199 | --Col; // Because of Column zero. |
Yo_Robot | 0:3abd8c2d7c34 | 200 | |
Yo_Robot | 1:48f417da268e | 201 | if( Col > Mat._nCols ) |
Yo_Robot | 0:3abd8c2d7c34 | 202 | { |
Yo_Robot | 1:48f417da268e | 203 | printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); |
Yo_Robot | 2:493402568a5e | 204 | |
Yo_Robot | 0:3abd8c2d7c34 | 205 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 206 | |
Yo_Robot | 1:48f417da268e | 207 | for( int i = 0; i < Mat._nRows; i++ ) |
stanley1228 | 6:3f01dc2d77f1 | 208 | for( int j = Col; j < Mat._nCols-1; j++ ) //stanley Mat._nCols=>Mat._nCols-1 |
Yo_Robot | 0:3abd8c2d7c34 | 209 | Mat._matrix[i][j] = Mat._matrix[i][j+1]; |
Yo_Robot | 0:3abd8c2d7c34 | 210 | |
Yo_Robot | 2:493402568a5e | 211 | // If adressing last element of Column, |
Yo_Robot | 2:493402568a5e | 212 | // wich no longer exists |
Yo_Robot | 2:493402568a5e | 213 | if( Mat._pCol == Mat._nCols ) |
Yo_Robot | 2:493402568a5e | 214 | Mat._pCol--; |
Yo_Robot | 2:493402568a5e | 215 | |
Yo_Robot | 0:3abd8c2d7c34 | 216 | // Decrease one column |
Yo_Robot | 1:48f417da268e | 217 | Mat._nCols--; |
Yo_Robot | 0:3abd8c2d7c34 | 218 | |
Yo_Robot | 0:3abd8c2d7c34 | 219 | //Erase last Column |
Yo_Robot | 1:48f417da268e | 220 | for( int i = 0; i < Mat._nRows; i++ ) |
stanley1228 | 6:3f01dc2d77f1 | 221 | Mat._matrix[i].reserve(Mat._nCols); //待理解 |
Yo_Robot | 0:3abd8c2d7c34 | 222 | |
Yo_Robot | 0:3abd8c2d7c34 | 223 | } |
Yo_Robot | 0:3abd8c2d7c34 | 224 | } |
Yo_Robot | 0:3abd8c2d7c34 | 225 | |
Yo_Robot | 1:48f417da268e | 226 | |
Yo_Robot | 1:48f417da268e | 227 | /// Delete a Single Row form Matrix |
Yo_Robot | 1:48f417da268e | 228 | void Matrix::DeleteRow(Matrix& Mat, int Row) |
Yo_Robot | 1:48f417da268e | 229 | { |
Yo_Robot | 1:48f417da268e | 230 | --Row; |
Yo_Robot | 0:3abd8c2d7c34 | 231 | |
Yo_Robot | 1:48f417da268e | 232 | if( Row > Mat._nRows ) |
Yo_Robot | 1:48f417da268e | 233 | { |
Yo_Robot | 1:48f417da268e | 234 | printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); |
Yo_Robot | 2:493402568a5e | 235 | |
Yo_Robot | 1:48f417da268e | 236 | }else{ |
Yo_Robot | 0:3abd8c2d7c34 | 237 | |
Yo_Robot | 1:48f417da268e | 238 | for( int i = Row; i < Mat._nRows - 1; i++ ) |
Yo_Robot | 2:493402568a5e | 239 | |
Yo_Robot | 1:48f417da268e | 240 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 241 | Mat._matrix[i][j] = Mat._matrix[i+1][j]; |
Yo_Robot | 1:48f417da268e | 242 | Mat._nRows--; |
Yo_Robot | 1:48f417da268e | 243 | Mat._matrix.resize(Mat._nRows); |
Yo_Robot | 1:48f417da268e | 244 | } |
Yo_Robot | 1:48f417da268e | 245 | } |
Yo_Robot | 1:48f417da268e | 246 | |
Yo_Robot | 1:48f417da268e | 247 | /*****************************************************************************************/ |
Yo_Robot | 1:48f417da268e | 248 | |
Yo_Robot | 1:48f417da268e | 249 | /// Extracts a single row form calling matrix and saves it to another matrix. |
Yo_Robot | 2:493402568a5e | 250 | const Matrix Matrix::ExportRow( const Matrix& Mat, int row ) |
Yo_Robot | 0:3abd8c2d7c34 | 251 | { |
Yo_Robot | 2:493402568a5e | 252 | --row; |
Yo_Robot | 1:48f417da268e | 253 | |
Yo_Robot | 2:493402568a5e | 254 | if( row > Mat._nRows ) |
Yo_Robot | 0:3abd8c2d7c34 | 255 | { |
Yo_Robot | 2:493402568a5e | 256 | printf( "\n\nERROR:\nRow out of dimmensions @ GetRow\n" |
Yo_Robot | 1:48f417da268e | 257 | "Nothing Done.\n\n" ); |
stanley1228 | 6:3f01dc2d77f1 | 258 | Matrix NULL_M( 1, 1 );//暫時這樣stanley |
stanley1228 | 6:3f01dc2d77f1 | 259 | return NULL_M; |
Yo_Robot | 0:3abd8c2d7c34 | 260 | |
Yo_Robot | 0:3abd8c2d7c34 | 261 | }else{ |
Yo_Robot | 1:48f417da268e | 262 | |
Yo_Robot | 2:493402568a5e | 263 | Matrix SingleRow( 1 , Mat._nCols ); |
Yo_Robot | 1:48f417da268e | 264 | SingleRow.Clear(); |
Yo_Robot | 4:c0c8f3edd60e | 265 | |
Yo_Robot | 2:493402568a5e | 266 | for( int j = 0; j < Mat._nCols; j++ ) |
Yo_Robot | 5:a4014ab0a8cf | 267 | SingleRow._matrix[0][j] = Mat._matrix[row][j]; |
Yo_Robot | 2:493402568a5e | 268 | |
Yo_Robot | 2:493402568a5e | 269 | SingleRow._pCol = SingleRow._nCols; |
Yo_Robot | 2:493402568a5e | 270 | SingleRow._pRow = 0; |
Yo_Robot | 2:493402568a5e | 271 | |
Yo_Robot | 2:493402568a5e | 272 | return SingleRow; |
Yo_Robot | 0:3abd8c2d7c34 | 273 | } |
Yo_Robot | 0:3abd8c2d7c34 | 274 | } |
Yo_Robot | 0:3abd8c2d7c34 | 275 | |
Yo_Robot | 0:3abd8c2d7c34 | 276 | |
Yo_Robot | 1:48f417da268e | 277 | /// Extracts a single column form calling matrix and saves it to another matrix. |
Yo_Robot | 2:493402568a5e | 278 | const Matrix Matrix::ExportCol( const Matrix& Mat, int col ) |
Yo_Robot | 1:48f417da268e | 279 | { |
Yo_Robot | 2:493402568a5e | 280 | --col; |
Yo_Robot | 4:c0c8f3edd60e | 281 | |
Yo_Robot | 2:493402568a5e | 282 | if( col > Mat._nCols ) |
Yo_Robot | 1:48f417da268e | 283 | { |
Yo_Robot | 2:493402568a5e | 284 | printf( "\n\nERROR:\nColumn out of dimmensions.\n" |
Yo_Robot | 1:48f417da268e | 285 | "Nothing Done.\n\n" ); |
stanley1228 | 6:3f01dc2d77f1 | 286 | Matrix NULL_M( 1, 1 );//暫時這樣stanley |
stanley1228 | 6:3f01dc2d77f1 | 287 | return NULL_M; |
stanley1228 | 6:3f01dc2d77f1 | 288 | |
Yo_Robot | 1:48f417da268e | 289 | }else{ |
Yo_Robot | 1:48f417da268e | 290 | |
Yo_Robot | 2:493402568a5e | 291 | Matrix SingleCol( Mat._nRows, 1 ); |
Yo_Robot | 2:493402568a5e | 292 | for(int i = 0; i < Mat._nRows; i++ ) |
Yo_Robot | 2:493402568a5e | 293 | SingleCol._matrix[i][0] = Mat._matrix[i][col]; |
Yo_Robot | 4:c0c8f3edd60e | 294 | |
Yo_Robot | 2:493402568a5e | 295 | SingleCol._pCol = 0; |
Yo_Robot | 2:493402568a5e | 296 | SingleCol._pRow = SingleCol._nRows; |
Yo_Robot | 4:c0c8f3edd60e | 297 | |
Yo_Robot | 2:493402568a5e | 298 | return SingleCol; |
Yo_Robot | 1:48f417da268e | 299 | } |
Yo_Robot | 1:48f417da268e | 300 | } |
Yo_Robot | 1:48f417da268e | 301 | |
Yo_Robot | 1:48f417da268e | 302 | |
Yo_Robot | 1:48f417da268e | 303 | /// Makes matrix Bigger! |
Yo_Robot | 1:48f417da268e | 304 | void Matrix::Resize( int Rows, int Cols ) |
Yo_Robot | 1:48f417da268e | 305 | { |
Yo_Robot | 2:493402568a5e | 306 | _nRows = Rows; //Decreases one because internally |
Yo_Robot | 2:493402568a5e | 307 | _nCols = Cols; // Index starts at zero. |
Yo_Robot | 2:493402568a5e | 308 | |
Yo_Robot | 2:493402568a5e | 309 | _matrix.resize( _nRows ); |
Yo_Robot | 2:493402568a5e | 310 | |
Yo_Robot | 1:48f417da268e | 311 | for( int i = 0; i< _nRows ; i++ ) |
Yo_Robot | 1:48f417da268e | 312 | _matrix[i].resize(_nCols); |
Yo_Robot | 2:493402568a5e | 313 | |
Yo_Robot | 2:493402568a5e | 314 | _pRow = 0; // If matrix is resized the << |
Yo_Robot | 2:493402568a5e | 315 | _pCol = 0; // operator overwrites everything! |
Yo_Robot | 1:48f417da268e | 316 | } |
Yo_Robot | 1:48f417da268e | 317 | |
Yo_Robot | 1:48f417da268e | 318 | |
Yo_Robot | 1:48f417da268e | 319 | /// Ask user for elemnts in Matrix |
Yo_Robot | 0:3abd8c2d7c34 | 320 | void Matrix::FillMatrix() |
Yo_Robot | 0:3abd8c2d7c34 | 321 | { |
Yo_Robot | 1:48f417da268e | 322 | for(int i = 0; i < _nRows; i++) |
Yo_Robot | 0:3abd8c2d7c34 | 323 | { |
Yo_Robot | 1:48f417da268e | 324 | for(int j = 0; j < _nCols; j++) |
Yo_Robot | 0:3abd8c2d7c34 | 325 | { |
Yo_Robot | 0:3abd8c2d7c34 | 326 | printf( "Position [%u][%u]: ", i, j ); |
Yo_Robot | 0:3abd8c2d7c34 | 327 | float numero; |
Yo_Robot | 0:3abd8c2d7c34 | 328 | scanf( "%f", &numero ); |
Yo_Robot | 0:3abd8c2d7c34 | 329 | printf("%.3f ", numero); |
Yo_Robot | 1:48f417da268e | 330 | this->_matrix[i][j] = numero; |
Yo_Robot | 0:3abd8c2d7c34 | 331 | } |
Yo_Robot | 0:3abd8c2d7c34 | 332 | printf("\n"); |
Yo_Robot | 0:3abd8c2d7c34 | 333 | } |
Yo_Robot | 0:3abd8c2d7c34 | 334 | printf("\n"); |
Yo_Robot | 2:493402568a5e | 335 | |
Yo_Robot | 2:493402568a5e | 336 | _pRow = _nRows; |
Yo_Robot | 2:493402568a5e | 337 | _pCol = _nCols; |
Yo_Robot | 0:3abd8c2d7c34 | 338 | } |
Yo_Robot | 0:3abd8c2d7c34 | 339 | |
Yo_Robot | 0:3abd8c2d7c34 | 340 | |
Yo_Robot | 1:48f417da268e | 341 | /// Prints out Matrix. |
Yo_Robot | 5:a4014ab0a8cf | 342 | void Matrix::print() const |
Yo_Robot | 0:3abd8c2d7c34 | 343 | { |
Yo_Robot | 1:48f417da268e | 344 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 345 | { |
Yo_Robot | 1:48f417da268e | 346 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 0:3abd8c2d7c34 | 347 | { |
Yo_Robot | 0:3abd8c2d7c34 | 348 | printf( "%.3f, ",_matrix[i][j] ); |
Yo_Robot | 1:48f417da268e | 349 | |
Yo_Robot | 0:3abd8c2d7c34 | 350 | } |
Yo_Robot | 1:48f417da268e | 351 | printf( "\n" ); |
Yo_Robot | 0:3abd8c2d7c34 | 352 | } |
Yo_Robot | 0:3abd8c2d7c34 | 353 | } |
Yo_Robot | 0:3abd8c2d7c34 | 354 | |
Yo_Robot | 0:3abd8c2d7c34 | 355 | |
Yo_Robot | 1:48f417da268e | 356 | /// Fills matrix with zeros. |
Yo_Robot | 1:48f417da268e | 357 | void Matrix::Clear() |
Yo_Robot | 1:48f417da268e | 358 | { |
Yo_Robot | 1:48f417da268e | 359 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 1:48f417da268e | 360 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 1:48f417da268e | 361 | _matrix[i][j] = 0; |
Yo_Robot | 2:493402568a5e | 362 | |
Yo_Robot | 2:493402568a5e | 363 | _pCol = 0; // New data can be added |
Yo_Robot | 2:493402568a5e | 364 | _pRow = 0; |
Yo_Robot | 1:48f417da268e | 365 | } |
Yo_Robot | 0:3abd8c2d7c34 | 366 | |
Yo_Robot | 1:48f417da268e | 367 | /********************************************************************************/ |
Yo_Robot | 1:48f417da268e | 368 | |
Yo_Robot | 2:493402568a5e | 369 | |
Yo_Robot | 1:48f417da268e | 370 | /// Inserts a Single element in a desired Position( Index starts at [1][1] ); |
Yo_Robot | 1:48f417da268e | 371 | void Matrix::add(int Row, int Col, float number) |
Yo_Robot | 1:48f417da268e | 372 | { |
Yo_Robot | 1:48f417da268e | 373 | --Col; --Row; |
Yo_Robot | 2:493402568a5e | 374 | |
Yo_Robot | 1:48f417da268e | 375 | if( Row > _nRows || Col > _nCols ) |
Yo_Robot | 1:48f417da268e | 376 | { |
Yo_Robot | 1:48f417da268e | 377 | printf("\n\nERROR:\nOut of limits of Matrix @ mat.Add()"); |
Yo_Robot | 2:493402568a5e | 378 | |
Yo_Robot | 1:48f417da268e | 379 | }else{ |
Yo_Robot | 1:48f417da268e | 380 | _matrix[Row][Col] = number; |
Yo_Robot | 1:48f417da268e | 381 | } |
Yo_Robot | 1:48f417da268e | 382 | } |
Yo_Robot | 1:48f417da268e | 383 | |
stanley1228 | 6:3f01dc2d77f1 | 384 | //stanley |
stanley1228 | 6:3f01dc2d77f1 | 385 | void Matrix::Vec_ext_1_row(Matrix& Vec,float number) |
stanley1228 | 6:3f01dc2d77f1 | 386 | { |
stanley1228 | 6:3f01dc2d77f1 | 387 | |
stanley1228 | 6:3f01dc2d77f1 | 388 | //Vec._nRows++; |
stanley1228 | 6:3f01dc2d77f1 | 389 | |
stanley1228 | 6:3f01dc2d77f1 | 390 | if( Vec._nCols > 1 ) |
stanley1228 | 6:3f01dc2d77f1 | 391 | { |
stanley1228 | 6:3f01dc2d77f1 | 392 | printf("\n\nERROR:\nOut of limits of Matrix @ mat.Vec_ext_1_row()"); |
stanley1228 | 6:3f01dc2d77f1 | 393 | |
stanley1228 | 6:3f01dc2d77f1 | 394 | } |
stanley1228 | 6:3f01dc2d77f1 | 395 | else |
stanley1228 | 6:3f01dc2d77f1 | 396 | { |
stanley1228 | 6:3f01dc2d77f1 | 397 | for( int i = 0; i < _nRows-1; i++ )//複製前三列 |
stanley1228 | 6:3f01dc2d77f1 | 398 | _matrix[i][0] = Vec._matrix[i][0]; |
stanley1228 | 6:3f01dc2d77f1 | 399 | |
stanley1228 | 6:3f01dc2d77f1 | 400 | _matrix[_nRows-1][0] = number;//最後一列補0 |
stanley1228 | 6:3f01dc2d77f1 | 401 | } |
stanley1228 | 6:3f01dc2d77f1 | 402 | } |
stanley1228 | 6:3f01dc2d77f1 | 403 | |
stanley1228 | 6:3f01dc2d77f1 | 404 | //stanley |
stanley1228 | 6:3f01dc2d77f1 | 405 | //[Vec1 vec2 vec3 number]' =>Just get [Vec1 vec2 vec3] |
stanley1228 | 6:3f01dc2d77f1 | 406 | void Matrix::Vec_export_3_row(Matrix& Vec) |
stanley1228 | 6:3f01dc2d77f1 | 407 | { |
stanley1228 | 6:3f01dc2d77f1 | 408 | _nRows=3; |
stanley1228 | 6:3f01dc2d77f1 | 409 | |
stanley1228 | 6:3f01dc2d77f1 | 410 | if( Vec._nCols > 1 ) |
stanley1228 | 6:3f01dc2d77f1 | 411 | { |
stanley1228 | 6:3f01dc2d77f1 | 412 | printf("\n\nERROR:\nOut of limits of Matrix @ mat.Vec_export_3_row()"); |
stanley1228 | 6:3f01dc2d77f1 | 413 | |
stanley1228 | 6:3f01dc2d77f1 | 414 | } |
stanley1228 | 6:3f01dc2d77f1 | 415 | else |
stanley1228 | 6:3f01dc2d77f1 | 416 | { |
stanley1228 | 6:3f01dc2d77f1 | 417 | for( int i = 0; i < _nRows; i++ )//複製前三列 |
stanley1228 | 6:3f01dc2d77f1 | 418 | _matrix[i][0] = Vec._matrix[i][0]; |
stanley1228 | 6:3f01dc2d77f1 | 419 | |
stanley1228 | 6:3f01dc2d77f1 | 420 | } |
stanley1228 | 6:3f01dc2d77f1 | 421 | |
stanley1228 | 6:3f01dc2d77f1 | 422 | } |
stanley1228 | 6:3f01dc2d77f1 | 423 | |
Yo_Robot | 1:48f417da268e | 424 | |
Yo_Robot | 1:48f417da268e | 425 | /// Adds all elements in matrix and returns the answer. |
Yo_Robot | 5:a4014ab0a8cf | 426 | float Matrix::sum() const |
Yo_Robot | 0:3abd8c2d7c34 | 427 | { |
Yo_Robot | 5:a4014ab0a8cf | 428 | float total = 0; |
Yo_Robot | 0:3abd8c2d7c34 | 429 | |
Yo_Robot | 5:a4014ab0a8cf | 430 | for( int i = 0; i < _nRows; i++ ) |
Yo_Robot | 5:a4014ab0a8cf | 431 | for( int j = 0; j < _nCols; j++ ) |
Yo_Robot | 5:a4014ab0a8cf | 432 | total += _matrix[i][j]; |
Yo_Robot | 0:3abd8c2d7c34 | 433 | return total; |
Yo_Robot | 0:3abd8c2d7c34 | 434 | } |
Yo_Robot | 0:3abd8c2d7c34 | 435 | |
Yo_Robot | 0:3abd8c2d7c34 | 436 | |
Yo_Robot | 1:48f417da268e | 437 | /// Returns the specified element. Index Starts at [1][1]. |
Yo_Robot | 5:a4014ab0a8cf | 438 | float Matrix::getNumber( int Row, int Col ) const |
Yo_Robot | 1:48f417da268e | 439 | { return this->_matrix[Row -1][Col - 1]; } |
Yo_Robot | 0:3abd8c2d7c34 | 440 | |
Yo_Robot | 1:48f417da268e | 441 | /// Returns the number of Rows in Matrix. |
Yo_Robot | 5:a4014ab0a8cf | 442 | int Matrix::getRows() const{ return this->_nRows; } |
Yo_Robot | 0:3abd8c2d7c34 | 443 | |
Yo_Robot | 0:3abd8c2d7c34 | 444 | |
Yo_Robot | 1:48f417da268e | 445 | /// Returns the number of Columns in Matrix. |
Yo_Robot | 5:a4014ab0a8cf | 446 | int Matrix::getCols() const{ return this->_nCols; } |