SCM

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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

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