/* Authors Martin Schlather, schlather@math.uni-mannheim.de Copyright (C) 2015 -- 2016 Martin Schlather, Reinhard Furrer This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include "kleinkram.h" void strcopyN(char *dest, const char *src, int n) { if (n > 1) { n--; strncpy(dest, src, n); } dest[n] = '\0'; } double scalar(double *A, double *B, int N) { double ANS; SCALAR_PROD(A, B, N, ANS); return ANS; } void AtA(double *a, int nrow, int ncol, double *C) { // C = A^T %*% A #ifdef DO_PARALLEL //#pragma omp parallel for num_threads(2) schedule(dynamic) if (MULTIMINSIZE(ncol)) #pragma omp parallel for schedule(dynamic) if (MULTIMINSIZE(ncol)) #endif for (int i=0; imax) return TooLarge(&n, 1); if (n<0) return TooSmall(); PROTECT(dummy=allocVector(INTSXP, n)); for (int i=0; imax) return TooLarge(&n, 1); if (n<0) return TooSmall(); PROTECT(dummy=allocVector(LGLSXP, n)); for (int i=0; imax) return TooLarge(&n, 1); if (n<0) return TooSmall(); PROTECT(dummy=allocVector(REALSXP, n)); for (int i=0; imax) return TooLarge(&n, 1); if (n<0) return TooSmall(); PROTECT(dummy=allocVector(REALSXP, n)); for (int i=0; imax) return TooLarge(&n, 1); if (n<0) return TooSmall(); PROTECT(dummy=allocVector(STRSXP, n)); for (int i=0; imax) { int nn[2]; nn[0] = row; nn[1] = col; return TooLarge(nn, 2); } SEXP dummy; PROTECT(dummy=allocMatrix(REALSXP, row, col)); for (int i=0; imax) { int nn[2]; nn[0] = row; nn[1] = col; return TooLarge(nn, 2); } SEXP dummy; PROTECT(dummy=allocMatrix(REALSXP, row, col)); for (int k=0, j=0; jmax) { int nn[2]; nn[0] = row; nn[1] = col; return TooLarge(nn, 2); } SEXP dummy; PROTECT(dummy=allocMatrix(INTSXP, row, col)); for (int i=0; imax) { int nn[3]; nn[0] = row; nn[1] = col; nn[2] = depth; return TooLarge(nn, 3); } SEXP dummy; PROTECT(dummy=alloc3DArray(REALSXP, depth, row, col)); for (int j=0; jmax) return TooLarge(&n, 1); if (n<0) return TooSmall(); PROTECT(str = allocVector(STRSXP, n)); for (int i=0; i= n) j=0; } return; } int Integer(SEXP p, char *name, int idx, bool nulltoNA) { if (p != R_NilValue) { assert(idx < length(p)); switch(TYPEOF(p)) { case INTSXP : return INTEGER(p)[idx]; case REALSXP : double value; value = REAL(p)[idx]; if (ISNAN(value)) { return NA_INTEGER; } if (value == TRUNC(value)) return (int) value; else { ERR2("%s: integer value expected. Got %e.", name, value); } case LGLSXP : return LOGICAL(p)[idx]==NA_LOGICAL ? NA_INTEGER : (int) LOGICAL(p)[idx]; default : {} } } else if (nulltoNA) return NA_INTEGER; ERR2("%s: unmatched type of parameter [type=%d]", name, TYPEOF(p)); return NA_INTEGER; // compiler warning vermeiden } int Integer(SEXP p, char *name, int idx) { return Integer(p, name, idx, false); } void Integer(SEXP el, char *name, int *vec, int maxn) { if (el == R_NilValue) { ERR1("'%s' cannot be transformed to integer.\n",name); } int n = length(el); for (int j=0, i=0; i= n) j=0; } } void Integer2(SEXP el, char *name, int *vec) { int n; if (el == R_NilValue || (n = length(el))==0) { ERR1("'%s' cannot be transformed to integer.\n",name); } vec[0] = Integer(el, name, 0); if (n==1) vec[1] = vec[0]; else { vec[1] = Integer(el, name, n-1); if (n > 2) { int v = vec[0] + 1; for (int i = 1; i maxlen) { ERR1("number of variable names exceeds %d. Take abbreviations?", maxlen); } type = TYPEOF(el); // printf("type=%d %d %d %d\n", TYPEOF(el), INTSXP, REALSXP, LGLSXP); if (type == CHARSXP) { for (int i=0; i0.0) { num=0.0; WARN1("%s which has been positive is set 0.\n",name); } return num; } double PositiveInteger(SEXP el, char *name) { int num; num = INT; if (num<=0) { num=0; WARN1("'%s' which has been negative is set 0.\n",name); } return num; } double PositiveReal(SEXP el, char *name) { double num; num = NUM; if (num<=0.0) { num=0.0; WARN1("%s which has been negative is set 0.\n",name); } return num; } SEXP ExtendedInteger(double x) { return ScalarInteger(R_FINITE(x) ? x : NA_INTEGER); } SEXP ExtendedBoolean(double x) { return ScalarLogical(ISNAN(x) ? NA_LOGICAL : x != 0.0); } SEXP ExtendedBooleanUsr(usr_bool x) { return ScalarLogical((int) x); } int Match(char *name, name_type List, int n) { // == -1 if no matching name is found // == -2 if multiple matching names are found, without one matching exactly unsigned int ln; int Nr; Nr=0; ln=strlen(name); // print("Match %d %d %s %s %d\n", Nr, n, name, List[Nr], ln); while ( Nr < n && strncmp(name, List[Nr], ln)) { Nr++; } if (Nr < n) { if (ln==strlen(List[Nr])) // exactmatching -- take first -- changed 1/7/07 return Nr; // a matching function is found. Are there other functions that match? int j; bool multiplematching=false; j=Nr+1; // if two or more covariance functions have the same name // the last one is taken while (j maxlen_ans) ERR2("option '%s' is too long. Maximum length is %d.", name, maxlen_ans); if (TYPEOF(el) == STRSXP) { for (k=0; k= 0) { ans[0] = defaultvalue; for (k=1; k