ccmath_grass_wrapper.c 18 KB

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