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 1575, Mon Sep 18 14:47:40 2006 UTC revision 1960, Fri Jul 6 16:54:43 2007 UTC
# Line 5  Line 5 
5  SEXP Csparse_validate(SEXP x)  SEXP Csparse_validate(SEXP x)
6  {  {
7      /* NB: we do *NOT* check a potential 'x' slot here, at all */      /* NB: we do *NOT* check a potential 'x' slot here, at all */
     cholmod_sparse *chx = as_cholmod_sparse(x);  
8      SEXP pslot = GET_SLOT(x, Matrix_pSym),      SEXP pslot = GET_SLOT(x, Matrix_pSym),
9          islot = GET_SLOT(x, Matrix_iSym);          islot = GET_SLOT(x, Matrix_iSym);
10      int j, k, ncol = length(pslot) - 1,      Rboolean sorted, strictly;
11        int j, k,
12          *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),          *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)),
13          nrow, sorted, *xp = INTEGER(pslot),          nrow = dims[0],
14            ncol = dims[1],
15            *xp = INTEGER(pslot),
16          *xi = INTEGER(islot);          *xi = INTEGER(islot);
17    
18      nrow = dims[0];      if (length(pslot) != dims[1] + 1)
19      if (length(pslot) <= 0)          return mkString(_("slot p must have length = ncol(.) + 1"));
         return mkString(_("slot p must have length > 0"));  
20      if (xp[0] != 0)      if (xp[0] != 0)
21          return mkString(_("first element of slot p must be zero"));          return mkString(_("first element of slot p must be zero"));
22      if (length(islot) != xp[ncol])      if (length(islot) < xp[ncol]) /* allow larger slots from over-allocation!*/
23          return          return
24              mkString(_("last element of slot p must match length of slots i and x"));              mkString(_("last element of slot p must match length of slots i and x"));
25      for (j = 0; j < length(islot); j++) {      for (j = 0; j < length(islot); j++) {
26          if (xi[j] < 0 || xi[j] >= nrow)          if (xi[j] < 0 || xi[j] >= nrow)
27              return mkString(_("all row indices must be between 0 and nrow-1"));              return mkString(_("all row indices must be between 0 and nrow-1"));
28      }      }
29      sorted = TRUE;      sorted = TRUE; strictly = TRUE;
30      for (j = 0; j < ncol; j++) {      for (j = 0; j < ncol; j++) {
31          if (xp[j] > xp[j+1])          if (xp[j] > xp[j+1])
32              return mkString(_("slot p must be non-decreasing"));              return mkString(_("slot p must be non-decreasing"));
33            if(sorted)
34                for (k = xp[j] + 1; k < xp[j + 1]; k++) {
35                    if (xi[k] < xi[k - 1])
36                        sorted = FALSE;
37                    else if (xi[k] == xi[k - 1])
38                        strictly = FALSE;
39                }
40        }
41        if (!sorted) {
42            CHM_SP chx = AS_CHM_SP(x);
43            R_CheckStack();
44    
45            cholmod_sort(chx, &c);
46            /* Now re-check that row indices are *strictly* increasing
47             * (and not just increasing) within each column : */
48            for (j = 0; j < ncol; j++) {
49          for (k = xp[j] + 1; k < xp[j + 1]; k++)          for (k = xp[j] + 1; k < xp[j + 1]; k++)
50              if (xi[k] < xi[k - 1]) sorted = FALSE;                  if (xi[k] == xi[k - 1])
51                        return mkString(_("slot i is not *strictly* increasing inside a column (even after cholmod_sort)"));
52            }
53    
54        } else if(!strictly) {  /* sorted, but not strictly */
55            return mkString(_("slot i is not *strictly* increasing inside a column"));
56      }      }
     if (!sorted) cholmod_sort(chx, &c);  
     Free(chx);  
