Matrix  $Rev: 3071 $ at $LastChangedDate: 2015-03-26 15:35:47 +0100 (Thu, 26 Mar 2015) $
dtpMatrix.c
Go to the documentation of this file.
1 /* double (precision) Triangular Packed Matrices
2  * Note: this means *square* {n x n} matrices
3 */
4 
5 #include "dtpMatrix.h"
6 
7 SEXP dtpMatrix_validate(SEXP obj)
8 {
9  SEXP val = triangularMatrix_validate(obj);
10  if(isString(val))
11  return(val);
12  else {
13  int d = INTEGER(GET_SLOT(obj, Matrix_DimSym))[0],
14  lx = length(GET_SLOT(obj, Matrix_xSym));
15  /* packed_ncol() [Mutils.h] checks, but gives *error* .. need string: */
16  if(lx * 2 != d*(d+1))
17  return(mkString(_("Incorrect length of 'x' slot")));
18  return ScalarLogical(1);
19  }
20 }
21 
22 static
23 double get_norm(SEXP obj, const char *typstr)
24 {
25  char typnm[] = {'\0', '\0'};
26  int *dims = INTEGER(GET_SLOT(obj, Matrix_DimSym));
27  double *work = (double *) NULL;
28 
29  typnm[0] = La_norm_type(typstr);
30  if (*typnm == 'I') {
31  work = (double *) R_alloc(dims[0], sizeof(double));
32  }
33  return F77_CALL(dlantp)(typnm, uplo_P(obj), diag_P(obj), dims,
34  REAL(GET_SLOT(obj, Matrix_xSym)), work);
35 }
36 
37 SEXP dtpMatrix_norm(SEXP obj, SEXP type)
38 {
39  return ScalarReal(get_norm(obj, CHAR(asChar(type))));
40 }
41 
42 SEXP dtpMatrix_rcond(SEXP obj, SEXP type)
43 {
44  int *dims = INTEGER(GET_SLOT(obj, Matrix_DimSym)), info;
45  char typnm[] = {'\0', '\0'};
46  double rcond;
47 
48  typnm[0] = La_rcond_type(CHAR(asChar(type)));
49  F77_CALL(dtpcon)(typnm, uplo_P(obj), diag_P(obj), dims,
50  REAL(GET_SLOT(obj, Matrix_xSym)), &rcond,
51  (double *) R_alloc(3*dims[0], sizeof(double)),
52  (int *) R_alloc(dims[0], sizeof(int)), &info);
53  return ScalarReal(rcond);
54 }
55 
56 SEXP dtpMatrix_solve(SEXP a)
57 {
58  SEXP val = PROTECT(duplicate(a));
59  int info, *Dim = INTEGER(GET_SLOT(val, Matrix_DimSym));
60  F77_CALL(dtptri)(uplo_P(val), diag_P(val), Dim,
61  REAL(GET_SLOT(val, Matrix_xSym)), &info);
62  UNPROTECT(1);
63  return val;
64 }
65 
66 
67 // also applicable to dspMatrix , dppMatrix :
68 SEXP dtpMatrix_getDiag(SEXP x)
69 {
70  int n = *INTEGER(GET_SLOT(x, Matrix_DimSym));
71  SEXP val = PROTECT(allocVector(REALSXP, n));
72 
73  tr_d_packed_getDiag(REAL(val), x, n);
74  UNPROTECT(1);
75  return val;
76 }
77 
78 // also applicable to lspMatrix :
79 SEXP ltpMatrix_getDiag(SEXP x)
80 {
81  int n = *INTEGER(GET_SLOT(x, Matrix_DimSym));
82  SEXP val = PROTECT(allocVector(LGLSXP, n));
83 
84  tr_l_packed_getDiag(LOGICAL(val), x, n);
85  UNPROTECT(1);
86  return val;
87 }
88 
89 SEXP dtpMatrix_setDiag(SEXP x, SEXP d)
90 {
91  int n = INTEGER(GET_SLOT(x, Matrix_DimSym))[0];
92  return tr_d_packed_setDiag(REAL(d), LENGTH(d), x, n);
93 }
94 
95 SEXP ltpMatrix_setDiag(SEXP x, SEXP d)
96 {
97  int n = INTEGER(GET_SLOT(x, Matrix_DimSym))[0];
98  return tr_l_packed_setDiag(INTEGER(d), LENGTH(d), x, n);
99 }
100 
101 SEXP dtpMatrix_addDiag(SEXP x, SEXP d)
102 {
103  int n = INTEGER(GET_SLOT(x, Matrix_DimSym))[0];
104  return tr_d_packed_addDiag(REAL(d), LENGTH(d), x, n);
105 }
106 
107 
108 SEXP dtpMatrix_matrix_mm(SEXP x, SEXP y, SEXP right, SEXP trans)
109 {
110  SEXP val = PROTECT(dup_mMatrix_as_dgeMatrix(y));
111  int rt = asLogical(right); // if(rt), compute b %*% op(a), else op(a) %*% b
112  int tr = asLogical(trans); // if(tr), op(a) = t(a), else op(a) = a
113  /* Since 'x' is square (n x n ), dim(x %*% y) = dim(y) */
114  int *xDim = INTEGER(GET_SLOT(x, Matrix_DimSym)),
115  *yDim = INTEGER(GET_SLOT(val, Matrix_DimSym));
116  int m = yDim[0], n = yDim[1];
117  int ione = 1;
118  const char *uplo = uplo_P(x), *diag = diag_P(x);
119  double *xx = REAL(GET_SLOT(x, Matrix_xSym)),
120  *vx = REAL(GET_SLOT(val, Matrix_xSym));
121 
122  if (yDim[0] != xDim[1])
123  if ((rt && xDim[0] != n) || (!rt && xDim[1] != m))
124  error(_("Dimensions of a (%d,%d) and b (%d,%d) do not conform"),
125  xDim[0], xDim[1], yDim[0], yDim[1]);
126  if (m < 1 || n < 1) {
127 /* error(_("Matrices with zero extents cannot be multiplied")); */
128  } else /* BLAS */
129  // go via BLAS 2 dtpmv(.); there is no dtpmm in Lapack!
130  if(rt) {
131  error(_("right=TRUE is not yet implemented __ FIXME"));
132  } else {
133  for (int j = 0; j < n; j++) // X %*% y[,j]
134  F77_CALL(dtpmv)(uplo, /*trans = */ tr ? "T" : "N",
135  diag, yDim, xx,
136  vx + j * m, &ione);
137  }
138  UNPROTECT(1);
139  return val;
140 }
141 
142 SEXP dtpMatrix_matrix_solve(SEXP a, SEXP b)
143 {
144  SEXP val = PROTECT(dup_mMatrix_as_dgeMatrix(b));
145  /* Since 'a' is square (n x n ), dim(a %*% b) = dim(b) */
146  int *aDim = INTEGER(GET_SLOT(a, Matrix_DimSym)),
147  *bDim = INTEGER(GET_SLOT(val, Matrix_DimSym));
148  int ione = 1;
149  const char *uplo = uplo_P(a), *diag = diag_P(a);
150 
151  if (bDim[0] != aDim[1])
152  error(_("Dimensions of a (%d,%d) and b (%d,%d) do not conform"),
153  aDim[0], aDim[1], bDim[0], bDim[1]);
154 #ifdef pre_2013_08_30
155  double *ax = REAL(GET_SLOT(a, Matrix_xSym)),
156  *vx = REAL(GET_SLOT(val, Matrix_xSym));
157  for (int j = 0; j < bDim[1]; j++) /* a^{-1} %*% b[,j] via BLAS 2 DTPSV(.) */
158  F77_CALL(dtpsv)(uplo, "N", diag, bDim, ax,
159  vx + j * bDim[0], &ione);
160 #else
161  F77_CALL(dtptrs)(uplo, "N", diag, /* n= */ aDim, /* nrhs = */ &bDim[1],
162  /* ap = */ REAL(GET_SLOT(a, Matrix_xSym)),
163  /* b = */ REAL(GET_SLOT(val, Matrix_xSym)),
164  bDim, &ione);
165 #endif
166  UNPROTECT(1);
167  return val;
168 }
169 
170 /* FIXME: This function should be removed and a rt argument added to
171  * dtpMatrix_matrix_mm -- also to be more parallel to ./dtrMatrix.c code */
172 SEXP dgeMatrix_dtpMatrix_mm(SEXP x, SEXP y)
173 {
174  SEXP val = PROTECT(duplicate(x));
175  /* Since 'y' is square (n x n ), dim(x %*% y) = dim(x) */
176  int *xDim = INTEGER(GET_SLOT(x, Matrix_DimSym)),
177  *yDim = INTEGER(GET_SLOT(y, Matrix_DimSym));
178  const char *uplo = uplo_P(y), *diag = diag_P(y);
179  double *yx = REAL(GET_SLOT(y, Matrix_xSym)),
180  *vx = REAL(GET_SLOT(val, Matrix_xSym));
181 
182  if (yDim[0] != xDim[1])
183  error(_("Dimensions of a (%d,%d) and b (%d,%d) do not conform"),
184  xDim[0], xDim[1], yDim[0], yDim[1]);
185  for (int i = 0; i < xDim[0]; i++)/* val[i,] := Y' %*% x[i,] */
186  F77_CALL(dtpmv)(uplo, "T", diag, yDim, yx,
187  vx + i, /* incr = */ xDim);
188  UNPROTECT(1);
189  return val;
190 }
191 
192 SEXP dtpMatrix_as_dtrMatrix(SEXP from)
193 {
194  SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("dtrMatrix"))),
195  uplo = GET_SLOT(from, Matrix_uploSym),
196  diag = GET_SLOT(from, Matrix_diagSym),
197  dimP = GET_SLOT(from, Matrix_DimSym),
198  dmnP = GET_SLOT(from, Matrix_DimNamesSym);
199  int n = *INTEGER(dimP);
200 
201  SET_SLOT(val, Matrix_DimSym, duplicate(dimP));
202  SET_SLOT(val, Matrix_DimNamesSym, duplicate(dmnP));
203  SET_SLOT(val, Matrix_diagSym, duplicate(diag));
204  SET_SLOT(val, Matrix_uploSym, duplicate(uplo));
205  packed_to_full_double(REAL(ALLOC_SLOT(val, Matrix_xSym, REALSXP, n*n)),
206  REAL(GET_SLOT(from, Matrix_xSym)), n,
207  *CHAR(STRING_ELT(uplo, 0)) == 'U' ? UPP : LOW);
208  SET_SLOT(val, Matrix_DimNamesSym,
209  duplicate(GET_SLOT(from, Matrix_DimNamesSym)));
210  UNPROTECT(1);
211  return val;
212 }
213 
SEXP Matrix_DimSym
Definition: Syms.h:2
SEXP ltpMatrix_getDiag(SEXP x)
Definition: dtpMatrix.c:79
void tr_l_packed_getDiag(int *dest, SEXP x, int n)
Definition: Mutils.c:561
SEXP dtpMatrix_setDiag(SEXP x, SEXP d)
Definition: dtpMatrix.c:89
SEXP Matrix_xSym
Definition: Syms.h:2
static double get_norm(SEXP obj, const char *typstr)
Definition: dtpMatrix.c:23
SEXP Matrix_DimNamesSym
Definition: Syms.h:2
SEXP tr_d_packed_addDiag(double *diag, int l_d, SEXP x, int n)
Definition: Mutils.c:539
SEXP Matrix_uploSym
Definition: Syms.h:2
SEXP dup_mMatrix_as_dgeMatrix(SEXP A)
Definition: Mutils.c:852
SEXP tr_l_packed_setDiag(int *diag, int l_d, SEXP x, int n)
Definition: Mutils.c:511
SEXP dtpMatrix_matrix_solve(SEXP a, SEXP b)
Definition: dtpMatrix.c:142
SEXP dtpMatrix_rcond(SEXP obj, SEXP type)
Definition: dtpMatrix.c:42
SEXP dtpMatrix_addDiag(SEXP x, SEXP d)
Definition: dtpMatrix.c:101
SEXP dgeMatrix_dtpMatrix_mm(SEXP x, SEXP y)
Definition: dtpMatrix.c:172
char La_rcond_type(const char *typstr)
Definition: Mutils.c:27
SEXP dtpMatrix_validate(SEXP obj)
Definition: dtpMatrix.c:7
SEXP dtpMatrix_norm(SEXP obj, SEXP type)
Definition: dtpMatrix.c:37
#define UPP
Definition: Mutils.h:81
SEXP dtpMatrix_as_dtrMatrix(SEXP from)
Definition: dtpMatrix.c:192
#define uplo_P(_x_)
Definition: Mutils.h:174
#define _(String)
Definition: Mutils.h:32
SEXP dtpMatrix_getDiag(SEXP x)
Definition: dtpMatrix.c:68
SEXP triangularMatrix_validate(SEXP obj)
Definition: dtrMatrix.c:5
char La_norm_type(const char *typstr)
Definition: Mutils.c:8
void tr_d_packed_getDiag(double *dest, SEXP x, int n)
Definition: Mutils.c:551
#define LOW
Definition: Mutils.h:82
#define diag_P(_x_)
Definition: Mutils.h:175
SEXP tr_d_packed_setDiag(double *diag, int l_d, SEXP x, int n)
Definition: Mutils.c:505
SEXP Matrix_diagSym
Definition: Syms.h:2
SEXP ltpMatrix_setDiag(SEXP x, SEXP d)
Definition: dtpMatrix.c:95
SEXP dtpMatrix_solve(SEXP a)
Definition: dtpMatrix.c:56
SEXP dtpMatrix_matrix_mm(SEXP x, SEXP y, SEXP right, SEXP trans)
Definition: dtpMatrix.c:108
static R_INLINE SEXP ALLOC_SLOT(SEXP obj, SEXP nm, SEXPTYPE type, int length)
Allocate an SEXP of given type and length, assign it as slot nm in the object, and return the SEXP...
Definition: Mutils.h:240