F # adding polynomials recursively

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

For example, 2x ^ 4 + 3x ^ 2 + x + 5 equals [(2,0,4), (3,0,2), (1,0,1), (5,0,0)]

All polynomials are correctly structured (terms with the same degree are not repeated, without terms with zero coefficients, if it is not a zero polynomial, terms that are sorted in descending order of the exponent without an empty input list).

I have problems with 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

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 is the correct answer

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

, , . . , :

// 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)]

. , . .

,

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

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]

, . , polyadd . :

// 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]
+4

-:

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

:

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

float +, int , float * int :

> 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)]
+2

Source: https://habr.com/ru/post/1628607/


All Articles