tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
|
|
@ -0,0 +1,34 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from itertools import izip
|
||||
from math import fabs
|
||||
|
||||
def degree(poly):
|
||||
while poly and poly[-1] == 0:
|
||||
poly.pop() # normalize
|
||||
return len(poly)-1
|
||||
|
||||
def poly_div(N, D):
|
||||
dD = degree(D)
|
||||
dN = degree(N)
|
||||
if dD < 0: raise ZeroDivisionError
|
||||
if dN >= dD:
|
||||
q = [0] * dN
|
||||
while dN >= dD:
|
||||
d = [0]*(dN - dD) + D
|
||||
mult = q[dN - dD] = N[-1] / float(d[-1])
|
||||
d = [coeff*mult for coeff in d]
|
||||
N = [fabs ( coeffN - coeffd ) for coeffN, coeffd in izip(N, d)]
|
||||
dN = degree(N)
|
||||
r = N
|
||||
else:
|
||||
q = [0]
|
||||
r = N
|
||||
return q, r
|
||||
|
||||
if __name__ == '__main__':
|
||||
print "POLYNOMIAL LONG DIVISION"
|
||||
N = [-42, 0, -12, 1]
|
||||
D = [-3, 1, 0, 0]
|
||||
print " %s / %s =" % (N,D),
|
||||
print " %s remainder %s" % poly_div(N, D)
|
||||
Loading…
Add table
Add a link
Reference in a new issue