Polynomial long division: Difference between revisions

m
a bit prettier
(polynomial long division in common lisp)
m (a bit prettier)
Line 514:
 
First define some utility operations on polynomials as lists (with highest power coefficient first).
<lang ocaml>let rec shift n l = if n <= 0 then l else shift (pred n) (l @ [0.0])
<lang ocaml>
let rec shift n l = if n <= 0 then l else shift (pred n) (l @ [0.0])
let rec pad n l = if n <= 0 then l else pad (pred n) (0.0 :: l)
let rec scale k l = List.map (( *.) k) l
let rec norm = function | 0.0 :: tl -> norm tl | x -> x
let deg l = List.length (norm l) - 1
 
let zip op p q =
let gapd = (List.length p) - (List.length q) in
if gap = 0 then List.map2 op (pad (-d) p) (pad d q else)</lang>
if gap > 0 then List.map2 op p (pad gap q)
else List.map2 op (pad (-gap) p) q
 
let sub = zip (-.)
let add = zip (+.)
</lang>
Then the main polynomial division function
<lang ocaml>let polydiv f g =
let polydiv f g =
let rec aux f s q =
let gapddif = (deg f) - (deg s) in
if gapddif < 0 then (q, f) else
let k = (List.hd f) /. (List.hd s) in
let q'ks = add q(List.map (scale( *.) k) (shift gapddif [1.0]s)) in
let fq' = normzip (List+.tl) (sub f (scale kq (shift gapddif s[k])))) in
and f' = norm (List.tl (zip (-.) f ks)) in
aux f' s q' in
aux (norm f) (norm g) []</lang>
</lang>
For output we need a pretty-printing function
<lang ocaml>let str_poly l =
let str_poly l =
let term v p = match (v, p) with
| ( _, 0) -> string_of_float v
Line 555 ⟶ 545:
| h :: t ->
if h = 0.0 then (terms t) else (term h (List.length t)) :: (terms t) in
String.concat " + " (terms l)</lang>
and then the example
</lang>
<lang ocaml>let _ =
and the test driver
<lang ocaml>
let _ =
let f = [1.0; -4.0; 6.0; 5.0; 3.0] and g = [1.0; 2.0; 1.0] in
let q, r = polydiv f g in
Printf.printf
begin
" (%s) div (%s)\ngives\nquotient:\t(%s)\nremainder:\t(%s)\n"
Printf.printf "f = %s\ng = %s\n\n" (str_poly f) (str_poly g);
Printf.printf(str_poly "qf) =(str_poly %s\nr = %s\n"g) (str_poly q) (str_poly r)</lang>
which gives the output:
end
</lang>
which gives the output:
<pre>
f = (x^4 + -4.*x^3 + 6.*x^2 + 5.*x + 3.) div (x^2 + 2.*x + 1.)
gives
g = quotient: (x^2 + 2-6.*x + 117.)
 
q = x^2 + remainder: (-623.*x + 17-14.)
r = -23.*x + -14.
</pre>
 
Anonymous user