SCM

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1209, Wed Jan 25 23:42:57 2006 UTC revision 1248, Thu Apr 13 22:05:22 2006 UTC
# Line 140  Line 140 
140      UNPROTECT(1);      UNPROTECT(1);
141      return ans;      return ans;
142  }  }
143    
144    SEXP dtCMatrix_solve(SEXP a)
145    {
146        SEXP ans = PROTECT(NEW_OBJECT(MAKE_CLASS("dtCMatrix")));
147        int lo = uplo_P(a)[0] == 'L', unit = diag_P(a)[0] == 'U',
148            n = INTEGER(GET_SLOT(a, Matrix_DimSym))[0], nnz,
149            *ai = INTEGER(GET_SLOT(a,Matrix_iSym)),
150            *ap = INTEGER(GET_SLOT(a, Matrix_pSym)), *bi,
151            *bp = INTEGER(ALLOC_SLOT(ans, Matrix_pSym, INTSXP, n + 1));
152        int bnz = 10 * ap[n];         /* initial estimate of nnz in b */
153        int *ri = Calloc(bnz, int), *ind = Calloc(n, int), j;
154        double *ax = REAL(GET_SLOT(a, Matrix_xSym)), *bx;
155    
156        SET_SLOT(ans, Matrix_DimSym, duplicate(GET_SLOT(a, Matrix_DimSym)));
157        SET_SLOT(ans, Matrix_DimNamesSym,
158                 duplicate(GET_SLOT(a, Matrix_DimNamesSym)));
159        SET_SLOT(ans, Matrix_uploSym, duplicate(GET_SLOT(a, Matrix_uploSym)));
160        SET_SLOT(ans, Matrix_diagSym, duplicate(GET_SLOT(a, Matrix_diagSym)));
161    
162        if (!(lo && unit))
163            error("code for non-unit or upper triangular not yet written");
164        /* Initially bp will contain increasing negative values ending at zero. */
165        /* Later we add the negative of bp[0] to all values. */
166        bp[n] = 0;
167        for (j = n - 1; j >= 0; j--) { /* columns in reverse order */
168            int i, i1 = ap[j], i2 = ap[j + 1], k, nr;
169            if (i1 < i2) AZERO(ind, n);
170            for (i = i1; i < i2; i++) {
171                ind[ai[i]] = 1;
172                for (k = -bp[ai[i] + 1]; k < -bp[ai[i]]; k++) ind[ri[k]] = 1;
173            }
174            for (k = 0, nr = 0; k < n; k++) if (ind[k]) nr++;
175            if ((nr - bp[j + 1]) > bnz) {
176                while (nr > (bnz + bp[j + 1])) bnz *= 2;
177                ri = Realloc(ri, bnz, int);
178            }
179            bp[j] = bp[j + 1] - nr;
180            for (k = 0, i = -bp[j + 1]; k < n; k++) if (ind[k]) ri[i++] = k;
181        }
182        bnz = -bp[0];
183        bi = INTEGER(ALLOC_SLOT(ans, Matrix_iSym, INTSXP, bnz));
184        bx = REAL(ALLOC_SLOT(ans, Matrix_xSym, REALSXP, bnz));
185        for (j = 0; j < n; j++) {
186            int bpnew = bp[j] + bnz;
187            Memcpy(bi + bpnew, ri - bp[j], bp[j + 1] - bp[j]);
188            bp[j] = bpnew;
189        }
190        /* insert code for calculating the actual values here */
191        for (j = 0; j < bnz; j++) bx[j] = NA_REAL;
192    
193        Free(ind); Free(ri);
194        UNPROTECT(1);
195        return ans;
196    }

Legend:
Removed from v.1209  
changed lines
  Added in v.1248

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