Matrix r5059
Loading...
Searching...
No Matches
Mdefines.h
Go to the documentation of this file.
1#ifndef MATRIX_MDEFINES_H
2#define MATRIX_MDEFINES_H
3
4#ifdef __GLIBC__
5# define _POSIX_C_SOURCE 200809L
6#endif
7
8#include <float.h> /* DBL_EPSILON */
9#include <limits.h> /* INT_MAX */
10#include <math.h> /* fabs, hypot, sqrt, ... */
11#include <stdarg.h> /* va_list, va_start, ... */
12#include <stddef.h> /* size_t */
13#include <stdint.h> /* int_fast64_t */
14#include <stdio.h> /* vsnprintf */
15#include <string.h> /* memcpy, memset */
16
17#include <Rconfig.h> /* HAVE_ALLOCA_H, ENABLE_NLS */
18#if !defined(__GNUC__) && defined(HAVE_ALLOCA_H)
19# include <alloca.h> /* alloca */
20#endif
21#ifdef ENABLE_NLS
22# include <libintl.h> /* dgettext, dngettext */
23#endif
24
25#include <R_ext/Arith.h> /* ISNAN, R_FINITE, ... */
26#include <R_ext/Boolean.h> /* Rboolean */
27#include <R_ext/Complex.h> /* Rcomplex */
28#include <R_ext/Error.h> /* Rf_error, Rf_warning */
29#include <R_ext/Memory.h> /* R_alloc */
30#include <R_ext/Utils.h> /* R_CheckStack */
31#include <R_ext/RS.h> /* R_Calloc, R_Free */
32#include <Rinternals.h> /* SEXP, ... */
33#include <Rversion.h> /* R_VERSION, ... */
34
35#define Matrix_ErrorBufferSize 4096
36#define Matrix_CallocThreshold 8192
37#define Matrix_TranslationDomain "Matrix"
38
39#ifdef __GNUC__
40# define Matrix_alloca(x) __builtin_alloca((x))
41#else
42# define Matrix_alloca(x) alloca((x))
43#endif
44
45#define Matrix_Calloc(p, n, t) \
46do { \
47 if ((n) >= Matrix_CallocThreshold) \
48 (p) = R_Calloc((n), t); \
49 else { \
50 (p) = (t *) Matrix_alloca(sizeof(t) * (size_t) (n)); \
51 R_CheckStack(); \
52 memset((p), 0, sizeof(t) * (size_t) (n)); \
53 } \
54} while (0)
55
56#define Matrix_Free(p, n) \
57do { \
58 if ((n) >= Matrix_CallocThreshold) \
59 R_Free((p)); \
60} while (0)
61
62#ifndef ENABLE_NLS
63# define dgettext(Domain, String) (String)
64# define dngettext(Domain, String, StringP, N) (((N) == 1) ? String : StringP)
65#endif
66#define _(String) dgettext(Matrix_TranslationDomain, String)
67
68#define errorChar(...) Rf_mkChar (Matrix_sprintf(__VA_ARGS__))
69#define errorString(...) Rf_mkString(Matrix_sprintf(__VA_ARGS__))
70
71#define HAS_SLOT(x, name) R_has_slot ((x), (name))
72#define GET_SLOT(x, name) R_do_slot ((x), (name))
73#define SET_SLOT(x, name, value) R_do_slot_assign((x), (name), (value))
74
75#define COPY_SLOT(dest, src, name) \
76 do { \
77 SEXP value = GET_SLOT((src), (name)); \
78 if (XLENGTH(value) > 0) { \
79 PROTECT(value); \
80 SET_SLOT((dest), (name), value); \
81 UNPROTECT(1); \
82 } \
83 } while (0)
84
85#define DIM(x) \
86 INTEGER(GET_SLOT((x), Matrix_DimSym))
87#define SET_DIM(x, m, n) \
88 do { \
89 int __m__ = (m), __n__ = (n); \
90 if (__m__ != __n__ || __n__ > 0) { \
91 int *p = INTEGER(GET_SLOT((x), Matrix_DimSym)); \
92 p[0] = (m); p[1] = (n); \
93 } \
94 } while (0)
95
96#define DIMNAMES(x, mode) \
97 (DIMNAMES)((x), (mode))
98#define SET_DIMNAMES(x, mode, value) \
99 (SET_DIMNAMES)((x), (mode), (value))
100
101#define UPLO(x) \
102 CHAR(STRING_ELT(GET_SLOT((x), Matrix_uploSym), 0))[0]
103#define SET_UPLO(x) \
104 SET_STRING_ELT(GET_SLOT((x), Matrix_uploSym), 0, Matrix_LChar)
105
106#define TRANS(x) \
107 CHAR(STRING_ELT(GET_SLOT((x), Matrix_transSym), 0))[0]
108#define SET_TRANS(x) \
109 SET_STRING_ELT(GET_SLOT((x), Matrix_transSym), 0, Matrix_TChar)
110
111#define DIAG(x) \
112 CHAR(STRING_ELT(GET_SLOT((x), Matrix_diagSym), 0))[0]
113#define SET_DIAG(x) \
114 SET_STRING_ELT(GET_SLOT((x), Matrix_diagSym), 0, Matrix_UChar)
115
116#define MARGIN(x) \
117 (INTEGER(GET_SLOT((x), Matrix_marginSym))[0] - 1)
118#define SET_MARGIN(x, j) \
119 do { \
120 INTEGER(GET_SLOT((x), Matrix_marginSym))[0] = (j) + 1; \
121 } while (0)
122
123#define TYPEOF(s) \
124 ((SEXPTYPE) (TYPEOF)((s)))
125
126#define DENSE_INDEX_N(i, j, m) \
127 ((i) + (j) * (m))
128#define DENSE_INDEX_U(i, j, m) \
129 ((i) + ((j) * ( (j) + 1U)) / 2U)
130#define DENSE_INDEX_L(i, j, m) \
131 ((i) + ((j) * ((m) - (j) - 1U + (m))) / 2U)
132#define PACKED_LENGTH(n) \
133 ((n) + ((n) * ( (n) - 1U)) / 2U)
134
135#define ABS(i) \
136 (((i) < 0) ? -(i) : (i))
137
138#define SWAP(a, b, t, op) \
139 do { t tmp = op(a); a = op(b); b = tmp; } while (0)
140
141#define ERROR_OOM(_FUNC_) \
142 Rf_error(_("out of memory in '%s'"), \
143 _FUNC_)
144
145#define ERROR_INVALID_TYPE(_X_, _FUNC_) \
146 Rf_error(_("invalid type \"%s\" in '%s'"), \
147 Rf_type2char(TYPEOF(_X_)), _FUNC_)
148
149#define ERROR_INVALID_CLASS(_X_, _FUNC_) \
150do { \
151 if (!Rf_isObject(_X_)) \
152 ERROR_INVALID_TYPE(_X_, _FUNC_); \
153 else \
154 Rf_error(_("invalid class \"%s\" in '%s'"), \
155 CHAR(STRING_ELT(Rf_getAttrib(_X_, R_ClassSymbol), 0)), _FUNC_); \
156} while (0)
157
158#define VALID_UPLO(s, c) \
159do { \
160 if (TYPEOF(s) != STRSXP || LENGTH(s) < 1 || \
161 (s = STRING_ELT(s, 0)) == NA_STRING || \
162 ((c = CHAR(s)[0]) != 'U' && c != 'L')) \
163 Rf_error(_("'%s' must be \"%c\" or \"%c\""), "uplo", 'U', 'L'); \
164} while (0)
165
166#define VALID_TRANS(s, c) \
167do { \
168 if (TYPEOF(s) != STRSXP || LENGTH(s) < 1 || \
169 (s = STRING_ELT(s, 0)) == NA_STRING || \
170 ((c = CHAR(s)[0]) != 'C' && c != 'T')) \
171 Rf_error(_("'%s' is not \"%c\" or \"%c\""), "trans", 'C', 'T'); \
172} while (0)
173
174#define VALID_DIAG(s, c) \
175do { \
176 if (TYPEOF(s) != STRSXP || LENGTH(s) < 1 || \
177 (s = STRING_ELT(s, 0)) == NA_STRING || \
178 ((c = CHAR(s)[0]) != 'N' && c != 'U')) \
179 Rf_error(_("'%s' is not \"%c\" or \"%c\""), "diag", 'N', 'U'); \
180} while (0)
181
182#define VALID_KIND(s, c) \
183do { \
184 if (TYPEOF(s) != STRSXP || LENGTH(s) < 1 || \
185 (s = STRING_ELT(s, 0)) == NA_STRING || \
186 ((c = CHAR(s)[0]) != 'n' && c != 'l' && c != 'i' && c != 'd' && c != 'z' && c != '.' && c != ',')) \
187 Rf_error(_("'%s' is not \"%c\", \"%c\", \"%c\", \"%c\", or \"%c\""), \
188 "kind", 'n', 'l', 'i', 'd', 'z'); \
189} while (0)
190
191#define VALID_SHAPE(s, c) \
192do { \
193 if (TYPEOF(s) != STRSXP || LENGTH(s) < 1 || \
194 (s = STRING_ELT(s, 0)) == NA_STRING || \
195 ((c = CHAR(s)[0]) != 'g' && c != 's' && c != 'p' && c != 't')) \
196 Rf_error(_("'%s' is not \"%c\", \"%c\", \"%c\", or \"%c\""), \
197 "shape", 'g', 's', 'p', 't'); \
198} while (0)
199
200#define VALID_REPR(s, c, dot) \
201do { \
202 if (TYPEOF(s) != STRSXP || LENGTH(s) < 1 || \
203 (s = STRING_ELT(s, 0)) == NA_STRING || \
204 ((c = CHAR(s)[0]) != 'C' && c != 'R' && c != 'T' && !(dot && c != '.'))) \
205 Rf_error(_("'%s' is not \"%c\", \"%c\", or \"%c\""), \
206 "repr", 'C', 'R', 'T'); \
207} while (0)
208
209#define VALID_MARGIN(s, d) \
210do { \
211 if (TYPEOF(s) != INTSXP || LENGTH(s) < 1 || \
212 ((d = INTEGER(s)[0] - 1) != 0 && d != 1)) \
213 Rf_error(_("'%s' is not %d or %d"), "margin", 1, 2); \
214} while (0)
215
216#define VALID_LOGIC2(s, d) \
217do { \
218 if (TYPEOF(s) != LGLSXP || LENGTH(s) < 1 || \
219 ((d = LOGICAL(s)[0]) == NA_LOGICAL)) \
220 Rf_error(_("'%s' is not %s or %s"), #d, "TRUE", "FALSE"); \
221} while (0)
222
223#define VALID_LOGIC3(s, d) \
224do { \
225 if (TYPEOF(s) != LGLSXP || LENGTH(s) < 1) \
226 Rf_error(_("'%s' is not %s, %s, or %s"), #d, "TRUE", "FALSE", "NA"); \
227 else d = LOGICAL(s)[0]; \
228} while (0)
229
230#define VALID_DENSE \
231"ngeMatrix", "lgeMatrix", "igeMatrix", "dgeMatrix", "zgeMatrix", \
232"nsyMatrix", "lsyMatrix", "isyMatrix", "dsyMatrix", "zsyMatrix", \
233 "dpoMatrix", "zpoMatrix", \
234 "corMatrix", \
235"ntrMatrix", "ltrMatrix", "itrMatrix", "dtrMatrix", "ztrMatrix", \
236"nspMatrix", "lspMatrix", "ispMatrix", "dspMatrix", "zspMatrix", \
237 "dppMatrix", "zppMatrix", \
238 "copMatrix", \
239"ntpMatrix", "ltpMatrix", "itpMatrix", "dtpMatrix", "ztpMatrix"
240
241#define VALID_SPARSE_COMPRESSED \
242"ngCMatrix", "lgCMatrix", "igCMatrix", "dgCMatrix", "zgCMatrix", \
243"nsCMatrix", "lsCMatrix", "isCMatrix", "dsCMatrix", "zsCMatrix", \
244 "dpCMatrix", "zpCMatrix", \
245"ntCMatrix", "ltCMatrix", "itCMatrix", "dtCMatrix", "ztCMatrix", \
246"ngRMatrix", "lgRMatrix", "igRMatrix", "dgRMatrix", "zgRMatrix", \
247"nsRMatrix", "lsRMatrix", "isRMatrix", "dsRMatrix", "zsRMatrix", \
248 "dpRMatrix", "zpRMatrix", \
249"ntRMatrix", "ltRMatrix", "itRMatrix", "dtRMatrix", "ztRMatrix"
250
251#define VALID_SPARSE_TRIPLET \
252"ngTMatrix", "lgTMatrix", "igTMatrix", "dgTMatrix", "zgTMatrix", \
253"nsTMatrix", "lsTMatrix", "isTMatrix", "dsTMatrix", "zsTMatrix", \
254 "dpTMatrix", "zpTMatrix", \
255"ntTMatrix", "ltTMatrix", "itTMatrix", "dtTMatrix", "ztTMatrix"
256
257#define VALID_SPARSE \
258VALID_SPARSE_COMPRESSED, VALID_SPARSE_TRIPLET
259
260#define VALID_DIAGONAL \
261"ndiMatrix", "ldiMatrix", "idiMatrix", "ddiMatrix", "zdiMatrix"
262
263#define VALID_INDEX \
264"indMatrix", "pMatrix"
265
266#define VALID_MATRIX \
267VALID_DENSE, VALID_SPARSE, VALID_DIAGONAL, VALID_INDEX
268
269#define VALID_VECTOR \
270"nsparseVector", "lsparseVector", "isparseVector", "dsparseVector", "zsparseVector"
271
272#define VALID_MATRIX_OR_VECTOR \
273VALID_MATRIX, VALID_VECTOR
274
275/* What we want declared "everywhere" : */
276
277extern SEXP
320
321extern Rcomplex
325
326extern const char
327 *valid_dense[],
336
337#if R_VERSION < R_Version(4, 5, 0)
338int ANY_ATTRIB(SEXP);
339void CLEAR_ATTRIB(SEXP);
340#endif
341
342char *Matrix_sprintf(const char *, ...);
343
344int equalString(SEXP, SEXP, R_xlen_t);
345SEXP duplicateVector(SEXP);
346SEXP allocZero(SEXPTYPE, R_xlen_t);
347SEXP allocUnit(SEXPTYPE, R_xlen_t);
348SEXP allocSeqInt(int, R_xlen_t);
349void naToUnit(SEXP);
350
351SEXP newObject(const char *);
352void validObject(SEXP, const char *);
353
354char typeToKind(SEXPTYPE);
355SEXPTYPE kindToType(char);
356size_t kindToSize(char);
357
358const char *Matrix_superclass(const char *, int);
359const char *Matrix_class(SEXP, const char **, int, const char *);
360
361int DimNames_is_trivial(SEXP);
362int DimNames_is_symmetric(SEXP);
363
364void symDN(SEXP, SEXP, int);
365void cpyDN(SEXP, SEXP, int);
366
367SEXP (DIMNAMES)(SEXP, int);
368void (SET_DIMNAMES)(SEXP, int, SEXP);
369
370SEXP get_factor(SEXP, const char *);
371void set_factor(SEXP, const char *, SEXP);
372
373#endif /* MATRIX_MDEFINES_H */
SEXP Matrix_nextSym
Definition Mdefines.h:299
const char * valid_vector[]
Definition Mdefines.h:334
SEXP Matrix_nzSym
Definition Mdefines.h:300
SEXP Matrix_transSym
Definition Mdefines.h:312
void symDN(SEXP, SEXP, int)
Definition attrib.c:69
SEXP Matrix_kindSym
Definition Mdefines.h:292
SEXP Matrix_sdSym
Definition Mdefines.h:310
SEXP Matrix_permSym
Definition Mdefines.h:304
const char * valid_diagonal[]
Definition Mdefines.h:331
SEXP Matrix_DimSym
Definition Mdefines.h:279
const char * valid_matrix_or_vector[]
Definition Mdefines.h:335
SEXP allocUnit(SEXPTYPE, R_xlen_t)
Definition utils.c:94
const char * valid_dense[]
Definition objects.c:3
SEXP Matrix_factorsSym
Definition Mdefines.h:287
SEXP Matrix_betaSym
Definition Mdefines.h:284
#define SET_DIMNAMES(x, mode, value)
Definition Mdefines.h:98
SEXP Matrix_valuesSym
Definition Mdefines.h:314
SEXP duplicateVector(SEXP)
Definition utils.c:42
SEXP Matrix_pxSym
Definition Mdefines.h:307
char typeToKind(SEXPTYPE)
Definition objects.c:20
SEXP Matrix_logarithmSym
Definition Mdefines.h:294
void set_factor(SEXP, const char *, SEXP)
Definition attrib.c:192
SEXP allocZero(SEXPTYPE, R_xlen_t)
Definition utils.c:69
const char * Matrix_class(SEXP, const char **, int, const char *)
Definition objects.c:112
SEXP Matrix_marginSym
Definition Mdefines.h:295
char * Matrix_sprintf(const char *,...)
Definition utils.c:18
SEXPTYPE kindToType(char)
Definition objects.c:38
SEXP Matrix_prevSym
Definition Mdefines.h:306
int DimNames_is_symmetric(SEXP)
Definition attrib.c:46
#define DIMNAMES(x, mode)
Definition Mdefines.h:96
SEXP Matrix_TChar
Definition Mdefines.h:318
SEXP allocSeqInt(int, R_xlen_t)
Definition utils.c:141
SEXP Matrix_orderingSym
Definition Mdefines.h:302
Rcomplex Matrix_zunit
Definition Mdefines.h:323
SEXP Matrix_RSym
Definition Mdefines.h:281
void naToUnit(SEXP)
Definition utils.c:151
const char * valid_index[]
Definition Mdefines.h:332
Rcomplex Matrix_zna
Definition Mdefines.h:324
SEXP Matrix_xSym
Definition Mdefines.h:316
SEXP Matrix_lengthSym
Definition Mdefines.h:293
SEXP Matrix_iSym
Definition Mdefines.h:288
SEXP Matrix_sSym
Definition Mdefines.h:309
SEXP Matrix_jSym
Definition Mdefines.h:291
SEXP Matrix_DimNamesSym
Definition init.c:597
SEXP Matrix_VSym
Definition Mdefines.h:283
SEXP get_factor(SEXP, const char *)
Definition attrib.c:180
void cpyDN(SEXP, SEXP, int)
Definition attrib.c:110
SEXP Matrix_UChar
Definition Mdefines.h:319
SEXP Matrix_LSym
Definition Mdefines.h:280
SEXP Matrix_maxesizeSym
Definition Mdefines.h:297
SEXP Matrix_diagSym
Definition Mdefines.h:286
const char * valid_sparse_triplet[]
Definition Mdefines.h:330
SEXP Matrix_superSym
Definition Mdefines.h:311
SEXP Matrix_LChar
Definition Mdefines.h:317
int equalString(SEXP, SEXP, R_xlen_t)
Definition utils.c:28
SEXP Matrix_offSym
Definition Mdefines.h:301
Rcomplex Matrix_zzero
Definition init.c:641
SEXP Matrix_uploSym
Definition Mdefines.h:313
SEXP Matrix_minorSym
Definition Mdefines.h:298
SEXP Matrix_isllSym
Definition Mdefines.h:289
SEXP Matrix_colcountSym
Definition Mdefines.h:285
SEXP Matrix_qSym
Definition Mdefines.h:308
int DimNames_is_trivial(SEXP)
Definition attrib.c:38
void validObject(SEXP, const char *)
Definition validity.c:2004
SEXP newObject(const char *)
Definition objects.c:13
SEXP Matrix_ismtSym
Definition Mdefines.h:290
size_t kindToSize(char)
Definition objects.c:56
const char * valid_sparse_compressed[]
Definition Mdefines.h:329
const char * valid_matrix[]
Definition Mdefines.h:333
const char * Matrix_superclass(const char *, int)
Definition objects.c:73
const char * valid_sparse[]
Definition Mdefines.h:328
SEXP Matrix_USym
Definition Mdefines.h:282
SEXP Matrix_pSym
Definition Mdefines.h:303
SEXP Matrix_vectorsSym
Definition Mdefines.h:315
SEXP Matrix_maxcsizeSym
Definition Mdefines.h:296
SEXP Matrix_piSym
Definition Mdefines.h:305