SCM

SCM Repository

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

Annotation of /pkg/src/poMatrix.c

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : bates 10 #include "poMatrix.h"
2 :    
3 :     SEXP poMatrix_chol(SEXP x)
4 :     {
5 :     SEXP val = get_factorization(x, "Cholesky");
6 :     int *dims, info;
7 :    
8 :     if (val != R_NilValue) return val;
9 :     dims = INTEGER(GET_SLOT(x, Matrix_DimSym));
10 :     val = PROTECT(NEW_OBJECT(MAKE_CLASS("Cholesky")));
11 :     SET_SLOT(val, install("uplo"), duplicate(GET_SLOT(x, install("uplo"))));
12 :     SET_SLOT(val, Matrix_xSym, duplicate(GET_SLOT(x, Matrix_xSym)));
13 :     SET_SLOT(val, Matrix_DimSym, duplicate(GET_SLOT(x, Matrix_DimSym)));
14 :     F77_CALL(dpotrf)(CHAR(asChar(GET_SLOT(val, Matrix_uploSym))), dims,
15 :     REAL(GET_SLOT(val, Matrix_xSym)), dims, &info);
16 :     if (info) error("Lapack routine dpotrf returned error code %d", info);
17 :     UNPROTECT(1);
18 :     return set_factorization(x, val, "Cholesky");
19 :     }
20 :    
21 :     static
22 :     double set_rcond(SEXP obj, char *typstr)
23 :     {
24 :     char typnm[] = {'O', '\0'}; /* always use the one norm */
25 :     SEXP rcv = GET_SLOT(obj, install("rcond"));
26 :     double rcond = get_double_by_name(rcv, typnm);
27 :    
28 :     if (R_IsNA(rcond)) {
29 :     SEXP Chol = poMatrix_chol(obj);
30 :     int *dims = INTEGER(GET_SLOT(Chol, Matrix_DimSym)), info;
31 :     double anorm = get_norm_sy(obj, typnm);
32 :    
33 :     F77_CALL(dpocon)(CHAR(asChar(GET_SLOT(Chol, Matrix_uploSym))),
34 :     dims, REAL(GET_SLOT(Chol, Matrix_xSym)),
35 :     dims, &anorm, &rcond,
36 :     (double *) R_alloc(3*dims[0], sizeof(double)),
37 :     (int *) R_alloc(dims[0], sizeof(int)), &info);
38 :     SET_SLOT(obj, install("rcond"),
39 :     set_double_by_name(rcv, rcond, typnm));
40 :     }
41 :     return rcond;
42 :     }
43 :    
44 :     SEXP poMatrix_rcond(SEXP obj, SEXP type)
45 :     {
46 :     return ScalarReal(set_rcond(obj, CHAR(asChar(type))));
47 :     }
48 :    
49 :     SEXP poMatrix_solve(SEXP x)
50 :     {
51 :     SEXP Chol = poMatrix_chol(x);
52 :     SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("poMatrix")));
53 :     int *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)), info;
54 :    
55 :     SET_SLOT(val, Matrix_uploSym, duplicate(GET_SLOT(x, Matrix_uploSym)));
56 :     SET_SLOT(val, Matrix_xSym, duplicate(GET_SLOT(Chol, Matrix_xSym)));
57 :     SET_SLOT(val, Matrix_DimSym, duplicate(GET_SLOT(x, Matrix_DimSym)));
58 :     F77_CALL(dpotri)(CHAR(asChar(GET_SLOT(val, Matrix_uploSym))),
59 :     dims, REAL(GET_SLOT(val, Matrix_xSym)), dims, &info);
60 :     UNPROTECT(1);
61 :     return val;
62 :     }
63 :    
64 :     SEXP poMatrix_geMatrix_solve(SEXP a, SEXP b)
65 :     {
66 :     SEXP Chol = poMatrix_chol(a),
67 :     val = PROTECT(NEW_OBJECT(MAKE_CLASS("geMatrix")));
68 :     int *adims = INTEGER(GET_SLOT(a, Matrix_DimSym)),
69 :     *bdims = INTEGER(GET_SLOT(b, Matrix_DimSym)),
70 :     info;
71 :    
72 :     if (*adims != *bdims || bdims[1] < 1 || *adims < 1)
73 :     error("Dimensions of system to be solved are inconsistent");
74 :     SET_SLOT(val, Matrix_DimSym, duplicate(GET_SLOT(b, Matrix_DimSym)));
75 :     SET_SLOT(val, Matrix_xSym, duplicate(GET_SLOT(b, Matrix_xSym)));
76 :     F77_CALL(dpotrs)(CHAR(asChar(GET_SLOT(Chol, Matrix_uploSym))),
77 :     adims, bdims + 1,
78 :     REAL(GET_SLOT(Chol, Matrix_xSym)), adims,
79 :     REAL(GET_SLOT(val, Matrix_xSym)),
80 :     bdims, &info);
81 :     UNPROTECT(1);
82 :     return val;
83 :     }
84 :    
85 :     SEXP poMatrix_matrix_solve(SEXP a, SEXP b)
86 :     {
87 :     SEXP Chol = poMatrix_chol(a),
88 :     val = PROTECT(duplicate(b));
89 :     int *adims = INTEGER(GET_SLOT(a, Matrix_DimSym)),
90 :     *bdims = INTEGER(getAttrib(b, R_DimSymbol)),
91 :     info;
92 :    
93 :     if (!(isReal(b) && isMatrix(b)))
94 :     error("Argument b must be a numeric matrix");
95 :     if (*adims != *bdims || bdims[1] < 1 || *adims < 1)
96 :     error("Dimensions of system to be solved are inconsistent");
97 :     F77_CALL(dpotrs)(CHAR(asChar(GET_SLOT(Chol, Matrix_uploSym))),
98 :     adims, bdims + 1,
99 :     REAL(GET_SLOT(Chol, Matrix_xSym)), adims,
100 :     REAL(val), bdims, &info);
101 :     UNPROTECT(1);
102 :     return val;
103 :     }

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