简体   繁体   中英

Integration with quadrature and multiprecision boost libraries in C++

after searching i had found amazing code for integration by

quadrature boost library.

rather than

log(x)/(1+x)

want to integrate

(poly[0]+poly[1]*x+poly[2]*x^2+...+poly[n]*x^n)*log(x)/(1+x) . But, i do not

know how to insert the vector

poly

to

struct f

or even how to call these operators from main function. The code :

#include<iostream>
#include<boost/math/constnats/constants.hpp>
#include<boost/multiprecision/cpp_dec_float.hpp>
#include <boost/numeric/quadrature/adaptive.hpp>
#include <boost/numeric/quadrature/kronrodgauss.hpp>
#include <boost/numeric/quadrature/epsilon.hpp>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
namespace quadrature=boost::numeric::quadrature;
struct f
{
 double operator()(double x) const {
 return (log(x)/(1+x); }
};
int main()
{ 
vector<cpp_dec_float_50> poly(0);
cpp_dec_float_50 p = 0;
for (int i=0;i<=n;i++)
{
   p=polynomial(i,n);
   poly.push_back(p); 
}

double answer,error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(f(),0.,1.,answer,error_estimate);
cout<<"ans"<<answer<<endl;
return 0;
}
cpp_dec_float_50 polynomial(int k ,int n)
{
.
.
.

}

Also, when changing the double operator, to cpp_dec_float_50 operator in

struct f

many problems arise. and the later type is necessary in my project. Any one can fix that ?

EDIT

i tried this, but i do sth wrong

#include<iostream>
#include <boost/numeric/quadrature/adaptive.hpp>
#include <boost/numeric/quadrature/kronrodgauss.hpp>
#include <boost/numeric/quadrature/epsilon.hpp>
#include<boost/math/constants/constants.hpp>
#include<boost/multiprecision/cpp_dec_float.hpp>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
namespace quadrature=boost::numeric::quadrature;
double polynomial(int k ,int n);
struct f
{ const cpp_dec_float_50 s=0;
 vector<cpp_dec_float_50> poly;
 cpp_dec_float_50 sum()const{
 for(int i=0;i<=poly.size();i++)
  s+=poly[i];
 return s

 }
 double operator()(double x) const {
 return
 s*log(x)/(1+x); }
 };
  int main()
  {
int n=2;
 f fun;
 cpp_dec_float_50 p = 0;
 for (int i=0;i<=n;i++)
 {
 p=polynomial(i,n);
 fun.poly.push_back(p);
 }

 double answer,error_estimate;
 quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
 (fun,0.,1.,answer,error_estimate);
 cout<<"ans"<<answer<<endl;
 return 0;
 }
 double polynomial(int k ,int n)
 {
  return k;

  }

Edit when using Patstew suggestion Two errors occur

Try something along the lines of:

struct f
{
 vector<cpp_dec_float_50> poly;
 double operator()(double x) const {
 return (poly[0]+poly[1]*x+poly[2]*x^2+...+poly[n]*x^n)*log(x)/(1+x); }
};
int main()
{ 
f fun;
cpp_dec_float_50 p = 0;
for (int i=0;i<=n;i++)
{
   p=polynomial(i,n);
   fun.poly.push_back(p); 
}

double answer,error_estimate;
quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
(fun,0.,1.,answer,error_estimate);
cout<<"ans"<<answer<<endl;
return 0;
}

EDIT: RE you own answer, you never call sum (and s is const so you couldn't change it if you did) so s is always 0 and you will always get 0 as your answer. Also you are iterating all the way up to poly.size() in sum() , but poly[poly.size()-1] is the last element. I think you really want your sum function to calculate a polynomial? Try this:

#include<iostream>
#include <boost/numeric/quadrature/adaptive.hpp>
#include <boost/numeric/quadrature/kronrodgauss.hpp>
#include <boost/numeric/quadrature/epsilon.hpp>
#include<boost/math/constants/constants.hpp>
#include<boost/multiprecision/cpp_dec_float.hpp>
using namespace std;
using boost::multiprecision::cpp_dec_float_50;
namespace quadrature=boost::numeric::quadrature;
double polynomial(int k ,int n);
struct f
{
 vector<double> poly;
 double polysum(double x) {
 double s = poly[0];
 double p = 1;
 for(int i=1;i<poly.size();i++) {
  p = p*x;
  s+= p*poly[i];
 }
 return s

 }
 double operator()(double x) {
 return polysum(x)*log(x)/(1+x); }
 };
  int main()
  {
int n=2;
 f fun;
 double p = 0;
 for (int i=0;i<=n;i++)
 {
 p=polynomial(i,n);
 fun.poly.push_back(p);
 }

 double answer,error_estimate;
 quadrature::adaptive().relative_accuracy(1e-5).absolute_accuracy(1e-7)
 (fun,0.,1.,answer,error_estimate);
 cout<<"ans"<<answer<<endl;
 return 0;
 }
 double polynomial(int k ,int n)
 {
  return k;

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM