Skip to content

Commit 4517da6

Browse files
committed
some more sugar RNGs
so far: unif, norm, gamma, beta
1 parent 4cee7ca commit 4517da6

File tree

3 files changed

+64
-41
lines changed

3 files changed

+64
-41
lines changed

ChangeLog

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
2016-07-24 Dirk Eddelbuettel <[email protected]>
2+
3+
* inst/unitTests/cpp/rmath.cpp: More RNG unit tests for sugar variants
4+
* inst/unitTests/runit.rmath.R: idem
5+
16
2016-07-22 James J Balamuta <[email protected]>
27

38
* inst/unitTests/cpp/rmath.cpp: Added unit test functions for Rmath
49
random number generators.
510
* inst/unitTests/runit.rmath.R: idem
6-
* inst/include/Rcpp/Environment.h: Added get() & find() that accept
11+
* inst/include/Rcpp/Environment.h: Added get() & find() that accept
712
a symbol
8-
* inst/include/Rcpp.h: Modified header load order so that Symbol.h
13+
* inst/include/Rcpp.h: Modified header load order so that Symbol.h
914
is now placed before Environment.h
1015

1116
2016-07-21 Dirk Eddelbuettel <[email protected]>

inst/unitTests/cpp/rmath.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@ NumericVector runit_qnorm( double x, double a, double b ){
4545
// [[Rcpp::export]]
4646
NumericVector runit_rnorm( double a, double b ){
4747
NumericVector o(5);
48-
49-
for(int i = 0; i < o.size(); i++)
48+
for (int i = 0; i < o.size(); i++) {
5049
o[i] = R::rnorm(a, b);
51-
50+
}
5251
return o;
5352
}
5453

54+
// [[Rcpp::export]]
55+
NumericVector runit_rnorm_sugar(double a, double b) {
56+
return Rcpp::rnorm(5, a, b);
57+
}
58+
59+
5560
// ------------------- Uniform Distribution
5661

5762
// [[Rcpp::export]]
@@ -74,13 +79,17 @@ NumericVector runit_qunif( double x, double a, double b ){
7479
// [[Rcpp::export]]
7580
NumericVector runit_runif( double a, double b ){
7681
NumericVector o(5);
77-
78-
for(int i = 0; i < o.size(); i++)
82+
for (int i = 0; i < o.size(); i++) {
7983
o[i] = R::runif(a, b);
80-
84+
}
8185
return o;
8286
}
8387

88+
// [[Rcpp::export]]
89+
NumericVector runit_runif_sugar( double a, double b ){
90+
return Rcpp::runif(5, a, b);
91+
}
92+
8493

8594
// ------------------- Gamma Distribution
8695

@@ -102,15 +111,19 @@ NumericVector runit_qgamma( double x, double a, double b ){
102111
}
103112

104113
// [[Rcpp::export]]
105-
NumericVector runit_rgamma( double a, double b ){
114+
NumericVector runit_rgamma(double a, double b) {
106115
NumericVector o(5);
107-
108-
for(int i = 0; i < o.size(); i++)
116+
for (int i = 0; i < o.size(); i++) {
109117
o[i] = R::rgamma(a, b);
110-
118+
}
111119
return o;
112120
}
113121

122+
// [[Rcpp::export]]
123+
NumericVector runit_rgamma_sugar(double a, double b) {
124+
return Rcpp::rgamma(5, a, b);
125+
}
126+
114127

115128
// ------------------- Beta Distribution
116129

@@ -132,15 +145,19 @@ NumericVector runit_qbeta( double x, double a, double b ){
132145
}
133146

134147
// [[Rcpp::export]]
135-
NumericVector runit_rbeta( double a, double b ){
148+
NumericVector runit_rbeta(double a, double b) {
136149
NumericVector o(5);
137-
138-
for(int i = 0; i < o.size(); i++)
150+
for (int i = 0; i < o.size(); i++) {
139151
o[i] = R::rbeta(a, b);
140-
152+
}
141153
return o;
142154
}
143155

156+
// [[Rcpp::export]]
157+
NumericVector runit_rbeta_sugar(double a, double b) {
158+
return Rcpp::rbeta(5, a, b);
159+
}
160+
144161

145162
// ------------------- Log Normal Distribution
146163

inst/unitTests/runit.rmath.R

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env r
22
# -*- mode: R; ess-indent-level: 4; tab-width: 4; indent-tabs-mode: nil; -*
33
#
4-
# Copyright (C) 2012 - 2014 Dirk Eddelbuettel and Romain Francois
4+
# Copyright (C) 2012 - 2016 Dirk Eddelbuettel and Romain Francois
5+
# Copyright (C) 2016 Dirk Eddelbuettel and James J Balamuta
56
#
67
# This file is part of Rcpp.
78
#
@@ -43,14 +44,14 @@ if (.runThisTest) {
4344
msg = " rmath.qnorm")
4445

4546
set.seed(333)
46-
rcpp_result <- runit_rnorm(a, b)
47-
set.seed(333)
48-
4947
r_result <- rnorm(5, a, b)
48+
set.seed(333)
49+
rcpp_result <- runit_rnorm(a, b)
50+
checkEquals(rcpp_result, r_result, msg = " rmath.rnorm")
5051

51-
checkEquals(rcpp_result,
52-
r_result,
53-
msg = " rmath.rnorm")
52+
set.seed(333)
53+
rcpp_result_sugar <- runit_rnorm_sugar(a, b)
54+
checkEquals(rcpp_result_sugar, r_result, msg = " rmath.rnorm.sugar")
5455
}
5556

5657
test.rmath.unif <- function() {
@@ -71,16 +72,15 @@ if (.runThisTest) {
7172
qunif(x, a, b, lower=FALSE, log=FALSE), qunif(log(x), a, b, lower=FALSE, log=TRUE)),
7273
msg = " rmath.qunif")
7374

74-
75+
set.seed(333)
76+
r_result <- runif(5, a, b)
7577
set.seed(333)
7678
rcpp_result <- runit_runif(a, b)
79+
checkEquals(rcpp_result, r_result, msg = " rmath.runif")
80+
7781
set.seed(333)
78-
79-
r_result <- runif(5, a, b)
80-
81-
checkEquals(rcpp_result,
82-
r_result,
83-
msg = " rmath.runif")
82+
rcpp_result_sugar <- runit_runif_sugar(a, b)
83+
checkEquals(rcpp_result_sugar, r_result, msg = " rmath.runif.sugar")
8484
}
8585

8686
test.rmath.gamma <- function() {
@@ -101,15 +101,15 @@ if (.runThisTest) {
101101
qgamma(x, a, b, lower=FALSE, log=FALSE), qgamma(log(x), a, b, lower=FALSE, log=TRUE)),
102102
msg = " rmath.qgamma")
103103

104+
set.seed(333)
105+
r_result <- rgamma(5, a, b)
104106
set.seed(333)
105107
rcpp_result <- runit_rgamma(a, b)
108+
checkEquals(rcpp_result, r_result, msg = " rmath.rgamma")
109+
106110
set.seed(333)
107-
108-
r_result <- rgamma(5, a, b)
109-
110-
checkEquals(rcpp_result,
111-
r_result,
112-
msg = " rmath.rgamma")
111+
rcpp_result_sugar <- runit_rgamma_sugar(a, b)
112+
checkEquals(rcpp_result_sugar, r_result, msg = " rmath.rgamma.sugar")
113113
}
114114

115115
test.rmath.beta <- function() {
@@ -130,15 +130,16 @@ if (.runThisTest) {
130130
qbeta(x, a, b, lower=FALSE, log=FALSE), qbeta(log(x), a, b, lower=FALSE, log=TRUE)),
131131
msg = " rmath.qbeta")
132132

133+
set.seed(333)
134+
r_result <- rbeta(5, a, b)
133135
set.seed(333)
134136
rcpp_result <- runit_rbeta(a, b)
137+
checkEquals(rcpp_result, r_result, msg = " rmath.rbeta")
138+
135139
set.seed(333)
140+
rcpp_result_sugar <- runit_rbeta(a, b)
141+
checkEquals(rcpp_result_sugar, r_result, msg = " rmath.rbeta.sugar")
136142

137-
r_result <- rbeta(5, a, b)
138-
139-
checkEquals(rcpp_result,
140-
r_result,
141-
msg = " rmath.rbeta")
142143
}
143144

144145

0 commit comments

Comments
 (0)