Skip to content

Commit 3334b9a

Browse files
authored
Custom exp and log (#44)
* custom exp and log * uncomment debug stuff in Makevars * replace a few more calls * use exp from openlibm * use log from openlibm * use log * use it everywhere * cleanup * remove duplicated function * remove newlines
1 parent a90efd6 commit 3334b9a

13 files changed

+717
-197
lines changed

.Rbuildignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
^renv$
2+
^renv\.lock$
13
^.*\.Rproj$
24
^\.Rproj\.user$
35

R/RcppExports.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
22
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
33

4+
get_explog_switch <- function() {
5+
.Call(`_bgms_get_explog_switch`)
6+
}
7+
8+
rcpp_ieee754_exp <- function(x) {
9+
.Call(`_bgms_rcpp_ieee754_exp`, x)
10+
}
11+
12+
rcpp_ieee754_log <- function(x) {
13+
.Call(`_bgms_rcpp_ieee754_log`, x)
14+
}
15+
416
sample_omrf_gibbs <- function(no_states, no_variables, no_categories, interactions, thresholds, iter) {
517
.Call(`_bgms_sample_omrf_gibbs`, no_states, no_variables, no_categories, interactions, thresholds, iter)
618
}

bgms.Rproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ StripTrailingWhitespace: Yes
1616

1717
BuildType: Package
1818
PackageUseDevtools: Yes
19+
PackageCleanBeforeInstall: No
1920
PackageInstallArgs: --no-multiarch --with-keep.source
2021
PackageCheckArgs: --as-cran

src/Makevars.win

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
1+
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)

src/RcppExports.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,38 @@ Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
1111
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
1212
#endif
1313

14+
// get_explog_switch
15+
Rcpp::String get_explog_switch();
16+
RcppExport SEXP _bgms_get_explog_switch() {
17+
BEGIN_RCPP
18+
Rcpp::RObject rcpp_result_gen;
19+
Rcpp::RNGScope rcpp_rngScope_gen;
20+
rcpp_result_gen = Rcpp::wrap(get_explog_switch());
21+
return rcpp_result_gen;
22+
END_RCPP
23+
}
24+
// rcpp_ieee754_exp
25+
Rcpp::NumericVector rcpp_ieee754_exp(Rcpp::NumericVector x);
26+
RcppExport SEXP _bgms_rcpp_ieee754_exp(SEXP xSEXP) {
27+
BEGIN_RCPP
28+
Rcpp::RObject rcpp_result_gen;
29+
Rcpp::RNGScope rcpp_rngScope_gen;
30+
Rcpp::traits::input_parameter< Rcpp::NumericVector >::type x(xSEXP);
31+
rcpp_result_gen = Rcpp::wrap(rcpp_ieee754_exp(x));
32+
return rcpp_result_gen;
33+
END_RCPP
34+
}
35+
// rcpp_ieee754_log
36+
Rcpp::NumericVector rcpp_ieee754_log(Rcpp::NumericVector x);
37+
RcppExport SEXP _bgms_rcpp_ieee754_log(SEXP xSEXP) {
38+
BEGIN_RCPP
39+
Rcpp::RObject rcpp_result_gen;
40+
Rcpp::RNGScope rcpp_rngScope_gen;
41+
Rcpp::traits::input_parameter< Rcpp::NumericVector >::type x(xSEXP);
42+
rcpp_result_gen = Rcpp::wrap(rcpp_ieee754_log(x));
43+
return rcpp_result_gen;
44+
END_RCPP
45+
}
1446
// sample_omrf_gibbs
1547
IntegerMatrix sample_omrf_gibbs(int no_states, int no_variables, IntegerVector no_categories, NumericMatrix interactions, NumericMatrix thresholds, int iter);
1648
RcppExport SEXP _bgms_sample_omrf_gibbs(SEXP no_statesSEXP, SEXP no_variablesSEXP, SEXP no_categoriesSEXP, SEXP interactionsSEXP, SEXP thresholdsSEXP, SEXP iterSEXP) {
@@ -143,6 +175,9 @@ END_RCPP
143175
}
144176

145177
static const R_CallMethodDef CallEntries[] = {
178+
{"_bgms_get_explog_switch", (DL_FUNC) &_bgms_get_explog_switch, 0},
179+
{"_bgms_rcpp_ieee754_exp", (DL_FUNC) &_bgms_rcpp_ieee754_exp, 1},
180+
{"_bgms_rcpp_ieee754_log", (DL_FUNC) &_bgms_rcpp_ieee754_log, 1},
146181
{"_bgms_sample_omrf_gibbs", (DL_FUNC) &_bgms_sample_omrf_gibbs, 6},
147182
{"_bgms_sample_bcomrf_gibbs", (DL_FUNC) &_bgms_sample_bcomrf_gibbs, 8},
148183
{"_bgms_gibbs_sampler", (DL_FUNC) &_bgms_gibbs_sampler, 28},

src/custom_exp.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "Rcpp.h"
2+
#include "explog_switch.h"
3+
4+
// [[Rcpp::export]]
5+
Rcpp::String get_explog_switch() {
6+
#if USE_CUSTOM_LOG
7+
return "custom";
8+
#else
9+
return "standard";
10+
#endif
11+
}
12+
13+
// [[Rcpp::export]]
14+
Rcpp::NumericVector rcpp_ieee754_exp(Rcpp::NumericVector x) {
15+
Rcpp::NumericVector y(x.size());
16+
for (int i = 0; i < x.size(); i++) {
17+
y[i] = MY_EXP(x[i]);
18+
}
19+
return y;
20+
}
21+
22+
// [[Rcpp::export]]
23+
Rcpp::NumericVector rcpp_ieee754_log(Rcpp::NumericVector x) {
24+
Rcpp::NumericVector y(x.size());
25+
for (int i = 0; i < x.size(); i++) {
26+
y[i] = MY_LOG(x[i]);
27+
}
28+
return y;
29+
}

src/data_simulation.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <Rcpp.h>
2+
#include "explog_switch.h"
23
using namespace Rcpp;
34

45
// [[Rcpp::export]]
@@ -53,7 +54,7 @@ IntegerMatrix sample_omrf_gibbs(int no_states,
5354
for(int category = 0; category < no_categories[variable]; category++) {
5455
exponent = thresholds(variable, category);
5556
exponent += (category + 1) * rest_score;
56-
cumsum += std::exp(exponent);
57+
cumsum += MY_EXP(exponent);
5758
probabilities[category + 1] = cumsum;
5859
}
5960

@@ -132,7 +133,7 @@ IntegerMatrix sample_bcomrf_gibbs(int no_states,
132133
(category - reference_category[variable]);
133134
//The pairwise interactions
134135
exponent += category * rest_score;
135-
cumsum += std::exp(exponent);
136+
cumsum += MY_EXP(exponent);
136137
probabilities[category] = cumsum;
137138
}
138139
} else {
@@ -141,7 +142,7 @@ IntegerMatrix sample_bcomrf_gibbs(int no_states,
141142
for(int category = 0; category < no_categories[variable]; category++) {
142143
exponent = thresholds(variable, category);
143144
exponent += (category + 1) * rest_score;
144-
cumsum += std::exp(exponent);
145+
cumsum += MY_EXP(exponent);
145146
probabilities[category + 1] = cumsum;
146147
}
147148
}

0 commit comments

Comments
 (0)