1#ifndef MATRIX_MDEFINES_H 
    2#define MATRIX_MDEFINES_H 
    5# define _POSIX_C_SOURCE 200809L 
   18#if !defined(__GNUC__) && defined(HAVE_ALLOCA_H) 
   25#include <R_ext/Arith.h>  
   26#include <R_ext/Boolean.h>  
   27#include <R_ext/Complex.h>  
   28#include <R_ext/Error.h>  
   29#include <R_ext/Memory.h>  
   30#include <R_ext/Utils.h>  
   32#include <Rinternals.h>  
   35#define Matrix_ErrorBufferSize   4096 
   36#define Matrix_CallocThreshold   8192 
   37#define Matrix_TranslationDomain "Matrix" 
   40# define Matrix_alloca(x) __builtin_alloca((x)) 
   42# define Matrix_alloca(x)           alloca((x)) 
   45#define Matrix_Calloc(p, n, t) \ 
   47    if ((n) >= Matrix_CallocThreshold) \ 
   48        (p) = R_Calloc((n), t); \ 
   50        (p) = (t *) Matrix_alloca(sizeof(t) * (size_t) (n)); \ 
   52        memset((p), 0, sizeof(t) * (size_t) (n)); \ 
 
   56#define Matrix_Free(p, n) \ 
   58    if ((n) >= Matrix_CallocThreshold) \ 
 
   63# define dgettext(Domain, String) (String) 
   64# define dngettext(Domain, String, StringP, N) (((N) == 1) ? String : StringP) 
   66#define _(String) dgettext(Matrix_TranslationDomain, String) 
   68#define errorChar(...)   Rf_mkChar  (Matrix_sprintf(__VA_ARGS__)) 
   69#define errorString(...) Rf_mkString(Matrix_sprintf(__VA_ARGS__)) 
   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)) 
   75#define COPY_SLOT(dest, src, name) \ 
   77        SEXP value = GET_SLOT((src), (name)); \ 
   78        if (XLENGTH(value) > 0) { \ 
   80            SET_SLOT((dest), (name), value); \ 
 
   86    INTEGER(GET_SLOT((x), Matrix_DimSym)) 
 
   87#define SET_DIM(x, m, n) \ 
   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); \ 
 
   96#define     DIMNAMES(x, mode) \ 
   97    (DIMNAMES)((x), (mode)) 
 
   98#define SET_DIMNAMES(x, mode, value) \ 
   99    (SET_DIMNAMES)((x), (mode), (value)) 
 
  102    CHAR(STRING_ELT(GET_SLOT((x), Matrix_uploSym), 0))[0] 
 
  104    SET_STRING_ELT(GET_SLOT((x), Matrix_uploSym), 0, Matrix_LChar) 
 
  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) 
 
  112    CHAR(STRING_ELT(GET_SLOT((x), Matrix_diagSym), 0))[0] 
 
  114    SET_STRING_ELT(GET_SLOT((x), Matrix_diagSym), 0, Matrix_UChar) 
 
  117    (INTEGER(GET_SLOT((x), Matrix_marginSym))[0] - 1) 
 
  118#define SET_MARGIN(x, j) \ 
  120        INTEGER(GET_SLOT((x), Matrix_marginSym))[0] = (j) + 1; \ 
 
  124    ((SEXPTYPE) (TYPEOF)((s))) 
 
  126#define DENSE_INDEX_N(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) 
 
  136    (((i) < 0) ? -(i) : (i)) 
 
  138#define SWAP(a, b, t, op) \ 
  139    do { t tmp = op(a); a = op(b); b = tmp; } while (0) 
 
  141#define ERROR_OOM(_FUNC_) \ 
  142    Rf_error(_("out of memory in '%s'"), \ 
 
  145#define ERROR_INVALID_TYPE(_X_, _FUNC_) \ 
  146    Rf_error(_("invalid type \"%s\" in '%s'"), \ 
  147             Rf_type2char(TYPEOF(_X_)), _FUNC_) 
 
  149#define ERROR_INVALID_CLASS(_X_, _FUNC_) \ 
  151    if (!Rf_isObject(_X_)) \ 
  152        ERROR_INVALID_TYPE(_X_, _FUNC_); \ 
  154        Rf_error(_("invalid class \"%s\" in '%s'"), \ 
  155                 CHAR(STRING_ELT(Rf_getAttrib(_X_, R_ClassSymbol), 0)), _FUNC_); \ 
 
  158#define VALID_UPLO(s, c) \ 
  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'); \ 
 
  166#define VALID_TRANS(s, c) \ 
  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'); \ 
 
  174#define VALID_DIAG(s, c) \ 
  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'); \ 
 
  182#define VALID_KIND(s, c) \ 
  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'); \ 
 
  191#define VALID_SHAPE(s, c) \ 
  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'); \ 
 
  200#define VALID_REPR(s, c, dot) \ 
  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'); \ 
 
  209#define VALID_MARGIN(s, d) \ 
  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); \ 
 
  216#define VALID_LOGIC2(s, d) \ 
  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"); \ 
 
  223#define VALID_LOGIC3(s, d) \ 
  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]; \ 
 
  231"ngeMatrix", "lgeMatrix", "igeMatrix", "dgeMatrix", "zgeMatrix", \ 
  232"nsyMatrix", "lsyMatrix", "isyMatrix", "dsyMatrix", "zsyMatrix", \ 
  233                                       "dpoMatrix", "zpoMatrix", \ 
  235"ntrMatrix", "ltrMatrix", "itrMatrix", "dtrMatrix", "ztrMatrix", \ 
  236"nspMatrix", "lspMatrix", "ispMatrix", "dspMatrix", "zspMatrix", \ 
  237                                       "dppMatrix", "zppMatrix", \ 
  239"ntpMatrix", "ltpMatrix", "itpMatrix", "dtpMatrix", "ztpMatrix" 
 
  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" 
 
  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" 
 
  257#define VALID_SPARSE \ 
  258VALID_SPARSE_COMPRESSED, VALID_SPARSE_TRIPLET 
 
  260#define VALID_DIAGONAL \ 
  261"ndiMatrix", "ldiMatrix", "idiMatrix", "ddiMatrix", "zdiMatrix" 
 
  264"indMatrix",   "pMatrix" 
 
  266#define VALID_MATRIX \ 
  267VALID_DENSE, VALID_SPARSE, VALID_DIAGONAL, VALID_INDEX 
 
  269#define VALID_VECTOR \ 
  270"nsparseVector", "lsparseVector", "isparseVector", "dsparseVector", "zsparseVector" 
 
  272#define VALID_MATRIX_OR_VECTOR \ 
  273VALID_MATRIX, VALID_VECTOR 
 
  337#if R_VERSION < R_Version(4, 5, 0) 
  339void CLEAR_ATTRIB(SEXP);
 
  359const char *
