Follow this link to skip to the main content

Algorithms
[Math Package]

Collaboration diagram for Algorithms:


Classes

class  claraty::Matrix_LU< T >
class  claraty::N_1D_Function_Solver
class  claraty::ND_Function_Minimizer

Functions

template<class T>
Matrix< M_FLOAT > claraty::inverse (const Matrix< T > &tm)
Vector< int > claraty::LU_decompose (Matrix< M_FLOAT > &a, int &sgn)
template<class T>
void claraty::matrix_cholesky (const Matrix< T > &A, Matrix< T > &L)

Function Documentation

template<class T>
Matrix< M_FLOAT > claraty::inverse ( const Matrix< T > &  tm  ) 

Matrix inverse.

Definition at line 58 of file matrix_inverse.h.

References claraty::N_2D_Array< T >::get_num_of_cols(), claraty::Matrix_LU< T >::inverse(), claraty::N_2D_Array< T >::is_square(), and claraty::to_float().

Referenced by claraty::Wheel_Model::get_ground_contact(), claraty::ME_Body::get_relative_transform(), claraty::Frame::get_relative_transform(), and claraty::inverse().

00059 {
00060   Matrix<M_FLOAT> fm =  to_float(m);
00061   int N = fm.get_num_of_cols();
00062   Matrix<M_FLOAT> im (N, N);
00063   int sgn;
00064 
00065   if (!m.is_square()) {
00066     std::cerr << "matrix error: cannot invert a non-square martrix"
00067               << std::endl;
00068     return fm;
00069   }
00070   else {
00071     // Perform the LU decomposition of the matrix.
00072     Matrix_LU<M_FLOAT> lu_matrix(fm);
00073     if (!lu_matrix.inverse(im)) {
00074       std::cerr << "matrix error: martrix inversion failed"
00075                 << std::endl;
00076       return fm;
00077     }
00078     
00079   }
00080   return im;
00081 }       

Here is the call graph for this function:

Vector<int> claraty::LU_decompose ( Matrix< M_FLOAT > &  a,
int &  sgn 
)

template<class T>
void claraty::matrix_cholesky ( const Matrix< T > &  A,
Matrix< T > &  L 
)

Cholesky factorization - computes the cholesky factor for a symmetric positive definite matrix A. The matrix must be square, symmetric, and positive definite. It's trivial to check that the matrix is square. It's O(n^2) times memory access, subtraction, absolute value, and comparison to check that the matrix is more or less symmetric. Checking whether the matrix is positive definite can only be done as part of the algorithm by looking for dangerously small or negative pivots.

Parameters:
[in] A Symmetric postive definite matrix A
[out] L Resultant matrix.

Definition at line 62 of file matrix_cholesky.h.

References claraty::N_2D_Array< T >::get_num_of_cols(), claraty::N_2D_Array< T >::get_num_of_rows(), and claraty::sum().

Referenced by claraty::matrix_solve_symposdef().

00063 {
00064   int i, j, k; // loop counters
00065   double sum;  // accumulator
00066   
00067   // Get matrix size.  Number of rows and columns should be the same
00068   int nrows = A.get_num_of_rows();
00069 
00070   // Check that input matrix is square
00071   if (A.get_num_of_cols()!=nrows){
00072     //print an error message about non-square matrix
00073     std::cerr << "matrix_cholesky: Come on, this matrix isn't even square!" << std::endl;
00074   }
00075 
00076   // Check that the input matrix is symmetric
00077   for (k=0; k<nrows; k++){
00078     for (j=0; j<k; j++){
00079       if (fabs(A(j,k)-A(k,j))>1e-14){
00080         // Print error message about non-symmetric matrix
00081         std::cerr << "matrix_cholesky: matrix is not symmetric" << std::endl;
00082       }
00083     }
00084   }
00085   
00086   // This only needs to be a size check or a resize for the matrix
00087   // that holds the factorization.  For now I'm using "=" because I
00088   // know that this will do what I need.
00089   L = A;
00090   
00091   // Compute lower triangular Cholesky factor (my algorithm)
00092   for (k=0; k<nrows; k++){
00093     // First compute diagonal element for this row
00094     sum = A(k,k);
00095     for (j=0; j<k; j++){
00096       sum = sum - L(k,j) * L(k,j);
00097     }
00098     if (sum<0)
00099       // If this is <0, we can't even take the square root.  This part
00100       // should throw an exception
00101       std::cerr << "matrix_cholesky: matrix is not positive definite" << std::endl;
00102     L(k,k) = sqrt( sum );
00103     if (L(k,k) < 1e-14){
00104       // This is a dangerously small pivot, issue a warning
00105       std::cerr << "matrix_cholesky: matrix is ill conditioned" << std::endl;
00106     }
00107 
00108     // Now fill in the rest of the column
00109     for (i=(k+1); i<nrows; i++){
00110       sum = A(k,i);
00111       for (j=0; j<k; j++){
00112         sum = sum - L(i,j)*L(k,j);
00113       }
00114       L(i,k) = sum / L(k,k);
00115     }
00116   }
00117   
00118   // Finally, ensure that the superdiagonal triangle is zeroed out,
00119   // since the result is lower triangular:
00120   for (i=0; i<nrows; i++){
00121     for (j=i+1; j<nrows; j++){
00122       L(i,j) = 0.0;
00123     }
00124   }
00125 }

Here is the call graph for this function: