SCM

SCM Repository

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

Diff of /pkg/src/Csparse.c

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

revision 1959, Fri Jul 6 16:40:47 2007 UTC revision 1960, Fri Jul 6 16:54:43 2007 UTC
# Line 39  Line 39 
39              }              }
40      }      }
41      if (!sorted) {      if (!sorted) {
42          cholmod_sparse *chx = as_cholmod_sparse(x);          CHM_SP chx = AS_CHM_SP(x);
43            R_CheckStack();
44    
45          cholmod_sort(chx, &c);          cholmod_sort(chx, &c);
         Free(chx);  
46          /* Now re-check that row indices are *strictly* increasing          /* Now re-check that row indices are *strictly* increasing
47           * (and not just increasing) within each column : */           * (and not just increasing) within each column : */
48          for (j = 0; j < ncol; j++) {          for (j = 0; j < ncol; j++) {
# Line 61  Line 62 
62   * FIXME: replace by non-CHOLMOD code ! */   * FIXME: replace by non-CHOLMOD code ! */
63  SEXP Csparse_to_dense(SEXP x)  SEXP Csparse_to_dense(SEXP x)
64  {  {
65      cholmod_sparse *chxs = as_cholmod_sparse(x);      CHM_SP chxs = AS_CHM_SP(x);
66      /* This loses the symmetry property, since cholmod_dense has none,      /* This loses the symmetry property, since cholmod_dense has none,
67       * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices       * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices
68       * to numeric (CHOLMOD_REAL) ones : */       * to numeric (CHOLMOD_REAL) ones : */
69      cholmod_dense *chxd = cholmod_sparse_to_dense(chxs, &c);      CHM_DN chxd = cholmod_sparse_to_dense(chxs, &c);
70      int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);      int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);
71        R_CheckStack();
72    
     Free(chxs);  
73      return chm_dense_to_SEXP(chxd, 1, Rkind, GET_SLOT(x, Matrix_DimNamesSym));      return chm_dense_to_SEXP(chxd, 1, Rkind, GET_SLOT(x, Matrix_DimNamesSym));
74  }  }
75    
76  SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)  SEXP Csparse_to_nz_pattern(SEXP x, SEXP tri)
77  {  {
78      cholmod_sparse *chxs = as_cholmod_sparse(x);      CHM_SP chxs = AS_CHM_SP(x);
79      cholmod_sparse      CHM_SP chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);
         *chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);  
80      int tr = asLogical(tri);      int tr = asLogical(tri);
81        R_CheckStack();
82    
83      Free(chxs);      return chm_sparse_to_SEXP(chxcp, 1/*do_free*/,
     return chm_sparse_to_SEXP(chxcp, 1,  
84                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
85                                0, tr ? diag_P(x) : "",                                0, tr ? diag_P(x) : "",
86                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
# Line 88  Line 88 
88    
89  SEXP Csparse_to_matrix(SEXP x)  SEXP Csparse_to_matrix(SEXP x)
90  {  {
91      cholmod_sparse *chxs = as_cholmod_sparse(x);      return chm_dense_to_matrix(cholmod_sparse_to_dense(AS_CHM_SP(x), &c),
92      cholmod_dense *chxd = cholmod_sparse_to_dense(chxs, &c);                                 1 /*do_free*/, GET_SLOT(x, Matrix_DimNamesSym));
   
     Free(chxs);  
     return chm_dense_to_matrix(chxd, 1,  
                                GET_SLOT(x, Matrix_DimNamesSym));  
93  }  }
94    
95  SEXP Csparse_to_Tsparse(SEXP x, SEXP tri)  SEXP Csparse_to_Tsparse(SEXP x, SEXP tri)
96  {  {
97      cholmod_sparse *chxs = as_cholmod_sparse(x);      CHM_SP chxs = AS_CHM_SP(x);
98      cholmod_triplet *chxt = cholmod_sparse_to_triplet(chxs, &c);      CHM_TR chxt = cholmod_sparse_to_triplet(chxs, &c);
99      int tr = asLogical(tri);      int tr = asLogical(tri);
100      int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
101        R_CheckStack();
102    
     Free(chxs);  
103      return chm_triplet_to_SEXP(chxt, 1,      return chm_triplet_to_SEXP(chxt, 1,
104                                 tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,                                 tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
105                                 Rkind, tr ? diag_P(x) : "",                                 Rkind, tr ? diag_P(x) : "",
# Line 113  Line 109 
109  /* this used to be called  sCMatrix_to_gCMatrix(..)   [in ./dsCMatrix.c ]: */  /* this used to be called  sCMatrix_to_gCMatrix(..)   [in ./dsCMatrix.c ]: */
110  SEXP Csparse_symmetric_to_general(SEXP x)  SEXP Csparse_symmetric_to_general(SEXP x)
111  {  {
112      cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;      CHM_SP chx = AS_CHM_SP(x), chgx;
113      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
114        R_CheckStack();
115    
116      if (!(chx->stype))      if (!(chx->stype))
117          error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));          error(_("Nonsymmetric matrix in Csparse_symmetric_to_general"));
118      chgx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);      chgx = cholmod_copy(chx, /* stype: */ 0, chx->xtype, &c);
119      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
     Free(chx);  
120      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
121                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
122  }  }
123    
124  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)  SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
125  {  {
126      cholmod_sparse *chx = as_cholmod_sparse(x), *chgx;      CHM_SP chx = AS_CHM_SP(x), chgx;
127      int uploT = (*CHAR(asChar(uplo)) == 'U') ? 1 : -1;      int uploT = (*CHAR(asChar(uplo)) == 'U') ? 1 : -1;
128      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
129        R_CheckStack();
130    
131      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);      chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
132      /* xtype: pattern, "real", complex or .. */      /* xtype: pattern, "real", complex or .. */
     Free(chx);  
133      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
134                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
135  }  }
# Line 142  Line 138 
138  {  {
139      /* TODO: lgCMatrix & igC* currently go via double prec. cholmod -      /* TODO: lgCMatrix & igC* currently go via double prec. cholmod -
140       *       since cholmod (& cs) lacks sparse 'int' matrices */       *       since cholmod (& cs) lacks sparse 'int' matrices */
141      cholmod_sparse *chx = as_cholmod_sparse(x);      CHM_SP chx = AS_CHM_SP(x);
142      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
143      cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c);      CHM_SP chxt = cholmod_transpose(chx, chx->xtype, &c);
144      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;      SEXP dn = PROTECT(duplicate(GET_SLOT(x, Matrix_DimNamesSym))), tmp;
145      int tr = asLogical(tri);      int tr = asLogical(tri);
146        R_CheckStack();
147    
     Free(chx);  
148      tmp = VECTOR_ELT(dn, 0);    /* swap the dimnames */      tmp = VECTOR_ELT(dn, 0);    /* swap the dimnames */
149      SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));      SET_VECTOR_ELT(dn, 0, VECTOR_ELT(dn, 1));
150      SET_VECTOR_ELT(dn, 1, tmp);      SET_VECTOR_ELT(dn, 1, tmp);
# Line 160  Line 156 
156    
157  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)
158  {  {
159      cholmod_sparse      CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b);
160          *cha = as_cholmod_sparse(a),      CHM_SP chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);
         *chb = as_cholmod_sparse(b);  
     cholmod_sparse *chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);  