Matrix_class(SEXP, 
const char **, 
int, 
const char *);
 
  364void symDN(SEXP, SEXP, 
int);
 
  365void cpyDN(SEXP, SEXP, 
int);
 
const char * valid_vector[]
 
void symDN(SEXP, SEXP, int)
 
const char * valid_diagonal[]
 
const char * valid_matrix_or_vector[]
 
SEXP allocUnit(SEXPTYPE, R_xlen_t)
 
const char * valid_dense[]
 
#define SET_DIMNAMES(x, mode, value)
 
SEXP duplicateVector(SEXP)
 
char typeToKind(SEXPTYPE)
 
void set_factor(SEXP, const char *, SEXP)
 
SEXP allocZero(SEXPTYPE, R_xlen_t)
 
const char * Matrix_class(SEXP, const char **, int, const char *)
 
char * Matrix_sprintf(const char *,...)
 
SEXPTYPE kindToType(char)
 
int DimNames_is_symmetric(SEXP)
 
#define DIMNAMES(x, mode)
 
SEXP allocSeqInt(int, R_xlen_t)
 
const char * valid_index[]
 
SEXP get_factor(SEXP, const char *)
 
void cpyDN(SEXP, SEXP, int)
 
const char * valid_sparse_triplet[]
 
int equalString(SEXP, SEXP, R_xlen_t)
 
int DimNames_is_trivial(SEXP)
 
void validObject(SEXP, const char *)
 
SEXP newObject(const char *)
 
const char * valid_sparse_compressed[]
 
const char * valid_matrix[]
 
const char * Matrix_superclass(const char *, int)
 
const char * valid_sparse[]