From ef59eee1385855715907ff77c96b50ed1d5fc61c Mon Sep 17 00:00:00 2001 From: Johannes Middeke Date: Fri, 6 May 2016 14:40:13 -0400 Subject: [PATCH] Documentation for polynomials now mentions Zarith; added LaTeX and ASCII printer for polynomials. --- polys_over_GF2.ml | 42 +++++++++++++++++++++++++++++++++++------- test.ml | 1 + 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/polys_over_GF2.ml b/polys_over_GF2.ml index b215fe4..14fb8d8 100644 --- a/polys_over_GF2.ml +++ b/polys_over_GF2.ml @@ -1,10 +1,12 @@ (* This module contains an implementation of polynomials over GF(2) - using integers (ie, bit-vectors) to represent the polynomials. Note - that this restricts the degrees to at most 63. *) + using integers (ie, bit-vectors) to represent the polynomials. We + use the Zarith package for big integers (the module [Z]) because + otherwise we would be restricted to polynomials of degree at most + 63. *) -(* As mentioned above, we use integers to represent polynomials. The - [n]-th bit will correspond to the [n]-th coefficient. We define - some constants, too. *) +(* As mentioned above, we use (big) integers to represent + polynomials. The [n]-th bit will correspond to the [n]-th + coefficient. We define some constants, too. *) type t = Polynom of Z.t @@ -19,7 +21,7 @@ let eql (Polynom a) (Polynom b) = Z.equal a b (* This function converts polynomials to strings using Maple notation. *) -let show (Polynom a) = +let string_of_polynomial exponent (Polynom a) = let rec loop accu a n = if Z.equal a Z.zero then @@ -30,7 +32,7 @@ let show (Polynom a) = let accu' = match (Z.to_int (Z.logand a Z.one), n) with | (1,0) -> "1" :: accu | (1,1) -> "x" :: accu - | (1,n) -> ("x^" ^ string_of_int n) :: accu + | (1,n) -> ("x" ^ exponent n) :: accu | _ -> accu in loop accu' (Z.shift_right a 1) (n+1) @@ -38,6 +40,32 @@ let show (Polynom a) = loop [] a 0 + +(* These functions converts polynomials to strings using Maple, LaTeX + and ASCII notation. *) + +let show = string_of_polynomial (fun i -> "^" ^ string_of_int i) +and show_latex = + let expt n = + if n < 10 + then "^" ^ string_of_int n + else "^{" ^ string_of_int n ^ "}" + in + string_of_polynomial expt +and show_ascii = + let digit = function + | 0 -> "⁰" | 1 -> "¹" | 2 -> "²" | 3 -> "³" | 4 -> "⁴" + | 5 -> "⁵" | 6 -> "⁶" | 7 -> "⁷" | 8 -> "⁸" | 9 -> "⁹" + | _ -> failwith "Digit too large!" + in + let rec ascii_expt x = + if x < 10 + then digit x + else ascii_expt (x / 10) ^ digit (x mod 10) + in + string_of_polynomial ascii_expt + + (* Use the conversion above, we can define a printer for polynomials at the toplevel. (Install with [#install_printer fprintf;;].) *) diff --git a/test.ml b/test.ml index 2db4727..ae28460 100644 --- a/test.ml +++ b/test.ml @@ -193,3 +193,4 @@ let main () = let () = main () + -- 2.1.4