57      return ScalarLogical(1);      return ScalarLogical(1);
58  }  }
59    
60    /* Called from ../R/Csparse.R : */
61    /* Can only return [dln]geMatrix (no symm/triang);
62     * 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      cholmod_dense *chxd = cholmod_sparse_to_dense(chxs, &c);      /* This loses the symmetry property, since cholmod_dense has none,
67         * BUT, much worse (FIXME!), it also transforms CHOLMOD_PATTERN ("n") matrices
68         * to numeric (CHOLMOD_REAL) ones : */
69        CHM_DN chxd = cholmod_sparse_to_dense(chxs, &c);
70        int Rkind = (chxs->xtype == CHOLMOD_PATTERN)? -1 : Real_kind(x);
71        R_CheckStack();
72    
73      Free(chxs);      return chm_dense_to_SEXP(chxd, 1, Rkind, GET_SLOT(x, Matrix_DimNamesSym));
     return chm_dense_to_SEXP(chxd, 1, Real_kind(x));  
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);
80          *chxcp = cholmod_copy(chxs, chxs->stype, CHOLMOD_PATTERN, &c);      int tr = asLogical(tri);
81      int uploT = 0; char *diag = "";      R_CheckStack();
82    
83      Free(chxs);      return chm_sparse_to_SEXP(chxcp, 1/*do_free*/,
84      if (asLogical(tri)) {       /* triangular sparse matrices */                                tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
85          uploT = (strcmp(CHAR(asChar(GET_SLOT(x, Matrix_uploSym))), "U")) ?                                0, tr ? diag_P(x) : "",
             -1 : 1;  
         diag = CHAR(asChar(GET_SLOT(x, Matrix_diagSym)));  
     }  
     return chm_sparse_to_SEXP(chxcp, 1, uploT, 0, diag,  
86                                GET_SLOT(x, Matrix_DimNamesSym));                                GET_SLOT(x, Matrix_DimNamesSym));
87  }  }
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 uploT = 0;      int tr = asLogical(tri);
100      char *diag = "";      int Rkind = (chxs->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
101      int Rkind = (chxs->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;      R_CheckStack();
102    
103      Free(chxs);      return chm_triplet_to_SEXP(chxt, 1,
104      if (asLogical(tri)) {       /* triangular sparse matrices */                                 tr ? ((*uplo_P(x) == 'U') ? 1 : -1) : 0,
105          uploT = (*uplo_P(x) == 'U') ? -1 : 1;                                 Rkind, tr ? diag_P(x) : "",
         diag = diag_P(x);  
     }  
     return chm_triplet_to_SEXP(chxt, 1, uploT, Rkind, diag,  
106                                 GET_SLOT(x, Matrix_DimNamesSym));                                 GET_SLOT(x, Matrix_DimNamesSym));
107  }  }
108    
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_REAL) ? 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 .. */
120      Free(chx);      return chm_sparse_to_SEXP(chgx, 1, 0, Rkind, "",
121                                  GET_SLOT(x, Matrix_DimNamesSym));
122    }
123    
124    SEXP Csparse_general_to_symmetric(SEXP x, SEXP uplo)
125    {
126        CHM_SP chx = AS_CHM_SP(x), chgx;
127        int uploT = (*CHAR(asChar(uplo)) == 'U') ? 1 : -1;
128        int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
129        R_CheckStack();
130    
131        chgx = cholmod_copy(chx, /* stype: */ uploT, chx->xtype, &c);
132        /* xtype: pattern, "real", complex or .. */
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  }  }
136    
137  SEXP Csparse_transpose(SEXP x, SEXP tri)  SEXP Csparse_transpose(SEXP x, SEXP tri)
138  {  {
139      cholmod_sparse *chx = as_cholmod_sparse(x);      /* TODO: lgCMatrix & igC* currently go via double prec. cholmod -
140      int Rkind = (chx->xtype == CHOLMOD_REAL) ? Real_kind(x) : 0;       *       since cholmod (& cs) lacks sparse 'int' matrices */
141      cholmod_sparse *chxt = cholmod_transpose(chx, (int) chx->xtype, &c);      CHM_SP chx = AS_CHM_SP(x);
142        int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
143        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 uploT = 0; char *diag = "";      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);
151      UNPROTECT(1);      UNPROTECT(1);
152      if (asLogical(tri)) {       /* triangular sparse matrices */      return chm_sparse_to_SEXP(chxt, 1, /* SWAP 'uplo' for triangular */
153          uploT = (*uplo_P(x) == 'U') ? -1 : 1;                                tr ? ((*uplo_P(x) == 'U') ? -1 : 1) : 0,
154          diag = diag_P(x);                                Rkind, tr ? diag_P(x) : "", dn);
     }  
     return chm_sparse_to_SEXP(chxt, 1, uploT, Rkind, diag, dn);  
155  }  }
156    
157  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)  SEXP Csparse_Csparse_prod(SEXP a, SEXP b)
158  {  {
159      cholmod_sparse *cha = as_cholmod_sparse(a),      CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b);
160          *chb = as_cholmod_sparse(b);      CHM_SP chc = cholmod_ssmult(cha, chb, 0, cha->xtype, 1, &c);
     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 140  Line 168 
168      return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);      return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);
169  }  }
170    
171    SEXP Csparse_Csparse_crossprod(SEXP a, SEXP b, SEXP trans)
172    {
173        int tr = asLogical(trans);
174        CHM_SP cha = AS_CHM_SP(a), chb = AS_CHM_SP(b), chTr, chc;
175        SEXP dn = allocVector(VECSXP, 2);
176        R_CheckStack();
177    
178        chTr = cholmod_transpose((tr) ? chb : cha, chb->xtype, &c);
179        chc = cholmod_ssmult((tr) ? cha : chTr, (tr) ? chTr : chb,
180                             0, cha->xtype, 1, &c);
181        cholmod_free_sparse(&chTr, &c);
182    
183        SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
184                       duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), (tr) ? 0 : 1)));
185        SET_VECTOR_ELT(dn, 1,
186                       duplicate(VECTOR_ELT(GET_SLOT(b, Matrix_DimNamesSym), (tr) ? 0 : 1)));
187        return chm_sparse_to_SEXP(chc, 1, 0, 0, "", dn);
188    }
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      cholmod_dense *chb = as_cholmod_dense(PROTECT(mMatrix_as_dgeMatrix(b)));      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
194      cholmod_dense *chc =      CHM_DN chb = AS_CHM_DN(b_M);
195          cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow, chb->xtype, &c);      CHM_DN chc = cholmod_allocate_dense(cha->nrow, chb->ncol, cha->nrow,
196      double alpha[] = {1,0}, beta[] = {0,0};                                          chb->xtype, &c);
197        SEXP dn = PROTECT(allocVector(VECSXP, 2));
198        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);
202      Free(cha); Free(chb);      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
203      UNPROTECT(1);                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 0)));
204      return chm_dense_to_SEXP(chc, 1, 0);      SET_VECTOR_ELT(dn, 1,
205                       duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));
206        UNPROTECT(2);
207        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      cholmod_dense *chb = as_cholmod_dense(PROTECT(mMatrix_as_dgeMatrix(b)));      SEXP b_M = PROTECT(mMatrix_as_dgeMatrix(b));
214      cholmod_dense *chc =      CHM_DN chb = AS_CHM_DN(b_M);
215          cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol, chb->xtype, &c);      CHM_DN chc = cholmod_allocate_dense(cha->ncol, chb->ncol, cha->ncol,
216      double alpha[] = {1,0}, beta[] = {0,0};                                          chb->xtype, &c);
217        SEXP dn = PROTECT(allocVector(VECSXP, 2));
218        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);
222      Free(cha); Free(chb);      SET_VECTOR_ELT(dn, 0,       /* establish dimnames */
223      UNPROTECT(1);                     duplicate(VECTOR_ELT(GET_SLOT(a, Matrix_DimNamesSym), 1)));
224      return chm_dense_to_SEXP(chc, 1, 0);      SET_VECTOR_ELT(dn, 1,
225                       duplicate(VECTOR_ELT(GET_SLOT(b_M, Matrix_DimNamesSym), 1)));
226        UNPROTECT(2);
227        return chm_dense_to_SEXP(chc, 1, 0, dn);
228  }  }
229    
230    /* Computes   x'x  or  x x'  -- see Csparse_Csparse_crossprod above for  x'y and x y' */
231  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)  SEXP Csparse_crossprod(SEXP x, SEXP trans, SEXP triplet)
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) ? 1 : 0)));                                          (tr) ? 0 : 1)));
251      SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));      SET_VECTOR_ELT(dn, 1, duplicate(VECTOR_ELT(dn, 0)));
252      UNPROTECT(1);      UNPROTECT(1);
253      return chm_sparse_to_SEXP(chcp, 1, 0, 0, "", dn);      return chm_sparse_to_SEXP(chcp, 1, 0, 0, "", dn);
254  }  }
255    
256    SEXP Csparse_drop(SEXP x, SEXP tol)
257    {
258        CHM_SP chx = AS_CHM_SP(x);
259        CHM_SP ans = cholmod_copy(chx, chx->stype, chx->xtype, &c);
260        double dtol = asReal(tol);
261        int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
262        R_CheckStack();
263    
264        if(!cholmod_drop(dtol, ans, &c))
265            error(_("cholmod_drop() failed"));
266        return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
267                                  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_REAL) ? 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    
299      ans = cholmod_band(chx, asInteger(k1), asInteger(k2), chx->xtype, &c);      return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "",
300      Free(chx);                                GET_SLOT(x, Matrix_DimNamesSym));
     return chm_sparse_to_SEXP(ans, 1, 0, Rkind, "", R_NilValue);  