161      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = allocVector(VECSXP, 2);
162        R_CheckStack();
163    
     Free(cha); Free(chb);  
164      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
165                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
166      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
# Line 177  Line 171 
171  SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)  SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)
172  {  {
173      int tr = asLogical(trans);      int tr = asLogical(trans);
174      cholmod_sparse      CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b), chTr, chc;
         *cha = as_cholmod_sparse(a),  
         *chb = as_cholmod_sparse(b);  
     cholmod_sparse *chTr, *chc;  
175      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = allocVector(VECSXP, 2);
176        R_CheckStack();
177    
178  /*     cholmod_sparse *chTr = cholmod_transpose(cha, 1, &c); */      chTr = cholmod_transpose((tr) ? chb : cha, chb->xtype, &c);
 /*     cholmod_sparse *chc = cholmod_ssmult(chTr, chb, 0, cha->xtype, 1, &c); */  
   
     if (tr)  
         chTr = cholmod_transpose(chb, chb->xtype, &c);  
     else  
         chTr = cholmod_transpose(cha, cha->xtype, &c);  
179      chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,      chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,
180                           0, cha->xtype, 1, &c);                           0, cha->xtype, 1, &c);
181        cholmod_free_sparse(&chTr, &c);
     Free(cha); Free(chb); cholmod_free_sparse(&chTr, &c);  
182    
183      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
184                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), (tr) ? 0 : 1)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), (tr) ? 0 : 1)));
# Line 204  Line 189 
189    
190  SEXP Csparse_dense_prod(SEXP a, SEXP b)  SEXP Csparse_dense_prod(SEXP a, SEXP b)
191  {  {
192      cholmod_sparse *cha = as_cholmod_sparse(a);      CHM_SP cha = AS_CHM_SP(a);
193      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
194      cholmod_dense *chb = as_cholmod_dense(b_M);      CHM_DN chb = AS_CHM_DN(b_M);
195      cholmod_dense *chc =      CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow,
196          cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow, chb->xtype, &c);                                          chb->xtype, &c);
197      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = PROTECT(allocVector(VECSXP, 2));
198      double alpha[] = {1,0}, beta[] = {0,0};      double one[] = {1,0}, zero[] = {0,0};
199        R_CheckStack();
200    
201      cholmod_sdmult(cha, 0, alpha, beta, chb, chc, &c);      cholmod_sdmult(cha, 0, one, zero, chb, chc, &c);
     Free(cha); Free(chb);  
     UNPROTECT(1);  
