Matrix r5059
Loading...
Searching...
No Matches
objects.c
Go to the documentation of this file.
1#include "Mdefines.h"
2
3const char *valid_dense [] = { VALID_DENSE , "" };
4const char *valid_sparse [] = { VALID_SPARSE , "" };
6const char *valid_sparse_triplet [] = { VALID_SPARSE_TRIPLET , "" };
7const char *valid_diagonal [] = { VALID_DIAGONAL , "" };
8const char *valid_index [] = { VALID_INDEX , "" };
9const char *valid_matrix [] = { VALID_MATRIX , "" };
10const char *valid_vector [] = { VALID_VECTOR , "" };
12
13SEXP newObject(const char *what)
14{
15 SEXP class = PROTECT(R_do_MAKE_CLASS(what)), obj = R_do_new_object(class);
16 UNPROTECT(1);
17 return obj;
18}
19
20char typeToKind(SEXPTYPE type)
21{
22 switch (type) {
23 case LGLSXP:
24 return 'l';
25 case INTSXP:
26 return 'i';
27 case REALSXP:
28 return 'd';
29 case CPLXSXP:
30 return 'z';
31 default:
32 Rf_error(_("unexpected type \"%s\" in '%s'"),
33 Rf_type2char(type), __func__);
34 return '\0';
35 }
36}
37
38SEXPTYPE kindToType(char kind)
39{
40 switch (kind) {
41 case 'n':
42 case 'l':
43 return LGLSXP;
44 case 'i':
45 return INTSXP;
46 case 'd':
47 return REALSXP;
48 case 'z':
49 return CPLXSXP;
50 default:
51 Rf_error(_("unexpected kind \"%c\" in '%s'"), kind, __func__);
52 return NILSXP;
53 }
54}
55
56size_t kindToSize(char kind)
57{
58 switch (kind) {
59 case 'n':
60 case 'l':
61 case 'i':
62 return sizeof(int);
63 case 'd':
64 return sizeof(double);
65 case 'z':
66 return sizeof(Rcomplex);
67 default:
68 Rf_error(_("unexpected kind \"%c\" in '%s'"), kind, __func__);
69 return 0;
70 }
71}
72
73const char *Matrix_superclass(const char *class, int mode)
74{
75 if (class[0] == 'p') {
76 if (mode & 1)
77 return "indMatrix";
78 } else if (class[1] == 'o') {
79 if (mode & 4)
80 switch (class[2]) {
81 case 'r': return "dsyMatrix";
82 case 'p': return "dspMatrix";
83 }
84 if (mode & 2)
85 switch (class[2]) {
86 case 'r': return "dpoMatrix";
87 case 'p': return "dppMatrix";
88 }
89 } else if (class[1] == 'p') {
90 if (mode & 4) {
91 if (class[0] == 'z')
92 switch (class[2]) {
93 case 'C': return "zsCMatrix";
94 case 'R': return "zsRMatrix";
95 case 'T': return "zsTMatrix";
96 case 'o': return "zsyMatrix";
97 case 'p': return "zspMatrix";
98 }
99 else
100 switch (class[2]) {
101 case 'C': return "dsCMatrix";
102 case 'R': return "dsRMatrix";
103 case 'T': return "dsTMatrix";
104 case 'o': return "dsyMatrix";
105 case 'p': return "dspMatrix";
106 }
107 }
108 }
109 return class;
110}
111
112const char *Matrix_class(SEXP x, const char **valid, int mode,
113 const char *caller)
114{
115 int i = R_check_class_etc(x, valid);
116 if (i >= 0)
117 return (mode <= 0) ? valid[i] : Matrix_superclass(valid[i], mode);
118 else {
119 if (caller)
120 ERROR_INVALID_CLASS(x, caller);
121 return NULL;
122 }
123}
124
125char Matrix_kind(SEXP obj)
126{
127 if (TYPEOF(obj) != OBJSXP)
128 switch (TYPEOF(obj)) {
129 case LGLSXP:
130 return 'l';
131 case INTSXP:
132 return 'i';
133 case REALSXP:
134 return 'd';
135 case CPLXSXP:
136 return 'z';
137 default:
138 return '\0';
139 }
140 const char *class = Matrix_class(obj, valid_matrix_or_vector, 7, NULL);
141 if (!class)
142 return '\0';
143 return (class[2] == 'd') ? 'n' : class[0];
144}
145
146char Matrix_shape(SEXP obj, int mode)
147{
148 if (TYPEOF(obj) != OBJSXP)
149 return '\0';
150 const char *class = Matrix_class(obj, valid_matrix_or_vector, mode, NULL);
151 if (!class)
152 return '\0';
153 return (class[2] == 'd') ? 'i' : (class[3] == 'M') ? class[1] : 'g';
154}
155
156char Matrix_repr(SEXP obj)
157{
158 if (TYPEOF(obj) != OBJSXP)
159 return '\0';
160 const char *class = Matrix_class(obj, valid_matrix_or_vector, 7, NULL);
161 if (!class)
162 return '\0';
163 switch (class[2]) {
164 case 'e':
165 case 'y':
166 case 'r':
167 return 'n'; /* unpackedMatrix */
168 case 'p':
169 return 'p'; /* packedMatrix */
170 case 'C':
171 return 'C'; /* CsparseMatrix */
172 case 'R':
173 return 'R'; /* RsparseMatrix */
174 case 'T':
175 return 'T'; /* TsparseMatrix */
176 case 'i':
177 return 'd'; /* diagonalMatrix */
178 case 'd':
179 return 'i'; /* indMatrix */
180 default:
181 return '\0';
182 }
183}
184
185SEXP R_Matrix_class(SEXP s_obj, SEXP s_mode)
186{
187 const char *class = Matrix_class(s_obj, valid_matrix_or_vector, Rf_asInteger(s_mode), NULL);
188 return Rf_mkString((!class) ? "" : class);
189}
190
191SEXP R_Matrix_kind(SEXP s_obj)
192{
193 char s[] = { Matrix_kind (s_obj), '\0' };
194 return Rf_mkString(s);
195}
196
197SEXP R_Matrix_shape(SEXP s_obj, SEXP s_mode)
198{
199 char s[] = { Matrix_shape(s_obj, Rf_asInteger(s_mode)), '\0' };
200 return Rf_mkString(s);
201}
202
203SEXP R_Matrix_repr(SEXP s_obj)
204{
205 char s[] = { Matrix_repr (s_obj), '\0' };
206 return Rf_mkString(s);
207}
#define VALID_MATRIX_OR_VECTOR
Definition Mdefines.h:272
#define ERROR_INVALID_CLASS(_X_, _FUNC_)
Definition Mdefines.h:149
#define VALID_DENSE
Definition Mdefines.h:230
#define VALID_SPARSE
Definition Mdefines.h:257
#define VALID_SPARSE_COMPRESSED
Definition Mdefines.h:241
#define _(String)
Definition Mdefines.h:66
#define VALID_DIAGONAL
Definition Mdefines.h:260
#define VALID_VECTOR
Definition Mdefines.h:269
#define VALID_INDEX
Definition Mdefines.h:263
#define VALID_MATRIX
Definition Mdefines.h:266
#define TYPEOF(s)
Definition Mdefines.h:123
#define VALID_SPARSE_TRIPLET
Definition Mdefines.h:251
const char * Matrix_superclass(const char *class, int mode)
Definition objects.c:73
const char * valid_sparse_compressed[]
Definition objects.c:5
SEXP R_Matrix_shape(SEXP s_obj, SEXP s_mode)
Definition objects.c:197
SEXP R_Matrix_repr(SEXP s_obj)
Definition objects.c:203
const char * valid_dense[]
Definition objects.c:3
const char * valid_matrix[]
Definition objects.c:9
const char * Matrix_class(SEXP x, const char **valid, int mode, const char *caller)
Definition objects.c:112
SEXP newObject(const char *what)
Definition objects.c:13
char Matrix_kind(SEXP obj)
Definition objects.c:125
char Matrix_shape(SEXP obj, int mode)
Definition objects.c:146
SEXP R_Matrix_class(SEXP s_obj, SEXP s_mode)
Definition objects.c:185
size_t kindToSize(char kind)
Definition objects.c:56
const char * valid_sparse[]
Definition objects.c:4
char Matrix_repr(SEXP obj)
Definition objects.c:156
const char * valid_index[]
Definition objects.c:8
SEXP R_Matrix_kind(SEXP s_obj)
Definition objects.c:191
char typeToKind(SEXPTYPE type)
Definition objects.c:20
const char * valid_matrix_or_vector[]
Definition objects.c:11
const char * valid_diagonal[]
Definition objects.c:7
SEXPTYPE kindToType(char kind)
Definition objects.c:38
const char * valid_vector[]
Definition objects.c:10
const char * valid_sparse_triplet[]
Definition objects.c:6