ocaml-tplib/ocaml-tplib-mlgmpidl.patch

154 lines
3.9 KiB
Diff
Raw Normal View History

--- src/numeric_plugins/mlgmp_plugin.ml.orig 2013-02-14 04:08:25.000000000 -0700
+++ src/numeric_plugins/mlgmp_plugin.ml 2017-12-03 14:00:36.471566987 -0700
@@ -19,48 +19,69 @@
*)
-open Gmp
-
-let bits_of_string s =
- let s' = String.sub s 2 ((String.length s)-2) in (* remove prefix "0b" *)
- Z.from_string_base ~base:2 s'
+let randobj = Gmp_random.init_default ();
module Mlgmp_int =
struct
- type t = Z.t
+ type t = Mpz.t
- let zero = Z.of_int 0
- let max x y = if Z.cmp x y >= 0 then x else y
- let min x y = if Z.cmp x y >= 0 then y else x
- let add = Z.add
- let neg = Z.neg
- let mul = Z.mul
- let div = Z.cdiv_q
- let pow = Z.pow_ui
- let compare = Z.cmp
+ let zero = Mpz.of_int 0
+ let max x y = if Mpz.cmp x y >= 0 then x else y
+ let min x y = if Mpz.cmp x y >= 0 then y else x
+ let add x y =
+ let res = Mpz.init () in
+ Mpz.add res x y;
+ res
+ let neg x =
+ let res = Mpz.init () in
+ Mpz.neg res x;
+ res
+ let mul x y =
+ let res = Mpz.init () in
+ Mpz.mul res x y;
+ res
+ let div x y =
+ let res = Mpz.init () in
+ Mpz.cdiv_q res x y;
+ res
+ let pow x y =
+ let res = Mpz.init () in
+ Mpz.pow_ui res x y;
+ res
+ let compare = Mpz.cmp
let random n =
- bits_of_string (Random_generator.to_string n)
- let of_int = Z.from_int
- let of_string x =
- try
- Z.from_string_base ~base:10 x
- with Invalid_argument _ ->
- raise (Failure ("Mpzf.of_string: "^x^" is not well-formatted"))
- let to_string = Z.to_string_base ~base:10
+ let res = Mpz.init () in
+ Gmp_random.Mpz.urandomb res randobj n;
+ res
+ let of_int = Mpz.of_int
+ let of_string = Mpz.of_string
+ let to_string = Mpz.get_str ~base:10
end
module Mlgmp_rat =
struct
- type t = Q.t
+ type t = Mpq.t
- let zero = Q.from_int 0
- let one = Q.from_int 1
- let max x y = if Q.cmp x y >= 0 then x else y
- let min x y = if Q.cmp x y >= 0 then y else x
- let add = Q.add
- let neg = Q.neg
- let mul = Q.mul
- let div = Q.div
+ let zero = Mpq.of_int 0
+ let one = Mpq.of_int 1
+ let max x y = if Mpq.cmp x y >= 0 then x else y
+ let min x y = if Mpq.cmp x y >= 0 then y else x
+ let add x y =
+ let res = Mpq.init () in
+ Mpq.add res x y;
+ res
+ let neg x =
+ let res = Mpq.init () in
+ Mpq.neg res x;
+ res
+ let mul x y =
+ let res = Mpq.init () in
+ Mpq.mul res x y;
+ res
+ let div x y =
+ let res = Mpq.init () in
+ Mpq.div res x y;
+ res
let pow x n =
let rec pow_aux x n =
@@ -68,31 +89,29 @@ struct
if n = 0 then one
else
let y = pow_aux x (n/2) in
- let z = mul y y in
- if n mod 2 = 0 then
- z
- else
- mul x z
+ let z = Mpq.init () in
+ Mpq.mul z y y;
+ if n mod 2 != 0 then
+ Mpq.mul z x z;
+ z
in
if n >= 0 then
pow_aux x n
else
- pow_aux (Q.inv x) (-n)
+ pow_aux (Mpq.inv x x; x) (-n)
- let compare = Q.cmp
+ let compare = Mpq.cmp
let random n =
- let num = bits_of_string (Random_generator.to_string n) in
- let den = bits_of_string (Random_generator.to_string n) in
- Q.div (Q.from_z num) (Q.add Q.one (Q.from_z den))
- let of_int = Q.from_int
- let of_string x =
- try
- let i = String.index x '/' in
- let num = Z.from_string_base ~base:10 (String.sub x 0 i) in
- let den = Z.from_string_base ~base:10 (String.sub x (i+1) ((String.length x)-(i+1))) in
- Q.div (Q.from_z num) (Q.from_z den)
- with Not_found -> Q.from_z (Z.from_string_base ~base:10 x)
- let to_string = Q.to_string
+ let num = Mpz.init () in
+ let den = Mpz.init () in
+ Gmp_random.Mpz.urandomb num randobj n;
+ Gmp_random.Mpz.urandomb den randobj n;
+ let res = Mpq.init () in
+ Mpq.div res (Mpq.init_set_z num) (Mpq.add res one (Mpq.init_set_z den); res);
+ res
+ let of_int = Mpq.of_int
+ let of_string = Mpq.of_string
+ let to_string = Mpq.to_string
end
let _ =