Algorithms.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #ifndef ALGORITHMS_H
  2. #define ALGORITHMS_H
  3. /*
  4. * NICE-Core - efficient algebra and computer vision methods
  5. * - libbasicvector - A simple vector library
  6. * See file License for license information.
  7. */
  8. #include "core/vector/ippwrapper.h"
  9. #include "core/vector/VectorT.h"
  10. #include "core/vector/MatrixT.h"
  11. #include "core/vector/RowMatrixT.h"
  12. #include <cmath>
  13. namespace NICE {
  14. /**
  15. * @brief Calculate mean of VectorT \c v
  16. * @param v vector
  17. * @return mean of vector
  18. */
  19. template<class T>
  20. inline T mean(const VectorT<T> &v);
  21. /**
  22. * @brief Calculate determinate of MatrixT \c A
  23. * @param A matrix
  24. * @return determinate of matrix A
  25. */
  26. template<class T>
  27. inline T det(const MatrixT<T> &A);
  28. /**
  29. * @brief Compute cholesky decomposition of MatrixT \c A (A = G*G^T), with
  30. * G being an lower triangle matrix.
  31. * If the flag \c resetUpperTriangle is set to false, only the lower triangle of G is set,
  32. * without setting the upper triangle to zero !
  33. * @param A matrix
  34. * @param G square root of A
  35. */
  36. template<class T>
  37. void choleskyDecomp ( const MatrixT<T> & A, MatrixT<T> & G, bool resetUpperTriangle = true );
  38. /**
  39. * @brief Compute the inverse matrix of a matrix given by its square root \c G (lower triangle)
  40. * @param G square root of A
  41. * @param Ainv matrix
  42. */
  43. template<class T>
  44. void choleskyInvert ( const MatrixT<T> & G, MatrixT<T> & Ainv );
  45. /**
  46. * @brief Compute cholesky decomposition of MatrixT \c A (A = G*G^T), with
  47. * G being an lower triangle matrix.
  48. * If the flag \c resetUpperTriangle is set to false, only the lower triangle of G is set,
  49. * without setting the upper triangle to zero !
  50. * This method uses Lapack (LinAl) if available and should be used for large matrices (e.g. dim=1000).
  51. * @param A matrix
  52. * @param G square root of A
  53. */
  54. void choleskyDecompLargeScale ( const Matrix & A, Matrix & G, bool resetUpperTriangle = true );
  55. /**
  56. * @brief Compute the inverse matrix of a matrix given by its square root \c G (lower triangle)
  57. * This method uses Lapack (LinAl) if available and should be used for large matrices (e.g. dim=1000).
  58. * @param G square root of A
  59. * @param Ainv matrix
  60. */
  61. void choleskyInvertLargeScale ( const Matrix & G, Matrix & Ainv );
  62. /**
  63. * @brief Solves a linear equation system using the cholesky decomposition
  64. * of the coefficient matrix.
  65. * @param G square root of A (lower triangle)
  66. * @param b right hand side of the equation system
  67. * @param x solution of A x = b
  68. */
  69. template<class T>
  70. void choleskySolve ( const MatrixT<T> & G, const VectorT<T> & b, VectorT<T> & x );
  71. /**
  72. * @brief Solves a linear equation system using the cholesky decomposition
  73. * of the coefficient matrix.
  74. * This method uses Lapack (LinAl) if available and should be used for large matrices (e.g. dim=1000).
  75. * @param G square root of A (lower triangle)
  76. * @param b right hand side of the equation system
  77. * @param x solution of A x = b
  78. */
  79. void choleskySolveLargeScale ( const Matrix & G, const Vector & b, Vector & x );
  80. /**
  81. * @brief Solves multiple linear equation systems using the cholesky decomposition
  82. * of the coefficient matrix.
  83. * This method uses Lapack (LinAl) if available and should be used for large matrices (e.g. dim=1000).
  84. * @param G square root of A (lower triangle)
  85. * @param B right hand side of the equation system AND solution of the system
  86. */
  87. void choleskySolveMatrixLargeScale ( const Matrix & G, Matrix & B );
  88. /**
  89. * @brief Solves multiple linear equation systems with a triangular coefficient matrix
  90. * @param G coefficient matrix (lower triangular!!)
  91. * @param B right hand side of the equation system AND solution of the system
  92. * @param transposedMatrix if set to true the system G^T B = X is solved instead of G B = X
  93. */
  94. void triangleSolveMatrix ( const Matrix & G, Matrix & B, bool transposedMatrix = false );
  95. /**
  96. * @brief Solves a linear equation system with a triangular coefficient matrix
  97. * @param G coefficient matrix (lower triangular!!)
  98. * @param b right hand side of the equation system
  99. * @param x solution of the system
  100. * @param transposedMatrix if set to true the system G^T B = X is solved instead of G B = X
  101. */
  102. void triangleSolve ( const Matrix & G, const Vector & x, Vector & b, bool transposedMatrix = false );
  103. /**
  104. * @brief Compute the determinant of a triangular matrix
  105. * @param G matrix
  106. * @param ignoreZero ignore all zero elements on the diagonal
  107. */
  108. template<class T>
  109. double triangleMatrixDet ( const MatrixT<T> & G, bool ignoreZero = false );
  110. /**
  111. * @brief Compute the logarithm of the determinant of a triangular matrix.
  112. * Computing the logarithm is often numerically more robust.
  113. * @param G matrix
  114. * @param ignoreZero ignore all zero elements on the diagonal
  115. */
  116. template<class T>
  117. double triangleMatrixLogDet ( const MatrixT<T> & G, bool ignoreZero = false );
  118. /**
  119. * @brief Calculate determinate of MatrixT \c A
  120. * @param A matrix
  121. * @return determinate of matrix A
  122. */
  123. template<class T>
  124. inline T det(const RowMatrixT<T> &A);
  125. /**
  126. * @brief Calculate natural logarithm of VectorT \c v in place.
  127. * @param v source and destination of vector
  128. */
  129. template<class T>
  130. inline void lnIP(VectorT<T> &v);
  131. /**
  132. * @brief Calculate natural logarithm of VectorT \c v.
  133. * @param v source vector
  134. * @param buffer pointer to destination vector
  135. * @return pointer to VectorT with logarithmic values.
  136. */
  137. template<class T>
  138. inline VectorT<T> *ln(const VectorT<T> &v, VectorT<T> *buffer=NULL);
  139. /**
  140. * @brief Create a 1D Gauss function
  141. * @param sigma if sigma>0 determine Gauss function with standard derivation \c sigma
  142. * @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)
  143. * @return pointer to VectorT with Gauss function
  144. */
  145. template<class T>
  146. inline VectorT<T> *createGaussFunc(float sigma, VectorT<T> *buffer=NULL);
  147. /**
  148. * @brief solve linear equation given by the matrix A and the right-vector b: A*x = b.
  149. * @param A matrix
  150. * @param b vector
  151. * @param x solution vector
  152. */
  153. template<class T>
  154. inline void solveLinearEquationQR(const MatrixT<T> &A, const VectorT<T> &b, VectorT<T> &x);
  155. /**
  156. * @brief solve linear equation given by the matrix A and the right-vector b: A*x = b.
  157. * @param A matrix
  158. * @param b vector
  159. * @param x solution vector
  160. */
  161. template<class T>
  162. inline void solveLinearEquationQR(const RowMatrixT<T> &A, const VectorT<T> &b, VectorT<T> &x);
  163. /**
  164. * @brief solve Minimum Description Length based on greedy algorithm
  165. * @param C quadratic matrix
  166. * @param h solution vector
  167. */
  168. template<class T>
  169. inline void solveMDLgreedy(const MatrixT<T> &C, VectorT<T> &h);
  170. /**
  171. * Invert a square matrix.
  172. */
  173. template<class T>
  174. MatrixT<T> invert(const MatrixT<T>& w);
  175. /**
  176. * Invert a 3x3 upper triangle matrix (inplace).
  177. * @note There is no check if the matrix is invertable.
  178. */
  179. template<class T>
  180. void invert3x3UpperTriangle(MatrixT<T>& w);
  181. /**
  182. * Invert a 3x3 lower triangle matrix (inplace).
  183. * @note There is no check if the matrix is invertable.
  184. */
  185. template<class T>
  186. void invert3x3LowerTriangle(MatrixT<T>& w);
  187. /**
  188. * Compute the angle (in radian, -M_PI to M_PI) between two vectors.
  189. * If one of the vectors is zero, the result will be M_PI/2.0.
  190. */
  191. inline double angleBetweenVectors(
  192. const Vector& v1, const Vector& v2) {
  193. const double denominator = v1.normL2() * v2.normL2();
  194. // if one of the vectors is zero, they are orthogonal -> return PI/2
  195. if (isZero(denominator, 1e-16)) {
  196. return M_PI / 2.0;
  197. }
  198. const double arg = v1.scalarProduct(v2) / denominator;
  199. // allow for values slightly larger than 1.0 or slightly smaller than -1.0
  200. if (arg >= 1.0 && arg < 1.0 + 1e-10) {
  201. return 0.0;
  202. } else if (arg <= -1.0 && arg > -1.0 - 1e-10) {
  203. return -M_PI;
  204. } else {
  205. return acos(arg);
  206. }
  207. }
  208. /**
  209. * Compute the angle (in radian, -M_PI/2 to M_PI/2) between
  210. * two vectors ignoring the direction.
  211. */
  212. inline double angleBetweenVectorsIgnoringDirection(
  213. const Vector& v1, const Vector& v2) {
  214. double a = fabs(v1.scalarProduct(v2) / (v1.normL2() * v2.normL2()));
  215. if (a > 1.0)
  216. a = 1.0;
  217. return acos(a);
  218. }
  219. /**
  220. * Compute the absolute angle (in radian, 0 to M_PI/2) between
  221. * two vectors ignoring the direction.
  222. */
  223. inline double absoluteAngleBetweenVectorsIgnoringDirection(
  224. const Vector& v1, const Vector& v2) {
  225. return fabs(angleBetweenVectorsIgnoringDirection(v1, v2));
  226. }
  227. } // namespace
  228. //#ifdef __GNUC__
  229. #include <core/vector/Algorithms.tcc>
  230. //#endif
  231. #endif // ALGORITHMS_H