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 2586, Sun Jul 25 02:32:06 2010 UTC revision 2889, Thu Aug 8 21:06:22 2013 UTC
# Line 74  Line 74 
74      }      }
75  }  }
76    
 #undef RETURN  
   
77  SEXP dtCMatrix_matrix_solve(SEXP a, SEXP b, SEXP classed)  SEXP dtCMatrix_matrix_solve(SEXP a, SEXP b, SEXP classed)
78  {  {
79      int cl = asLogical(classed);      int cl = asLogical(classed);
# Line 96  Line 94 
94                  REAL(cl ? GET_SLOT(b, Matrix_xSym):b), n * nrhs);                  REAL(cl ? GET_SLOT(b, Matrix_xSym):b), n * nrhs);
95      for (j = 0; j < nrhs; j++)      for (j = 0; j < nrhs; j++)
96          lo ? cs_lsolve(A, bx + n * j) : cs_usolve(A, bx + n * j);          lo ? cs_lsolve(A, bx + n * j) : cs_usolve(A, bx + n * j);
97      UNPROTECT(1);      RETURN(ans);
     return ans;  
98  }  }
99    
100  SEXP dtCMatrix_sparse_solve(SEXP a, SEXP b)  SEXP dtCMatrix_sparse_solve(SEXP a, SEXP b)
101  {  {
102      SEXP ans = PROTECT(NEW_OBJECT(MAKE_CLASS("dgCMatrix")));      SEXP ans = PROTECT(NEW_OBJECT(MAKE_CLASS("dgCMatrix")));
103      CSP A = AS_CSP(a), B = AS_CSP(b);      CSP A = AS_CSP(a), B = AS_CSP(b);
     int *xp = INTEGER(ALLOC_SLOT(ans, Matrix_pSym, INTSXP, (B->n) + 1)),  
         xnz = 10 * B->p[B->n];  /* initial estimate of nnz in x */  
     int *ti = Calloc(xnz, int), k, lo = uplo_P(a)[0] == 'L', pos = 0;  
     double *tx = Calloc(xnz, double);  
     double  *wrk = Alloca(A->n, double);  
     int *xi = Alloca(2*A->n, int);      /* for cs_reach */  
104      R_CheckStack();      R_CheckStack();
   
105      if (A->m != A->n || B->n < 1 || A->n < 1 || A->n != B->m)      if (A->m != A->n || B->n < 1 || A->n < 1 || A->n != B->m)
106          error(_("Dimensions of system to be solved are inconsistent"));          error(_("Dimensions of system to be solved are inconsistent"));
107        // *before* Calloc()ing below [memory leak]!
108    
109        int *xp = INTEGER(ALLOC_SLOT(ans, Matrix_pSym, INTSXP, (B->n) + 1)),
110            xnz = 10 * B->p[B->n];  /* initial estimate of nnz in x */
111        int k, lo = uplo_P(a)[0] == 'L', pos = 0;
112        int    *ti = Calloc(xnz, int),     *xi = Calloc(2*A->n, int); /* for cs_reach */
113        double *tx = Calloc(xnz, double), *wrk = Calloc(  A->n, double);
114    
115      slot_dup(ans, b, Matrix_DimSym);      slot_dup(ans, b, Matrix_DimSym);
116      SET_DimNames(ans, b);      SET_DimNames(ans, b);
117      xp[0] = 0;      xp[0] = 0;
118      for (k = 0; k < B->n; k++) {      for (k = 0; k < B->n; k++) {
119          int top = cs_spsolve (A, B, k, xi, wrk, (int *)NULL, lo);          int top = cs_spsolve (A, B, k, xi, wrk, (int *)NULL, lo);
120          int nz = A->n - top, p;          int nz = A->n - top;
121    
122          xp[k + 1] = nz + xp[k];          xp[k + 1] = nz + xp[k];
123          if (xp[k + 1] > xnz) {          if (xp[k + 1] > xnz) {
# Line 128  Line 126 
126              tx = Realloc(tx, xnz, double);              tx = Realloc(tx, xnz, double);
127          }          }
128          if (lo)                 /* increasing row order */          if (lo)                 /* increasing row order */
129              for(p = top; p < A->n; p++, pos++) {              for(int p = top; p < A->n; p++, pos++) {
130                  ti[pos] = xi[p];                  ti[pos] = xi[p];
131                  tx[pos] = wrk[xi[p]];                  tx[pos] = wrk[xi[p]];
132              }              }
133          else                    /* decreasing order, reverse copy */          else                    /* decreasing order, reverse copy */
134              for(p = A->n - 1; p >= top; p--, pos++) {              for(int p = A->n - 1; p >= top; p--, pos++) {
135                  ti[pos] = xi[p];                  ti[pos] = xi[p];
136                  tx[pos] = wrk[xi[p]];                  tx[pos] = wrk[xi[p]];
137              }              }
# Line 143  Line 141 
141      Memcpy(   REAL(ALLOC_SLOT(ans, Matrix_xSym, REALSXP, xnz)), tx, xnz);      Memcpy(   REAL(ALLOC_SLOT(ans, Matrix_xSym, REALSXP, xnz)), tx, xnz);
142    
143      Free(ti); Free(tx);      Free(ti); Free(tx);
144      UNPROTECT(1);      Free(wrk); Free(xi);
145      return ans;  
146        RETURN(ans);
147  }  }
148    #undef RETURN
149    

Legend:
Removed from v.2586  
changed lines
  Added in v.2889

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge