1 # module 'poly' -- Polynomials
3 # A polynomial is represented by a list of coefficients, e.g.,
4 # [1, 10, 5] represents 1*x**0 + 10*x**1 + 5*x**2 (or 1 + 10x + 5x**2).
5 # There is no way to suppress internal zeros; trailing zeros are
6 # taken out by normalize().
8 def normalize(p
): # Strip unnecessary zero coefficients
11 if p
[n
-1]: return p
[:n
]
16 if len(a
) < len(b
): a
, b
= b
, a
# make sure a is the longest
17 res
= a
[:] # make a copy
18 for i
in range(len(b
)):
19 res
[i
] = res
[i
] + b
[i
]
23 neg_b
= map(lambda x
: -x
, b
[:])
26 def one(power
, coeff
): # Representation of coeff * x**power
28 for i
in range(power
): res
.append(0)
33 for i
in range(len(a
)):
34 for j
in range(len(b
)):
35 res
= plus(res
, one(i
+j
, a
[i
]*b
[j
]))
38 def power(a
, n
): # Raise polynomial a to the positive integral power n
44 return times(power(a
, n
-1), a
)
46 def der(a
): # First derivative
48 for i
in range(len(res
)):
49 res
[i
] = res
[i
] * (i
+1)
52 # Computing a primitive function would require rational arithmetic...