SCM

SCM Repository

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

Annotation of /pkg/src/dsTMatrix.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 589 - (view) (download) (as text)

1 : bates 557 /* Sparse symmetric matrices in triplet format */
2 :     #include "dsTMatrix.h"
3 :    
4 :     SEXP dsTMatrix_validate(SEXP x)
5 :     {
6 :     SEXP xiP = GET_SLOT(x, Matrix_iSym),
7 :     xjP = GET_SLOT(x, Matrix_jSym),
8 :     xxP = GET_SLOT(x, Matrix_xSym);
9 :     int *dims = INTEGER(GET_SLOT(x, Matrix_DimSym));
10 :    
11 :     if (dims[0] != dims[1])
12 : bates 589 return mkString(_("dsTMatrix must be square"));
13 : bates 557 if (length(xiP) != length(xjP) || length(xjP) != length(xxP))
14 : bates 582 return mkString(_("slots i, j and x must have the same length"));
15 : bates 557 return ScalarLogical(1);
16 :     }
17 :    
18 :     SEXP dsTMatrix_as_dsyMatrix(SEXP x)
19 :     {
20 :     SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("dsyMatrix"))),
21 :     DimP = GET_SLOT(x, Matrix_DimSym),
22 :     xiP = GET_SLOT(x, Matrix_iSym);
23 :     int k, n = INTEGER(DimP)[1], nnz = length(xiP);
24 :     int *xi = INTEGER(xiP), *xj = INTEGER(GET_SLOT(x, Matrix_jSym)),
25 :     sz = n * n;
26 :     double *tx = REAL(ALLOC_SLOT(val, Matrix_xSym, REALSXP, sz)),
27 :     *xx = REAL(GET_SLOT(x, Matrix_xSym));
28 :    
29 :     SET_SLOT(val, Matrix_DimSym, duplicate(DimP));
30 :     SET_SLOT(val, Matrix_uploSym, duplicate(GET_SLOT(x, Matrix_uploSym)));
31 :     AZERO(tx, sz);
32 :     for (k = 0; k < nnz; k++) tx[xi[k] + xj[k] * n] = xx[k];
33 :     UNPROTECT(1);
34 :     return val;
35 :     }
36 : bates 588
37 :     SEXP dsTMatrix_as_dsCMatrix(SEXP x)
38 :     {
39 :     SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("dsCMatrix"))),
40 :     dimP = GET_SLOT(x, Matrix_DimSym),
41 :     xiP = GET_SLOT(x, Matrix_iSym);
42 :     int n = INTEGER(dimP)[0],
43 :     nnz = length(xiP);
44 :     int *ti = Calloc(nnz, int),
45 :     *vp = INTEGER(ALLOC_SLOT(val, Matrix_pSym, INTSXP, n + 1));
46 :     double *tx = Calloc(nnz, double);
47 :    
48 :     SET_SLOT(val, Matrix_uploSym, duplicate(GET_SLOT(x, Matrix_uploSym)));
49 :     SET_SLOT(val, Matrix_DimSym, duplicate(dimP));
50 :     triplet_to_col(n, n, nnz, INTEGER(xiP),
51 :     INTEGER(GET_SLOT(x, Matrix_jSym)),
52 :     REAL(GET_SLOT(x, Matrix_xSym)),
53 :     vp, ti, tx);
54 :     nnz = vp[n];
55 :     Memcpy(INTEGER(ALLOC_SLOT(val, Matrix_iSym, INTSXP, nnz)), ti, nnz);
56 :     Memcpy(REAL(ALLOC_SLOT(val, Matrix_iSym, REALSXP, nnz)), tx, nnz);
57 :     Free(ti); Free(tx);
58 :     UNPROTECT(1);
59 :     return val;
60 :     }
61 :    

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