diff --git a/polynome.ml b/polynome.ml index f8fbe3ea60ba7684613250feab0e0c33880f6116..b44e23c83bdffbdce368bb991fac4ed3395b48af 100644 --- a/polynome.ml +++ b/polynome.ml @@ -7,8 +7,9 @@ module type Coefficient = type coeff val print: coeff -> string val is_null: coeff -> bool - val add: coeff -> coeff -> coeff val equal: coeff -> coeff -> bool + val add: coeff -> coeff -> coeff + val mul: coeff -> coeff -> coeff end ;; @@ -40,50 +41,57 @@ module Polynome (C : Coefficient) (D : Degree) = let rec print p = match p with |Null -> "0" |NotNull (cc, dd, pp)-> - (C.print cc)^"X^"^(D.print dd)^"+"^(print pp) + (C.print cc)^"X^"^(D.print dd)^"+"^(print pp) let rec equal x y = match x with - |Null -> - (match y with - |Null -> true - |NotNull (_, _, _)-> false) + |Null -> (match y with + |Null -> true + |NotNull (_, _, _)-> false) |NotNull (cx, dx, xx)-> - (match y with - |Null -> false - |NotNull (cy, dy, yy) -> - if D.equal dx dy - then - if C.equal cx cy - then equal xx yy - else false - else false) + (match y with + |Null -> false + |NotNull (cy, dy, yy) -> + if D.equal dx dy + then + if C.equal cx cy + then equal xx yy + else false + else false + ) let rec add x y = match x with |Null -> y |NotNull (cx, dx, xx)-> - (match y with - |Null -> x - |NotNull (cy, dy, yy)-> - if D.is_bigger dy dx - then NotNull (cy, dy, add x yy) - else - if D.is_bigger dx dy - then NotNull (cx, dx, add xx yy) - else - let m = C.add cx cy in - if C.is_null m - then add xx yy - else NotNull (m, dx, add xx yy) - ) - (*let rec naive_mul x y = match x with + (match y with + |Null -> x + |NotNull (cy, dy, yy)-> + if D.is_bigger dy dx + then NotNull (cy, dy, add x yy) + else + if D.is_bigger dx dy + then NotNull (cx, dx, add xx yy) + else + let m = C.add cx cy in + if C.is_null m + then add xx yy + else NotNull (m, dx, add xx yy) + ) + (*private function for incrementing the degree of a polynome*) + let rec increment_degree d x = match x with + |Null -> Null + |NotNull (cx, dx, xx)-> NotNull (cx, (D.add d dx), (increment_degree d xx)) + (*private function in order to multiply all the coeffs of a pol*) + let rec multiply_coeffs c x = match x with + |Null -> Null + |NotNull (cx, dx, xx) -> + if C.is_null c then Null + else NotNull ((C.mul c cx), dx, (multiply_coeffs c xx)) + (*Naive multiplication of two polynomes*) + let rec naive_mul x y = match x with |Null -> Null - |NotNull -> (cx, dx, xx)-> - (match y with - |Null -> Null - |NotNull (cy, dy, yy)-> - match xx with - |Null - |NotNull - ) - *) - + |NotNull (cx, dx, xx) -> + (match y with + |Null -> Null + |NotNull (_, _, _)-> + add (increment_degree dx (multiply_coeffs cx y))(naive_mul xx y) + ) end ;;