aboutsummaryrefslogtreecommitdiff
path: root/lib/poly.py
diff options
context:
space:
mode:
authorSkip Montanaro <[email protected]>2021-02-16 20:14:16 -0600
committerSkip Montanaro <[email protected]>2021-02-16 20:14:16 -0600
commitc2587c76f1b416cdbecb979e54941933246bf856 (patch)
treebb61ee9128075ce22af4eafa232f13c2e5a07896 /lib/poly.py
parentd90761a005b24018ae237bf551515772a1de656f (diff)
downloadpython-0.9.1-patched-QoL-c2587c76f1b416cdbecb979e54941933246bf856.tar.xz
python-0.9.1-patched-QoL-c2587c76f1b416cdbecb979e54941933246bf856.zip
starting over
Diffstat (limited to 'lib/poly.py')
-rw-r--r--lib/poly.py66
1 files changed, 33 insertions, 33 deletions
diff --git a/lib/poly.py b/lib/poly.py
index a7c24ec..abac4c8 100644
--- a/lib/poly.py
+++ b/lib/poly.py
@@ -6,50 +6,50 @@
# taken out by normalize().
def normalize(p): # Strip unnecessary zero coefficients
- n = len(p)
- while p:
- if p[n-1]: return p[:n]
- n = n-1
- return []
+ n = len(p)
+ while p:
+ if p[n-1]: return p[:n]
+ n = n-1
+ return []
def plus(a, b):
- if len(a) < len(b): a, b = b, a # make sure a is the longest
- res = a[:] # make a copy
- for i in range(len(b)):
- res[i] = res[i] + b[i]
- return normalize(res)
+ if len(a) < len(b): a, b = b, a # make sure a is the longest
+ res = a[:] # make a copy
+ for i in range(len(b)):
+ res[i] = res[i] + b[i]
+ return normalize(res)
def minus(a, b):
- if len(a) < len(b): a, b = b, a # make sure a is the longest
- res = a[:] # make a copy
- for i in range(len(b)):
- res[i] = res[i] - b[i]
- return normalize(res)
+ if len(a) < len(b): a, b = b, a # make sure a is the longest
+ res = a[:] # make a copy
+ for i in range(len(b)):
+ res[i] = res[i] - b[i]
+ return normalize(res)
def one(power, coeff): # Representation of coeff * x**power
- res = []
- for i in range(power): res.append(0)
- return res + [coeff]
+ res = []
+ for i in range(power): res.append(0)
+ return res + [coeff]
def times(a, b):
- res = []
- for i in range(len(a)):
- for j in range(len(b)):
- res = plus(res, one(i+j, a[i]*b[j]))
- return res
+ res = []
+ for i in range(len(a)):
+ for j in range(len(b)):
+ res = plus(res, one(i+j, a[i]*b[j]))
+ return res
def power(a, n): # Raise polynomial a to the positive integral power n
- if n = 0: return [1]
- if n = 1: return a
- if n/2*2 = n:
- b = power(a, n/2)
- return times(b, b)
- return times(power(a, n-1), a)
+ if n = 0: return [1]
+ if n = 1: return a
+ if n/2*2 = n:
+ b = power(a, n/2)
+ return times(b, b)
+ return times(power(a, n-1), a)
def der(a): # First derivative
- res = a[1:]
- for i in range(len(res)):
- res[i] = res[i] * (i+1)
- return res
+ res = a[1:]
+ for i in range(len(res)):
+ res[i] = res[i] * (i+1)
+ return res
# Computing a primitive function would require rational arithmetic...