SCM

SCM Repository

[matrix] Annotation of /pkg/Matrix/src/dtCMatrix.c
ViewVC logotype

Annotation of /pkg/Matrix/src/dtCMatrix.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1661 - (view) (download) (as text)
Original Path: pkg/src/dtCMatrix.c

1 : bates 741 /* Sparse triangular numeric matrices */
2 : bates 478 #include "dtCMatrix.h"
3 : bates 1251 #include "cs_utils.h"
4 : bates 10
5 : maechler 534 /**
6 : bates 358 * Derive the column pointer vector for the inverse of L from the parent array
7 : maechler 534 *
8 : bates 358 * @param n length of parent array
9 :     * @param countDiag 0 for a unit triangular matrix with implicit diagonal, otherwise 1
10 :     * @param pr parent vector describing the elimination tree
11 :     * @param ap array of length n+1 to be filled with the column pointers
12 : maechler 534 *
13 : bates 358 * @return the number of non-zero entries (ap[n])
14 :     */
15 :     int parent_inv_ap(int n, int countDiag, const int pr[], int ap[])
16 :     {
17 :     int *sz = Calloc(n, int), j;
18 :    
19 :     for (j = n - 1; j >= 0; j--) {
20 :     int parent = pr[j];
21 :     sz[j] = (parent < 0) ? countDiag : (1 + sz[parent]);
22 :     }
23 :     ap[0] = 0;
24 :     for (j = 0; j < n; j++)
25 :     ap[j+1] = ap[j] + sz[j];
26 :     Free(sz);
27 :     return ap[n];
28 :     }
29 :    
30 : maechler 534 /**
31 : bates 358 * Derive the row index array for the inverse of L from the parent array
32 : maechler 534 *
33 : bates 358 * @param n length of parent array
34 :     * @param countDiag 0 for a unit triangular matrix with implicit diagonal, otherwise 1
35 :     * @param pr parent vector describing the elimination tree
36 :     * @param ai row index vector of length ap[n]
37 :     */
38 :     void parent_inv_ai(int n, int countDiag, const int pr[], int ai[])
39 :     {
40 :     int j, k, pos = 0;
41 :     for (j = 0; j < n; j++) {
42 :     if (countDiag) ai[pos++] = j;
43 :     for (k = pr[j]; k >= 0; k = pr[k]) ai[pos++] = k;
44 :     }
45 :     }
46 : maechler 534
47 : bates 338 SEXP Parent_inverse(SEXP par, SEXP unitdiag)
48 :     {
49 : bates 478 SEXP ans = PROTECT(NEW_OBJECT(MAKE_CLASS("dtCMatrix")));
50 : bates 338 int *ap, *ai, *dims, *pr = INTEGER(par),
51 :     countDiag = 1 - asLogical(unitdiag),
52 : bates 367 j, n = length(par), nnz;
53 : bates 338 double *ax;
54 : maechler 534
55 : bates 582 if (!isInteger(par)) error(_("par argument must be an integer vector"));
56 : bates 338 SET_SLOT(ans, Matrix_pSym, allocVector(INTSXP, n + 1));
57 :     ap = INTEGER(GET_SLOT(ans, Matrix_pSym));
58 : bates 358 nnz = parent_inv_ap(n, countDiag, pr, ap);
59 : bates 338 SET_SLOT(ans, Matrix_iSym, allocVector(INTSXP, nnz));
60 :     ai = INTEGER(GET_SLOT(ans, Matrix_iSym));
61 :     SET_SLOT(ans, Matrix_xSym, allocVector(REALSXP, nnz));
62 :     ax = REAL(GET_SLOT(ans, Matrix_xSym));
63 :     for (j = 0; j < nnz; j++) ax[j] = 1.;
64 :     SET_SLOT(ans, Matrix_DimSym, allocVector(INTSXP, 2));
65 :     dims = INTEGER(GET_SLOT(ans, Matrix_DimSym));
66 :     dims[0] = dims[1] = n;
67 : maechler 534 SET_SLOT(ans, Matrix_uploSym, mkString("L"));
68 :     SET_SLOT(ans, Matrix_diagSym, (countDiag ? mkString("N") : mkString("U")));
69 : bates 358 parent_inv_ai(n, countDiag, pr, ai);
70 : bates 338 UNPROTECT(1);
71 :     return ans;
72 :     }
73 : bates 1248
74 :     SEXP dtCMatrix_solve(SEXP a)
75 :     {
76 :     SEXP ans = PROTECT(NEW_OBJECT(MAKE_CLASS("dtCMatrix")));
77 : bates 1251 cs *A = Matrix_as_cs(a);
78 :     int *bp = INTEGER(ALLOC_SLOT(ans, Matrix_pSym, INTSXP, (A->n) + 1)),
79 :     lo = uplo_P(a)[0] == 'L',
80 :     bnz = 10 * A->n; /* initial estimate of nnz in b */
81 :     int *ti = Calloc(bnz, int), i, j, nz, pos = 0;
82 :     double *tx = Calloc(bnz, double), *wrk = Calloc(A->n, double);
83 :    
84 :     SET_SLOT(ans, Matrix_DimSym, duplicate(GET_SLOT(a, Matrix_DimSym)));
85 :     SET_SLOT(ans, Matrix_DimNamesSym,
86 :     duplicate(GET_SLOT(a, Matrix_DimNamesSym)));
87 :     SET_SLOT(ans, Matrix_uploSym, duplicate(GET_SLOT(a, Matrix_uploSym)));
88 :     SET_SLOT(ans, Matrix_diagSym, duplicate(GET_SLOT(a, Matrix_diagSym)));
89 :     bp[0] = 0;
90 :     for (j = 0; j < A->n; j++) {
91 :     AZERO(wrk, A->n);
92 :     wrk[j] = 1;
93 :     lo ? cs_lsolve(A, wrk) : cs_usolve(A, wrk);
94 :     for (i = 0, nz = 0; i < A->n; i++) if (wrk[i]) nz++;
95 :     bp[j + 1] = nz + bp[j];
96 :     if (bp[j + 1] > bnz) {
97 :     while (bp[j + 1] > bnz) bnz *= 2;
98 :     ti = Realloc(ti, bnz, int);
99 :     tx = Realloc(tx, bnz, double);
100 :     }
101 :     for (i = 0; i < A->n; i++)
102 :     if (wrk[i]) {ti[pos] = i; tx[pos] = wrk[i]; pos++;}
103 :     }
104 :     nz = bp[A->n];
105 :     Memcpy(INTEGER(ALLOC_SLOT(ans, Matrix_iSym, INTSXP, nz)), ti, nz);
106 :     Memcpy(REAL(ALLOC_SLOT(ans, Matrix_xSym, REALSXP, nz)), tx, nz);
107 :    
108 :     Free(A); Free(ti); Free(tx);
109 :     UNPROTECT(1);
110 :     return ans;
111 :     }
112 :    
113 :     SEXP dtCMatrix_matrix_solve(SEXP a, SEXP b, SEXP classed)
114 :     {
115 :     int cl = asLogical(classed);
116 :     SEXP ans = PROTECT(NEW_OBJECT(MAKE_CLASS("dgeMatrix")));
117 : maechler 1661 cs *A = Matrix_as_cs(a);
118 : bates 1251 int *adims = INTEGER(GET_SLOT(a, Matrix_DimSym)),
119 :     *bdims = INTEGER(cl ? GET_SLOT(b, Matrix_DimSym) :
120 :     getAttrib(b, R_DimSymbol));
121 :     int j, n = bdims[0], nrhs = bdims[1], lo = (*uplo_P(a) == 'L');
122 :     double *bx;
123 :    
124 :     if (*adims != n || nrhs < 1 || *adims < 1 || *adims != adims[1])
125 :     error(_("Dimensions of system to be solved are inconsistent"));
126 :     Memcpy(INTEGER(ALLOC_SLOT(ans, Matrix_DimSym, INTSXP, 2)), bdims, 2);
127 :     /* copy dimnames or Dimnames as well */
128 :     bx = Memcpy(REAL(ALLOC_SLOT(ans, Matrix_xSym, REALSXP, n * nrhs)),
129 :     REAL(cl ? GET_SLOT(b, Matrix_xSym):b), n * nrhs);
130 :     for (j = 0; j < nrhs; j++)
131 :     lo ? cs_lsolve(A, bx + n * j) : cs_usolve(A, bx + n * j);
132 :     Free(A);
133 :     UNPROTECT(1);
134 :     return ans;
135 :     }
136 : bates 1262
137 :     SEXP dtCMatrix_upper_solve(SEXP a)
138 :     {
139 :     SEXP ans = PROTECT(NEW_OBJECT(MAKE_CLASS("dtCMatrix")));
140 :     int lo = uplo_P(a)[0] == 'L', unit = diag_P(a)[0] == 'U',
141 : bates 1310 n = INTEGER(GET_SLOT(a, Matrix_DimSym))[0],
142 : bates 1262 *ai = INTEGER(GET_SLOT(a,Matrix_iSym)),
143 : maechler 1661 *ap = INTEGER(GET_SLOT(a, Matrix_pSym)),
144 : bates 1262 *bp = INTEGER(ALLOC_SLOT(ans, Matrix_pSym, INTSXP, n + 1));
145 :     int bnz = 10 * ap[n]; /* initial estimate of nnz in b */
146 :     int *ti = Calloc(bnz, int), j, nz;
147 :     double *ax = REAL(GET_SLOT(a, Matrix_xSym)), *tx = Calloc(bnz, double),
148 :     *tmp = Calloc(n, double);
149 : maechler 1661
150 : bates 1386 if (lo || (!unit))
151 :     error(_("Code written for unit upper triangular unit matrices"));
152 : bates 1262 bp[0] = 0;
153 :     for (j = 0; j < n; j++) {
154 :     int i, i1 = ap[j + 1];
155 :     AZERO(tmp, n);
156 :     for (i = ap[j]; i < i1; i++) {
157 :     int ii = ai[i], k;
158 :     if (unit) tmp[ii] -= ax[i];
159 :     for (k = bp[ii]; k < bp[ii + 1]; k++) tmp[ti[k]] -= ax[i] * tx[k];
160 :     }
161 :     for (i = 0, nz = 0; i < n; i++) if (tmp[i]) nz++;
162 :     bp[j + 1] = bp[j] + nz;
163 :     if (bp[j + 1] > bnz) {
164 :     while (bp[j + 1] > bnz) bnz *= 2;
165 :     ti = Realloc(ti, bnz, int);
166 :     tx = Realloc(tx, bnz, double);
167 :     }
168 :     i1 = bp[j];
169 :     for (i = 0; i < n; i++) if (tmp[i]) {ti[i1] = i; tx[i1] = tmp[i]; i1++;}
170 :     }
171 :     nz = bp[n];
172 :     Memcpy(INTEGER(ALLOC_SLOT(ans, Matrix_iSym, INTSXP, nz)), ti, nz);
173 :     Memcpy(REAL(ALLOC_SLOT(ans, Matrix_xSym, REALSXP, nz)), tx, nz);
174 :     Free(tmp); Free(tx); Free(ti);
175 :     SET_SLOT(ans, Matrix_DimSym, duplicate(GET_SLOT(a, Matrix_DimSym)));
176 :     SET_SLOT(ans, Matrix_DimNamesSym, duplicate(GET_SLOT(a, Matrix_DimNamesSym)));
177 :     SET_SLOT(ans, Matrix_uploSym, duplicate(GET_SLOT(a, Matrix_uploSym)));
178 :     SET_SLOT(ans, Matrix_diagSym, duplicate(GET_SLOT(a, Matrix_diagSym)));
179 :     UNPROTECT(1);
180 :     return ans;
181 :     }

R-Forge@R-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business University of Wisconsin - Madison Powered By FusionForge