sundials_dense.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * -----------------------------------------------------------------
  3. * $Revision: 4272 $
  4. * $Date: 2014-12-02 11:19:41 -0800 (Tue, 02 Dec 2014) $
  5. * -----------------------------------------------------------------
  6. * Programmer(s): Scott D. Cohen, Alan C. Hindmarsh and
  7. * Radu Serban @ LLNL
  8. * -----------------------------------------------------------------
  9. * LLNS Copyright Start
  10. * Copyright (c) 2014, Lawrence Livermore National Security
  11. * This work was performed under the auspices of the U.S. Department
  12. * of Energy by Lawrence Livermore National Laboratory in part under
  13. * Contract W-7405-Eng-48 and in part under Contract DE-AC52-07NA27344.
  14. * Produced at the Lawrence Livermore National Laboratory.
  15. * All rights reserved.
  16. * For details, see the LICENSE file.
  17. * LLNS Copyright End
  18. * -----------------------------------------------------------------
  19. * This is the implementation file for a generic package of dense
  20. * matrix operations.
  21. * -----------------------------------------------------------------
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <sundials/sundials_dense.h>
  26. #include <sundials/sundials_math.h>
  27. #define ZERO RCONST(0.0)
  28. #define ONE RCONST(1.0)
  29. #define TWO RCONST(2.0)
  30. /*
  31. * -----------------------------------------------------
  32. * Functions working on DlsMat
  33. * -----------------------------------------------------
  34. */
  35. long int DenseGETRF(DlsMat A, long int *p)
  36. {
  37. return(denseGETRF(A->cols, A->M, A->N, p));
  38. }
  39. void DenseGETRS(DlsMat A, long int *p, realtype *b)
  40. {
  41. denseGETRS(A->cols, A->N, p, b);
  42. }
  43. long int DensePOTRF(DlsMat A)
  44. {
  45. return(densePOTRF(A->cols, A->M));
  46. }
  47. void DensePOTRS(DlsMat A, realtype *b)
  48. {
  49. densePOTRS(A->cols, A->M, b);
  50. }
  51. int DenseGEQRF(DlsMat A, realtype *beta, realtype *wrk)
  52. {
  53. return(denseGEQRF(A->cols, A->M, A->N, beta, wrk));
  54. }
  55. int DenseORMQR(DlsMat A, realtype *beta, realtype *vn, realtype *vm, realtype *wrk)
  56. {
  57. return(denseORMQR(A->cols, A->M, A->N, beta, vn, vm, wrk));
  58. }
  59. void DenseCopy(DlsMat A, DlsMat B)
  60. {
  61. denseCopy(A->cols, B->cols, A->M, A->N);
  62. }
  63. void DenseScale(realtype c, DlsMat A)
  64. {
  65. denseScale(c, A->cols, A->M, A->N);
  66. }
  67. void DenseMatvec(DlsMat A, realtype *x, realtype *y)
  68. {
  69. denseMatvec(A->cols, x, y, A->M, A->N);
  70. }
  71. long int denseGETRF(realtype **a, long int m, long int n, long int *p)
  72. {
  73. long int i, j, k, l;
  74. realtype *col_j, *col_k;
  75. realtype temp, mult, a_kj;
  76. /* k-th elimination step number */
  77. for (k=0; k < n; k++) {
  78. col_k = a[k];
  79. /* find l = pivot row number */
  80. l=k;
  81. for (i=k+1; i < m; i++)
  82. if (SUNRabs(col_k[i]) > SUNRabs(col_k[l])) l=i;
  83. p[k] = l;
  84. /* check for zero pivot element */
  85. if (col_k[l] == ZERO) return(k+1);
  86. /* swap a(k,1:n) and a(l,1:n) if necessary */
  87. if ( l!= k ) {
  88. for (i=0; i<n; i++) {
  89. temp = a[i][l];
  90. a[i][l] = a[i][k];
  91. a[i][k] = temp;
  92. }
  93. }
  94. /* Scale the elements below the diagonal in
  95. * column k by 1.0/a(k,k). After the above swap
  96. * a(k,k) holds the pivot element. This scaling
  97. * stores the pivot row multipliers a(i,k)/a(k,k)
  98. * in a(i,k), i=k+1, ..., m-1.
  99. */
  100. mult = ONE/col_k[k];
  101. for(i=k+1; i < m; i++) col_k[i] *= mult;
  102. /* row_i = row_i - [a(i,k)/a(k,k)] row_k, i=k+1, ..., m-1 */
  103. /* row k is the pivot row after swapping with row l. */
  104. /* The computation is done one column at a time, */
  105. /* column j=k+1, ..., n-1. */
  106. for (j=k+1; j < n; j++) {
  107. col_j = a[j];
  108. a_kj = col_j[k];
  109. /* a(i,j) = a(i,j) - [a(i,k)/a(k,k)]*a(k,j) */
  110. /* a_kj = a(k,j), col_k[i] = - a(i,k)/a(k,k) */
  111. if (a_kj != ZERO) {
  112. for (i=k+1; i < m; i++)
  113. col_j[i] -= a_kj * col_k[i];
  114. }
  115. }
  116. }
  117. /* return 0 to indicate success */
  118. return(0);
  119. }
  120. void denseGETRS(realtype **a, long int n, long int *p, realtype *b)
  121. {
  122. long int i, k, pk;
  123. realtype *col_k, tmp;
  124. /* Permute b, based on pivot information in p */
  125. for (k=0; k<n; k++) {
  126. pk = p[k];
  127. if(pk != k) {
  128. tmp = b[k];
  129. b[k] = b[pk];
  130. b[pk] = tmp;
  131. }
  132. }
  133. /* Solve Ly = b, store solution y in b */
  134. for (k=0; k<n-1; k++) {
  135. col_k = a[k];
  136. for (i=k+1; i<n; i++) b[i] -= col_k[i]*b[k];
  137. }
  138. /* Solve Ux = y, store solution x in b */
  139. for (k = n-1; k > 0; k--) {
  140. col_k = a[k];
  141. b[k] /= col_k[k];
  142. for (i=0; i<k; i++) b[i] -= col_k[i]*b[k];
  143. }
  144. b[0] /= a[0][0];
  145. }
  146. /*
  147. * Cholesky decomposition of a symmetric positive-definite matrix
  148. * A = C^T*C: gaxpy version.
  149. * Only the lower triangle of A is accessed and it is overwritten with
  150. * the lower triangle of C.
  151. */
  152. long int densePOTRF(realtype **a, long int m)
  153. {
  154. realtype *a_col_j, *a_col_k;
  155. realtype a_diag;
  156. long int i, j, k;
  157. for (j=0; j<m; j++) {
  158. a_col_j = a[j];
  159. if (j>0) {
  160. for(i=j; i<m; i++) {
  161. for(k=0;k<j;k++) {
  162. a_col_k = a[k];
  163. a_col_j[i] -= a_col_k[i]*a_col_k[j];
  164. }
  165. }
  166. }
  167. a_diag = a_col_j[j];
  168. if (a_diag <= ZERO) return(j+1);
  169. a_diag = SUNRsqrt(a_diag);
  170. for(i=j; i<m; i++) a_col_j[i] /= a_diag;
  171. }
  172. return(0);
  173. }
  174. /*
  175. * Solution of Ax=b, with A s.p.d., based on the Cholesky decomposition
  176. * obtained with denPOTRF.; A = C*C^T, C lower triangular
  177. *
  178. */
  179. void densePOTRS(realtype **a, long int m, realtype *b)
  180. {
  181. realtype *col_j, *col_i;
  182. long int i, j;
  183. /* Solve C y = b, forward substitution - column version.
  184. Store solution y in b */
  185. for (j=0; j < m-1; j++) {
  186. col_j = a[j];
  187. b[j] /= col_j[j];
  188. for (i=j+1; i < m; i++)
  189. b[i] -= b[j]*col_j[i];
  190. }
  191. col_j = a[m-1];
  192. b[m-1] /= col_j[m-1];
  193. /* Solve C^T x = y, backward substitution - row version.
  194. Store solution x in b */
  195. col_j = a[m-1];
  196. b[m-1] /= col_j[m-1];
  197. for (i=m-2; i>=0; i--) {
  198. col_i = a[i];
  199. for (j=i+1; j<m; j++)
  200. b[i] -= col_i[j]*b[j];
  201. b[i] /= col_i[i];
  202. }
  203. }
  204. /*
  205. * QR factorization of a rectangular matrix A of size m by n (m >= n)
  206. * using Householder reflections.
  207. *
  208. * On exit, the elements on and above the diagonal of A contain the n by n
  209. * upper triangular matrix R; the elements below the diagonal, with the array beta,
  210. * represent the orthogonal matrix Q as a product of elementary reflectors .
  211. *
  212. * v (of length m) must be provided as workspace.
  213. *
  214. */
  215. int denseGEQRF(realtype **a, long int m, long int n, realtype *beta, realtype *v)
  216. {
  217. realtype ajj, s, mu, v1, v1_2;
  218. realtype *col_j, *col_k;
  219. long int i, j, k;
  220. /* For each column...*/
  221. for(j=0; j<n; j++) {
  222. col_j = a[j];
  223. ajj = col_j[j];
  224. /* Compute the j-th Householder vector (of length m-j) */
  225. v[0] = ONE;
  226. s = ZERO;
  227. for(i=1; i<m-j; i++) {
  228. v[i] = col_j[i+j];
  229. s += v[i]*v[i];
  230. }
  231. if(s != ZERO) {
  232. mu = SUNRsqrt(ajj*ajj+s);
  233. v1 = (ajj <= ZERO) ? ajj-mu : -s/(ajj+mu);
  234. v1_2 = v1*v1;
  235. beta[j] = TWO * v1_2 / (s + v1_2);
  236. for(i=1; i<m-j; i++) v[i] /= v1;
  237. } else {
  238. beta[j] = ZERO;
  239. }
  240. /* Update upper triangle of A (load R) */
  241. for(k=j; k<n; k++) {
  242. col_k = a[k];
  243. s = ZERO;
  244. for(i=0; i<m-j; i++) s += col_k[i+j]*v[i];
  245. s *= beta[j];
  246. for(i=0; i<m-j; i++) col_k[i+j] -= s*v[i];
  247. }
  248. /* Update A (load Householder vector) */
  249. if(j<m-1) {
  250. for(i=1; i<m-j; i++) col_j[i+j] = v[i];
  251. }
  252. }
  253. return(0);
  254. }
  255. /*
  256. * Computes vm = Q * vn, where the orthogonal matrix Q is stored as
  257. * elementary reflectors in the m by n matrix A and in the vector beta.
  258. * (NOTE: It is assumed that an QR factorization has been previously
  259. * computed with denGEQRF).
  260. *
  261. * vn (IN) has length n, vm (OUT) has length m, and it's assumed that m >= n.
  262. *
  263. * v (of length m) must be provided as workspace.
  264. */
  265. int denseORMQR(realtype **a, long int m, long int n, realtype *beta,
  266. realtype *vn, realtype *vm, realtype *v)
  267. {
  268. realtype *col_j, s;
  269. long int i, j;
  270. /* Initialize vm */
  271. for(i=0; i<n; i++) vm[i] = vn[i];
  272. for(i=n; i<m; i++) vm[i] = ZERO;
  273. /* Accumulate (backwards) corrections into vm */
  274. for(j=n-1; j>=0; j--) {
  275. col_j = a[j];
  276. v[0] = ONE;
  277. s = vm[j];
  278. for(i=1; i<m-j; i++) {
  279. v[i] = col_j[i+j];
  280. s += v[i]*vm[i+j];
  281. }
  282. s *= beta[j];
  283. for(i=0; i<m-j; i++) vm[i+j] -= s * v[i];
  284. }
  285. return(0);
  286. }
  287. void denseCopy(realtype **a, realtype **b, long int m, long int n)
  288. {
  289. long int i, j;
  290. realtype *a_col_j, *b_col_j;
  291. for (j=0; j < n; j++) {
  292. a_col_j = a[j];
  293. b_col_j = b[j];
  294. for (i=0; i < m; i++)
  295. b_col_j[i] = a_col_j[i];
  296. }
  297. }
  298. void denseScale(realtype c, realtype **a, long int m, long int n)
  299. {
  300. long int i, j;
  301. realtype *col_j;
  302. for (j=0; j < n; j++) {
  303. col_j = a[j];
  304. for (i=0; i < m; i++)
  305. col_j[i] *= c;
  306. }
  307. }
  308. void denseAddIdentity(realtype **a, long int n)
  309. {
  310. long int i;
  311. for (i=0; i < n; i++) a[i][i] += ONE;
  312. }
  313. void denseMatvec(realtype **a, realtype *x, realtype *y, long int m, long int n)
  314. {
  315. long int i, j;
  316. realtype *col_j;
  317. for (i=0; i<m; i++) {
  318. y[i] = 0.0;
  319. }
  320. for (j=0; j<n; j++) {
  321. col_j = a[j];
  322. for (i=0; i<m; i++)
  323. y[i] += col_j[i]*x[j];
  324. }
  325. }