I have a problem where I need to numerically integrate a univariate function with multiple extra inputs other than the variable that's being integrated over. The integration is from zero to infinity.
I said without extra parameters because I already defined a class with the extra parameters being the private member variables. And then the operator functor is defined to accept just the integration variable (hence, univariate). With this class, I want to use the GSL numerical integration library (gsl/gsl_integration.h) to do the integration. Is there a way to define a member function for this integration inside the class using GSL?
#include <cmath>
#include <Rmath.h>
#include <algorithm>
#include <iterator>
#include <RcppArmadillo.h>
#include <progress.hpp>
#include <progress_bar.hpp>
#include <RcppGSL.h>
#include <gsl/gsl_integration.h>
#include <Rdefines.h>
// [[Rcpp::depends(RcppArmadillo, RcppProgress, RcppGSL)]]
using namespace arma;
class ObservedLik
{
private:
const int& Tk;
const arma::vec& resid;
const arma::mat& ZEREZ_S;
const double& nu;
const double& maxll;
public:
ObservedLik(const int& Tk_,
const arma::vec& resid_,
const arma::mat& ZEREZ_S_,
const double& nu_,
const double& maxll_) : Tk(Tk_), resid(resid_), ZEREZ_S(ZEREZ_S_), nu(nu_), maxll(maxll_) {}
double operator()(const double& lam) const {
double loglik = -M_LN_SQRT_2PI * static_cast<double>(Tk) + (0.5 * nu - 1.0) * lam - 0.5 * nu * lam + 0.5 * nu * (std::log(nu) - M_LN2) - R::lgammafn(0.5 * nu);
double logdet_val;
double logdet_sign;
log_det(logdet_val, logdet_sign, ZEREZ_S);
loglik -= 0.5 * (logdet_val + arma::accu(resid % arma::solve(ZEREZ_S, resid)));
/***********************************
subtract by maximum likelihood value
for numerical stability
***********************************/
return std::exp(loglik - maxll);
}
double integrate() {
/* do the integration here */
gsl_integration_workspace * w
= gsl_integration_workspace_alloc (1000);
double result, error;
gsl_function F;
F.function = &f; // make this the operator()
F.params = α // I don't need this part
gsl_integration_qagiu (&F, 0.0, 0, 1, 0, 1e-7, 1000,
w, &result, &error);
return result;
}
};