在C++中评估基于矩阵的多项式的最佳方法
Best way to evaluate a matrix-based polynomial in C++
all.我正在尝试设置一个程序,该程序根据 X 的用户输入来评估多项式。我想要的程序的另一部分是将这些多项式加在一起。我正在使用 2D 数组来执行此操作。 您认为编写评估函数的最佳方式是什么。已经为此工作了几个小时,我仍然不太确定该怎么做。提前谢谢。
多项式
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
using namespace std;
#define MAX 100
class Polynomial {
friend ostream &operator<< (ostream &, const Polynomial &);
public :
Polynomial ();
void enterTerms();
int evaluate(Polynomial p, int x);
Polynomial operator +(const Polynomial & );
private :
int terms[MAX][2]; //either static size(MAX rows) or use "new" for dynamic allocation
int n; //number of terms
};
#endif
多项式.cpp
#include "polynomial.h"
using namespace std;
ostream &operator<< (ostream & out, const Polynomial & p){
for ( int i = 0 ; i < p.n ; i++ ){
if ( i == p.n - 1 )//last term does not have + appended
out << p.terms[i][0] <<"x^"<<p.terms[i][1]<<endl;
else
out << p.terms[i][0]<<"x^"<<p.terms[i][1]<<" + ";
}
return out;
}
Polynomial :: Polynomial(){
for ( int i = 0; i < MAX; i++ ){
terms[i][0] = 0;
terms[i][1] = 0;
}
}
void Polynomial :: enterTerms(){//enterTerms() not in constructor so that no prompt for entering
//terms while doing + - etc., they also produce Poylnomial instance (i.e. invoke constructor)
int num;
cout<<"enter number of terms in polynomialn";
cin >> num;
n = num >= 0 ? num : 1;
cout << "enter coefficient followed by exponent for each term in polynomialn";
for ( int i = 0; i < n ; i++)
cin >> terms[i][0] >> terms[i][1] ;
}
Polynomial Polynomial :: operator + ( const Polynomial & p ){
Polynomial temp, sum;
temp.n = n + p.n;
int common = 0;
// first write sum as concatenation of p1 and p2
for ( int i = 0 ; i < n ; i++ ){
temp.terms[i][0] = terms[i][0];
temp.terms[i][1] = terms[i][1];
}
//notice j and k for traversing second half of sum, and whole p2 resp
for ( int j = n, k = 0; j < n + p.n, k < p.n ; j++, k++ ){
temp.terms[j][0] = p.terms[k][0];
temp.terms[j][1] = p.terms[k][1];
}
for ( int l = 0; l < temp.n - 1 ; l++ ){ // 0 to 1 less than length
for ( int m = l + 1 ; m < temp.n ; m++ ){ // 1 more than l to length,so that compared pairs are non redundant
if( temp.terms[l][1] == temp.terms[m][1] ){
common++; //common terms reduce no. of terms in sum (see sum.n decl)
temp.terms[l][0] += temp.terms[m][0]; //coefficients added if exponents same
temp.terms[m][0] = 0;
}
}
}
sum.n = temp.n - common; //if you place it above, common taken as 0 and sum.n is same as temp.n (logical error)
//just to debug, print temporary array
cout << endl << temp;
for ( int q = 0, r = 0; q < temp.n; q++ ){
if ( temp.terms[q][0] == 0 )
continue;
else{
sum.terms[r][0] = temp.terms[q][0];
sum.terms[r][1] = temp.terms[q][1];
r++;
}
}
cout << endl << sum;
return sum;
}
int Polynomial :: evaluate(Polynomial p, int x)
{
Polynomial terms;
return 0;
}
int main()
{
Polynomial p1 , p2;
p1.enterTerms();
p2.enterTerms();
cout << "Please enter the value of x:" << endl;
cin >> x;
//evaluate(p1);
//evaluate(p2);
p1 + p2;
system("PAUSE");
//cin.get();
return 1;
}
请考虑更简单的数据结构。 一种常见的方法是使用单个数组,其中索引是 x 的幂。 只需在没有此类术语的情况下使用零。 然后x^3 + 2*x + 1
写{1, 2, 0, 1}
,因为没有x^2
。 另请注意相反的顺序,因为p[0]
表示x^0
。 这大大简化了加法等操作。
就评估而言,只需考虑等式。 如果你的多项式是x^2 + 3*x + 5
,并且你想计算x=7,你会怎么做? 从幂 0 开始,并将每个项累加到单个变量中。
您可以在此处关注并完成我的函数:
float polyval_point(Eigen::VectorXf v,float x)
{
float s = 0;
for (int i=0;i<v.size();i++)
{
s += v[i] * pow(x,i);
}
return s;
}
Eigen::VectorXf polyval_vector(Eigen::VectorXf v,Eigen::VectorXf X)
{
Eigen::VectorXf S(X.size());
for (int i=0;i<X.size();i++)
{
S[i] = polyval_point(v,X[i]);
}
return S;
}
相关文章:
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 在c代码之间共享数据的最佳方式
- 使用std::source_location报告错误的最佳实践
- 派生类销毁的最佳实践是什么
- 将寄存器设计成可由C和C++访问的外设的最佳实践
- c++模板来表示多项式
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 在C++中向零方向近似的最佳方法
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 检测win32服务创建和删除的最佳方法
- 在reactor中存储eventHandlers的最佳方式是什么
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 在AVX通道中混洗的最佳方式
- 在C++中评估基于矩阵的多项式的最佳方法