[Rcpp-devel] Checking a condition like any(x < lowerBd)
Dirk Eddelbuettel
edd at debian.org
Thu Jun 10 18:20:03 CEST 2010
On 10 June 2010 at 10:47, Douglas Bates wrote:
| I thought I had had enough coffee this morning not to make really dumb
| mistakes but perhaps not.
|
| I want to check that a particular NumericVector is within a feasible
| region defined by lower and upper bounds on each element. For the
| lower bound I am checking for the equivalent of any(x < lowerBd) in R.
| I can do it in two stages, as in the enclosed chkfun2, but my attempt
| to do this in a single operation using std::inner_product failed. Can
| anyone suggest why?
What about a proletarian version like
R> chkfun <- cxxfunction(signature(x = "numeric", lowerBd = "numeric"), '
+ NumericVector xx(x), ll(lowerBd);
+ if (xx.size() != ll.size()) return R_NilValue;
+ bool ans = false;
+ for (int i=0; i<xx.size() && !ans; i++) ans = xx[i] < ll[i];
+ return wrap(ans);
+ ', plugin = "Rcpp")
R> chkfun(1, 0)
[1] FALSE
R> chkfun(c(1, 0, 1), c(0, -Inf, 0))
[1] FALSE
R> chkfun(c(1, 0, 1), c(0, -Inf, 2))
[1] TRUE
R>
I am as guilty as the next to 'go for cute' but if it that easy 'get it
done', well ...
| P.S. I used cxxfunction here because I couldn't find cppfunction in
| either the inline or the Rcpp packages. Again, am I missing
| something?
With inline 0.3.5 and the plugin mechanism, it become cxxfunction. Quoting
from NEWS:
o cppfunction has been withdrawn since the introduction of the more
flexible cxxfunction in the inline package (0.3.5). Rcpp no longer
depends on inline since many uses of Rcpp do not require inline at
all. We still use inline for unit tests but this is now handled
locally in the unit tests loader runTests.R.
Users of the now-withdrawn function cppfunction can redefine it as:
cppfunction <- function(...) cxxfunction( ..., plugin = "Rcpp" )
Sorry about the imposed change.
--
Regards, Dirk
More information about the Rcpp-devel
mailing list