SCM

SCM Repository

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

Annotation of /pkg/src/dgTMatrix.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 476 - (view) (download) (as text)
Original Path: pkg/src/triplet.c

1 : bates 10 /* Sparse matrices in triplet form */
2 :     #include "triplet.h"
3 :    
4 :     SEXP triplet_validate(SEXP x)
5 :     {
6 :     SEXP
7 :     islot = GET_SLOT(x, Matrix_iSym),
8 :     jslot = GET_SLOT(x, Matrix_jSym),
9 :     xslot = GET_SLOT(x, Matrix_xSym),
10 :     dimslot = GET_SLOT(x, Matrix_DimSym);
11 :     int j,
12 :     *dims = INTEGER(dimslot),
13 :     ncol, nrow, nnz = length(islot),
14 :     *xj = INTEGER(jslot),
15 :     *xi = INTEGER(islot);
16 :    
17 :     if (length(xslot) != nnz || length(jslot) != nnz)
18 :     return ScalarString(mkChar("lengths of slots i, j, and x must match"));
19 :     if (length(dimslot) != 2)
20 :     return ScalarString(mkChar("slot Dim must have length 2"));
21 :     nrow = dims[0]; ncol = dims[1];
22 :     for (j = 0; j < nnz; j++) {
23 :     if (xi[j] < 0 || xi[j] >= nrow)
24 :     return ScalarString(
25 :     mkChar("all row indices must be between 0 and nrow-1"));
26 :     if (xj[j] < 0 || xj[j] >= ncol)
27 :     return ScalarString(
28 :     mkChar("all column indices must be between 0 and ncol-1"));
29 :     }
30 :     return ScalarLogical(1);
31 :     }
32 : bates 70
33 : bates 390 static void
34 :     insert_triplets_in_array(int m, int n, int nnz,
35 :     const int xi[], const int xj[], const double xx[],
36 :     double vx[])
37 :     {
38 :     int i;
39 :     memset(vx, 0, sizeof(double) * m * n);
40 :     for (i = 0; i < nnz; i++) {
41 :     vx[xi[i] + xj[i] * m] += xx[i]; /* allow redundant entries in x */
42 :     }
43 :     }
44 :    
45 : bates 70 SEXP triplet_to_geMatrix(SEXP x)
46 :     {
47 :     SEXP dd = GET_SLOT(x, Matrix_DimSym),
48 :     islot = GET_SLOT(x, Matrix_iSym),
49 :     ans = PROTECT(NEW_OBJECT(MAKE_CLASS("geMatrix")));
50 :    
51 :     int *dims = INTEGER(dd),
52 :     m = dims[0],
53 : bates 390 n = dims[1];
54 : bates 70
55 : bates 342 SET_SLOT(ans, Matrix_rcondSym, allocVector(REALSXP, 0));
56 : bates 476 SET_SLOT(ans, Matrix_factorSym, allocVector(VECSXP, 0));
57 : bates 70 SET_SLOT(ans, Matrix_DimSym, duplicate(dd));
58 :     SET_SLOT(ans, Matrix_xSym, allocVector(REALSXP, m * n));
59 : bates 390 insert_triplets_in_array(m, n, length(islot),
60 :     INTEGER(islot), INTEGER(GET_SLOT(x, Matrix_jSym)),
61 :     REAL(GET_SLOT(x, Matrix_xSym)),
62 :     REAL(GET_SLOT(ans, Matrix_xSym)));
63 : bates 70 UNPROTECT(1);
64 :     return ans;
65 :     }
66 :    
67 : bates 390 SEXP triplet_to_matrix(SEXP x)
68 :     {
69 :     SEXP dd = GET_SLOT(x, Matrix_DimSym),
70 :     islot = GET_SLOT(x, Matrix_iSym);
71 :     int m = INTEGER(dd)[0],
72 :     n = INTEGER(dd)[1];
73 :     SEXP ans = PROTECT(allocMatrix(REALSXP, m, n));
74 :    
75 :     insert_triplets_in_array(m, n, length(islot),
76 :     INTEGER(islot), INTEGER(GET_SLOT(x, Matrix_jSym)),
77 :     REAL(GET_SLOT(x, Matrix_xSym)),
78 :     REAL(ans));
79 :     UNPROTECT(1);
80 :     return ans;
81 :     }
82 :    

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