matrix.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. /* optimized and unoptimized vector and matrix functions */
  23. #ifndef _ODE_MATRIX_H_
  24. #define _ODE_MATRIX_H_
  25. #include <ode/common.h>
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /* set a vector/matrix of size n to all zeros, or to a specific value. */
  30. ODE_API void dSetZero (dReal *a, int n);
  31. ODE_API void dSetValue (dReal *a, int n, dReal value);
  32. /* get the dot product of two n*1 vectors. if n <= 0 then
  33. * zero will be returned (in which case a and b need not be valid).
  34. */
  35. ODE_API dReal dDot (const dReal *a, const dReal *b, int n);
  36. /* get the dot products of (a0,b), (a1,b), etc and return them in outsum.
  37. * all vectors are n*1. if n <= 0 then zeroes will be returned (in which case
  38. * the input vectors need not be valid). this function is somewhat faster
  39. * than calling dDot() for all of the combinations separately.
  40. */
  41. /* NOT INCLUDED in the library for now.
  42. void dMultidot2 (const dReal *a0, const dReal *a1,
  43. const dReal *b, dReal *outsum, int n);
  44. */
  45. /* matrix multiplication. all matrices are stored in standard row format.
  46. * the digit refers to the argument that is transposed:
  47. * 0: A = B * C (sizes: A:p*r B:p*q C:q*r)
  48. * 1: A = B' * C (sizes: A:p*r B:q*p C:q*r)
  49. * 2: A = B * C' (sizes: A:p*r B:p*q C:r*q)
  50. * case 1,2 are equivalent to saying that the operation is A=B*C but
  51. * B or C are stored in standard column format.
  52. */
  53. ODE_API void dMultiply0 (dReal *A, const dReal *B, const dReal *C, int p,int q,int r);
  54. ODE_API void dMultiply1 (dReal *A, const dReal *B, const dReal *C, int p,int q,int r);
  55. ODE_API void dMultiply2 (dReal *A, const dReal *B, const dReal *C, int p,int q,int r);
  56. /* do an in-place cholesky decomposition on the lower triangle of the n*n
  57. * symmetric matrix A (which is stored by rows). the resulting lower triangle
  58. * will be such that L*L'=A. return 1 on success and 0 on failure (on failure
  59. * the matrix is not positive definite).
  60. */
  61. ODE_API int dFactorCholesky (dReal *A, int n);
  62. /* solve for x: L*L'*x = b, and put the result back into x.
  63. * L is size n*n, b is size n*1. only the lower triangle of L is considered.
  64. */
  65. ODE_API void dSolveCholesky (const dReal *L, dReal *b, int n);
  66. /* compute the inverse of the n*n positive definite matrix A and put it in
  67. * Ainv. this is not especially fast. this returns 1 on success (A was
  68. * positive definite) or 0 on failure (not PD).
  69. */
  70. ODE_API int dInvertPDMatrix (const dReal *A, dReal *Ainv, int n);
  71. /* check whether an n*n matrix A is positive definite, return 1/0 (yes/no).
  72. * positive definite means that x'*A*x > 0 for any x. this performs a
  73. * cholesky decomposition of A. if the decomposition fails then the matrix
  74. * is not positive definite. A is stored by rows. A is not altered.
  75. */
  76. ODE_API int dIsPositiveDefinite (const dReal *A, int n);
  77. /* factorize a matrix A into L*D*L', where L is lower triangular with ones on
  78. * the diagonal, and D is diagonal.
  79. * A is an n*n matrix stored by rows, with a leading dimension of n rounded
  80. * up to 4. L is written into the strict lower triangle of A (the ones are not
  81. * written) and the reciprocal of the diagonal elements of D are written into
  82. * d.
  83. */
  84. ODE_API void dFactorLDLT (dReal *A, dReal *d, int n, int nskip);
  85. /* solve L*x=b, where L is n*n lower triangular with ones on the diagonal,
  86. * and x,b are n*1. b is overwritten with x.
  87. * the leading dimension of L is `nskip'.
  88. */
  89. ODE_API void dSolveL1 (const dReal *L, dReal *b, int n, int nskip);
  90. /* solve L'*x=b, where L is n*n lower triangular with ones on the diagonal,
  91. * and x,b are n*1. b is overwritten with x.
  92. * the leading dimension of L is `nskip'.
  93. */
  94. ODE_API void dSolveL1T (const dReal *L, dReal *b, int n, int nskip);
  95. /* in matlab syntax: a(1:n) = a(1:n) .* d(1:n)
  96. */
  97. ODE_API void dScaleVector (dReal *a, const dReal *d, int n);
  98. /* The function is an alias for @c dScaleVector.
  99. * It has been deprecated because of a wrong naming schema used.
  100. */
  101. ODE_API_DEPRECATED ODE_API void dVectorScale (dReal *a, const dReal *d, int n);
  102. /* given `L', a n*n lower triangular matrix with ones on the diagonal,
  103. * and `d', a n*1 vector of the reciprocal diagonal elements of an n*n matrix
  104. * D, solve L*D*L'*x=b where x,b are n*1. x overwrites b.
  105. * the leading dimension of L is `nskip'.
  106. */
  107. ODE_API void dSolveLDLT (const dReal *L, const dReal *d, dReal *b, int n, int nskip);
  108. /* given an L*D*L' factorization of an n*n matrix A, return the updated
  109. * factorization L2*D2*L2' of A plus the following "top left" matrix:
  110. *
  111. * [ b a' ] <-- b is a[0]
  112. * [ a 0 ] <-- a is a[1..n-1]
  113. *
  114. * - L has size n*n, its leading dimension is nskip. L is lower triangular
  115. * with ones on the diagonal. only the lower triangle of L is referenced.
  116. * - d has size n. d contains the reciprocal diagonal elements of D.
  117. * - a has size n.
  118. * the result is written into L, except that the left column of L and d[0]
  119. * are not actually modified. see ldltaddTL.m for further comments.
  120. */
  121. ODE_API void dLDLTAddTL (dReal *L, dReal *d, const dReal *a, int n, int nskip);
  122. /* given an L*D*L' factorization of a permuted matrix A, produce a new
  123. * factorization for row and column `r' removed.
  124. * - A has size n1*n1, its leading dimension in nskip. A is symmetric and
  125. * positive definite. only the lower triangle of A is referenced.
  126. * A itself may actually be an array of row pointers.
  127. * - L has size n2*n2, its leading dimension in nskip. L is lower triangular
  128. * with ones on the diagonal. only the lower triangle of L is referenced.
  129. * - d has size n2. d contains the reciprocal diagonal elements of D.
  130. * - p is a permutation vector. it contains n2 indexes into A. each index
  131. * must be in the range 0..n1-1.
  132. * - r is the row/column of L to remove.
  133. * the new L will be written within the old L, i.e. will have the same leading
  134. * dimension. the last row and column of L, and the last element of d, are
  135. * undefined on exit.
  136. *
  137. * a fast O(n^2) algorithm is used. see ldltremove.m for further comments.
  138. */
  139. ODE_API void dLDLTRemove (dReal **A, const int *p, dReal *L, dReal *d,
  140. int n1, int n2, int r, int nskip);
  141. /* given an n*n matrix A (with leading dimension nskip), remove the r'th row
  142. * and column by moving elements. the new matrix will have the same leading
  143. * dimension. the last row and column of A are untouched on exit.
  144. */
  145. ODE_API void dRemoveRowCol (dReal *A, int n, int nskip, int r);
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #endif