Skip to content

Commit 9c3df00

Browse files
committed
a better example for infinite interval
1 parent 8800716 commit 9c3df00

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

vignettes/introduction.Rmd

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ public:
122122
};
123123
124124
// [[Rcpp::export]]
125-
Rcpp::List integrate_test(const double& lower, const double& upper)
125+
Rcpp::List integrate_test()
126126
{
127127
const double a = 3, b = 10;
128+
const double lower = 0.3, upper = 0.8;
128129
const double true_val = R::pbeta(upper, a, b, 1, 0) -
129130
R::pbeta(lower, a, b, 1, 0);
130131
@@ -141,17 +142,47 @@ Rcpp::List integrate_test(const double& lower, const double& upper)
141142
}
142143
```
143144

144-
Runing the `integrate_test()` function for some finite interval in R gives
145-
145+
Runing the `integrate_test()` function in R gives
146146

147147
```{r}
148-
integrate_test(0.3, 0.8)
148+
integrate_test()
149149
```
150150

151-
Note that infinite intervals are also possible in the case of one-dimensional integration
151+
Note that infinite intervals are also possible in the case of one-dimensional integration:
152+
153+
```{Rcpp}
154+
// [[Rcpp::depends(RcppEigen)]]
155+
// [[Rcpp::depends(RcppNumerical)]]
156+
#include <RcppNumerical.h>
157+
using namespace Numer;
158+
159+
class TestInf: public Func
160+
{
161+
public:
162+
double operator()(const double& x) const
163+
{
164+
return x * x * R::dnorm(x, 0.0, 1.0, 0);
165+
}
166+
};
167+
168+
// [[Rcpp::export]]
169+
Rcpp::List integrate_test2(const double& lower, const double& upper)
170+
{
171+
TestInf f;
172+
double err_est;
173+
int err_code;
174+
const double res = integrate(f, lower, upper, err_est, err_code);
175+
return Rcpp::List::create(
176+
Rcpp::Named("approximate") = res,
177+
Rcpp::Named("error_estimate") = err_est,
178+
Rcpp::Named("error_code") = err_code
179+
);
180+
}
181+
```
152182

153183
```{r}
154-
integrate_test(0.1, Inf)
184+
integrate(function(x) x^2 * dnorm(x), 0.5, Inf) # integrate() in R
185+
integrate_test2(0.5, Inf)
155186
```
156187

157188
### Multi-dimensional

0 commit comments

Comments
 (0)