- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
我正在尝试编写一个从输入文件构造多项式的程序。它读入多项式并将值存储到类属性“系数”和“指数”中。例如。系数 = 2,指数 = 3 将导致 2x^3。在读取多项式和输出时,有很多烦人的极端情况必须处理。 (operator<<
和 operator>>
函数)我的主要函数彻底测试了我的 polynomial.cpp。我相信我的问题之一来自构造多项式,正如您可能注意到的,我在为派生函数编写代码时也遇到了问题。这是我所拥有的:
#ifndef _POLYNOMIAL_H
#define _POLYNOMIAL_H
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
class Polynomial {
public:
Polynomial();
Polynomial(vector<double> iCoefficients, vector<int> iExponents);
int Degree() const;
double Evaluate(double x) const;
Polynomial Derivative() const;
friend Polynomial operator+(const Polynomial & p, const Polynomial & p2);
friend Polynomial operator*(const Polynomial & p, const Polynomial & p2);
friend ostream& operator<<(ostream& out, const Polynomial & p);
friend istream& operator>>(istream& in, Polynomial & p);
private:
vector<double> coefficients;
};
#endif
#include "polynomial.h"
#include <stdexcept>
#include <vector>
#include <cmath>
using namespace std;
// Default Constructor
Polynomial::Polynomial() {
coefficients.push_back(0);
}
// Constructor for a Polynomial
Polynomial::Polynomial(vector<double> iCoefficients, vector<int> iExponents) {
for (int i = 0; i < iExponents[0]; i++) {
coefficients.push_back(0);
}
for (size_t i = 0; i < iExponents.size(); i++) {
coefficients[(Degree() - iExponents[i])] = iCoefficients[i];
}
}
// Returns highest exponent of the polynomial
int Polynomial::Degree() const {
return coefficients.size();
}
// Evaluates the polynomial at a particular point
double Polynomial::Evaluate(double x) const {
double result;
for(int i = 0; i <= Degree(); i++) {
result += pow(x, Degree() - i) * coefficients[i];
}
return result;
}
// Returns first derivative of the polynomial
Polynomial Polynomial::Derivative() const { //----------------------???
// Polynomial result;
// for(int i = 0; i <= Degree(); i++) {
// result.coefficients[i] = coefficients[i] * (Degree() - i);
// }
// return result;
}
// Returns polynomial object that is the sum of parameters
Polynomial operator+(const Polynomial & p, const Polynomial & p2) {
int d = p.Degree();
int d2 = p2.Degree();
Polynomial sum;
for (int j = 0; j < d; j++) {
for (int i = 0; i < d2; i ++) {
sum.coefficients.push_back(p.coefficients[j] + p2.coefficients[i]);
}
}
return sum;
}
// Returns polynomial object that is the product of parameters
Polynomial operator*(const Polynomial & p, const Polynomial & p2) {
int d = p.Degree();
int d2 = p2.Degree();
Polynomial product;
for (int j = 0; j < d; j++) {
for (int i = 0; i < d2; i ++) {
product.coefficients.push_back(p.coefficients[j] * p2.coefficients[i]);
}
}
return product;
}
// Output operator
ostream& operator<<(ostream& out, const Polynomial & p) {
for (int i = 0; i <= p.Degree(); i++) {
if(i == 0 && p.Degree() <= 1) {
out << 0;
}
if (p.coefficients[i] != 0 && i != 0) {
out << '+';
}
if (p.coefficients[i] != 0) {
out << p.coefficients[i];
if(i < (p.Degree() - 1)) {
out << "x^";
out << (i - p.Degree()) * (-1);
}
}
}
return out;
}
// Input operator
istream& operator>>(istream& in, Polynomial & p) {
char ch;
int exponent;
double coefficient;
vector<double> coefficients;
vector<int> exponents;
while(isspace(ch) == false) {
ch = in.peek();
if(ch == '+') {
in.ignore();
in >> coefficient;
}
else if(ch == '-') {
in.ignore();
in >> coefficient;
coefficient = coefficient * (-1);
}
else {
in >> coefficient;
}
ch = in.peek();
if((ch <= 'z') && (ch >= 'a')) {
in >> ch;
ch = in.peek();
if(ch == '^') {
in.ignore();
in >> exponent;
}
else
exponent = 1;
}
else
exponent = 0;
coefficients.push_back(coefficient);
exponents.push_back(exponent);
}
p = Polynomial(coefficients, exponents);
return in;
}
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
#include "polynomial.h"
using namespace std;
bool testPolynomial(const Polynomial& p, string expected);
bool testOperations(const Polynomial& p, int degree, double expected);
bool testInput(string s);
int main() {
int errors = 0;
cerr << "Note: Nearly all of the tests expect a working output operator. If a test fails, check that first" << endl;
cerr << "Testing default constructor" << endl;
Polynomial p1; // test default constructor
errors += testPolynomial(p1, "0");
cerr << "Testing explicit value constructor" << endl;
double c_arr[] = {1.1, 2, 4, 7};
int e_arr[] = {6, 3, 2, 0};
vector<double> c(c_arr, c_arr+4);
vector<int> e(e_arr, e_arr+4);
Polynomial p2(c, e);
errors += testPolynomial(p2, "1.1x^6+2x^3+4x^2+7");
c.clear(); e.clear();
cout << '1' << endl;
Polynomial p3(c, e);
errors += testPolynomial(p3, "0");
cout << '2' << endl;
cerr << "Testing operations" << endl;
double c2_arr[] = {-1.1, 2, -4, 7};
int e2_arr[] = {4, 3, 2, 0};
vector<double> c2(c2_arr, c2_arr+4);
vector<int> e2(e2_arr, e2_arr+4);
Polynomial p4(c2,e2);
errors += testOperations(p1, 0, 0);
errors += testOperations(p2, 6, 109.4);
errors += testOperations(p4, 4, -10.6);
errors += testPolynomial(p1.Derivative(), "0");
errors += testPolynomial(p2.Derivative(), "6.6x^5+6x^2+8x");
errors += testPolynomial(p4.Derivative(), "-4.4x^3+6x^2-8x");
errors += testPolynomial(p1+p2, "1.1x^6+2x^3+4x^2+7");
errors += testPolynomial(p2+p4, "1.1x^6-1.1x^4+4x^3+14");
errors += testPolynomial(p1*p2, "0");
errors += testPolynomial(p2*p2, "1.21x^12+4.4x^9+8.8x^8+19.4x^6+16x^5+16x^4+28x^3+56x^2+49");
double c_arr3[] = {-1};
int e_arr3[] = {0};
vector<double> c3 = vector<double>(c_arr3, c_arr3+1);
vector<int> e3 = vector<int>(e_arr3, e_arr3+1);
Polynomial p5(c3, e3);
errors += testPolynomial(p2 * p5 + p2, "0");
errors += testPolynomial(p5, "-1");
cerr << "Testing input operator." << endl;
testInput("0");
testInput("51");
testInput("-1.1");
testInput("3x^2");
testInput("-5x^3-5");
testInput("x^5+x-1");
testInput("-x^4+2");
return errors;
}
bool testPolynomial(const Polynomial& p, string expected) {
ostringstream out;
out << p;
if (out.str() != expected) {
cerr << "Test failed: expected " << expected << " got " << out.str() << endl;
return true;
} else {
return false;
}
}
bool testOperations(const Polynomial& p, int degree, double expected) {
if(p.Degree() != degree) {
cerr << "Failed Degree operation" << endl;
return true;
}
double result = p.Evaluate(2.0);
if (fabs(result - expected) > 1e-5) {
cerr << "Failed Evaluation operation" << endl;
}
return false;
}
bool testInput(string s) {
Polynomial p;
istringstream in(s+" ");
in >> p;
ostringstream out;
out << p;
if (out.str() != s) {
cerr << "Failed input test. Expected: " << s << " got " << out.str() << endl;
return true;
}
return false;
}
所以我想创建一个程序,当用户输入值 c 且 a = 1 时,打印出可因式分解的二次方程。程序应确定 b 的所有可能的整数值,以便三项式以 x^2 + bx + c 的形式打印出来 一个例子是,如果用户
我有自己定义的多项式类,它是系数列表的形式。 有点像 axˆ2 + bx + c is equals to [c, b, a] (for ax + b == [b, a] similarly, for
我必须制作一个对多项式执行运算的 GUI,但我不断收到无法摆脱的 NullPointerExceptions。在输出上它没有显示任何内容。我尝试调试我的程序,据我所知,我从键盘插入的多项式在某种程度上
numpy.lib.polynomial.polyval 允许您使用另一个多项式评估多项式: numpy.polyval(poly1d([1, 2, 3]), 2) Out[832]: 11 nump
如果我想计算多项式,如何在 C 中定义具有可变数量参数的函数?我的函数必须有这个参数:第一个参数:float x,第二个:int n,其余的 float (系数)。非常感谢! 最佳答案 用 varia
我正在尝试求多项式的不定积分,但是我的数学和编码都不是很好。我的代码可以编译,但我相信我的公式有误: Polynomial Polynomial :: indefiniteIntegral() co
我有 3 个数据集。 2 表示多项式本身(我们称它们为 x 和 y),1 表示函数值(它将是 z)。 多项式看起来像这样(假设两个维度的幂都是 3): z = a00 + a01*x + a02*x^
如何在 python 中计算最佳拟合线,然后将其绘制在 matplotlib 中的散点图上? 我使用普通最小二乘回归计算线性最佳拟合线如下: from sklearn import linear_mo
我正在尝试分解 bool 多项式以获得逻辑网络的最小形式。我的变量是 a1、a2、a3 ... 以及负对应项 na1、na2、na3 ... 如果需要一个函数 f = a1*a2*b2*nb1 + a
长话短说 如何使用系数数组构建表达式并将其转换为 Func ?有没有比表达式树更好的方法? 我有一个使用 Func formula 构造的不可变序列类型用于为序列 A 生成术语 An。我开始构建一个辅
我在我的 Mac OS Sierra 上运行 Spark 2.1.1(这应该有帮助)。我尝试在网上找到的测试数据集上拟合多项式逻辑回归,我在此处报告前几行(我不知道如何在此处附加文件): 1,0,24
我必须构建一个从类 lista(列表)继承的类多项式(polinom)。我必须从多项式类中加、减、乘、除 2 个对象。我有这段代码。我不明白为什么我的析构函数不工作。我还必须重载运算符:+、-、> 但
我有一个 Polynomial类,我正在尝试定义 operator++ ,递增前和递增后,以及尝试定义递减前和递减后,即 operator-- .这是我的代码片段: class Polynomial
我是编程新手(Python 是我的第一语言),但我喜欢设计算法。我目前正在研究方程组(整数),但找不到任何解决我的特定问题的引用。 让我解释一下。 我有一个等式(一个测试,如果你愿意的话): raw_
我正在尝试使用 scipy.stats (python) 中的 multinominal.pmf 函数。 当我在输入中所有概率都大于零的情况下使用此函数时,它工作正常。问题是当我想在其中一个概率为零时
我想用 0xA001 多项式计算字节数组的 CRC-16 校验和。但我真的不知道如何在 Java 中做到这一点,以及如何使用给定的多项式。它是某种特殊值(0xA001)吗?你能告诉我一个可以为我计算校
由于我的分类器在测试数据上产生了大约 99% 的准确率,我有点怀疑并想深入了解我的 NB 分类器最有用的特征,看看它正在学习什么样的特征。以下主题非常有用:How to get most inform
如 McFadden (1978)表明,如果多项 logit 模型中的备选方案数量大到无法计算,则通过对备选方案进行随机子集来获得一致估计仍然是可行的,因此每个个体的估计概率基于所选备选方案和 C其他
我现在有一些离散点,我使用 scipy.interpolate.splprep () 函数(B 样条插值)对其进行插值,以获得令人满意的平滑曲线。这是代码(借鉴另一个问题的答案)和我得到的结果。 im
我在 IPython notebook 中有一些多项式 x: import numpy as np x = np.polynomial.polynomial.Polynomial([1,2,3]) x
我是一名优秀的程序员,十分优秀!