ccmath_grass_wrapper.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #if defined(HAVE_CCMATH)
  2. #include <ccmath.h>
  3. #else
  4. #include <grass/ccmath_grass.h>
  5. #endif
  6. /**
  7. Chapter 1
  8. LINEAR ALGEBRA
  9. Summary
  10. The matrix algebra library contains functions that
  11. perform the standard computations of linear algebra.
  12. General areas covered are:
  13. o Solution of Linear Systems
  14. o Matrix Inversion
  15. o Eigensystem Analysis
  16. o Matrix Utility Operations
  17. o Singular Value Decomposition
  18. The operations covered here are fundamental to many
  19. areas of mathematics and statistics. Thus, functions
  20. in this library segment are called by other library
  21. functions. Both real and complex valued matrices
  22. are covered by functions in the first four of these
  23. categories.
  24. Notes on Contents
  25. Functions in this library segment provide the basic operations of
  26. numerical linear algebra and some useful utility functions for operations on
  27. vectors and matrices. The following list describes the functions available for
  28. operations with real-valued matrices.
  29. o Solving and Inverting Linear Systems:
  30. solv --------- solve a general system of real linear equations.
  31. solvps ------- solve a real symmetric linear system.
  32. solvru ------- solve a real right upper triangular linear system.
  33. solvtd ------- solve a tridiagonal real linear system.
  34. minv --------- invert a general real square matrix.
  35. psinv -------- invert a real symmetric matrix.
  36. ruinv -------- invert a right upper triangular matrix.
  37. The solution of a general linear system and efficient algorithms for
  38. solving special systems with symmetric and tridiagonal matrices are provided
  39. by these functions. The general solution function employs a LU factorization
  40. with partial pivoting and it is very robust. It will work efficiently on any
  41. problem that is not ill-conditioned. The symmetric matrix solution is based
  42. on a modified Cholesky factorization. It is best used on positive definite
  43. matrices that do not require pivoting for numeric stability. Tridiagonal
  44. solvers require order-N operations (N = dimension). Thus, they are highly
  45. recommended for this important class of sparse systems. Two matrix inversion
  46. routines are provided. The general inversion function is again LU based. It
  47. is suitable for use on any stable (ie. well-conditioned) problem. The
  48. Cholesky based symmetric matrix inversion is efficient and safe for use on
  49. matrices known to be positive definite, such as the variance matrices
  50. encountered in statistical computations. Both the solver and the inverse
  51. functions are designed to enhance data locality. They are very effective
  52. on modern microprocessors.
  53. o Eigensystem Analysis:
  54. eigen ------ extract all eigen values and vectors of a real
  55. symmetric matrix.
  56. eigval ----- extract the eigen values of a real symmetric matrix.
  57. evmax ------ compute the eigen value of maximum absolute magnitude
  58. and its corresponding vector for a symmetric matrix.
  59. Eigensystem functions operate on real symmetric matrices. Two forms of
  60. the general eigen routine are provided because the computation of eigen values
  61. only is much faster when vectors are not required. The basic algorithms use
  62. a Householder reduction to tridiagonal form followed by QR iterations with
  63. shifts to enhance convergence. This has become the accepted standard for
  64. symmetric eigensystem computation. The evmax function uses an efficient
  65. iterative power method algorithm to extract the eigen value of maximum
  66. absolute size and the corresponding eigenvector.
  67. o Singular Value Decomposition:
  68. svdval ----- compute the singular values of a m by n real matrix.
  69. sv2val ----- compute the singular values of a real matrix
  70. efficiently for m >> n.
  71. svduv ------ compute the singular values and the transformation
  72. matrices u and v for a real m by n matrix.
  73. sv2uv ------ compute the singular values and transformation
  74. matrices efficiently for m >> n.
  75. svdu1v ----- compute the singular values and transformation
  76. matrices u1 and v, where u1 overloads the input
  77. with the first n column vectors of u.
  78. sv2u1v ----- compute the singular values and the transformation
  79. matrices u1 and v efficiently for m >> n.
  80. Singular value decomposition is extremely useful when dealing with linear
  81. systems that may be singular. Singular values with values near zero are flags
  82. of a potential rank deficiency in the system matrix. They can be used to
  83. identify the presence of an ill-conditioned problem and, in some cases, to
  84. deal with the potential instability. They are applied to the linear least
  85. squares problem in this library. Singular values also define some important
  86. matrix norm parameters such as the 2-norm and the condition value. A complete
  87. decomposition provides both singular values and an orthogonal decomposition of
  88. vector spaces related to the matrix identifying the range and null-space.
  89. Fortunately, a highly stable algorithm based on Householder reduction to
  90. bidiagonal form and QR rotations can be used to implement the decomposition.
  91. The library provides two forms with one more efficient when the dimensions
  92. satisfy m > (3/2)n.
  93. General Technical Comments
  94. Efficient computation with matrices on modern processors must be
  95. adapted to the storage scheme employed for matrix elements. The functions
  96. of this library segment do not employ the multidimensional array intrinsic
  97. of the C language. Access to elements employs the simple row-major scheme
  98. described here.
  99. Matrices are modeled by the library functions as arrays with elements
  100. stored in row order. Thus, the element in the jth row and kth column of
  101. the n by n matrix M, stored in the array mat[], is addressed by
  102. M[j,k] = mat[n*j+k] , with 0 =< j,k <= n-1 .
  103. (Remember that C employs zero as the starting index.) The storage order has
  104. important implications for data locality.
  105. The algorithms employed here all have excellent numerical stability, and
  106. the default double precision arithmetic of C enhances this. Thus, any
  107. problems encountered in using the matrix algebra functions will almost
  108. certainly be due to an ill-conditioned matrix. (The Hilbert matrices,
  109. H[i,j] = 1/(1+i+j) for i,j < n
  110. form a good example of such ill-conditioned systems.) We remind the reader
  111. that the appropriate response to such ill-conditioning is to seek an
  112. alternative approach to the problem. The option of increasing precision has
  113. already been exploited. Modification of the linear algebra algorithm code is
  114. not normally effective in an ill-conditioned problem.
  115. ------------------------------------------------------------------------------
  116. FUNCTION SYNOPSES
  117. ------------------------------------------------------------------------------
  118. Linear System Solutions:
  119. -----------------------------------------------------------------------------
  120. */
  121. /**
  122. \brief Solve a general linear system A*x = b.
  123. \param a = array containing system matrix A in row order (altered to L-U factored form by computation)
  124. \param b = array containing system vector b at entry and solution vector x at exit
  125. \param n = dimension of system
  126. \return 0 -> normal exit; -1 -> singular input
  127. */
  128. int G_math_solv(double **a,double *b,int n)
  129. {
  130. return solv(a[0],b, n);
  131. }
  132. /**
  133. \brief Solve a symmetric positive definite linear system S*x = b.
  134. \param a = array containing system matrix S (altered to Cholesky upper right factor by computation)
  135. \param b = array containing system vector b as input and solution vector x as output
  136. \param n = dimension of system
  137. \return: 0 -> normal exit; -1 -> input matrix not positive definite
  138. */
  139. int G_math_solvps(double **a,double *b,int n)
  140. {
  141. return solvps(a[0], b,n);
  142. }
  143. /**
  144. \brief Solve a tridiagonal linear system M*x = y.
  145. \param a = array containing m+1 diagonal elements of M
  146. \param b = array of m elements below the main diagonal of M
  147. \param c = array of m elements above the main diagonal
  148. \param x = array containing the system vector y initially, and the solution vector at exit (m+1 elements)
  149. \param m = dimension parameter ( M is (m+1)x(m+1) )
  150. */
  151. void G_math_solvtd(double *a,double *b,double *c,double *x,int m)
  152. {
  153. solvtd(a, b, c, x, m);
  154. return;
  155. }
  156. /*
  157. \brief Solve an upper right triangular linear system T*x = b.
  158. \param a = pointer to array of upper right triangular matrix T
  159. \param b = pointer to array of system vector The computation overloads this with the solution vector x.
  160. \param n = dimension (dim(a)=n*n,dim(b)=n)
  161. \return value: f = status flag, with 0 -> normal exit, -1 -> system singular
  162. */
  163. int G_math_solvru(double **a,double *b,int n)
  164. {
  165. return solvru(a[0], b, n);
  166. }
  167. /**
  168. \brief Invert (in place) a general real matrix A -> Inv(A).
  169. \param a = array containing the input matrix A. This is converted to the inverse matrix.
  170. \param n = dimension of the system (i.e. A is n x n )
  171. \return: 0 -> normal exit, 1 -> singular input matrix
  172. */
  173. int G_math_minv(double **a,int n)
  174. {
  175. return minv(a[0], n);
  176. }
  177. /**
  178. \brief Invert (in place) a symmetric real matrix, V -> Inv(V).
  179. The input matrix V is symmetric (V[i,j] = V[j,i]).
  180. \param v = array containing a symmetric input matrix. This is converted to the inverse matrix.
  181. \param n = dimension of the system (dim(v)=n*n)
  182. \return: 0 -> normal exit 1 -> input matrix not positive definite
  183. */
  184. int G_math_psinv(double **a,int n)
  185. {
  186. return psinv( a[0], n);
  187. }
  188. /**
  189. \brief Invert an upper right triangular matrix T -> Inv(T).
  190. \param a = pointer to array of upper right triangular matrix, This is replaced by the inverse matrix.
  191. \param n = dimension (dim(a)=n*n)
  192. \return value: status flag, with 0 -> matrix inverted -1 -> matrix singular
  193. */
  194. int G_math_ruinv(double **a,int n)
  195. {
  196. return ruinv(a[0], n);
  197. }
  198. /*
  199. -----------------------------------------------------------------------------
  200. Symmetric Eigensystem Analysis:
  201. -----------------------------------------------------------------------------
  202. */
  203. /**
  204. \brief Compute the eigenvalues of a real symmetric matrix A.
  205. \param a = pointer to array of symmetric n by n input matrix A. The computation alters these values.
  206. \param ev = pointer to array of the output eigenvalues
  207. \param n = dimension parameter (dim(a)= n*n, dim(ev)= n)
  208. */
  209. void G_math_eigval(double **a,double *ev,int n)
  210. {
  211. eigval(a[0], ev, n);
  212. return;
  213. }
  214. /**
  215. \brief Compute the eigenvalues and eigenvectors of a real symmetric matrix A.
  216. The input and output matrices are related by
  217. A = E*D*E~ where D is the diagonal matrix of eigenvalues
  218. D[i,j] = ev[i] if i=j and 0 otherwise.
  219. The columns of E are the eigenvectors.
  220. \param a = pointer to store for symmetric n by n input matrix A. The computation overloads this with an orthogonal matrix of eigenvectors E.
  221. \param ev = pointer to the array of the output eigenvalues
  222. \param n = dimension parameter (dim(a)= n*n, dim(ev)= n)
  223. */
  224. void G_math_eigen(double **a,double *ev,int n)
  225. {
  226. eigen(a[0], ev, n);
  227. return;
  228. }
  229. /*
  230. \brief Compute the maximum (absolute) eigenvalue and corresponding eigenvector of a real symmetric matrix A.
  231. \param a = array containing symmetric input matrix A
  232. \param u = array containing the n components of the eigenvector at exit (vector normalized to 1)
  233. \param n = dimension of system
  234. \return: ev = eigenvalue of A with maximum absolute value HUGE -> convergence failure
  235. */
  236. double G_math_evmax(double **a,double *u,int n)
  237. {
  238. return evmax(a[0], u, n);
  239. }
  240. /*
  241. ------------------------------------------------------------------------------
  242. Singular Value Decomposition:
  243. ------------------------------------------------------------------------------
  244. A number of versions of the Singular Value Decomposition (SVD)
  245. are implemented in the library. They support the efficient
  246. computation of this important factorization for a real m by n
  247. matrix A. The general form of the SVD is
  248. A = U*S*V~ with S = | D |
  249. | 0 |
  250. where U is an m by m orthogonal matrix, V is an n by n orthogonal matrix,
  251. D is the n by n diagonal matrix of singular value, and S is the singular
  252. m by n matrix produced by the transformation.
  253. The singular values computed by these functions provide important
  254. information on the rank of the matrix A, and on several matrix
  255. norms of A. The number of non-zero singular values d[i] in D
  256. equal to the rank of A. The two norm of A is
  257. ||A|| = max(d[i]) , and the condition number is
  258. k(A) = max(d[i])/min(d[i]) .
  259. The Frobenius norm of the matrix A is
  260. Fn(A) = Sum(i=0 to n-1) d[i]^2 .
  261. Singular values consistent with zero are easily recognized, since
  262. the decomposition algorithms have excellent numerical stability.
  263. The value of a 'zero' d[i] is no larger than a few times the
  264. computational rounding error e.
  265. The matrix U1 is formed from the first n orthonormal column vectors
  266. of U. U1[i,j] = U[i,j] for i = 1 to m and j = 1 to n. A singular
  267. value decomposition of A can also be expressed in terms of the m by\
  268. n matrix U1, with
  269. A = U1*D*V~ .
  270. SVD functions with three forms of output are provided. The first
  271. form computes only the singular values, while the second computes
  272. the singular values and the U and V orthogonal transformation
  273. matrices. The third form of output computes singular values, the
  274. V matrix, and saves space by overloading the input array with
  275. the U1 matrix.
  276. Two forms of decomposition algorithm are available for each of the
  277. three output types. One is computationally efficient when m ~ n.
  278. The second, distinguished by the prefix 'sv2' in the function name,
  279. employs a two stage Householder reduction to accelerate computation
  280. when m substantially exceeds n. Use of functions of the second form
  281. is recommended for m > 2n.
  282. Singular value output from each of the six SVD functions satisfies
  283. d[i] >= 0 for i = 0 to n-1.
  284. -------------------------------------------------------------------------------
  285. */
  286. /**
  287. \brief Compute the singular values of a real m by n matrix A.
  288. \param d = pointer to double array of dimension n (output = singular values of A)
  289. \param a = pointer to store of the m by n input matrix A (A is altered by the computation)
  290. \param m = number of rows in A
  291. \param n = number of columns in A (m>=n required)
  292. \return value: status flag with: 0 -> success -1 -> input error m < n
  293. */
  294. int G_math_svdval(double *d,double **a,int m,int n)
  295. {
  296. return svdval(d, a[0], m, n);
  297. }
  298. /**
  299. \brief Compute singular values when m >> n.
  300. \param d = pointer to double array of dimension n (output = singular values of A)
  301. \param a = pointer to store of the m by n input matrix A (A is altered by the computation)
  302. \param m = number of rows in A
  303. \param n = number of columns in A (m>=n required)
  304. \return value: status flag with: 0 -> success -1 -> input error m < n
  305. */
  306. int G_math_sv2val(double *d,double **a,int m,int n)
  307. {
  308. return sv2val(d, a[0], m, n);
  309. }
  310. /*
  311. \brief Compute the singular value transformation S = U~*A*V.
  312. \param d = pointer to double array of dimension n (output = singular values of A)
  313. \param a = pointer to store of the m by n input matrix A (A is altered by the computation)
  314. \param u = pointer to store for m by m orthogonal matrix U
  315. \param v = pointer to store for n by n orthogonal matrix V
  316. \param m = number of rows in A
  317. \param n = number of columns in A (m>=n required)
  318. \return value: status flag with: 0 -> success -1 -> input error m < n
  319. */
  320. int G_math_svduv(double *d,double **a,double **u,int m,double **v,int n)
  321. {
  322. return svduv(d, a[0], u[0], m, v[0], n);
  323. }
  324. /**
  325. \brief Compute the singular value transformation when m >> n.
  326. \param d = pointer to double array of dimension n (output = singular values of A)
  327. \param a = pointer to store of the m by n input matrix A (A is altered by the computation)
  328. \param u = pointer to store for m by m orthogonal matrix U
  329. \param v = pointer to store for n by n orthogonal matrix V
  330. \param m = number of rows in A
  331. \param n = number of columns in A (m>=n required)
  332. \return value: status flag with: 0 -> success -1 -> input error m < n
  333. */
  334. int G_math_sv2uv(double *d,double **a,double **u,int m,double **v,int n)
  335. {
  336. return sv2uv(d, a[0], u[0], m, v[0], n);
  337. }
  338. /**
  339. \brief Compute the singular value transformation with A overloaded by the partial U-matrix.
  340. \param d = pointer to double array of dimension n
  341. (output = singular values of A)
  342. \param a = pointer to store of the m by n input matrix A (At output a is overloaded by the matrix U1 whose n columns are orthogonal vectors equal to the first n columns of U.)
  343. \param v = pointer to store for n by n orthogonal matrix V
  344. \param m = number of rows in A
  345. \param n = number of columns in A (m>=n required)
  346. \return value: status flag with: 0 -> success -1 -> input error m < n
  347. */
  348. int G_math_svdu1v(double *d,double **a,int m,double **v,int n)
  349. {
  350. return svdu1v(d, a[0], m, v[0], n);
  351. }