0

搜索后我发现了令人惊叹的集成代码

正交升压库。

而不是

对数(x)/(1+x)

想整合

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

知道如何插入向量

结构体

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

#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 运算符

结构体

出现很多问题。在我的项目中需要后一种类型。任何人都可以解决这个问题?

编辑

我试过这个,但我做错了

#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 建议时编辑出现两个错误

4

1 回答 1

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()in 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;

  }
于 2016-05-19T21:05:49.677 回答