SCM

SCM Repository

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

Annotation of /pkg/src/dpoMatrix.c

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : bates 478 #include "dpoMatrix.h"
2 : bates 10
3 : bates 488 SEXP dpoMatrix_validate(SEXP obj)
4 :     {
5 :     int i, n = INTEGER(GET_SLOT(obj, Matrix_DimSym))[0];
6 :     int np1 = n + 1;
7 :     double *x = REAL(GET_SLOT(obj, Matrix_xSym));
8 :    
9 :     /* quick but nondefinitive check on positive definiteness */
10 :     for (i = 0; i < n; i++)
11 : bates 582 if (x[i * np1] < 0)
12 :     return mkString(_("dpoMatrix is not positive definite"));
13 : bates 488 return ScalarLogical(1);
14 :     }
15 :    
16 : bates 478 SEXP dpoMatrix_chol(SEXP x)
17 : bates 10 {
18 : bates 476 SEXP val = get_factors(x, "Cholesky"),
19 : bates 342 dimP = GET_SLOT(x, Matrix_DimSym),
20 :     uploP = GET_SLOT(x, Matrix_uploSym);
21 : bates 648 char *uplo = CHAR(STRING_ELT(uploP, 0));
22 :     int *dims = INTEGER(dimP), info;
23 :     int n = dims[0];
24 :     double *vx;
25 : maechler 534
26 : bates 10 if (val != R_NilValue) return val;
27 : bates 342 dims = INTEGER(dimP);
28 : bates 10 val = PROTECT(NEW_OBJECT(MAKE_CLASS("Cholesky")));
29 : bates 342 SET_SLOT(val, Matrix_uploSym, duplicate(uploP));
30 : maechler 534 SET_SLOT(val, Matrix_diagSym, mkString("N"));
31 : bates 342 SET_SLOT(val, Matrix_DimSym, duplicate(dimP));
32 : bates 648 vx = REAL(ALLOC_SLOT(val, Matrix_xSym, REALSXP, n * n));
33 :     AZERO(vx, n * n);
34 :     F77_CALL(dlacpy)(uplo, &n, &n, REAL(GET_SLOT(x, Matrix_xSym)), &n, vx, &n);
35 :     F77_CALL(dpotrf)(uplo, &n, vx, &n, &info);
36 :     if (info) error(_("Lapack routine %s returned error code %d"), "dpotrf", info);
37 : bates 10 UNPROTECT(1);
38 : bates 476 return set_factors(x, val, "Cholesky");
39 : bates 10 }
40 :    
41 :     static
42 :     double set_rcond(SEXP obj, char *typstr)
43 :     {
44 :     char typnm[] = {'O', '\0'}; /* always use the one norm */
45 : bates 342 SEXP rcv = GET_SLOT(obj, Matrix_rcondSym);
46 : bates 10 double rcond = get_double_by_name(rcv, typnm);
47 :    
48 :     if (R_IsNA(rcond)) {
49 : bates 478 SEXP Chol = dpoMatrix_chol(obj);
50 : bates 10 int *dims = INTEGER(GET_SLOT(Chol, Matrix_DimSym)), info;
51 :     double anorm = get_norm_sy(obj, typnm);
52 :    
53 :     F77_CALL(dpocon)(CHAR(asChar(GET_SLOT(Chol, Matrix_uploSym))),
54 :     dims, REAL(GET_SLOT(Chol, Matrix_xSym)),
55 :     dims, &anorm, &rcond,
56 :     (double *) R_alloc(3*dims[0], sizeof(double)),
57 :     (int *) R_alloc(dims[0], sizeof(int)), &info);
58 : bates 342 SET_SLOT(obj, Matrix_rcondSym,
59 : bates 10 set_double_by_name(rcv, rcond, typnm));
60 :     }
61 :     return rcond;
62 :     }
63 :    
64 : bates 478 SEXP dpoMatrix_rcond(SEXP obj, SEXP type)
65 : bates 10 {
66 : bates 648 return ScalarReal(set_rcond(obj, CHAR(asChar(type))));
67 : bates 10 }
68 :    
69 : bates 478 SEXP dpoMatrix_solve(SEXP x)
70 : bates 10 {
71 : bates 478 SEXP Chol = dpoMatrix_chol(x);
72 :     SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("dpoMatrix")));
73 : bates 10 int *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)), info;
74 :    
75 : bates 476 SET_SLOT(val, Matrix_factorSym, allocVector(VECSXP, 0));
76 : bates 342 SET_SLOT(val, Matrix_uploSym, duplicate(GET_SLOT(Chol, Matrix_uploSym)));
77 : bates 10 SET_SLOT(val, Matrix_xSym, duplicate(GET_SLOT(Chol, Matrix_xSym)));
78 : bates 342 SET_SLOT(val, Matrix_DimSym, duplicate(GET_SLOT(Chol, Matrix_DimSym)));
79 : bates 10 F77_CALL(dpotri)(CHAR(asChar(GET_SLOT(val, Matrix_uploSym))),
80 :     dims, REAL(GET_SLOT(val, Matrix_xSym)), dims, &info);
81 : bates 342 SET_SLOT(val, Matrix_rcondSym, duplicate(GET_SLOT(x, Matrix_rcondSym)));
82 : bates 10 UNPROTECT(1);
83 :     return val;
84 :     }
85 :    
86 : bates 478 SEXP dpoMatrix_dgeMatrix_solve(SEXP a, SEXP b)
87 : bates 10 {
88 : bates 478 SEXP Chol = dpoMatrix_chol(a),
89 :     val = PROTECT(NEW_OBJECT(MAKE_CLASS("dgeMatrix")));
90 : bates 10 int *adims = INTEGER(GET_SLOT(a, Matrix_DimSym)),
91 :     *bdims = INTEGER(GET_SLOT(b, Matrix_DimSym)),
92 :     info;
93 :    
94 :     if (*adims != *bdims || bdims[1] < 1 || *adims < 1)
95 : bates 582 error(_("Dimensions of system to be solved are inconsistent"));
96 : bates 342 SET_SLOT(val, Matrix_rcondSym, allocVector(REALSXP, 0));
97 : bates 476 SET_SLOT(val, Matrix_factorSym, allocVector(VECSXP, 0));
98 : bates 10 SET_SLOT(val, Matrix_DimSym, duplicate(GET_SLOT(b, Matrix_DimSym)));
99 :     SET_SLOT(val, Matrix_xSym, duplicate(GET_SLOT(b, Matrix_xSym)));
100 :     F77_CALL(dpotrs)(CHAR(asChar(GET_SLOT(Chol, Matrix_uploSym))),
101 :     adims, bdims + 1,
102 :     REAL(GET_SLOT(Chol, Matrix_xSym)), adims,
103 :     REAL(GET_SLOT(val, Matrix_xSym)),
104 :     bdims, &info);
105 :     UNPROTECT(1);
106 :     return val;
107 :     }
108 :    
109 : bates 478 SEXP dpoMatrix_matrix_solve(SEXP a, SEXP b)
110 : bates 10 {
111 : bates 478 SEXP Chol = dpoMatrix_chol(a),
112 : bates 10 val = PROTECT(duplicate(b));
113 :     int *adims = INTEGER(GET_SLOT(a, Matrix_DimSym)),
114 :     *bdims = INTEGER(getAttrib(b, R_DimSymbol)),
115 :     info;
116 :    
117 :     if (!(isReal(b) && isMatrix(b)))
118 : bates 582 error(_("Argument b must be a numeric matrix"));
119 : bates 10 if (*adims != *bdims || bdims[1] < 1 || *adims < 1)
120 : bates 582 error(_("Dimensions of system to be solved are inconsistent"));
121 : bates 10 F77_CALL(dpotrs)(CHAR(asChar(GET_SLOT(Chol, Matrix_uploSym))),
122 :     adims, bdims + 1,
123 :     REAL(GET_SLOT(Chol, Matrix_xSym)), adims,
124 :     REAL(val), bdims, &info);
125 :     UNPROTECT(1);
126 :     return val;
127 :     }

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