Polynomial Long Division
J, 94
f=:>@(0&{)
d=:0{[%~0{[:f]
D=:4 :'x((1}.([:f])-((#@]{.[)f)*d);([:>1{]),d)^:(>:((#f y)-(#x)))y'
eg.
(1 0 _5) D (12 _5 3 _1;'')
63 _26 | 12 _5
Explanation of some snippets, given that a: (12 -5 3 -1) and b: (1 0 -5)
length of a:
#a
4
make a and b same order by appending zeroes to b:
(#a) {. b
1 0 -5 0
divide higher powers (first elements) of a, b:
(0{a) % (0{b)
12
multiply b by that and subtract it from a:
a - 12*b
12 0 _60
repeat n times b = f(a,b):
a f^:n b
Python 2, 260 258 257 255 bytes
exec'''def d(p,q):
R=range;D=len(p);F=len(q)-1;d=q[0];q=[q[i]/-d@R(1,F+1)];r=[0@R(D)];a=[[0@R(F)]@R(D)]
@R(D):
p[i]/=d;r[i]=sum(a[i])+p[i]
for j in R(F):
if i<D-F:a[i+j+1][F-j-1]=r[i]*q[j]
return r[:D-F],[d*i@r[D-F:]]'''.replace('@',' for i in ')
This executes:
def d(p,q):
R=range;D=len(p);F=len(q)-1;d=q[0];q=[q[i]/-d for i in R(1,F+1)];r=[0 for i in R(D)];a=[[0 for i in R(F)] for i in R(D)]
for i in R(D):
p[i]/=d;r[i]=sum(a[i])+p[i]
for j in R(F):
if i<D-F:a[i+j+1][F-j-1]=r[i]*q[j]
return r[:D-F],[d*i for i in r[D-F:]]
Use like so:
>>>d([12., -5., 3., -1.],[1.,0.,-5.])
([12.0, -5.0], [63.0, -26.0])
Haskell, 126
For a start:
l s _ 0=s
l(x:s)(y:t)n=x/y:l(zipWith(-)s$map(*(x/y))t++repeat 0)(y:t)(n-1)
d s t=splitAt n$l s t n where n=length s-length t+1
Sample use:
*Main> d [12, -5, 3, -1] [1, 0, -5]
([12.0,-5.0],[63.0,-26.0])