初级
验证函数是向量空间
未完成
初级参考
完整示例代码供参考,建议自己理解后重新输入
class Polynomial(Vector):
def __init__(self, *coefficients):
self.coefficients = coefficients
def __call__(self,x):
return sum(coefficient * x ** power for (power,coefficient) in enumerate(self.coefficients))
def add(self,p):
return Polynomial([a + b for a,b in zip(self.coefficients, p.coefficients)])
def scale(self,scalar):
return Polynomial([scalar * a for a in self.coefficients])
def _repr_latex_(self):
monomials = [repr(coefficient) if power == 0
else "x ^ {%d}" % power if coefficient == 1
else "%s x ^ {%d}" % (coefficient,power)
for (power,coefficient) in enumerate(self.coefficients)
if coefficient != 0]
return "$ %s $" % (" + ".join(monomials))
@classmethod
def zero(cls):
return Polynomial(0)
def random_function():
degree = randint(0,5)
p = Polynomial(*[uniform(-10,10) for _ in range(0,degree)])
return Function(lambda x: p(x))
for i in range(0,100):
a,b = random_scalar(), random_scalar()
u,v,w = random_function(), random_function(), random_function()
test(Function.zero(), approx_equal_function, a,b,u,v,w)
👑
升级 VIP
解锁全部题目,畅通无阻地学习
- ✓ 解锁全部训练包所有题目
- ✓ 查看完整参考代码和提示
- ✓ 浏览器内直接运行 Python 代码
- ✓ 自动批改 + 进度追踪
30天
¥18
1年
¥99
2年
¥158
3年
¥199