202      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
203                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
204      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
205                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));
206        UNPROTECT(2);
207      return chm_dense_to_SEXP(chc, 1, 0, dn);      return chm_dense_to_SEXP(chc, 1, 0, dn);
208  }  }
209    
210  SEXP Csparse_dense_crossprod(SEXP a, SEXP b)  SEXP Csparse_dense_crossprod(SEXP a, SEXP b)
211  {  {
212      cholmod_sparse *cha = as_cholmod_sparse(a);      CHM_SP cha = AS_CHM_SP(a);
213      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
214      cholmod_dense *chb = as_cholmod_dense(b_M);      CHM_DN chb = AS_CHM_DN(b_M);
215      cholmod_dense *chc =      CHM_DN chc = cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol,
216          cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol, chb->xtype, &c);                                          chb->xtype, &c);
217      SEXP dn = allocVector(VECSXP, 2);      SEXP dn = PROTECT(allocVector(VECSXP, 2));
218      double alpha[] = {1,0}, beta[] = {0,0};      double one[] = {1,0}, zero[] = {0,0};
219        R_CheckStack();
220    
221      cholmod_sdmult(cha, 1, alpha, beta, chb, chc, &c);      cholmod_sdmult(cha, 1, one, zero, chb, chc, &c);
     Free(cha); Free(chb);  
     UNPROTECT(1);  
222      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
223                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));
224      SET_VECTOR_ELT(dn, 1,      SET_VECTOR_ELT(dn, 1,
225                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));                     duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));
226        UNPROTECT(2);
227      return chm_dense_to_SEXP(chc, 1, 0, dn);      return chm_dense_to_SEXP(chc, 1, 0, dn);
228  }  }
229    
# Line 247  Line 232 
232  {  {
233      int trip = asLogical(triplet),      int trip = asLogical(triplet),
234          tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */          tr   = asLogical(trans); /* gets reversed because _aat is tcrossprod */
235      cholmod_triplet      CHM_TR cht = trip ? AS_CHM_TR(x) : (CHM_TR) NULL;
236          *cht = trip ? as_cholmod_triplet(x) : (cholmod_triplet*) NULL;      CHM_SP chcp, chxt,
237      cholmod_sparse *chcp, *chxt,          chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c) : AS_CHM_SP(x);
         *chx = trip ? cholmod_triplet_to_sparse(cht, cht->nnz, &c)  
         : as_cholmod_sparse(x);  
238      SEXP dn = PROTECT(allocVector(VECSXP, 2));      SEXP dn = PROTECT(allocVector(VECSXP, 2));
239        R_CheckStack();
240    
241      if (!tr)      if (!tr) chxt = cholmod_transpose(chx, chx->xtype, &c);
         chxt = cholmod_transpose(chx, chx->xtype, &c);  
242      chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);      chcp = cholmod_aat((!tr) ? chxt : chx, (int *) NULL, 0, chx->xtype, &c);
243      if(!chcp)      if(!chcp) error(_("Csparse_crossprod(): error return from cholmod_aat()"));
         error(_("Csparse_crossprod(): error return from cholmod_aat()"));  
244      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);      cholmod_band_inplace(0, chcp->ncol, chcp->xtype, chcp, &c);
245      chcp->stype = 1;      chcp->stype = 1;
246      if (trip) {      if (trip) cholmod_free_sparse(&chx, &c);
         cholmod_free_sparse(&chx, &c);  
         Free(cht);  
     } else {  
         Free(chx);  
     }  
247      if (!tr) cholmod_free_sparse(&chxt, &c);      if (!tr) cholmod_free_sparse(&chxt, &c);
248                                  /* create dimnames */      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
     SET_VECTOR_ELT(dn, 0,  
249                     duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),                     duplicate(VECTOR_ELT(GET_SLOT(x, Matrix_DimNamesSym),
250                                          (tr) ? 0 : 1)));                                          (tr) ? 0 : 1)));
251      SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));      SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));
# Line 279  Line 255 
255    
256  SEXP Csparse_drop(SEXP x, SEXP tol)  SEXP Csparse_drop(SEXP x, SEXP tol)
257  {  {
258      cholmod_sparse *chx = as_cholmod_sparse(x),      CHM_SP chx = AS_CHM_SP(x);
259          *ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);      CHM_SP ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);
260      double dtol = asReal(tol);      double dtol = asReal(tol);
261      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
262        R_CheckStack();
263    
264      if(!cholmod_drop(dtol, ans, &c))      if(!cholmod_drop(dtol, ans, &c))
265          error(_("cholmod_drop() failed"));          error(_("cholmod_drop() failed"));
     Free(chx);  
