Skip to content

Commit

Permalink
update snf
Browse files Browse the repository at this point in the history
  • Loading branch information
jtcoolen committed Oct 29, 2024
1 parent d534a06 commit d3bd187
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
7 changes: 7 additions & 0 deletions examples/number_fields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ let () =
Printf.eprintf "%b\n"
Number_field.(equal a (add gaussian_integers (mul gaussian_integers b q) r))

(* Smith normal form *)
let m = gp_read_str "[1, 0, x; 0, x, 0; 0,0,1+x]"
let d, u, v = Number_field.smith_normal_form gaussian_integers m

let _ =
Printf.eprintf "res=%s %s\n" (gentostr Matrix.(mul (mul u m) v)) (gentostr d)

let _nf2 =
Number_field.create (Option.get @@ Polynomial.of_string "x^4-2" |> inj_rat)

Expand Down
21 changes: 20 additions & 1 deletion src/pari.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type integer_mod = Integer_mod
type finite_field = Finite_field
type number_field = Number_field
type 'a elliptic_curve = Elliptic_curve of 'a
type 'a matrix = private Matrix of 'a

let register_gc v =
Gc.finalise_last (fun () -> pari_free Ctypes.(coerce gen (ptr void) v)) v
Expand Down Expand Up @@ -557,7 +558,23 @@ module Number_field = struct
let nfmulmodpr = nfmulmodpr
let nfpowmodpr = nfpowmodpr
let nfreduce = nfreduce
let nfsnf = nfsnf

let smith_normal_form nf sipm =
(*square integral invertible pseudo-matrix sipm*)
let d, _ = Matrix.dimensions sipm in
let sipm =
Vector.of_array
[|
sipm;
Vector.init d ~f:(fun _ -> Integer.of_int 1);
Vector.init d ~f:(fun _ -> Integer.of_int 1);
|]
in
let res = nfsnf0 nf sipm Signed.Long.one in
( Vector.(res.%[1]),
Vector.(res.%[2]) |> matbasistoalg nf,
Vector.(res.%[3]) |> matbasistoalg nf )

let nfsnf0 = nfsnf0
let nfsolvemodpr = nfsolvemodpr
let nfsubfields = nfsubfields
Expand Down Expand Up @@ -914,6 +931,8 @@ module Elliptic_curve = struct
let ell_is_inf = ell_is_inf
end

let isdiagonal e = isdiagonal e = 1

let factor e =
let m = factor e in
let n_rows, n_cols = Matrix.dimensions m in
Expand Down
30 changes: 27 additions & 3 deletions src/pari.mli
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,10 @@ module Vector : sig
end
end

type 'a matrix = private Matrix of 'a

module Matrix : sig
type 'a t constraint 'a = 'b ty
type 'a t = 'a matrix ty constraint 'a = 'b ty

val dimensions : 'a t -> int * int
val id : int -> Integer.t t
Expand Down Expand Up @@ -742,7 +744,29 @@ module Number_field : sig
val nfmulmodpr : 'a ty -> 'a ty -> 'a ty -> 'a ty -> 'a ty
val nfpowmodpr : 'a ty -> 'a ty -> 'a ty -> 'a ty -> 'a ty
val nfreduce : 'a ty -> 'a ty -> 'a ty -> 'a ty
val nfsnf : 'a ty -> 'a ty -> 'a ty

val smith_normal_form :
t -> elt Matrix.t -> elt Matrix.t * elt Matrix.t * elt Matrix.t
(**
{@ocaml[
# let gaussian_integers =
(* Gaussian integers: the ring Z[i] (here we work in the field Q(i)) *)
Number_field.create
(Polynomial.create [| Rational.of_int 1; Rational.of_int 0; Rational.of_int 1 |]);;
val gaussian_integers : Number_field.t = <abstr>
# let m = gp_read_str "[1, 0, x; 0, x, 0; 0,0,1+x]"
val m : '_weak1 ty = <abstr>
# let _, u, v = Number_field.smith_normal_form gaussian_integers m
val u : Number_field.elt Matrix.t = <abstr>
val v : Number_field.elt Matrix.t = <abstr>
# let d = Matrix.(mul (mul u m) v);;
val d : Number_field.elt Matrix.t = <abstr>
# gentostr d
- : string = "[1, 0, 0; 0, 1, 0; 0, 0, 1]"
# isdiagonal d
- : bool = true
]} *)

val nfsnf0 : 'a ty -> 'a ty -> Signed.long -> 'a ty
val nfsolvemodpr : 'a ty -> 'a ty -> 'a ty -> 'a ty -> 'a ty
val nfsubfields : 'a ty -> Signed.long -> 'a ty
Expand Down Expand Up @@ -3639,7 +3663,7 @@ val rgv_zc_mul : 'a ty -> 'a ty -> 'a ty
val rgv_zm_mul : 'a ty -> 'a ty -> 'a ty
val rgx_rgm_eval : 'a ty -> 'a ty -> 'a ty
val rgx_rgmv_eval : 'a ty -> 'a ty -> 'a ty
val isdiagonal : 'a ty -> int
val isdiagonal : 'a ty -> bool
val scalarcol : 'a ty -> Signed.long -> 'a ty
val scalarcol_shallow : 'a ty -> Signed.long -> 'a ty
val scalarmat : 'a ty -> Signed.long -> 'a ty
Expand Down

0 comments on commit d3bd187

Please sign in to comment.