SCM

SCM Repository

[matrix] Annotation of /branches/Matrix-mer2/src/dtTMatrix.c
ViewVC logotype

Annotation of /branches/Matrix-mer2/src/dtTMatrix.c

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : maechler 951 /* Sparse triangular matrices in triplet format */
2 : bates 551 #include "dtTMatrix.h"
3 :    
4 :     SEXP dtTMatrix_validate(SEXP x)
5 :     {
6 : maechler 890 return triangularMatrix_validate(x);
7 :     /* see ./dtpMatrix.c as example to do more testing here */
8 : bates 551 }
9 :    
10 :     SEXP dtTMatrix_as_dtrMatrix(SEXP x)
11 :     {
12 :     SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("dtrMatrix"))),
13 : maechler 945 dimP = GET_SLOT(x, Matrix_DimSym),
14 : bates 551 xiP = GET_SLOT(x, Matrix_iSym);
15 : maechler 945 int k, m = INTEGER(dimP)[0], n = INTEGER(dimP)[1], nnz = length(xiP);
16 : bates 551 int *xi = INTEGER(xiP), *xj = INTEGER(GET_SLOT(x, Matrix_jSym)),
17 :     sz = m * n;
18 :     double *tx = REAL(ALLOC_SLOT(val, Matrix_xSym, REALSXP, sz)),
19 :     *xx = REAL(GET_SLOT(x, Matrix_xSym));
20 : maechler 890
21 : maechler 945 SET_SLOT(val, Matrix_DimSym, duplicate(dimP));
22 : bates 551 SET_SLOT(val, Matrix_uploSym, duplicate(GET_SLOT(x, Matrix_uploSym)));
23 :     SET_SLOT(val, Matrix_diagSym, duplicate(GET_SLOT(x, Matrix_diagSym)));
24 :     AZERO(tx, sz);
25 :     for (k = 0; k < nnz; k++) tx[xi[k] + xj[k] * m] = xx[k];
26 :     UNPROTECT(1);
27 :     return val;
28 :     }
29 : maechler 945
30 :     SEXP dtTMatrix_as_dtCMatrix(SEXP x)
31 :     {
32 :     SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("dtCMatrix"))),
33 :     dimP = GET_SLOT(x, Matrix_DimSym),
34 :     xiP = GET_SLOT(x, Matrix_iSym);
35 :     int n = INTEGER(dimP)[0],
36 :     nnz = length(xiP);
37 :     int *ti = Calloc(nnz, int),
38 :     *vp = INTEGER(ALLOC_SLOT(val, Matrix_pSym, INTSXP, n + 1));
39 :     double *tx = Calloc(nnz, double);
40 :    
41 :     SET_SLOT(val, Matrix_DimSym, duplicate(dimP));
42 :     SET_SLOT(val, Matrix_uploSym, duplicate(GET_SLOT(x, Matrix_uploSym)));
43 :     SET_SLOT(val, Matrix_diagSym, duplicate(GET_SLOT(x, Matrix_diagSym)));
44 :    
45 :     triplet_to_col(n, n, nnz, INTEGER(xiP),
46 :     INTEGER(GET_SLOT(x, Matrix_jSym)),
47 :     REAL(GET_SLOT(x, Matrix_xSym)),
48 :     vp, ti, tx);
49 :     nnz = vp[n];
50 :     Memcpy(INTEGER(ALLOC_SLOT(val, Matrix_iSym, INTSXP, nnz)), ti, nnz);
51 :     Memcpy( REAL(ALLOC_SLOT(val, Matrix_xSym, REALSXP, nnz)), tx, nnz);
52 :     Free(ti); Free(tx);
53 :     UNPROTECT(1);
54 :     return val;
55 :     }
56 :    

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