1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
/*
SMat - Simple Matrix Classes v0.1beta
Copyright (C) 2011 by Heinrich Fritz (heinrich_fritz@hotmail.com)
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, see <http://www.gnu.org/licenses/>.
*/
// R.meal.cpp
// R Mathematical Environment Abstraction Layer
// VT::18.08.2024 - replace Free() by R_Free(). Actually this is never used.
#define STRICT_R_HEADERS 1
#define USE_FC_LEN_T
#ifdef R_PACKAGE_FILE
#define R_USE_C99_IN_CXX
#include "R_meal.h"
/////////////////////
// CRmealSettings //
/////////////////////
CRmealSettings &GetRealSettings ()
{
static CRmealSettings settings ;
return settings ;
}
CRmealSettings::CRmealSettings ()
: m_szEmail ("<NA>")
{
}
CRmealSettings::CRmealSettings (const char *szEmail)
{
if (szEmail) GetRealSettings ().m_szEmail = szEmail ;
}
#include <R.h>
#include <R_ext/BLAS.h>
#include <R_ext/Lapack.h>
// LAPACK
void meal_geev (const char* jobvl, const char* jobvr, const int* n, double* a, const int* lda, double* wr, double* wi, double* vl, const int* ldvl, double* vr, const int* ldvr, double* work, const int* lwork, int* info)
{ F77_CALL(dgeev)(jobvl, jobvr, n, a, lda, wr, wi, vl, ldvl, vr, ldvr, work, lwork, info FCONE FCONE) ; }
void meal_gesv (const int* n, const int* nrhs, double* a, const int* lda, int* ipiv, double* b, const int* ldb, int* info)
{ F77_CALL(dgesv)(n, nrhs, a, lda, ipiv, b, ldb, info) ; }
void meal_gesvd (const char* jobu, const char* jobvt, const int* m, const int* n, double* a, const int* lda, double* s, double* u, const int* ldu, double* vt, const int* ldvt, double* work, const int* lwork, int* info)
{ F77_CALL(dgesvd)(jobu, jobvt, m, n, a, lda, s, u, ldu, vt, ldvt, work, lwork, info FCONE FCONE) ; }
// SORT
void meal_sort (double *d, int l)
{ R_qsort (d, 1, l) ; }
void meal_sort_order (double *d, int *o, int l)
{
int i ;
for (i = l - 1; i != -1; i--)
o[i] = i ;
rsort_with_index(d, o, l);
}
void meal_sort_order_rev (double *d, int *o, int l)
{
int i ;
for (i = l - 1; i != -1; i--)
o[i] = i ;
rsort_with_index(d, o, l); // 2do: use r_qsort_I instead!
double dTemp ;
int nTemp ;
for (i = 0, --l; i < l; ++i, --l) // 2do: implement an reverse order - function. this should be as fast! (could be called twice)
{
// sm_swap (d[i], d[l], dTemp) ; // 2do: check if this works..
// sm_swap (o[i], o[l], nTemp) ;
dTemp = d[i] ;
d[i] = d[l] ;
d[l] = dTemp ;
nTemp = o[i]; ;
o[i] = o[l]; ;
o[l] = nTemp; ;
}
}
////////////////////////
// Random Generator //
////////////////////////
void meal_PutRNGstate () { PutRNGstate () ; }
void meal_GetRNGstate () { GetRNGstate () ; }
double meal_unif_rand () { return unif_rand () ; }
double meal_norm_rand () { return norm_rand () ; }
double meal_exp_rand () { return exp_rand () ; }
////////////////////////////////////
// special values amd constants //
////////////////////////////////////
double meal_NaN () { return R_NaN ; }
double meal_PosInf () { return R_PosInf ; }
double meal_NegInf () { return R_NegInf ; }
double meal_NaReal () { return R_NaReal ; }
int meal_NaInt () { return R_NaInt ; }
double meal_PI () { return M_PI ; }
//////////////////////////
// printing functions //
//////////////////////////
void meal_printf (const char *sz, ...)
{
va_list va_l ;
va_start (va_l, sz) ;
Rvprintf (sz, va_l) ;
}
// VT::07.12.2023 - fix warning format string is not a string literal (potentially insecure)
/*
void meal_warning (const char *sz)
{
Rf_warning (sz) ;
}
void meal_error (const char *sz)
{
Rf_error (sz) ;
}
*/
void *meal_alloc (size_t n, int s)
{
return std::calloc (n, s) ;
}
void meal_free (void *p)
{
// VT::18.08.2024 - replace Free() by R_Free(). Actually this is never used.
R_Free (p) ;
}
//////////////////
// Exceptions //
//////////////////
void meal_OnException (const char * szDate, const char * szFile, int nLine)
{
meal_printf (
"\n"
" An exception occurred.\n"
" Please contact the author (%s), providing\n"
" the following information:\n"
"\n"
" - The R-code which caused the problem\n"
" - Eventually used data sets and the state of the random generator (seed)\n"
" - R version number\n"
" - Package version number\n"
" - File: %s\n"
" - Line: %d\n"
"\n"
" Your contribution is appreciated!\n\n",
GetRealSettings ().GetEmail (), szFile, nLine) ;
// VT::07.12.2023 - fix warning format string is not a string literal (potentially insecure)
// meal_error ("An exception has occurred.") ;
Rf_error("An exception has occurred.");
}
void meal_OnUException ()
{
meal_printf (
"\n"
" An unknown exception occurred.\n"
" Please contact the author (%s), providing\n"
" the following details:\n"
"\n"
" - The R-code which caused the problem\n"
" - Eventually used data sets and the state of the random generator (seed)\n"
" - R version number\n"
" - Package version number\n"
"\n"
" Your contribution is appreciated!\n\n",
GetRealSettings ().GetEmail ()) ;
// VT::07.12.2023 - fix warning format string is not a string literal (potentially insecure)
// meal_error ("An unknown exception has occurred.");
Rf_error("An unknown exception has occurred.");
}
#endif // #ifdef R_PACKAGE_FILE
|