Matrix r4655
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#include "version.h"
5
6#define Matrix_Domain "Matrix"
7#define Matrix_CallocThreshold 8192
8#define Matrix_ErrorBufferSize 4096
9
10/* NB: system headers should come before R headers */
11
12#ifdef __GLIBC__
13/* ensure that strdup() and others are declared when string.h is included : */
14# define _POSIX_C_SOURCE 200809L
15#endif
16
17#include <string.h>
18#include <stdint.h>
19#include <limits.h>
20#include <float.h>
21
22#ifdef INT_FAST64_MAX
23typedef int_fast64_t Matrix_int_fast64_t;
24# define MATRIX_INT_FAST64_MIN INT_FAST64_MIN
25# define MATRIX_INT_FAST64_MAX INT_FAST64_MAX
26#else
27typedef long long Matrix_int_fast64_t;
28# define MATRIX_INT_FAST64_MIN LLONG_MIN
29# define MATRIX_INT_FAST64_MAX LLONG_MAX
30#endif
31
32#ifndef STRICT_R_HEADERS
33# define STRICT_R_HEADERS
34#endif
35
36#include <R.h>
37#include <Rinternals.h>
38
39/* Copy and paste from WRE : */
40#ifdef ENABLE_NLS
41# include <libintl.h>
42# define _(String) dgettext(Matrix_Domain, String)
43#else
44# define _(String) (String)
45# define dngettext(Domain, String, StringP, N) ((N == 1) ? String : StringP)
46#endif
47
48/* Copy and paste from Defn.h : */
49/* 'alloca' is neither C99 nor POSIX */
50#ifdef __GNUC__
51/* This covers GNU, Clang and Intel compilers */
52/* #undef needed in case some other header, e.g. malloc.h, already did this */
53# undef alloca
54# define alloca(x) __builtin_alloca((x))
55#else
56# ifdef HAVE_ALLOCA_H
57/* This covers native compilers on Solaris and AIX */
58# include <alloca.h>
59# endif
60/* It might have been defined via some other standard header, e.g. stdlib.h */
61# if !HAVE_DECL_ALLOCA
62extern void *alloca(size_t);
63# endif
64#endif
65
66#define Matrix_Calloc(_VAR_, _N_, _CTYPE_) \
67do { \
68 if (_N_ >= Matrix_CallocThreshold) \
69 _VAR_ = R_Calloc(_N_, _CTYPE_); \
70 else { \
71 _VAR_ = (_CTYPE_ *) alloca((size_t) (_N_) * sizeof(_CTYPE_)); \
72 R_CheckStack(); \
73 memset(_VAR_, 0, (size_t) (_N_) * sizeof(_CTYPE_)); \
74 } \
75} while (0)
76
77#define Matrix_Free(_VAR_, _N_) \
78do { \
79 if (_N_ >= Matrix_CallocThreshold) \
80 R_Free(_VAR_); \
81} while (0)
82
83/* Copy and paste from now-deprecated Rdefines.h : */
84#ifndef R_DEFINES_H
85# define GET_SLOT(x, what) R_do_slot(x, what)
86# define SET_SLOT(x, what, value) R_do_slot_assign(x, what, value)
87#endif
88
89/* Often used symbols, defined in ./init.c */
90extern
91#include "Msymbols.h"
92
93/* Often used numbers, defined in ./init.c */
94extern
95Rcomplex Matrix_zzero, Matrix_zone, Matrix_zna; /* 0+0i, 1+0i, NA+NAi */
96
97#define MINOF(x, y) ((x < y) ? x : y)
98#define MAXOF(x, y) ((x < y) ? y : x)
99#define FIRSTOF(x, y) (x)
100#define SECONDOF(x, y) (y)
101
102#define ISNA_PATTERN(_X_) (0)
103#define ISNA_LOGICAL(_X_) ((_X_) == NA_LOGICAL)
104#define ISNA_INTEGER(_X_) ((_X_) == NA_INTEGER)
105#define ISNA_REAL(_X_) (ISNAN(_X_))
106#define ISNA_COMPLEX(_X_) (ISNAN((_X_).r) || ISNAN((_X_).i))
107
108#define ISNZ_PATTERN(_X_) ((_X_) != 0)
109#define ISNZ_LOGICAL(_X_) ((_X_) != 0)
110#define ISNZ_INTEGER(_X_) ((_X_) != 0)
111#define ISNZ_REAL(_X_) ((_X_) != 0.0)
112#define ISNZ_COMPLEX(_X_) ((_X_).r != 0.0 || (_X_).i != 0.0)
113
114#define STRICTLY_ISNZ_PATTERN(_X_) \
115 ( ISNZ_PATTERN(_X_))
116#define STRICTLY_ISNZ_LOGICAL(_X_) \
117 (!ISNA_LOGICAL(_X_) && ISNZ_LOGICAL(_X_))
118#define STRICTLY_ISNZ_INTEGER(_X_) \
119 (!ISNA_INTEGER(_X_) && ISNZ_INTEGER(_X_))
120#define STRICTLY_ISNZ_REAL(_X_) \
121 (!ISNA_REAL( _X_) && ISNZ_REAL( _X_))
122#define STRICTLY_ISNZ_COMPLEX(_X_) \
123 (!ISNA_COMPLEX(_X_) && ISNZ_COMPLEX(_X_))
124
125#define NOTREAL_PATTERN(_X_) 0
126#define NOTREAL_LOGICAL(_X_) 0
127#define NOTREAL_INTEGER(_X_) 0
128#define NOTREAL_REAL(_X_) 0
129#define NOTREAL_COMPLEX(_X_) (_X_.i != 0.0)
130
131#define NOTCONJ_PATTERN(_X_, _Y_) \
132 ((_X_ != 0) != (_Y_ != 0))
133#define NOTCONJ_LOGICAL(_X_, _Y_) \
134 (_X_ != _Y_)
135#define NOTCONJ_INTEGER(_X_, _Y_) \
136 (_X_ != _Y_)
137#define NOTCONJ_REAL(_X_, _Y_) \
138 ((ISNAN(_X_)) ? !ISNAN(_Y_) : ISNAN(_Y_) || _X_ != _Y_)
139#define NOTCONJ_COMPLEX(_X_, _Y_) \
140 (((ISNAN(_X_.r)) ? !ISNAN(_Y_.r) : ISNAN(_Y_.r) || _X_.r != _Y_.r) || \
141 ((ISNAN(_X_.i)) ? !ISNAN(_Y_.i) : ISNAN(_Y_.i) || _X_.r != -_Y_.r))
142
143#define INCREMENT_PATTERN(_X_, _Y_) \
144 do { \
145 _X_ = 1; \
146 } while (0)
147#define INCREMENT_LOGICAL(_X_, _Y_) \
148 do { \
149 if (_Y_ == NA_LOGICAL) { \
150 if (_X_ == 0) \
151 _X_ = NA_LOGICAL; \
152 } else if (_Y_ != 0) \
153 _X_ = 1; \
154 } while (0)
155#define INCREMENT_INTEGER(_X_, _Y_) \
156 do { \
157 if (_X_ != NA_INTEGER) { \
158 if (_Y_ == NA_INTEGER) \
159 _X_ = NA_INTEGER; \
160 else if ((_Y_ < 0) \
161 ? (_X_ <= INT_MIN - _Y_) \
162 : (_X_ > INT_MAX - _Y_)) { \
163 warning(_("NAs produced by integer overflow")); \
164 _X_ = NA_INTEGER; \
165 } else \
166 _X_ += _Y_; \
167 } \
168 } while (0)
169#define INCREMENT_REAL(_X_, _Y_) \
170 do { \
171 _X_ += _Y_; \
172 } while (0)
173#define INCREMENT_COMPLEX(_X_, _Y_) \
174 do { \
175 _X_.r += _Y_.r; \
176 _X_.i += _Y_.i; \
177 } while (0)
178
179#define ASSIGN_REAL(_X_, _Y_) \
180 do { _X_ = _Y_ ; } while (0)
181#define ASSIGN_COMPLEX(_X_, _Y_) \
182 do { _X_.r = _Y_.r; _X_.i = _Y_.i; } while (0)
183
184#define SCALE1_REAL(_X_, _A_) \
185 do { _X_ *= _A_; } while (0)
186#define SCALE1_COMPLEX(_X_, _A_) \
187 do { _X_.r *= _A_; _X_.i *= _A_; } while (0)
188
189#define SCALE2_REAL(_X_, _A_) \
190 do { _X_ /= _A_; } while (0)
191#define SCALE2_COMPLEX(_X_, _A_) \
192 do { _X_.r /= _A_; _X_.i /= _A_; } while (0)
193
194#define PACKED_AR21_UP(i, j) \
195 ((R_xlen_t) ((i) + ((Matrix_int_fast64_t) (j) * ( (j) + 1)) / 2))
196#define PACKED_AR21_LO(i, j, m2) \
197 ((R_xlen_t) ((i) + ((Matrix_int_fast64_t) (j) * ((m2) - (j) - 1)) / 2))
198#define PACKED_LENGTH(m) \
199 ((R_xlen_t) ((m) + ((Matrix_int_fast64_t) (m) * ( (m) - 1)) / 2))
200
201#define SHOW(...) __VA_ARGS__
202#define HIDE(...)
203
204#define ERROR_INVALID_TYPE(_X_, _FUNC_) \
205 error(_("invalid type \"%s\" in '%s'"), \
206 type2char(TYPEOF(_X_)), _FUNC_)
207
208#define ERROR_INVALID_CLASS(_X_, _FUNC_) \
209do { \
210 if (!OBJECT(_X_)) \
211 ERROR_INVALID_TYPE(_X_, _FUNC_); \
212 else { \
213 SEXP class = PROTECT(getAttrib(_X_, R_ClassSymbol)); \
214 error(_("invalid class \"%s\" in '%s'"), \
215 CHAR(STRING_ELT(class, 0)), _FUNC_); \
216 UNPROTECT(1); \
217 } \
218} while (0)
219
220#define VALID_NONVIRTUAL_MATRIX \
221/* 0 */ "dpoMatrix", "dppMatrix", \
222/* 2 */ "corMatrix", "copMatrix", \
223/* 4 */ "pMatrix", "indMatrix", \
224/* 6 */ "ngCMatrix", "ngRMatrix", "ngTMatrix", "ngeMatrix", "ndiMatrix", \
225/* 11 */ "nsCMatrix", "nsRMatrix", "nsTMatrix", "nsyMatrix", "nspMatrix", \
226/* 16 */ "ntCMatrix", "ntRMatrix", "ntTMatrix", "ntrMatrix", "ntpMatrix", \
227/* 21 */ "lgCMatrix", "lgRMatrix", "lgTMatrix", "lgeMatrix", "ldiMatrix", \
228/* 26 */ "lsCMatrix", "lsRMatrix", "lsTMatrix", "lsyMatrix", "lspMatrix", \
229/* 31 */ "ltCMatrix", "ltRMatrix", "ltTMatrix", "ltrMatrix", "ltpMatrix", \
230/* 36 */ "igCMatrix", "igRMatrix", "igTMatrix", "igeMatrix", "idiMatrix", \
231/* 41 */ "isCMatrix", "isRMatrix", "isTMatrix", "isyMatrix", "ispMatrix", \
232/* 46 */ "itCMatrix", "itRMatrix", "itTMatrix", "itrMatrix", "itpMatrix", \
233/* 51 */ "dgCMatrix", "dgRMatrix", "dgTMatrix", "dgeMatrix", "ddiMatrix", \
234/* 56 */ "dsCMatrix", "dsRMatrix", "dsTMatrix", "dsyMatrix", "dspMatrix", \
235/* 61 */ "dtCMatrix", "dtRMatrix", "dtTMatrix", "dtrMatrix", "dtpMatrix", \
236/* 66 */ "zgCMatrix", "zgRMatrix", "zgTMatrix", "zgeMatrix", "zdiMatrix", \
237/* 71 */ "zsCMatrix", "zsRMatrix", "zsTMatrix", "zsyMatrix", "zspMatrix", \
238/* 76 */ "ztCMatrix", "ztRMatrix", "ztTMatrix", "ztrMatrix", "ztpMatrix"
239
240#define VALID_NONVIRTUAL_VECTOR \
241/* 81 */ "nsparseVector", "lsparseVector", "isparseVector", \
242 "dsparseVector", "zsparseVector"
243
244#define VALID_NONVIRTUAL VALID_NONVIRTUAL_MATRIX, VALID_NONVIRTUAL_VECTOR
245
246/* dpoMatrix->dsyMatrix, etc. */
247#define VALID_NONVIRTUAL_SHIFT(i, pToInd) \
248 ((i >= 5) ? 0 : ((i >= 4) ? pToInd != 0 : ((i >= 2) ? 57 : 59)))
249
250#define VALID_DENSE \
251"ngeMatrix", "nsyMatrix", "nspMatrix", "ntrMatrix", "ntpMatrix", \
252"lgeMatrix", "lsyMatrix", "lspMatrix", "ltrMatrix", "ltpMatrix", \
253"igeMatrix", "isyMatrix", "ispMatrix", "itrMatrix", "itpMatrix", \
254"dgeMatrix", "dsyMatrix", "dspMatrix", "dtrMatrix", "dtpMatrix", \
255"zgeMatrix", "zsyMatrix", "zspMatrix", "ztrMatrix", "ztpMatrix"
256
257#define VALID_CSPARSE \
258"ngCMatrix", "nsCMatrix", "ntCMatrix", \
259"lgCMatrix", "lsCMatrix", "ltCMatrix", \
260"igCMatrix", "isCMatrix", "itCMatrix", \
261"dgCMatrix", "dsCMatrix", "dtCMatrix", \
262"zgCMatrix", "zsCMatrix", "ztCMatrix"
263
264#define VALID_RSPARSE \
265"ngRMatrix", "nsRMatrix", "ntRMatrix", \
266"lgRMatrix", "lsRMatrix", "ltRMatrix", \
267"igRMatrix", "isRMatrix", "itRMatrix", \
268"dgRMatrix", "dsRMatrix", "dtRMatrix", \
269"zgRMatrix", "zsRMatrix", "ztRMatrix"
270
271#define VALID_TSPARSE \
272"ngTMatrix", "nsTMatrix", "ntTMatrix", \
273"lgTMatrix", "lsTMatrix", "ltTMatrix", \
274"igTMatrix", "isTMatrix", "itTMatrix", \
275"dgTMatrix", "dsTMatrix", "dtTMatrix", \
276"zgTMatrix", "zsTMatrix", "ztTMatrix"
277
278#define VALID_DIAGONAL \
279"ndiMatrix", "ldiMatrix", "idiMatrix", "ddiMatrix", "zdiMatrix"
280
281
282/* What we want declared "everywhere" : */
283
284#include "utils.h"
285
286SEXP newObject(const char *);
287void validObject(SEXP, const char *);
288
289char typeToKind(SEXPTYPE);
290SEXPTYPE kindToType(char);
291size_t kindToSize(char);
292
293int DimNames_is_trivial(SEXP);
294int DimNames_is_symmetric(SEXP);
295
296void symDN(SEXP, SEXP, int);
297void revDN(SEXP, SEXP);
298
299SEXP get_symmetrized_DimNames(SEXP, int);
300SEXP get_reversed_DimNames(SEXP);
301
302void set_symmetrized_DimNames(SEXP, SEXP, int);
303void set_reversed_DimNames(SEXP, SEXP);
304
305#endif /* MATRIX_MDEFINES_H */
long long Matrix_int_fast64_t
Definition Mdefines.h:27
void symDN(SEXP, SEXP, int)
Definition attrib.c:37
char typeToKind(SEXPTYPE)
Definition objects.c:11
void * alloca(size_t)
void set_reversed_DimNames(SEXP, SEXP)
Definition attrib.c:142
SEXPTYPE kindToType(char)
Definition objects.c:28
int DimNames_is_symmetric(SEXP)
Definition attrib.c:14
void set_symmetrized_DimNames(SEXP, SEXP, int)
Definition attrib.c:132
Rcomplex Matrix_zna
Definition Mdefines.h:95
Rcomplex Matrix_zone
Definition Mdefines.h:95
Rcomplex Matrix_zzero
Definition init.c:26
void revDN(SEXP, SEXP)
Definition attrib.c:68
SEXP get_reversed_DimNames(SEXP)
Definition attrib.c:120
int DimNames_is_trivial(SEXP)
Definition attrib.c:6
void validObject(SEXP, const char *)
Definition validity.c:1802
SEXP newObject(const char *)
Definition objects.c:4
SEXP get_symmetrized_DimNames(SEXP, int)
Definition attrib.c:108
size_t kindToSize(char)
Definition objects.c:46