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 964 - (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 :    
23 :     if (val != R_NilValue) return val;
24 :     dims = INTEGER(dimP);
25 :     val = PROTECT(NEW_OBJECT(MAKE_CLASS("pCholesky")));
26 :     SET_SLOT(val, Matrix_uploSym, duplicate(uploP));
27 :     SET_SLOT(val, Matrix_diagSym, mkString("N"));
28 :     SET_SLOT(val, Matrix_DimSym, duplicate(dimP));
29 :     SET_SLOT(val, Matrix_xSym, duplicate(GET_SLOT(x, Matrix_xSym)));
30 :     F77_CALL(dpptrf)(uplo, dims, REAL(GET_SLOT(val, Matrix_xSym)), &info);
31 : maechler 964 if (info) {
32 :     if(info > 0) /* e.g. x singular */
33 :     error(_("the leading minor of order %d is not positive definite"),
34 :     info);
35 :     else /* should never happen! */
36 :     error(_("Lapack routine %s returned error code %d"), "dpptrf", info);
37 :     }
38 : bates 635 UNPROTECT(1);
39 :     return set_factors(x, val, "pCholesky");
40 :     }
41 :    
42 :     static
43 :     double set_rcond(SEXP obj, char *typstr)
44 :     {
45 :     char typnm[] = {'O', '\0'}; /* always use the one norm */
46 :     SEXP rcv = GET_SLOT(obj, Matrix_rcondSym);
47 :     double rcond = get_double_by_name(rcv, typnm);
48 :    
49 :     if (R_IsNA(rcond)) {
50 :     SEXP Chol = dppMatrix_chol(obj);
51 :     int *dims = INTEGER(GET_SLOT(Chol, Matrix_DimSym)), info;
52 :     double anorm = get_norm_sp(obj, typnm);
53 :    
54 : maechler 951 F77_CALL(dppcon)(uplo_P(Chol), dims,
55 :     REAL(GET_SLOT(Chol, Matrix_xSym)), &anorm, &rcond,
56 : bates 635 (double *) R_alloc(3*dims[0], sizeof(double)),
57 :     (int *) R_alloc(dims[0], sizeof(int)), &info);
58 :     SET_SLOT(obj, Matrix_rcondSym,
59 :     set_double_by_name(rcv, rcond, typnm));
60 :     }
61 :     return rcond;
62 :     }
63 :    
64 :     SEXP dppMatrix_rcond(SEXP obj, SEXP type)
65 :     {
66 :     return ScalarReal(set_rcond(obj, CHAR(asChar(type))));
67 :     }
68 :    
69 :     SEXP dppMatrix_solve(SEXP x)
70 :     {
71 :     SEXP Chol = dppMatrix_chol(x);
72 :     SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("dppMatrix")));
73 :     int *dims = INTEGER(GET_SLOT(x, Matrix_DimSym)), info;
74 :    
75 :     SET_SLOT(val, Matrix_uploSym, duplicate(GET_SLOT(Chol, Matrix_uploSym)));
76 :     SET_SLOT(val, Matrix_xSym, duplicate(GET_SLOT(Chol, Matrix_xSym)));
77 :     SET_SLOT(val, Matrix_DimSym, duplicate(GET_SLOT(Chol, Matrix_DimSym)));
78 : maechler 951 F77_CALL(dpptri)(uplo_P(val), dims,
79 :     REAL(GET_SLOT(val, Matrix_xSym)), &info);
80 : bates 635 UNPROTECT(1);
81 :     return val;
82 :     }
83 :    
84 : bates 654 SEXP dppMatrix_matrix_solve(SEXP a, SEXP b, SEXP classed)
85 : bates 635 {
86 : bates 654 int cl = asLogical(classed);
87 : bates 635 SEXP Chol = dppMatrix_chol(a),
88 :     val = PROTECT(NEW_OBJECT(MAKE_CLASS("dgeMatrix")));
89 :     int *adims = INTEGER(GET_SLOT(a, Matrix_DimSym)),
90 : bates 654 *bdims = (cl ? INTEGER(GET_SLOT(b, Matrix_DimSym)) :
91 : bates 652 INTEGER(getAttrib(b, R_DimSymbol)));
92 :     int n = bdims[0], nrhs = bdims[1], info;
93 :     int sz = n * nrhs;
94 : bates 635
95 : bates 654 if (!cl && !(isReal(b) && isMatrix(b)))
96 : bates 635 error(_("Argument b must be a numeric matrix"));
97 :     if (*adims != *bdims || bdims[1] < 1 || *adims < 1)
98 :     error(_("Dimensions of system to be solved are inconsistent"));
99 : bates 652 Memcpy(INTEGER(ALLOC_SLOT(val, Matrix_DimSym, INTSXP, 2)), bdims, 2);
100 : maechler 951 F77_CALL(dpptrs)(uplo_P(Chol), &n, &nrhs,
101 :     REAL(GET_SLOT(Chol, Matrix_xSym)),
102 :     Memcpy(REAL(ALLOC_SLOT(val, Matrix_xSym, REALSXP, sz)),
103 :     REAL(cl ? GET_SLOT(b, Matrix_xSym) : b), sz),
104 :     &n, &info);
105 : bates 635 UNPROTECT(1);
106 :     return val;
107 :     }

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