301  }  }
302    
303  SEXP Csparse_diagU2N(SEXP x)  SEXP Csparse_diagU2N(SEXP x)
304  {  {
305      cholmod_sparse *chx = as_cholmod_sparse(x);      if (*diag_P(x) != 'U') {/* "trivially fast" when there's no 'diag' slot at all */
306      cholmod_sparse *eye = cholmod_speye(chx->nrow, chx->ncol, chx->xtype, &c);          return (x);
307        }
308        else {
309            CHM_SP chx = AS_CHM_SP(x);
310            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 = (strcmp(CHAR(asChar(GET_SLOT(x, Matrix_uploSym))), "U")) ?          int uploT = (*uplo_P(x) == 'U') ? 1 : -1;
314          -1 : 1;          int Rkind = (chx->xtype != CHOLMOD_PATTERN) ? Real_kind(x) : 0;
     int Rkind = (chx->xtype == CHOLMOD_REAL) ? 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                                duplicate(GET_SLOT(x, Matrix_DimNamesSym)));                                    GET_SLOT(x, Matrix_DimNamesSym));
320        }
321  }  }
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_REAL) ? 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"));
333      if (csize >= 0 && !isInteger(j))      if (csize >= 0 && !isInteger(j))
334          error(_("Index j must be NULL or integer"));          error(_("Index j must be NULL or integer"));
335    
336      return chm_sparse_to_SEXP(cholmod_submatrix(chx, INTEGER(i), rsize,      return chm_sparse_to_SEXP(cholmod_submatrix(chx, INTEGER(i), rsize,
337                                                  INTEGER(j), csize,                                                  INTEGER(j), csize,
338                                                  TRUE, TRUE, &c),                                                  TRUE, TRUE, &c),
339                                1, 0, Rkind, "", R_NilValue);                                1, 0, Rkind, "",
340                                  /* FIXME: drops dimnames */ R_NilValue);
341  }  }

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

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