编程语言
首页 > 编程语言> > C++实现Polyval函数(多项式生成函数,np.polyval()等价实现)

C++实现Polyval函数(多项式生成函数,np.polyval()等价实现)

作者:互联网

#include<iostream>
#include<vector>
#include<math.h>

std::vector<double> Polyval(std::vector<double> coeffs, std::vector<double> values)
{
  std::vector<double> results;
    for (auto const &val:values)
        {
          double result = 0;
          for (int i = 0, deg = coeffs.size() - 1; deg >= 0; deg--, i++)
            {
                result += coeffs[i] * std::pow(val, deg);
            }
          results.push_back (result);
        }
        return results;
}

int main()
{
  std::vector<double> coeffs = { 4, 2, -2, 5};
  std::vector<double> valuesToEvaluate = { 0, 2 , -4};
  std::vector<double> results = Polyval (coeffs, valuesToEvaluate);

  for (auto const &res:results)
    {
      std::cout << res << std::endl;
    }
}

链接:https://stackoverflow.com/questions/60396667/is-there-a-polyval-matlabs-function-equivalent-in-c-stl

标签:Polyval,polyval,函数,results,std,vector,coeffs,deg
来源: https://www.cnblogs.com/LuckCoder/p/16381057.html