对于作业,我创建了一个类来表示多项式,方法是将列表及其系数存储为该类的数据成员。
- 列表的第一个元素(索引0)代表常量;
- 第二个(索引 1)表示 x 项的系数,依此类推,
- 每个下一个元素代表多项式的下一个幂的系数。
虽然我在为其创建最后一个函数时遇到问题“polySequence”
:
A function polySequence which takes a start, end, and step, and returns a generator. The generator will evaluate the polynomial for the value start, then start + end, and so on, up to end and yield these values one at a time. If no step is given, a step of 1 should be used (similar to the range method). As an example, if p represents the polynomial 2x + 1 (coefficient list [1,2]), the code
for val in p.polySequence(0,5):
print(val)
应该在单独的行上打印值 1、3、5、7 和 9。
这是我在底部实现的 Polysequence:
最佳答案
看起来您只将第一个参数传递给了 polySequence 函数。您应该传递所有声明的参数(开始、停止和步骤):
p = Polynomial(1, 2)
for val in p.polySequence(0, 5, 1):
print(val)
关于python - 如何为我的类(class)构造多项式序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56511096/