与C ++中的正交和多精度增强库集成

时间:2016-05-18 10:09:12

标签: c++ boost vector boost-multiprecision

在搜索之后,我找到了很好的集成代码

正交升压库。

而不是

  

日志(X)/(1 + x)的

想要整合

(poly[0]+poly[1]*x+poly[2]*x^2+...+poly[n]*x^n)*log(x)/(1+x)。但是,我不

知道如何插入矢量

  

  

struct f

甚至如何从main函数调用这些运算符。代码:

#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)
{
.
.
.

}

此外,在更改double运算符时,

中的cpp_dec_float_50运算符
  

struct f

出现了很多问题。在我的项目中,后一种类型是必要的。任何人都能解决这个问题?

修改

我试过这个,但我做错了

#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;

  }

编辑 当使用Patstew建议时 发生了两个errors

1 个答案:

答案 0 :(得分:0)

尝试以下方面的内容:

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;
}

编辑:如你自己的答案,你永远不会打电话给sum(而s是const,所以如果你这样做就不能改变它)所以s总是0并且你将永远得到0作为你的答案。此外,您在poly.size()中一直迭代到sum(),但poly[poly.size()-1]是最后一个元素。我想你真的希望你的sum函数计算多项式?试试这个:

#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;

  }