00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef __OPENKN_MATH__DETERMINANT_HPP__
00027 #define __OPENKN_MATH__DETERMINANT_HPP__
00028
00029
00030
00031
00032 #include <algorithm>
00033
00034
00035
00036
00037 #include "MathException.hpp"
00038 #include "Matrix.hpp"
00039 #include "Matrix3x3.hpp"
00040
00041
00042
00043
00044
00045 namespace kn{
00046
00053 template <class T>
00054 T determinant3x3(Matrix<T> &mat){
00055 if(mat.rows() != 3 || mat.columns() != 3)
00056 throw MathException("the input matrix is not a 3x3 matrix");
00057
00058 return mat[0][0]*mat[1][1]*mat[2][2] -
00059 mat[0][0]*mat[2][1]*mat[1][2] -
00060 mat[0][1]*mat[1][0]*mat[2][2] +
00061 mat[0][1]*mat[2][0]*mat[1][2] +
00062 mat[0][2]*mat[1][0]*mat[2][1] -
00063 mat[0][2]*mat[2][0]*mat[1][1];
00064 }
00065
00066
00072 template <class T>
00073 T determinant3x3(Matrix3x3<T> &mat){
00074
00075 return mat[0][0]*mat[1][1]*mat[2][2] -
00076 mat[0][0]*mat[2][1]*mat[1][2] -
00077 mat[0][1]*mat[1][0]*mat[2][2] +
00078 mat[0][1]*mat[2][0]*mat[1][2] +
00079 mat[0][2]*mat[1][0]*mat[2][1] -
00080 mat[0][2]*mat[2][0]*mat[1][1];
00081 }
00082
00083
00090 template <class T>
00091 T determinant(const Matrix<T> & A_){
00092
00093 const double determinantZero = 1.0e-10;
00094
00095
00096 if(A_.columns() != A_.rows())
00097 throw MathException("The input matrix is not a square matrix" ,"determinant");
00098
00099
00100 Matrix<T> A(A_);
00101
00102
00103 T permutation = T(1);
00104
00105
00106 unsigned int nbIteration = 0;
00107 while(nbIteration < A.rows()-1){
00108
00109
00110 T maxValue;
00111 unsigned int maxValueRow,maxValueCol;
00112 determinantFindPivot(A,nbIteration,maxValueRow,maxValueCol,maxValue);
00113
00114
00115 if(fabs(maxValue) < determinantZero)
00116 return T(0);
00117
00118
00119 if(maxValueRow != nbIteration){
00120
00121 std::swap_ranges(&(A[maxValueRow][nbIteration]),A[maxValueRow]+A.columns(),&(A[nbIteration][nbIteration]));
00122
00123 permutation *= T(-1);
00124 }
00125
00126
00127 if(maxValueCol != nbIteration){
00128
00129 A.swapColumns(maxValueCol,nbIteration);
00130
00131 permutation *= T(-1);
00132 }
00133
00134
00135 determinantRowsElimination(A,nbIteration);
00136
00137
00138 ++nbIteration;
00139 }
00140
00141
00142 T determinant = T(1);
00143 for(unsigned int i=0; i<A.rows(); ++i)
00144 determinant = determinant * A[i][i];
00145
00146 return determinant * permutation;
00147 }
00148
00149
00159 template <class T>
00160 void determinantFindPivot(const Matrix<T> & A, const unsigned int nbIteration,
00161 unsigned int & maxValueRow, unsigned int & maxValueCol, T & maxValue){
00162
00163 maxValueRow = maxValueCol = nbIteration;
00164 maxValue = A[nbIteration][nbIteration];
00165
00166 for(unsigned int i = nbIteration ; i < A.rows(); ++i)
00167 for(unsigned int j = nbIteration ; j < A.columns(); ++j) {
00168 T tmp = T(fabs((A[i][j])));
00169 if((tmp > maxValue)){
00170 maxValueRow = i;
00171 maxValueCol = j;
00172 maxValue = tmp;
00173 }
00174 }
00175 }
00177
00178
00185 template <class T>
00186 void determinantRowsElimination(Matrix<T> & A, const unsigned int nbIteration){
00187
00188 for(unsigned int i = nbIteration+1; i < A.rows(); ++i){
00189 T tmp = T(A[i][nbIteration] / A[nbIteration][nbIteration]);
00190
00191
00192 A[i][nbIteration] = T(0);
00193 for(unsigned int j = nbIteration+1; j < A.columns(); ++j){
00194 A[i][j] -= tmp * A[nbIteration][j];
00195 }
00196 }
00197 }
00199
00200
00207 inline int determinant(const Matrix<int> & A_){
00208 throw MathException("invalid input matrix type, convert your matrix in float or double" ,"determinant");
00209 }
00211
00212
00213
00214
00215
00216 }
00217
00218
00219
00220
00221 #endif
00222
00223
00224
00225
00226
00227
00228