Matrix r5059
Loading...
Searching...
No Matches
norm.c
Go to the documentation of this file.
1/* C implementation of methods for norm */
2
3#include "Lapack-etc.h"
4#include "Mdefines.h"
5
6static
7char La_norm_type(SEXP s)
8{
9#define ARGNAME "type"
10 if (TYPEOF(s) != STRSXP)
11 Rf_error(_("argument '%s' is not of type \"%s\""),
12 ARGNAME, "character");
13 if (LENGTH(s) == 0)
14 Rf_error(_("argument '%s' has length %d"),
15 ARGNAME, 0);
16 s = STRING_ELT(s, 0);
17 if (CHAR(s)[0] == '\0' || CHAR(s)[1] != '\0')
18 Rf_error(_("argument '%s' (\"%s\") does not have string length %d"),
19 ARGNAME, CHAR(s), 1);
20 char type = '\0';
21 switch (CHAR(s)[0]) {
22 case 'M':
23 case 'm':
24 type = 'M';
25 break;
26 case 'O':
27 case 'o':
28 case '1':
29 type = 'O';
30 break;
31 case 'I':
32 case 'i':
33 type = 'I';
34 break;
35 case 'F':
36 case 'f':
37 case 'E':
38 case 'e':
39 type = 'F';
40 break;
41 default:
42 Rf_error(_("argument '%s' (\"%s\") is not \"%s\", \"%s\", \"%s\", \"%s\", \"%s\", or \"%s\""),
43 ARGNAME, CHAR(s), "M", "O", "1", "I", "F", "E");
44 break;
45 }
46 return type;
47#undef ARGNAME
48}
49
50SEXP geMatrix_norm(SEXP s_obj, SEXP s_type)
51{
52 char type = La_norm_type(s_type);
53
54 int *pdim = DIM(s_obj), m = pdim[0], n = pdim[1];
55 if (m == 0 || n == 0)
56 return Rf_ScalarReal(0.0);
57
58 SEXP x = PROTECT(GET_SLOT(s_obj, Matrix_xSym));
59 double norm, *work = NULL;
60 if (type == 'I')
61 work = (double *) R_alloc((size_t) m, sizeof(double));
62 if (TYPEOF(x) == CPLXSXP)
63 norm =
64 F77_CALL(zlange)(&type, &m, &n, COMPLEX(x), &m, work FCONE);
65 else
66 norm =
67 F77_CALL(dlange)(&type, &m, &n, REAL(x), &m, work FCONE);
68 UNPROTECT(1); /* x */
69
70 return Rf_ScalarReal(norm);
71}
72
73SEXP syMatrix_norm(SEXP s_obj, SEXP s_type)
74{
75 char type = La_norm_type(s_type);
76
77 int n = DIM(s_obj)[1];
78 if (n == 0)
79 return Rf_ScalarReal(0.0);
80 char ul = UPLO(s_obj);
81
82 SEXP x = PROTECT(GET_SLOT(s_obj, Matrix_xSym));
83 double norm, *work = NULL;
84 if (type == 'O' || type == 'I')
85 work = (double *) R_alloc((size_t) n, sizeof(double));
86 if (TYPEOF(x) == CPLXSXP) {
87 if (TRANS(s_obj) == 'C')
88 norm =
89 F77_CALL(zlanhe)(&type, &ul, &n, COMPLEX(x), &n, work FCONE FCONE);
90 else
91 norm =
92 F77_CALL(zlansy)(&type, &ul, &n, COMPLEX(x), &n, work FCONE FCONE);
93 }
94 else
95 norm =
96 F77_CALL(dlansy)(&type, &ul, &n, REAL(x), &n, work FCONE FCONE);
97 UNPROTECT(1); /* x */
98
99 return Rf_ScalarReal(norm);
100}
101
102SEXP spMatrix_norm(SEXP s_obj, SEXP s_type)
103{
104 char type = La_norm_type(s_type);
105
106 int n = DIM(s_obj)[1];
107 if (n == 0)
108 return Rf_ScalarReal(0.0);
109 char ul = UPLO(s_obj);
110
111 SEXP x = PROTECT(GET_SLOT(s_obj, Matrix_xSym));
112 double norm, *work = NULL;
113 if (type == 'O' || type == 'I')
114 work = (double *) R_alloc((size_t) n, sizeof(double));
115 if (TYPEOF(x) == CPLXSXP) {
116 if (TRANS(s_obj) == 'C')
117 norm =
118 F77_CALL(zlanhp)(&type, &ul, &n, COMPLEX(x), work FCONE FCONE);
119 else
120 norm =
121 F77_CALL(zlansp)(&type, &ul, &n, COMPLEX(x), work FCONE FCONE);
122 }
123 else
124 norm =
125 F77_CALL(dlansp)(&type, &ul, &n, REAL(x), work FCONE FCONE);
126 UNPROTECT(1); /* x */
127
128 return Rf_ScalarReal(norm);
129}
130
131SEXP trMatrix_norm(SEXP s_obj, SEXP s_type)
132{
133 char type = La_norm_type(s_type);
134
135 int n = DIM(s_obj)[1];
136 if (n == 0)
137 return Rf_ScalarReal(0.0);
138 char ul = UPLO(s_obj), nu = DIAG(s_obj);
139
140 SEXP x = PROTECT(GET_SLOT(s_obj, Matrix_xSym));
141 double norm, *work = NULL;
142 if (type == 'I')
143 work = (double *) R_alloc((size_t) n, sizeof(double));
144 if (TYPEOF(x) == CPLXSXP)
145 norm =
146 F77_CALL(zlantr)(&type, &ul, &nu, &n, &n, COMPLEX(x), &n, work FCONE FCONE FCONE);
147 else
148 norm =
149 F77_CALL(dlantr)(&type, &ul, &nu, &n, &n, REAL(x), &n, work FCONE FCONE FCONE);
150 UNPROTECT(1); /* x */
151
152 return Rf_ScalarReal(norm);
153}
154
155SEXP tpMatrix_norm(SEXP s_obj, SEXP s_type)
156{
157 char type = La_norm_type(s_type);
158
159 int n = DIM(s_obj)[1];
160 if (n == 0)
161 return Rf_ScalarReal(0.0);
162 char ul = UPLO(s_obj), nu = DIAG(s_obj);
163
164 SEXP x = PROTECT(GET_SLOT(s_obj, Matrix_xSym));
165 double norm, *work = NULL;
166 if (type == 'I')
167 work = (double *) R_alloc((size_t) n, sizeof(double));
168 if (TYPEOF(x) == CPLXSXP)
169 norm =
170 F77_CALL(zlantp)(&type, &ul, &nu, &n, COMPLEX(x), work FCONE FCONE FCONE);
171 else
172 norm =
173 F77_CALL(dlantp)(&type, &ul, &nu, &n, REAL(x), work FCONE FCONE FCONE);
174 UNPROTECT(1); /* x */
175
176 return Rf_ScalarReal(norm);
177}
#define FCONE
Definition Lapack-etc.h:13
#define _(String)
Definition Mdefines.h:66
#define DIAG(x)
Definition Mdefines.h:111
#define UPLO(x)
Definition Mdefines.h:101
#define TRANS(x)
Definition Mdefines.h:106
#define DIM(x)
Definition Mdefines.h:85
#define GET_SLOT(x, name)
Definition Mdefines.h:72
#define TYPEOF(s)
Definition Mdefines.h:123
SEXP Matrix_xSym
Definition init.c:635
SEXP syMatrix_norm(SEXP s_obj, SEXP s_type)
Definition norm.c:73
SEXP geMatrix_norm(SEXP s_obj, SEXP s_type)
Definition norm.c:50
static char La_norm_type(SEXP s)
Definition norm.c:7
SEXP trMatrix_norm(SEXP s_obj, SEXP s_type)
Definition norm.c:131
#define ARGNAME
SEXP tpMatrix_norm(SEXP s_obj, SEXP s_type)
Definition norm.c:155
SEXP spMatrix_norm(SEXP s_obj, SEXP s_type)
Definition norm.c:102