Skip to content

Commit c96a6e9

Browse files
committed
added note to sugar vignette about the need to init. the RNG as per Section 6.3 of Writing R Extensions
also added a short comment to each of the corresponding header files reinident header files while we were at it (using Emacs/ESS) reset ChangeLog to 80 columns (using Emacs)
1 parent 3c3ccfb commit c96a6e9

File tree

23 files changed

+639
-505
lines changed

23 files changed

+639
-505
lines changed

inst/ChangeLog

Lines changed: 73 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1+
2010-10-03 Dirk Eddelbuettel <[email protected]>
2+
3+
* inst/include/Rcpp/stats/random/r*: Added comment reminding users
4+
read up on Section 6.3 of "Writing R Extensions" and the need to
5+
call GetRNGstate() and PutRNGstate() when using R's RNGs
6+
17
2010-09-30 Romain Francois <[email protected]>
28

3-
* inst/include/Rcpp/vector/MatrixRow.h: Matrix row inherits
4-
VectorBase so that it becomes a sugar expression, so that we can do
5-
something like this:
6-
NumericMatrix x( ... ) ;
7-
NumericVector x0 = x.row(0) ;
8-
9+
* inst/include/Rcpp/vector/MatrixRow.h: Matrix row inherits
10+
VectorBase so that it becomes a sugar expression, so that we can do
11+
something like this:
12+
NumericMatrix x( ... ) ;
13+
NumericVector x0 = x.row(0) ;
14+
915
* inst/include/Rcpp/vector/MatrixColumn.h: idem for columns
10-
11-
* cleanup: run make clean in the inst/doc directory to install
16+
17+
* cleanup: run make clean in the inst/doc directory to install
1218
the fake vignettes
1319

1420
2010-09-27 Romain Francois <[email protected]>
1521

1622
* inst/include/Rcpp/traits/is_sugar_expression.h: new trait class
1723
to recognize sugar expressions
18-
19-
* inst/include/Rcpp/vector/Vector.h: operator=( sugar expression)
20-
no longer allocate unnecessary memory.
24+
25+
* inst/include/Rcpp/vector/Vector.h: operator=( sugar expression)
26+
no longer allocate unnecessary memory.
2127

2228
2010-09-26 Romain Francois <[email protected]>
2329

2430
* inst/include/Rpp/Fast.h: new helper class Rcpp::Fast that allows
2531
faster indexing of Vector
26-
27-
* inst/include/Rcpp/sugar/operators/plus.h: port to operator+ the same
32+
33+
* inst/include/Rcpp/sugar/operators/plus.h: port to operator+ the same
2834
improvements than in operator*
2935

3036
2010-09-25 Romain Francois <[email protected]>
@@ -33,74 +39,79 @@
3339

3440
* src/Rcpp_init.c: register routines
3541

