Sample of program breaking when a certain set of source files are in a folder, but is fine when it is in the root. In this case, it is tested with RF12B.cpp, RF12B.h and rfdefs
Matrix.cpp
00001 /** 00002 * @brief Source Code for the Matrix Class. 00003 * @file Matrix.cpp 00004 * @author Ernesto Palacios 00005 * 00006 * Created on September 2011. 00007 * 00008 * Develop Under GPL v3.0 License 00009 * http://www.gnu.org/licenses/gpl-3.0.html 00010 * 00011 */ 00012 00013 #include "mbed.h" 00014 #include "Matrix.h" 00015 00016 /// Rows by Cols Matrix Constructor 00017 Matrix::Matrix(int Rows, int Cols): _nRows(Rows), _nCols(Cols) 00018 { 00019 _matrix.resize(_nRows); 00020 for( int i = 0; i < _nRows; i++ ) 00021 _matrix[i].resize(_nCols); 00022 00023 _pRow = 0; 00024 _pCol = 0; 00025 00026 this->Clear(); //Make all elements zero by default. 00027 } 00028 00029 00030 /// Copies one matrix into a new one 00031 Matrix::Matrix(const Matrix& base) 00032 { 00033 _nCols = base._nCols; 00034 _nRows = base._nRows; 00035 00036 _pRow = base._pRow; 00037 _pCol = base._pCol; 00038 00039 _matrix.resize(_nRows); 00040 for( int i = 0; i < _nRows; i++ ) 00041 _matrix[i].resize(_nCols); 00042 00043 for( int i = 0; i < _nRows; i++ ) 00044 for( int j = 0; j < _nCols; j++ ) 00045 _matrix[i][j] = base._matrix[i][j]; 00046 } 00047 00048 00049 /// Default Constructor 00050 Matrix::Matrix() 00051 { 00052 _nCols = 0; 00053 _nRows = 0; 00054 00055 _pRow = 0; 00056 _pCol = 0; 00057 00058 } 00059 00060 /***********************************************************************/ 00061 00062 /// Returns true if matrix is full of zeros 00063 bool Matrix::isZero() const 00064 { 00065 bool zero = false; 00066 for( int i = 0; i < this->_nRows; i++ ) 00067 for( int j = 0; j < this->_nCols; j++ ) 00068 if( _matrix[i][j] != 0 ) 00069 zero = zero || true; 00070 return !zero; 00071 } 00072 00073 00074 /// Returns true if Matrix is Single Row ot Single Column. 00075 bool Matrix::isVector() const 00076 { 00077 if( _nRows == 1 || _nCols == 1 ) 00078 return true; 00079 else 00080 return false; 00081 } 00082 00083 /*************************************************************************/ 00084 00085 /// Returns all elements in Matrix as a single Row vector. 00086 const Matrix Matrix::ToPackedVector( const Matrix& Mat ) 00087 { 00088 00089 Matrix Crushed( 1, Mat._nRows * Mat._nCols ); 00090 00091 int cont = 0; 00092 00093 for( int i = 0; i < Mat._nRows; i++ ) 00094 for( int j = 0; j < Mat._nCols; j++ ) 00095 { 00096 Crushed._matrix[0][cont] = Mat._matrix[i][j]; 00097 cont++; 00098 } 00099 00100 Crushed._pRow = Crushed._nRows; 00101 Crushed._pCol = Crushed._nCols; 00102 00103 return Crushed; 00104 } 00105 00106 00107 00108 /// To add (Insert) a Single Row to a Matrix. 00109 void Matrix::AddRow(Matrix& Mat, int index) 00110 { 00111 --index; 00112 00113 if( index > Mat._nRows + 1) 00114 { 00115 printf("\n\nERROR:\nRow out of Limits @ AddRow()\n"); 00116 00117 }else{ 00118 00119 Mat._nRows++; 00120 Mat._matrix.resize( Mat._nRows ); 00121 00122 Mat._matrix[ Mat._nRows - 1 ].resize( Mat._nCols ); 00123 00124 for( int i = Mat._nRows - 1; i > index; i-- ) 00125 for( int j = 0; j < Mat._nCols; j++ ) 00126 Mat._matrix[i][j] = Mat._matrix[i - 1][j]; 00127 00128 for( int j = 0; j < Mat._nCols; j++ ) 00129 Mat._matrix[index][j] = 0.0; 00130 } 00131 } 00132 00133 00134 void Matrix::AddRow(Matrix& Receip, const Matrix& Row, int index) 00135 { 00136 Matrix::AddRow( Receip, index ); //Make Room 00137 00138 --index; 00139 for( int i = 0; i < Receip._nCols; i++ ) 00140 Receip._matrix[index][i] = Row._matrix[0][i]; //Copy Data. 00141 00142 } 00143 00144 00145 /// To add (Insert) a single Column to a Matrix 00146 void Matrix::AddCol( Matrix& Mat, int index ) 00147 { 00148 --index; 00149 00150 if( index > Mat._nCols + 1 ) 00151 { 00152 printf("\n\nERROR:\nRow out of Limits on AddCol()\n"); 00153 00154 }else{ 00155 00156 00157 Mat._nCols++; 00158 for( int i = 0; i < Mat._nRows; i++ ) 00159 Mat._matrix[i].resize( Mat._nCols ); 00160 00161 for( int i = 0; i < Mat._nRows; i++ ) 00162 for( int j = Mat._nCols; j > index; j-- ) 00163 Mat._matrix[i][j] = Mat._matrix[i][j - 1]; 00164 00165 for( int i = 0; i < Mat._nRows; i++ ) 00166 Mat._matrix[i][index] = 0.0; 00167 00168 } 00169 } 00170 00171 00172 void Matrix::AddCol(Matrix& Receip, const Matrix& Row, int index) 00173 { 00174 Matrix::AddCol( Receip, index ); // Make Rom 00175 00176 --index; 00177 for( int i = 0; i < Receip._nRows; i++ ) 00178 Receip._matrix[i][index] = Row._matrix[i][0]; //Copy Data. 00179 } 00180 00181 00182 /// Delete a Single Column From Matrix. 00183 void Matrix::DeleteCol( Matrix& Mat, int Col) 00184 { 00185 --Col; // Because of Column zero. 00186 00187 if( Col > Mat._nCols ) 00188 { 00189 printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); 00190 00191 }else{ 00192 00193 for( int i = 0; i < Mat._nRows; i++ ) 00194 for( int j = Col; j < Mat._nCols; j++ ) 00195 Mat._matrix[i][j] = Mat._matrix[i][j+1]; 00196 00197 // If adressing last element of Column, 00198 // wich no longer exists 00199 if( Mat._pCol == Mat._nCols ) 00200 Mat._pCol--; 00201 00202 // Decrease one column 00203 Mat._nCols--; 00204 00205 //Erase last Column 00206 for( int i = 0; i < Mat._nRows; i++ ) 00207 Mat._matrix[i].reserve(Mat._nCols); 00208 00209 } 00210 } 00211 00212 00213 /// Delete a Single Row form Matrix 00214 void Matrix::DeleteRow(Matrix& Mat, int Row) 00215 { 00216 --Row; 00217 00218 if( Row > Mat._nRows ) 00219 { 00220 printf("\n\nERROR:\nColumn out of Limits @ DeleteCol()\n"); 00221 00222 }else{ 00223 00224 for( int i = Row; i < Mat._nRows - 1; i++ ) 00225 00226 for( int j = 0; j < Mat._nCols; j++ ) 00227 Mat._matrix[i][j] = Mat._matrix[i+1][j]; 00228 Mat._nRows--; 00229 Mat._matrix.resize(Mat._nRows); 00230 } 00231 } 00232 00233 /*****************************************************************************************/ 00234 00235 /// Extracts a single row form calling matrix and saves it to another matrix. 00236 const Matrix Matrix::ExportRow( const Matrix& Mat, int row ) 00237 { 00238 --row; 00239 00240 if( row > Mat._nRows ) 00241 { 00242 printf( "\n\nERROR:\nRow out of dimmensions @ GetRow\n" 00243 "Nothing Done.\n\n" ); 00244 return Matrix(); 00245 00246 }else{ 00247 00248 Matrix SingleRow( 1 , Mat._nCols ); 00249 SingleRow.Clear(); 00250 00251 for( int j = 0; j < Mat._nCols; j++ ) 00252 SingleRow._matrix[0][j] = Mat._matrix[row][j]; 00253 00254 SingleRow._pCol = SingleRow._nCols; 00255 SingleRow._pRow = 0; 00256 00257 return SingleRow; 00258 } 00259 } 00260 00261 00262 /// Extracts a single column form calling matrix and saves it to another matrix. 00263 const Matrix Matrix::ExportCol( const Matrix& Mat, int col ) 00264 { 00265 --col; 00266 00267 if( col > Mat._nCols ) 00268 { 00269 printf( "\n\nERROR:\nColumn out of dimmensions.\n" 00270 "Nothing Done.\n\n" ); 00271 return Matrix(); 00272 }else{ 00273 00274 Matrix SingleCol( Mat._nRows, 1 ); 00275 for(int i = 0; i < Mat._nRows; i++ ) 00276 SingleCol._matrix[i][0] = Mat._matrix[i][col]; 00277 00278 SingleCol._pCol = 0; 00279 SingleCol._pRow = SingleCol._nRows; 00280 00281 return SingleCol; 00282 } 00283 } 00284 00285 00286 /// Makes matrix Bigger! 00287 void Matrix::Resize( int Rows, int Cols ) 00288 { 00289 _nRows = Rows; //Decreases one because internally 00290 _nCols = Cols; // Index starts at zero. 00291 00292 _matrix.resize( _nRows ); 00293 00294 for( int i = 0; i< _nRows ; i++ ) 00295 _matrix[i].resize(_nCols); 00296 00297 _pRow = 0; // If matrix is resized the << 00298 _pCol = 0; // operator overwrites everything! 00299 } 00300 00301 00302 /// Ask user for elemnts in Matrix 00303 void Matrix::FillMatrix() 00304 { 00305 for(int i = 0; i < _nRows; i++) 00306 { 00307 for(int j = 0; j < _nCols; j++) 00308 { 00309 printf( "Position [%u][%u]: ", i, j ); 00310 float numero; 00311 scanf( "%f", &numero ); 00312 printf("%.3f ", numero); 00313 this->_matrix[i][j] = numero; 00314 } 00315 printf("\n"); 00316 } 00317 printf("\n"); 00318 00319 _pRow = _nRows; 00320 _pCol = _nCols; 00321 } 00322 00323 00324 /// Prints out Matrix. 00325 void Matrix::print() const 00326 { 00327 for( int i = 0; i < _nRows; i++ ) 00328 { 00329 for( int j = 0; j < _nCols; j++ ) 00330 { 00331 printf( "%.3f, ",_matrix[i][j] ); 00332 00333 } 00334 printf( "\n" ); 00335 } 00336 } 00337 00338 00339 /// Fills matrix with zeros. 00340 void Matrix::Clear() 00341 { 00342 for( int i = 0; i < _nRows; i++ ) 00343 for( int j = 0; j < _nCols; j++ ) 00344 _matrix[i][j] = 0; 00345 00346 _pCol = 0; // New data can be added 00347 _pRow = 0; 00348 } 00349 00350 /********************************************************************************/ 00351 00352 00353 /// Inserts a Single element in a desired Position( Index starts at [1][1] ); 00354 void Matrix::add(int Row, int Col, float number) 00355 { 00356 --Col; --Row; 00357 00358 if( Row > _nRows || Col > _nCols ) 00359 { 00360 printf("\n\nERROR:\nOut of limits of Matrix @ mat.Add()"); 00361 00362 }else{ 00363 _matrix[Row][Col] = number; 00364 } 00365 } 00366 00367 00368 /// Adds all elements in matrix and returns the answer. 00369 float Matrix::sum() const 00370 { 00371 float total = 0; 00372 00373 for( int i = 0; i < _nRows; i++ ) 00374 for( int j = 0; j < _nCols; j++ ) 00375 total += _matrix[i][j]; 00376 return total; 00377 } 00378 00379 00380 /// Returns the specified element. Index Starts at [1][1]. 00381 float Matrix::getNumber( int Row, int Col ) const 00382 { return this->_matrix[Row -1][Col - 1]; } 00383 00384 /// Returns the number of Rows in Matrix. 00385 int Matrix::getRows() const{ return this->_nRows; } 00386 00387 00388 /// Returns the number of Columns in Matrix. 00389 int Matrix::getCols() const{ return this->_nCols; }
Generated on Fri Jul 29 2022 06:49:56 by 1.7.2