-
Notifications
You must be signed in to change notification settings - Fork 0
/
Polynomium.py
61 lines (45 loc) · 1.23 KB
/
Polynomium.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from Vector import Vector
class Polynomium(Vector):
def __init__(P,Q=[]):
P.__Alloc__(len(Q))
for i in range(len(Q)):
P[i]=1.0*Q[i]
def __add__(v,w):
w=Polynomium(v)
for i in range(len(v)):
w[i]+=w[i]
return w
def __sub__(v,w):
w=Polynomium(v)
for i in range(len(v)):
w[i]-=w[i]
return w
def __mul__(P,Q):
PP=Polynomium()
PP.__Alloc__( len(P)+len(Q)-1 )
for i in range(len(P)):
for j in range(len(Q)):
PP[ i+j ]+=P[i]*Q[j]
return PP
def __str__(P):
text=""
for i in range( len(P) ):
if (abs(P[i])>0.0):
if (i==0):
text+=str(P[i])+"aa"
else:
if (P[i]>0):
text+="+"
else:
text+="-"
text+=str(abs(P[i]))+"x"+"bb"
if (i>1):
text+="^"+str(i)+"cc"
return text
P=Polynomium([1,-2,1])
Q=Polynomium([1,1])
print "P=",P
print "Q=",Q
R=P-Q
print "P+Q=",P+Q
print "P-Q=",P-Q