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
Kalman/MatrixMath/MatrixMath.cpp@0:349dc9b0984f, 2012-03-25 (annotated)
- Committer:
- narshu
- Date:
- Sun Mar 25 13:39:11 2012 +0000
- Revision:
- 0:349dc9b0984f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
narshu | 0:349dc9b0984f | 1 | /** |
narshu | 0:349dc9b0984f | 2 | * @brief Still under work version 0.2 |
narshu | 0:349dc9b0984f | 3 | * @file MatrixMath.cpp |
narshu | 0:349dc9b0984f | 4 | * @author Erneseto Palacios |
narshu | 0:349dc9b0984f | 5 | * |
narshu | 0:349dc9b0984f | 6 | * Develop Under GPL v3.0 License |
narshu | 0:349dc9b0984f | 7 | * http://www.gnu.org/licenses/gpl-3.0.html |
narshu | 0:349dc9b0984f | 8 | */ |
narshu | 0:349dc9b0984f | 9 | |
narshu | 0:349dc9b0984f | 10 | #include "mbed.h" |
narshu | 0:349dc9b0984f | 11 | #include "MatrixMath.h" |
narshu | 0:349dc9b0984f | 12 | |
narshu | 0:349dc9b0984f | 13 | ///Transpose matrix |
narshu | 0:349dc9b0984f | 14 | Matrix MatrixMath::Transpose(const Matrix& Mat) |
narshu | 0:349dc9b0984f | 15 | { |
narshu | 0:349dc9b0984f | 16 | Matrix result( Mat._nCols, Mat._nRows ); //Transpose Matrix |
narshu | 0:349dc9b0984f | 17 | |
narshu | 0:349dc9b0984f | 18 | for( int i = 0; i < result._nRows; i++ ) |
narshu | 0:349dc9b0984f | 19 | for( int j = 0; j < result._nCols; j++ ) |
narshu | 0:349dc9b0984f | 20 | result._matrix[i][j] = Mat._matrix[j][i]; |
narshu | 0:349dc9b0984f | 21 | |
narshu | 0:349dc9b0984f | 22 | return result; |
narshu | 0:349dc9b0984f | 23 | } |
narshu | 0:349dc9b0984f | 24 | |
narshu | 0:349dc9b0984f | 25 | Matrix MatrixMath::Inv(const Matrix& Mat) |
narshu | 0:349dc9b0984f | 26 | { |
narshu | 0:349dc9b0984f | 27 | if( Mat._nRows == Mat._nCols ) |
narshu | 0:349dc9b0984f | 28 | { |
narshu | 0:349dc9b0984f | 29 | if( Mat._nRows == 2 ) // 2x2 Matrices |
narshu | 0:349dc9b0984f | 30 | { |
narshu | 0:349dc9b0984f | 31 | float det = MatrixMath::det( Mat ); |
narshu | 0:349dc9b0984f | 32 | if( det != 0 ) |
narshu | 0:349dc9b0984f | 33 | { |
narshu | 0:349dc9b0984f | 34 | Matrix Inv(2,2); |
narshu | 0:349dc9b0984f | 35 | Inv._matrix[0][0] = Mat._matrix[1][1]; |
narshu | 0:349dc9b0984f | 36 | Inv._matrix[1][0] = -Mat._matrix[1][0]; |
narshu | 0:349dc9b0984f | 37 | Inv._matrix[0][1] = -Mat._matrix[0][1]; |
narshu | 0:349dc9b0984f | 38 | Inv._matrix[1][1] = Mat._matrix[0][0] ; |
narshu | 0:349dc9b0984f | 39 | |
narshu | 0:349dc9b0984f | 40 | Inv *= 1/det; |
narshu | 0:349dc9b0984f | 41 | |
narshu | 0:349dc9b0984f | 42 | return Inv; |
narshu | 0:349dc9b0984f | 43 | |
narshu | 0:349dc9b0984f | 44 | }else{ |
narshu | 0:349dc9b0984f | 45 | printf( "\n\nWANRING: same matrix returned"); |
narshu | 0:349dc9b0984f | 46 | printf( "\nSingular Matrix, cannot perform Invert @matrix " ); |
narshu | 0:349dc9b0984f | 47 | // Mat.print(); |
narshu | 0:349dc9b0984f | 48 | printf( "\n _____________\n" ); |
narshu | 0:349dc9b0984f | 49 | |
narshu | 0:349dc9b0984f | 50 | return Mat; |
narshu | 0:349dc9b0984f | 51 | } |
narshu | 0:349dc9b0984f | 52 | |
narshu | 0:349dc9b0984f | 53 | }else{ // nxn Matrices |
narshu | 0:349dc9b0984f | 54 | |
narshu | 0:349dc9b0984f | 55 | float det = MatrixMath::det( Mat ); |
narshu | 0:349dc9b0984f | 56 | if( det!= 0 ) |
narshu | 0:349dc9b0984f | 57 | { |
narshu | 0:349dc9b0984f | 58 | Matrix Inv( Mat ); // |
narshu | 0:349dc9b0984f | 59 | Matrix SubMat; |
narshu | 0:349dc9b0984f | 60 | |
narshu | 0:349dc9b0984f | 61 | // Matrix of Co-factors |
narshu | 0:349dc9b0984f | 62 | for( int i = 0; i < Mat._nRows; i++ ) |
narshu | 0:349dc9b0984f | 63 | for( int j = 0; j < Mat._nCols; j++ ) |
narshu | 0:349dc9b0984f | 64 | { |
narshu | 0:349dc9b0984f | 65 | SubMat = Mat ; |
narshu | 0:349dc9b0984f | 66 | |
narshu | 0:349dc9b0984f | 67 | Matrix::DeleteRow( SubMat, i+1 ); |
narshu | 0:349dc9b0984f | 68 | Matrix::DeleteCol( SubMat, j+1 ); |
narshu | 0:349dc9b0984f | 69 | |
narshu | 0:349dc9b0984f | 70 | if( (i+j)%2 == 0 ) |
narshu | 0:349dc9b0984f | 71 | Inv._matrix[i][j] = MatrixMath::det( SubMat ); |
narshu | 0:349dc9b0984f | 72 | else |
narshu | 0:349dc9b0984f | 73 | Inv._matrix[i][j] = -MatrixMath::det( SubMat ); |
narshu | 0:349dc9b0984f | 74 | } |
narshu | 0:349dc9b0984f | 75 | |
narshu | 0:349dc9b0984f | 76 | // Adjugate Matrix |
narshu | 0:349dc9b0984f | 77 | Inv = MatrixMath::Transpose( Inv ); |
narshu | 0:349dc9b0984f | 78 | |
narshu | 0:349dc9b0984f | 79 | // Inverse Matrix |
narshu | 0:349dc9b0984f | 80 | Inv = 1/det * Inv; |
narshu | 0:349dc9b0984f | 81 | |
narshu | 0:349dc9b0984f | 82 | return Inv; |
narshu | 0:349dc9b0984f | 83 | |
narshu | 0:349dc9b0984f | 84 | }else{ |
narshu | 0:349dc9b0984f | 85 | printf( "\n\nWANRING: same matrix returned"); |
narshu | 0:349dc9b0984f | 86 | printf( "\nSingular Matrix, cannot perform Invert @matrix " ); |
narshu | 0:349dc9b0984f | 87 | // Mat.print(); |
narshu | 0:349dc9b0984f | 88 | printf( "\n _____________\n" ); |
narshu | 0:349dc9b0984f | 89 | |
narshu | 0:349dc9b0984f | 90 | return Mat; |
narshu | 0:349dc9b0984f | 91 | } |
narshu | 0:349dc9b0984f | 92 | |
narshu | 0:349dc9b0984f | 93 | } |
narshu | 0:349dc9b0984f | 94 | |
narshu | 0:349dc9b0984f | 95 | }else{ |
narshu | 0:349dc9b0984f | 96 | printf( "\n\nERROR:\nMust be square Matrix @ MatrixMath::Determinant " ); |
narshu | 0:349dc9b0984f | 97 | |
narshu | 0:349dc9b0984f | 98 | return Mat; |
narshu | 0:349dc9b0984f | 99 | } |
narshu | 0:349dc9b0984f | 100 | } |
narshu | 0:349dc9b0984f | 101 | |
narshu | 0:349dc9b0984f | 102 | float MatrixMath::det(const Matrix& Mat) |
narshu | 0:349dc9b0984f | 103 | { |
narshu | 0:349dc9b0984f | 104 | if( Mat._nRows == Mat._nCols ) |
narshu | 0:349dc9b0984f | 105 | { |
narshu | 0:349dc9b0984f | 106 | |
narshu | 0:349dc9b0984f | 107 | if( Mat._nRows == 2 ) // 2x2 Matrix |
narshu | 0:349dc9b0984f | 108 | { |
narshu | 0:349dc9b0984f | 109 | float det; |
narshu | 0:349dc9b0984f | 110 | det = Mat._matrix[0][0] * Mat._matrix[1][1] - |
narshu | 0:349dc9b0984f | 111 | Mat._matrix[1][0] * Mat._matrix[0][1]; |
narshu | 0:349dc9b0984f | 112 | return det; |
narshu | 0:349dc9b0984f | 113 | } |
narshu | 0:349dc9b0984f | 114 | else if( Mat._nRows == 3 ) // 3x3 Matrix |
narshu | 0:349dc9b0984f | 115 | { |
narshu | 0:349dc9b0984f | 116 | float det; |
narshu | 0:349dc9b0984f | 117 | MatrixMath dummy; |
narshu | 0:349dc9b0984f | 118 | |
narshu | 0:349dc9b0984f | 119 | det = dummy.Det3x3( Mat ); |
narshu | 0:349dc9b0984f | 120 | return det; |
narshu | 0:349dc9b0984f | 121 | |
narshu | 0:349dc9b0984f | 122 | } else { |
narshu | 0:349dc9b0984f | 123 | |
narshu | 0:349dc9b0984f | 124 | float part1= 0; |
narshu | 0:349dc9b0984f | 125 | float part2= 0; |
narshu | 0:349dc9b0984f | 126 | |
narshu | 0:349dc9b0984f | 127 | //Find +/- on First Row |
narshu | 0:349dc9b0984f | 128 | for( int i = 0; i < Mat._nCols; i++) |
narshu | 0:349dc9b0984f | 129 | { |
narshu | 0:349dc9b0984f | 130 | Matrix reduced( Mat ); // Copy Original Matrix |
narshu | 0:349dc9b0984f | 131 | Matrix::DeleteRow( reduced, 1); // Delete First Row |
narshu | 0:349dc9b0984f | 132 | |
narshu | 0:349dc9b0984f | 133 | if( i%2 == 0 ) //Odd Rows |
narshu | 0:349dc9b0984f | 134 | { |
narshu | 0:349dc9b0984f | 135 | |
narshu | 0:349dc9b0984f | 136 | Matrix::DeleteCol( reduced, i+1); |
narshu | 0:349dc9b0984f | 137 | part1 += Mat._matrix[0][i] * MatrixMath::det(reduced); |
narshu | 0:349dc9b0984f | 138 | } |
narshu | 0:349dc9b0984f | 139 | else // Even Rows |
narshu | 0:349dc9b0984f | 140 | { |
narshu | 0:349dc9b0984f | 141 | Matrix::DeleteCol( reduced, i+1); |
narshu | 0:349dc9b0984f | 142 | part2 += Mat._matrix[0][i] * MatrixMath::det(reduced); |
narshu | 0:349dc9b0984f | 143 | } |
narshu | 0:349dc9b0984f | 144 | } |
narshu | 0:349dc9b0984f | 145 | return part1 - part2; // |
narshu | 0:349dc9b0984f | 146 | } |
narshu | 0:349dc9b0984f | 147 | |
narshu | 0:349dc9b0984f | 148 | }else{ |
narshu | 0:349dc9b0984f | 149 | printf("\n\nERROR:\nMatrix must be square Matrix @ MatrixMath::Det"); |
narshu | 0:349dc9b0984f | 150 | return 0; |
narshu | 0:349dc9b0984f | 151 | } |
narshu | 0:349dc9b0984f | 152 | } |
narshu | 0:349dc9b0984f | 153 | |
narshu | 0:349dc9b0984f | 154 | |
narshu | 0:349dc9b0984f | 155 | /************************************/ |
narshu | 0:349dc9b0984f | 156 | |
narshu | 0:349dc9b0984f | 157 | //Private Functions |
narshu | 0:349dc9b0984f | 158 | |
narshu | 0:349dc9b0984f | 159 | /**@brief |
narshu | 0:349dc9b0984f | 160 | * Expands the Matrix adding first and second column to the Matrix then |
narshu | 0:349dc9b0984f | 161 | * performs the Algorithm. |
narshu | 0:349dc9b0984f | 162 | * @param Mat |
narshu | 0:349dc9b0984f | 163 | * @return Determinant |
narshu | 0:349dc9b0984f | 164 | */ |
narshu | 0:349dc9b0984f | 165 | float MatrixMath::Det3x3(const Matrix& Mat) |
narshu | 0:349dc9b0984f | 166 | { |
narshu | 0:349dc9b0984f | 167 | Matrix D( Mat ); //Copy Initial matrix |
narshu | 0:349dc9b0984f | 168 | |
narshu | 0:349dc9b0984f | 169 | Matrix::AddCol(D, Matrix::ExportCol(Mat, 1), 4); //Repeat First Column |
narshu | 0:349dc9b0984f | 170 | Matrix::AddCol(D, Matrix::ExportCol(Mat, 2), 5); //Repeat Second Column |
narshu | 0:349dc9b0984f | 171 | |
narshu | 0:349dc9b0984f | 172 | float det = 0; |
narshu | 0:349dc9b0984f | 173 | for( int i = 0; i < 3; i++ ) |
narshu | 0:349dc9b0984f | 174 | det += D._matrix[0][i] * D._matrix[1][1+i] * D._matrix[2][2+i] |
narshu | 0:349dc9b0984f | 175 | - D._matrix[0][2+i] * D._matrix[1][1+i] * D._matrix[2][i]; |
narshu | 0:349dc9b0984f | 176 | |
narshu | 0:349dc9b0984f | 177 | return det; |
narshu | 0:349dc9b0984f | 178 | } |