简体   繁体   中英

F# adding polynomials recursively

I'm trying to write a function in F# that adds polynomials recursively. My polynomials can be represented as a list of tuples.

For example, 2x^4 + 3x^2 + x + 5 is equal to [(2.0,4);(3.0,2);(1.0,1);(5.0,0)]

All polynomials are properly structured (no repeated terms with the same degree, no terms with zero coefficients unless it is the zero polynomial, terms sorted by decreasing exponent no empty input list).

I'm having trouble doing this. Here is my code

type term = float * int
type poly = term list

let rec atp(t:term,p:poly):poly =
  match p with
  | [] -> []
  | (a, b) :: tail -> if snd t = b then (fst t + a, b) :: [] elif snd t > b then t :: [] else ([]) :: atp(t, tail)
(* val atp : t:term * p:poly -> poly *)

let rec addpolys(p1:poly,p2:poly):poly =
  match p1 with
  | [] -> []
  | (a,b) :: tail -> atp((a,b), p2) @ addpolys(tail, p2)

I have two polynomials

val p2 : poly = [(4.5, 7); (3.0, 4); (10.5, 3); (2.25, 2)]
val p1 : poly = [(3.0, 5); (2.0, 2); (7.0, 1); (1.5, 0)]

and when I call the function, my result is

val p4 : poly =
  [(4.5, 7); (3.0, 5); (3.0, 4); (3.0, 5); (10.5, 3); (3.0, 5); (4.25, 2)]

When the correct answer is

  [(4.5, 7); (3.0, 5); (3.0, 4); (10.5, 3); (4.25, 2); (7.0, 1); (1.5, 0)]

Unfortunately your code does not compile therefore it is difficult for me to understand your intentions. But I've got an own implementation for your problem. Maybe it will help you:

// addpoly:  (float * 'a) list -> (float * 'a) list -> (float * 'a) list
let rec addpoly p1 p2 =
    match (p1, p2) with
    | [], p2 -> p2
    | p1, [] -> p1
    | (a1, n1)::p1s, (a2, n2)::p2s -> 
        if   n1 < n2 then (a2,    n2) :: addpoly p1  p2s
        elif n1 > n2 then (a1,    n1) :: addpoly p1s p2
        else              (a1+a2, n1) :: addpoly p1s p2s

let p1 = [(3.0, 5); (2.0, 2); ( 7.0, 1);  (1.5, 0)]
let p2 = [(4.5, 7); (3.0, 4); (10.5, 3); (2.25, 2)]

let q = addpoly p1 p2
// val q : (float * int) list =
//   [(4.5, 7); (3.0, 5); (3.0, 4); (10.5, 3); (4.25, 2); (7.0, 1); (1.5, 0)]

I would like to make a little note. When you change the representation of the polynomials a little bit then you can simplify the implementation of your function. You can express a polynomial as a list of its coefficients.

For example when you have this polynomial

p1 = 5.0x^5 + 2.0x^2 + 7.0x

you can write it also like this

p1 = 1.5x^0 + 7.0x^1 + 2.0x^2 + 0.0x^3 + 0.0x^4 + 5.0x^5     

Therefore you are able to define the polynomial with this list:

let p1 = [1.5; 7.0; 2.0; 0.0; 0.0; 5.0]

Here are two functions which operates on the representation. polyval calculates the result for a given value and polyadd adds two polynomials. There implementation are rather simple:

// p1 = 1.5x^0 + 7.0x^1 + 2.0x^2 + 0.0x^3 + 0.0x^4 + 5.0x^5
let p1 = [1.5; 7.0; 2.0; 0.0; 0.0; 5.0]

// p2 = 0.0x^0 + 0.0x^1 + 2.25x^2 + 10.5x^3 + 3.0x^4 + 0.0x^5 + 0.0x^6 + 4.5x^7
let p2 = [0.0; 0.0; 2.25; 10.5; 3.0; 0.0; 0.0; 4.5]

// polyval: float list -> float -> float
let rec polyval ps x =
    match ps with
    | []    -> 0.0
    | p::ps -> p + x * (polyval ps x)

// polyadd: float int -> float int -> float int
let rec polyadd ps qs =
    match (ps, qs) with
    | [], ys       -> ys
    | xs, []       -> xs
    | x::xs, y::ys -> (x+y)::polyadd xs ys

let v = polyval p1 2.3
// val v : float = 349.99715

let p = polyadd p1 p2
// val p : float list = [1.5; 7.0; 4.25; 10.5; 3.0; 5.0; 0.0; 4.5]

Here's a completely generic, tail-recursive implementation:

let inline addPolys xs ys =
    let rec imp acc = function
        | (coeffx, degx)::xt, (coeffy, degy)::yt when degx = degy ->
            imp ((coeffx + coeffy, degx)::acc) (xt, yt)
        | (coeffx, degx)::xt, (coeffy, degy)::yt when degx > degy ->
            imp ((coeffx, degx)::acc) (xt, (coeffy, degy)::yt)
        | xs, yh::yt -> imp (yh::acc) (xs, yt)
        | xh::xt, [] -> imp (xh::acc) (xt, [])
        | [], yh::yt -> imp (yh::acc) ([], yt)
        | [], [] -> acc
    imp [] (xs, ys) |> List.rev

It has the type:

xs:( ^a * 'b) list -> ys:( ^a * 'b) list -> ( ^a * 'b) list
    when  ^a : (static member ( + ) :  ^a *  ^a ->  ^a) and 'b : comparison

Since float has the member + , and int supports comparison, the type float * int matches these generic constraints:

> addPolys p1 p2;;
val it : (float * int) list =
  [(4.5, 7); (3.0, 5); (3.0, 4); (10.5, 3); (4.25, 2); (7.0, 1); (1.5, 0)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM