Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for r-devel issue #133

Merged
merged 1 commit into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2017-05-21 Dirk Eddelbuettel <edd@debian.org>

* inst/unitTests/cpp/rmultinom.cpp: More careful use of Armadillo
vectors and Rcpp vectors

2017-05-15 Dirk Eddelbuettel <edd@debian.org>

* DESCRIPTION (Version, Date): Release 0.7.900.1.0 (GH-only)
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: RcppArmadillo
Type: Package
Title: 'Rcpp' Integration for the 'Armadillo' Templated Linear Algebra Library
Version: 0.7.900.1.0
Date: 2017-05-15
Version: 0.7.900.1.1
Date: 2017-05-21
Author: Dirk Eddelbuettel, Romain Francois and Doug Bates
Maintainer: Dirk Eddelbuettel <edd@debian.org>
Description: 'Armadillo' is a templated C++ linear algebra library (by Conrad
Expand Down
1 change: 1 addition & 0 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
}
\item Added gcc 7 to support compiler check (James Balamuta in \ghpr{128}
addressing \ghit{126}).
\item A unit test helper function for \code{rmultinom} was corrected.
}
}

Expand Down
21 changes: 9 additions & 12 deletions inst/include/RcppArmadilloExtensions/rmultinom.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ namespace Rcpp{
int probsize = prob.size();
// Return object
IntegerVector draws(probsize);
if ( size < 0 || size == NA_INTEGER) throw std::range_error( "Invalid size" );
if (size < 0 || size == NA_INTEGER) throw std::range_error( "Invalid size");
long double p_tot = 0.;
p_tot = std::accumulate(prob.begin(), prob.end(), p_tot);
if(fabs((double)(p_tot - 1.)) > 1e-7){
throw std::range_error( "Probabilities don't sum to 1, please use FixProb" );
p_tot = std::accumulate(prob.begin(), prob.end(), p_tot);
if (fabs((double)(p_tot - 1.)) > 1e-7) {
throw std::range_error("Probabilities don't sum to 1, please use FixProb");
}

// do as rbinom
Expand All @@ -50,18 +50,15 @@ namespace Rcpp{
}
//rmultinom(size, REAL(prob), k, &INTEGER(ans)[ik]);
// for each slot
for(ii = 0; ii < probsize-1; ii++) { /* (p_tot, n) are for "remaining binomial" */
if(prob[ii]) {
for (ii = 0; ii < probsize-1; ii++) { /* (p_tot, n) are for "remaining binomial" */
if (prob[ii]) {
pp = prob[ii] / p_tot;
// >= 1; > 1 happens because of rounding
draws[ii] = ((pp < 1.) ?
(int) Rf_rbinom((double) size, pp) :
size
);
draws[ii] = ((pp < 1.) ? (int) Rf_rbinom((double) size, pp) : size);
size -= draws[ii];
}; // else { ret[ii] = 0; }
} // else { ret[ii] = 0; }
// all done
if(size <= 0) return draws;
if (size <= 0) return draws;
// i.e. p_tot = sum(prob[(k+1):K])
p_tot -= prob[ii];
}
Expand Down
14 changes: 7 additions & 7 deletions inst/unitTests/cpp/rmultinom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
#include <RcppArmadilloExtensions/rmultinom.h>
#include <RcppArmadilloExtensions/fixprob.h>

using namespace Rcpp ;
using namespace Rcpp;

// [[Rcpp::export]]
IntegerVector rmultinomC( int n, int size,
NumericVector prob
) {
IntegerVector rmultinomC(int n, int size, NumericVector prob) {
IntegerMatrix draws(prob.size(), n);
// FixProb modifies in-place
NumericVector fixprob = clone(prob);
arma::colvec fixprob(prob.begin(), prob.size()); // forced copy
RcppArmadillo::FixProb(fixprob, 1, true);
NumericVector newprob(Rcpp::wrap(fixprob));
RNGScope scope;
for (int ii=0; ii<n; ii++){
draws(_,ii) = RcppArmadillo::rmultinom( size, fixprob);
for (int ii=0; ii<n; ii++) {
draws(_, ii) = RcppArmadillo::rmultinom(size, newprob);
}
return draws;
}