(* 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
(* 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
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)
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;;].) *)