#ifndef ALGORITHMS_H #define ALGORITHMS_H /* * NICE-Core - efficient algebra and computer vision methods * - libbasicvector - A simple vector library * See file License for license information. */ #include "core/vector/ippwrapper.h" #include "core/vector/VectorT.h" #include "core/vector/MatrixT.h" #include "core/vector/RowMatrixT.h" #include namespace NICE { /** * @brief Calculate mean of VectorT \c v * @param v vector * @return mean of vector */ template inline T mean(const VectorT &v); /** * @brief Calculate determinate of MatrixT \c A * @param A matrix * @return determinate of matrix A */ template inline T det(const MatrixT &A); /** * @brief Compute cholesky decomposition of MatrixT \c A (A = G*G^T), with * G being an lower triangle matrix. * If the flag \c resetUpperTriangle is set to false, only the lower triangle of G is set, * without setting the upper triangle to zero ! * @param A matrix * @param G square root of A */ template void choleskyDecomp ( const MatrixT & A, MatrixT & G, bool resetUpperTriangle = true ); /** * @brief Compute the inverse matrix of a matrix given by its square root \c G (lower triangle) * @param G square root of A * @param Ainv matrix */ template void choleskyInvert ( const MatrixT & G, MatrixT & Ainv ); /** * @brief Compute cholesky decomposition of MatrixT \c A (A = G*G^T), with * G being an lower triangle matrix. * If the flag \c resetUpperTriangle is set to false, only the lower triangle of G is set, * without setting the upper triangle to zero ! * This method uses Lapack (LinAl) if available and should be used for large matrices (e.g. dim=1000). * @param A matrix * @param G square root of A */ void choleskyDecompLargeScale ( const Matrix & A, Matrix & G, bool resetUpperTriangle = true ); /** * @brief Compute the inverse matrix of a matrix given by its square root \c G (lower triangle) * This method uses Lapack (LinAl) if available and should be used for large matrices (e.g. dim=1000). * @param G square root of A * @param Ainv matrix */ void choleskyInvertLargeScale ( const Matrix & G, Matrix & Ainv ); /** * @brief Solves a linear equation system using the cholesky decomposition * of the coefficient matrix. * @param G square root of A (lower triangle) * @param b right hand side of the equation system * @param x solution of A x = b */ template void choleskySolve ( const MatrixT & G, const VectorT & b, VectorT & x ); /** * @brief Solves a linear equation system using the cholesky decomposition * of the coefficient matrix. * This method uses Lapack (LinAl) if available and should be used for large matrices (e.g. dim=1000). * @param G square root of A (lower triangle) * @param b right hand side of the equation system * @param x solution of A x = b */ void choleskySolveLargeScale ( const Matrix & G, const Vector & b, Vector & x ); /** * @brief Solves multiple linear equation systems using the cholesky decomposition * of the coefficient matrix. * This method uses Lapack (LinAl) if available and should be used for large matrices (e.g. dim=1000). * @param G square root of A (lower triangle) * @param B right hand side of the equation system AND solution of the system */ void choleskySolveMatrixLargeScale ( const Matrix & G, Matrix & B ); /** * @brief Solves multiple linear equation systems with a triangular coefficient matrix * @param G coefficient matrix (lower triangular!!) * @param B right hand side of the equation system AND solution of the system * @param transposedMatrix if set to true the system G^T B = X is solved instead of G B = X */ void triangleSolveMatrix ( const Matrix & G, Matrix & B, bool transposedMatrix = false ); /** * @brief Solves a linear equation system with a triangular coefficient matrix * @param G coefficient matrix (lower triangular!!) * @param b right hand side of the equation system * @param x solution of the system * @param transposedMatrix if set to true the system G^T B = X is solved instead of G B = X */ void triangleSolve ( const Matrix & G, const Vector & x, Vector & b, bool transposedMatrix = false ); /** * @brief Compute the determinant of a triangular matrix * @param G matrix * @param ignoreZero ignore all zero elements on the diagonal */ template double triangleMatrixDet ( const MatrixT & G, bool ignoreZero = false ); /** * @brief Compute the logarithm of the determinant of a triangular matrix. * Computing the logarithm is often numerically more robust. * @param G matrix * @param ignoreZero ignore all zero elements on the diagonal */ template double triangleMatrixLogDet ( const MatrixT & G, bool ignoreZero = false ); /** * @brief Calculate determinate of MatrixT \c A * @param A matrix * @return determinate of matrix A */ template inline T det(const RowMatrixT &A); /** * @brief Calculate natural logarithm of VectorT \c v in place. * @param v source and destination of vector */ template inline void lnIP(VectorT &v); /** * @brief Calculate natural logarithm of VectorT \c v. * @param v source vector * @param buffer pointer to destination vector * @return pointer to VectorT with logarithmic values. */ template inline VectorT *ln(const VectorT &v, VectorT *buffer=NULL); /** * @brief Create a 1D Gauss function * @param sigma if sigma>0 determine Gauss function with standard derivation \c sigma * @param buffer pointer to destination vector (size of detination vector is automatically calculated if buffer pointer is NULL or size of buffer pointer is 0) * @return pointer to VectorT with Gauss function */ template inline VectorT *createGaussFunc(float sigma, VectorT *buffer=NULL); /** * @brief solve linear equation given by the matrix A and the right-vector b: A*x = b. * @param A matrix * @param b vector * @param x solution vector */ template inline void solveLinearEquationQR(const MatrixT &A, const VectorT &b, VectorT &x); /** * @brief solve linear equation given by the matrix A and the right-vector b: A*x = b. * @param A matrix * @param b vector * @param x solution vector */ template inline void solveLinearEquationQR(const RowMatrixT &A, const VectorT &b, VectorT &x); /** * @brief solve Minimum Description Length based on greedy algorithm * @param C quadratic matrix * @param h solution vector */ template inline void solveMDLgreedy(const MatrixT &C, VectorT &h); /** * Invert a square matrix. */ template MatrixT invert(const MatrixT& w); /** * Invert a 3x3 upper triangle matrix (inplace). * @note There is no check if the matrix is invertable. */ template void invert3x3UpperTriangle(MatrixT& w); /** * Invert a 3x3 lower triangle matrix (inplace). * @note There is no check if the matrix is invertable. */ template void invert3x3LowerTriangle(MatrixT& w); /** * Compute the angle (in radian, -M_PI to M_PI) between two vectors. * If one of the vectors is zero, the result will be M_PI/2.0. */ inline double angleBetweenVectors( const Vector& v1, const Vector& v2) { const double denominator = v1.normL2() * v2.normL2(); // if one of the vectors is zero, they are orthogonal -> return PI/2 if (isZero(denominator, 1e-16)) { return M_PI / 2.0; } const double arg = v1.scalarProduct(v2) / denominator; // allow for values slightly larger than 1.0 or slightly smaller than -1.0 if (arg >= 1.0 && arg < 1.0 + 1e-10) { return 0.0; } else if (arg <= -1.0 && arg > -1.0 - 1e-10) { return -M_PI; } else { return acos(arg); } } /** * Compute the angle (in radian, -M_PI/2 to M_PI/2) between * two vectors ignoring the direction. */ inline double angleBetweenVectorsIgnoringDirection( const Vector& v1, const Vector& v2) { double a = fabs(v1.scalarProduct(v2) / (v1.normL2() * v2.normL2())); if (a > 1.0) a = 1.0; return acos(a); } /** * Compute the absolute angle (in radian, 0 to M_PI/2) between * two vectors ignoring the direction. */ inline double absoluteAngleBetweenVectorsIgnoringDirection( const Vector& v1, const Vector& v2) { return fabs(angleBetweenVectorsIgnoringDirection(v1, v2)); } } // namespace //#ifdef __GNUC__ #include //#endif #endif // ALGORITHMS_H