# Python 类多项式不起作用，就像我期望的那样Python class Polynomials won't work, like I expect it to work

``````import numpy as np

class Polynomials (np.poly1d):
def __init__(self, coefficients):
self.name = np.poly1d(coefficients)

def Coefficients(self):
return self.c

def evaluation(self, x):
return self(x)

def mul(self, a):
return np.polymul(self, a)

def sum(self, a):

def subtraction(self, a):
return np.polysub(self, a)

def __str__(self):
return str(self.name)

def differentiate(self):
return np.polyder(self)

def integrate(self):
return np.polyint(self)

a= Polynomials([2,5,7,8])
b = Polynomials([3,5,7,1])
print (a)
print(b)
#print(a(10))
c = a.mul(b)
#print(c)
``````

``````line 14, in mul
return np.polymul(self, a)
File "<__array_function__ internals>", line 6, in polymul
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/numpy/lib/polynomial.py", line 904, in polymul
a1, a2 = poly1d(a1), poly1d(a2)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-               packages/numpy/lib/polynomial.py", line 1155, in __init__
self._variable = c_or_r._variable
AttributeError: 'Polynomials' object has no attribute '_variable'
``````

## 1 个回复1

### #1楼 票数：0 已采纳

``````class Polynomials (np.poly1d):
def __init__(self, coefficients):
super().__init__(coefficients)
self.name = np.poly1d(coefficients)

# ...
``````

1回复

1回复

1回复

1回复

1回复

1回复

1回复

4回复