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 951 - (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 : maechler 951 if (info)
37 :     error(_("Lapack routine %s returned error code %d"), "dpotrf", info);
38 : bates 10 UNPROTECT(1);
39 : bates 476 return set_factors(x, val, "Cholesky");
40 : bates 10 }
41 :    
42 :     static
43 :     double set_rcond(SEXP obj, char *typstr)
44 :     {
45 :     char typnm[] = {'O', '\0'}; /* always use the one norm */
46 : bates 342 SEXP rcv = GET_SLOT(obj, Matrix_rcondSym);
47 : bates 10 double rcond = get_double_by_name(rcv, typnm);
48 :    
49 :     if (R_IsNA(rcond)) {
50 : bates 478 SEXP Chol = dpoMatrix_chol(obj);
51 : bates 10 int *dims = INTEGER(GET_SLOT(Chol, Matrix_DimSym)), info;
52 :     double anorm = get_norm_sy(obj, typnm);
53 :    
54 : maechler 951 F77_CALL(dpocon)(uplo_P(Chol),
55 : bates 10 dims, REAL(GET_SLOT(Chol, Matrix_xSym)),
56 :     dims, &anorm, &rcond,
57 :     (double *) R_alloc(3*dims[0], sizeof(double)),
58 :     (int *) R_alloc(dims[0], sizeof(int)), &info);
59 : bates 342 SET_SLOT(obj, Matrix_rcondSym,
60 : bates 10 set_double_by_name(rcv, rcond, typnm));
61 :     }
62 :     return rcond;
63 :     }
64 :    
65 : bates 478 SEXP dpoMatrix_rcond(SEXP obj, SEXP type)
66 : bates 10 {
67 : bates 648 return ScalarReal(set_rcond(obj, CHAR(asChar(type))));
68 : bates 10 }
69 :    
70 : bates 478 SEXP dpoMatrix_solve(SEXP x)
71 : bates 10 {
72 : bates 478 SEXP Chol = dpoMatrix_chol(x);
73 :     SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("dpoMatrix")));
74 : bates 10 int *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)), info;
75 :    
76 : bates 476 SET_SLOT(val, Matrix_factorSym, allocVector(VECSXP, 0));
77 : bates 342 SET_SLOT(val, Matrix_uploSym, duplicate(GET_SLOT(Chol, Matrix_uploSym)));
78 : bates 10 SET_SLOT(val, Matrix_xSym, duplicate(GET_SLOT(Chol, Matrix_xSym)));
79 : bates 342 SET_SLOT(val, Matrix_DimSym, duplicate(GET_SLOT(Chol, Matrix_DimSym)));
80 : maechler 951 F77_CALL(dpotri)(uplo_P(val), dims,
81 :     REAL(GET_SLOT(val, Matrix_xSym)), dims, &info);
82 : bates 342 SET_SLOT(val, Matrix_rcondSym, duplicate(GET_SLOT(x, Matrix_rcondSym)));
83 : bates 10 UNPROTECT(1);
84 :     return val;
85 :     }
86 :    
87 : bates 478 SEXP dpoMatrix_dgeMatrix_solve(SEXP a, SEXP b)
88 : bates 10 {
89 : bates 478 SEXP Chol = dpoMatrix_chol(a),
90 :     val = PROTECT(NEW_OBJECT(MAKE_CLASS("dgeMatrix")));
91 : bates 10 int *adims = INTEGER(GET_SLOT(a, Matrix_DimSym)),
92 :     *bdims = INTEGER(GET_SLOT(b, Matrix_DimSym)),
93 :     info;
94 :    
95 :     if (*adims != *bdims || bdims[1] < 1 || *adims < 1)
96 : bates 582 error(_("Dimensions of system to be solved are inconsistent"));
97 : bates 342 SET_SLOT(val, Matrix_rcondSym, allocVector(REALSXP, 0));
98 : bates 476 SET_SLOT(val, Matrix_factorSym, allocVector(VECSXP, 0));
99 : bates 10 SET_SLOT(val, Matrix_DimSym, duplicate(GET_SLOT(b, Matrix_DimSym)));
100 :     SET_SLOT(val, Matrix_xSym, duplicate(GET_SLOT(b, Matrix_xSym)));
101 : maechler 951 F77_CALL(dpotrs)(uplo_P(Chol), adims, bdims + 1,
102 : bates 10 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 : maechler 951 F77_CALL(dpotrs)(uplo_P(Chol), adims, bdims + 1,
122 : bates 10 REAL(GET_SLOT(Chol, Matrix_xSym)), adims,
123 :     REAL(val), bdims, &info);
124 :     UNPROTECT(1);
125 :     return val;
126 :     }

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