266      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
267                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
268  }  }
269    
   
270  SEXP Csparse_horzcat(SEXP x, SEXP y)  SEXP Csparse_horzcat(SEXP x, SEXP y)
271  {  {
272      cholmod_sparse *chx = as_cholmod_sparse(x),      CHM_SP chx = AS_CHM_SP(x), chy = AS_CHM_SP(y);
         *chy = as_cholmod_sparse(y), *ans;  
273      int Rkind = 0; /* only for "d" - FIXME */      int Rkind = 0; /* only for "d" - FIXME */
274        R_CheckStack();
275    
     ans = cholmod_horzcat(chx, chy, 1, &c);  
     Free(chx); Free(chy);  
276      /* FIXME: currently drops dimnames */      /* FIXME: currently drops dimnames */
277      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);      return chm_sparse_to_SEXP(cholmod_horzcat(chx, chy, 1, &c),
278                                  1, 0, Rkind, "", R_NilValue);
279  }  }
280    
281  SEXP Csparse_vertcat(SEXP x, SEXP y)  SEXP Csparse_vertcat(SEXP x, SEXP y)
282  {  {
283      cholmod_sparse *chx = as_cholmod_sparse(x),      CHM_SP chx = AS_CHM_SP(x), chy = AS_CHM_SP(y);
         *chy = as_cholmod_sparse(y), *ans;  
284      int Rkind = 0; /* only for "d" - FIXME */      int Rkind = 0; /* only for "d" - FIXME */
285        R_CheckStack();
286    
     ans = cholmod_vertcat(chx, chy, 1, &c);  
     Free(chx); Free(chy);  
287      /* FIXME: currently drops dimnames */      /* FIXME: currently drops dimnames */
288      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);      return chm_sparse_to_SEXP(cholmod_vertcat(chx, chy, 1, &c),
289                                  1, 0, Rkind, "", R_NilValue);
290  }  }
291    
292  SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2)  SEXP Csparse_band(SEXP x, SEXP k1, SEXP k2)
293  {  {
294      cholmod_sparse *chx = as_cholmod_sparse(x), *ans;      CHM_SP chx = AS_CHM_SP(x);
295      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
296        CHM_SP ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);
297        R_CheckStack();
298    
     ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);  
     Free(chx);  
299      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
300                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
301  }  }
# Line 333  Line 306 
306          return (x);          return (x);
307      }      }
308      else {      else {
309          cholmod_sparse *chx = as_cholmod_sparse(x);          CHM_SP chx = AS_CHM_SP(x);
310          cholmod_sparse *eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c);          CHM_SP eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c);
311          double one[] = {1, 0};          double one[] = {1, 0};
312          cholmod_sparse *ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);          CHM_SP ans = cholmod_add(chx, eye, one, one, TRUE, TRUE, &c);
313          int uploT = (*uplo_P(x) == 'U') ? 1 : -1;          int uploT = (*uplo_P(x) == 'U') ? 1 : -1;
314          int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;          int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
315    
316          Free(chx); cholmod_free_sparse(&eye, &c);          R_CheckStack();
317            cholmod_free_sparse(&eye, &c);
318          return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",          return chm_sparse_to_SEXP(ans, 1, uploT, Rkind, "N",
319                                    GET_SLOT(x, Matrix_DimNamesSym));                                    GET_SLOT(x, Matrix_DimNamesSym));
320      }      }
# Line 348  Line 322 
322    
323  SEXP Csparse_submatrix(SEXP x, SEXP i, SEXP j)  SEXP Csparse_submatrix(SEXP x, SEXP i, SEXP j)
324  {  {
325      cholmod_sparse *chx = as_cholmod_sparse(x);      CHM_SP chx = AS_CHM_SP(x);
326      int rsize = (isNull(i)) ? -1 : LENGTH(i),      int rsize = (isNull(i)) ? -1 : LENGTH(i),
327          csize = (isNull(j)) ? -1 : LENGTH(j);          csize = (isNull(j)) ? -1 : LENGTH(j);
328      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;      int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
329        R_CheckStack();
330    
331      if (rsize >= 0 && !isInteger(i))      if (rsize >= 0 && !isInteger(i))
332          error(_("Index i must be NULL or integer"));          error(_("Index i must be NULL or integer"));

Legend:
Removed from v.1959  
changed lines
  Added in v.1960

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