36-
* R/*.R: use registration information in many .Call and .External functions
37-
to speed things up
42+
* R/*.R: use registration information in many .Call and .External
43+
functions to speed things up
3844

3945
* inst/include/Rcpp/RangeIndexer.h: experimenting with loop unrolling
4046

4147
2010-09-24 Romain Francois <[email protected]>
4248

43-
* inst/include/Rcpp/sugar/Range.h : Range gains some operators (++,--,n etc ...)
49+
* inst/include/Rcpp/sugar/Range.h : Range gains some
50+
operators (++,--,n etc ...)
4451

45-
* inst/examples/ConvolveBenchmarks/convolve3_cpp.cpp: using the new Range
46-
operators
52+
* inst/examples/ConvolveBenchmarks/convolve3_cpp.cpp: using the new
53+
Range operators
4754

48-
* inst/include/Rcpp/sugar/operators/times.h: speed improvements. Not using
49-
pointer to member functions seems to be beneficial.
55+
* inst/include/Rcpp/sugar/operators/times.h: speed improvements. Not
56+
using pointer to member functions seems to be beneficial.
5057

51-
* inst/include/Rcpp/sugar/nona/nona.h: sugar function that wraps a sugar
52-
expression in an object that pretends it does not contain any missing
53-
values. This allows many sugar functions and operators to use their
54-
fast versions when we know that the object does not contain missing values.
58+
* inst/include/Rcpp/sugar/nona/nona.h: sugar function that wraps a
59+
sugar expression in an object that pretends it does not contain any
60+
missing values. This allows many sugar functions and operators to use
61+
their fast versions when we know that the object does not contain
62+
missing values.
5563

5664
2010-09-22 Romain Francois <[email protected]>
5765

58-
* R/Module.R: set [[ and [[<- as regular methods too, which restores previous
59-
behavior
66+
* R/Module.R: set [[ and [[<- as regular methods too, which restores
67+
previous behavior
6068

6169
2010-09-17 Romain Francois <[email protected]>
6270

63-
* src/Reference.cpp: new objects are created via a callback to R's new
64-
function, as R_do_new_object did always return the same environment
71+
* src/Reference.cpp: new objects are created via a callback to R's
72+
new function, as R_do_new_object did always return the same
73+
environment
6574

66-
* R/00_classes.R: new ref class C++Method to represent C++ methods and holding
67-
their external pointers directly (same idea as C++Field)
75+
* R/00_classes.R: new ref class C++Method to represent C++ methods
76+
and holding their external pointers directly (same idea as C++Field)
6877

69-
* inst/include/Rcpp/Module.h: internal support for C++Method (template class
70-
S4_CppMethod)
78+
* inst/include/Rcpp/Module.h: internal support for
79+
C++Method (template class S4_CppMethod)
7180

7281
2010-09-16 Romain Francois <[email protected]>
7382

7483
* R/00_classes.R: moving classes definition here
7584

76-
* inst/Rcpp/Module.h: added C++ class S4_field that builds S4 objects of
77-
class C++Field. Build the list of fields as part of the creation of the
78-
C++Class objects
85+
* inst/Rcpp/Module.h: added C++ class S4_field that builds S4 objects
86+
of class C++Field. Build the list of fields as part of the creation
87+
of the C++Class objects
7988

80-
* src/Module.cpp: .Call functions CppField__get and CppField__set to get/set
81-
values of an object's field using external pointers directly (no std::map
82-
lookup internally)
89+
* src/Module.cpp: .Call functions CppField__get and CppField__set to
90+
get/set values of an object's field using external pointers
91+
directly (no std::map lookup internally)
8392

8493
* R/Module.R: (unexported) functions .getField and .setField that
8594
call CppField__get and CppField__set
8695

87-
* inst/include/Rcpp/Reference.h: skeleton for a Rcpp::Reference class that
88-
will help dealing with reference classes on the C++ side
96+
* inst/include/Rcpp/Reference.h: skeleton for a Rcpp::Reference class
97+
that will help dealing with reference classes on the C++ side
8998

9099
* src/Reference.cpp: implementation (needs update)
91100

92101
2010-09-15 Romain Francois <[email protected]>
93102

94-
* DESCRIPTION: added the declaration MinimumSvnRev to control which version
95-
of R-devel we need so that Rcpp fully works, currently set to 52905
103+
* DESCRIPTION: added the declaration MinimumSvnRev to control which
104+
version of R-devel we need so that Rcpp fully works, currently set to
105+
52905
96106

97-
* R/zzz.R: check the svn revision of R against the declared MinimumSvnRev
98-
requirement and print a message if necessary (this will only stay for the
99-
interim period while we develop 0.8.7 so that we all are on the same page.
107+
* R/zzz.R: check the svn revision of R against the declared
108+
MinimumSvnRev requirement and print a message if necessary (this will
109+
only stay for the interim period while we develop 0.8.7 so that we
110+
all are on the same page.
100111

101-
* inst/include/Rcpp/Module.h: added methods to class_Base : methods_arity
102-
and methods_voidness to query the number of arguments of methods of a class
103-
and if the method is void
112+
* inst/include/Rcpp/Module.h: added methods to class_Base :
113+
methods_arity and methods_voidness to query the number of arguments
114+
of methods of a class and if the method is void
104115

105116
* R/Module.R: implement referenceMethods (from methods) for 'C++Class'
106117

@@ -120,23 +131,25 @@
120131

121132
2010-09-08 Romain Francois <[email protected]>
122133

123-
* inst/include/Rcpp/Module.h : added class_Base::property_names method
124-
to grab the names of all fields (properties)
134+
* inst/include/Rcpp/Module.h : added class_Base::property_names
135+
method to grab the names of all fields (properties)
125136

126-
* src/Module.cpp : added R access (.Call) function CppClass__properties
127-
to grab the names of the fields (property) from the XP of a class
137+
* src/Module.cpp : added R access (.Call) function
138+
CppClass__properties to grab the names of the fields (property) from
139+
the XP of a class
128140

129-
* inst/include/Rcpp/Module.h : added class_Base::property_is_readonly method
130-
to query if a property is read only
141+
* inst/include/Rcpp/Module.h : added class_Base::property_is_readonly
142+
method to query if a property is read only
131143

132-
* src/Module.cpp : added R access (.Call) function CppClass__property_is_readonly
133-
to query if a class property is read only
144+
* src/Module.cpp : added R access (.Call) function
145+
CppClass__property_is_readonly to query if a class property is read
146+
only
134147

135-
* inst/include/Rcpp/Module.h : added class_Base::property_class method
136-
to grab the C++ class of a property
148+
* inst/include/Rcpp/Module.h : added class_Base::property_class
149+
method to grab the C++ class of a property
137150

138-
* src/Module.cpp : added R access (.Call) function CppClass__property_class
139-
to grab the C++ class of a property
151+
* src/Module.cpp : added R access (.Call) function
152+
CppClass__property_class to grab the C++ class of a property
140153

141154
2010-09-06 Dirk Eddelbuettel <[email protected]>
142155

inst/doc/Rcpp-sugar/Rcpp-sugar.Rnw

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,31 @@ pow(x, z) # x to the power of z
443443

444444
% log() and log10() maybe? Or ln() ?
445445

446+
\subsection{The d/q/p/q statistical functions}
447+
448+
The framework provided by \sugar also permits easy and efficient access the
449+
density, distribution function, quantile and random number generation
450+
functions function by \proglang{R} in the \code{Rmath} library.
451+
452+
Currently, most of these functions are vectorised for the first element which
453+
denote size. Consequently, these calls works in \proglang{C++} just as they
454+
would in \proglang{R}:
455+
456+
<<lang=cpp>>=
457+
x1 = dnorm(y1, 0, 1); // density of y1 at m=0, sd=1
458+
x2 = pnorm(y2, 0, 1); // distribution function of y2
459+
x3 = qnorm(y3, 0, 1); // quantiles of y3
460+
x4 = rnorm(n, 0, 1); // 'n' RNG draws of N(0, 1)
461+
@
462+
463+
Similar d/q/p/r functions are provided for the most common distributions:
464+
beta, binom, cauchy, chisq, exp, f, gamma, geom, hyper, lnorm, logis, nbeta,
465+
nbinom, nbinom_mu, nchisq, nf, norm, nt, pois, t, unif, and weibull.
466+
467+
One point to note is that the programmer using these functions needs to
468+
initialize the state of the random number generator as detailed in Section
469+
6.3 of the `Writing R Extensions' manual \citep{R:Extensions}.
470+
446471
\section{Performance}
447472
\label{sec:performance}
448473

inst/include/Rcpp/stats/random/rbeta.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424

2525
namespace Rcpp {
2626

27-
inline NumericVector rbeta( int n, double a, double b ){
28-
return NumericVector( n, ::Rf_rbeta, a, b ) ;
29-
}
27+
// Please make sure you to read Section 6.3 of "Writing R Extensions"
28+
// about the need to call GetRNGstate() and PutRNGstate() when using
29+
// the random number generators provided by R.
30+
inline NumericVector rbeta( int n, double a, double b ){
31+
return NumericVector( n, ::Rf_rbeta, a, b ) ;
32+
}
3033

3134
} // Rcpp
3235

inst/include/Rcpp/stats/random/rbinom.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424

2525
namespace Rcpp {
2626

27-
inline NumericVector rbinom( int n, double nin, double pp ){
28-
return NumericVector( n, ::Rf_rbinom, nin, pp) ;
29-
}
27+
// Please make sure you to read Section 6.3 of "Writing R Extensions"
28+
// about the need to call GetRNGstate() and PutRNGstate() when using
29+
// the random number generators provided by R.
30+
inline NumericVector rbinom( int n, double nin, double pp ){
31+
return NumericVector( n, ::Rf_rbinom, nin, pp) ;
32+
}
3033

3134
} // Rcpp
3235

inst/include/Rcpp/stats/random/rcauchy.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ class CauchyGenerator_0 : public ::Rcpp::Generator<false,double> {
6767
} // stats
6868

6969
// perhaps this should go to a cpp file
70+
71+
// Please make sure you to read Section 6.3 of "Writing R Extensions"
72+
// about the need to call GetRNGstate() and PutRNGstate() when using
73+
// the random number generators provided by R.
7074
inline NumericVector rcauchy( int n, double location, double scale ){
7175
if (ISNAN(location) || !R_FINITE(scale) || scale < 0)
7276
return NumericVector( n, R_NaN ) ;
@@ -77,6 +81,9 @@ inline NumericVector rcauchy( int n, double location, double scale ){
7781
return NumericVector( n, stats::CauchyGenerator( location, scale ) ) ;
7882
}
7983

84+
// Please make sure you to read Section 6.3 of "Writing R Extensions"
85+
// about the need to call GetRNGstate() and PutRNGstate() when using
86+
// the random number generators provided by R.
8087
inline NumericVector rcauchy( int n, double location /* , double scale [=1.0] */ ){
8188
if (ISNAN(location))
8289
return NumericVector( n, R_NaN ) ;
@@ -87,6 +94,9 @@ inline NumericVector rcauchy( int n, double location /* , double scale [=1.0] */
8794
return NumericVector( n, stats::CauchyGenerator_1( location ) ) ;
8895
}
8996

97+
// Please make sure you to read Section 6.3 of "Writing R Extensions"
98+
// about the need to call GetRNGstate() and PutRNGstate() when using
99+
// the random number generators provided by R.
90100
inline NumericVector rcauchy( int n /*, double location [=0.0] , double scale [=1.0] */ ){
91101
return NumericVector( n, stats::CauchyGenerator_0() ) ;
92102
}

inst/include/Rcpp/stats/random/rchisq.h

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,29 @@
2323
#define Rcpp__stats__random_rchisq_h
2424

2525
namespace Rcpp {
26-
namespace stats {
26+
namespace stats {
27+
28+
class ChisqGenerator : public ::Rcpp::Generator<false,double> {
29+
public:
30+
31+
ChisqGenerator( double df_ ) : df_2(df_ / 2.0) {}
32+
33+
inline double operator()() const {
34+
return ::Rf_rgamma( df_2, 2.0 ) ;
35+
}
36+
37+
private:
38+
double df_2 ;
39+
} ;
40+
} // stats
2741

28-
class ChisqGenerator : public ::Rcpp::Generator<false,double> {
29-
public:
30-
31-
ChisqGenerator( double df_ ) : df_2(df_ / 2.0) {}
32-
33-
inline double operator()() const {
34-
return ::Rf_rgamma( df_2, 2.0 ) ;
42+
// Please make sure you to read Section 6.3 of "Writing R Extensions"
43+
// about the need to call GetRNGstate() and PutRNGstate() when using
44+
// the random number generators provided by R.
45+
inline NumericVector rchisq( int n, double df ){
46+
if (!R_FINITE(df) || df < 0.0) return NumericVector(n, R_NaN) ;
47+
return NumericVector( n, stats::ChisqGenerator( df ) ) ;
3548
}
36-
37-
private:
38-
double df_2 ;
39-
} ;
40-
} // stats
41-
42-
inline NumericVector rchisq( int n, double df ){
43-
if (!R_FINITE(df) || df < 0.0) return NumericVector(n, R_NaN) ;
44-
return NumericVector( n, stats::ChisqGenerator( df ) ) ;
45-
}
4649

4750
} // Rcpp
4851

0 commit comments

Comments
 (0)