package flint

  1. Overview
  2. Docs

Source file flint.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
open Ctypes
open C.Type
open C.Function

module FMPZ = struct
  type t = fmpz_t

  module C = struct
    let fmpz_t = C.Type.fmpz_t

    let mk_fmpz () : fmpz_t =
      allocate_n ~count:1 ~finalise:C.Function.fmpz_clear fmpz
  end

  module External = struct
    module CI = Cstubs_internals

    type 'a fatptr = (Obj.t option, 'a) CI.fatptr

    external extern_z_of_fmpz : fmpz fatptr -> Z.t
      = "flint_stubs_utils_z_of_fmpz"

    let z_of_fmpz (CI.CPointer p) = extern_z_of_fmpz p

    external extern_fmpz_of_z : fmpz fatptr -> Z.t -> unit
      = "flint_stubs_utils_fmpz_of_z"

    let fmpz_of_z z : fmpz_t =
      let (CI.CPointer p as f) = C.mk_fmpz () in
      extern_fmpz_of_z p z;
      f

    external flint_stubs_utils_to_string : fmpz fatptr -> string
      = "flint_stubs_utils_to_string"

    let to_string (CI.CPointer p) = flint_stubs_utils_to_string p
  end

  let to_z = External.z_of_fmpz
  let of_z = External.fmpz_of_z
  let to_string = External.to_string
  let pp fmt f = Format.pp_print_string fmt (to_string f)

  let of_int i =
    let f = C.mk_fmpz () in
    fmpz_init f;
    fmpz_set_si f (Signed.Long.of_int i);
    f
end

module FMPQ = struct
  type t = fmpq_t

  module C = struct
    let fmpq_t = C.Type.fmpq_t

    let mk_fmpq () : fmpq_t =
      allocate_n ~count:1 ~finalise:C.Function.fmpq_clear fmpq
  end

  let mk num den =
    let q = C.mk_fmpq () in
    fmpq_set_fmpz_frac q num den;
    q

  let of_q q = mk (FMPZ.of_z q.Q.num) (FMPZ.of_z q.Q.den)
  let to_q q = Q.make (FMPZ.to_z (q |-> FMPQ.num)) (FMPZ.to_z (q |-> FMPQ.den))
end