Skip to content

Commit

Permalink
Export denominator! and numerator! (#1959)
Browse files Browse the repository at this point in the history
Also allow passing pointers into `numerator!`, and change both to not
use `ccall`. This makes it a little bit faster.

Before:

    julia> using Nemo; z = ZZRingElem(); a = QQFieldElem(2, 3);

    julia> @Btime Nemo.numerator!($z, $a);
      5.208 ns (0 allocations: 0 bytes)

    julia> @Btime Nemo.denominator!($z, $a);
      5.166 ns (0 allocations: 0 bytes)

    julia> a = a^100;

    julia> @Btime Nemo.numerator!($z, $a);
      7.083 ns (0 allocations: 0 bytes)

    julia> @Btime Nemo.denominator!($z, $a);
      6.750 ns (0 allocations: 0 bytes)

After:

    julia> using Nemo; z = ZZRingElem(); a = QQFieldElem(2, 3);

    julia> @Btime numerator!($z, $a);
      4.000 ns (0 allocations: 0 bytes)

    julia> @Btime denominator!($z, $a);
      4.000 ns (0 allocations: 0 bytes)

    julia> a = a^100;

    julia> @Btime numerator!($z, $a);
      5.875 ns (0 allocations: 0 bytes)

    julia> @Btime denominator!($z, $a);
      5.625 ns (0 allocations: 0 bytes)
  • Loading branch information
fingolfin authored Dec 5, 2024
1 parent 2d12643 commit 3490994
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export degree_fmpz
export degrees
export degrees_fit_int
export degrees_fmpz
#export denominator! # not exported for now to avoid clash with Hecke
export denominator!
export derivative
export det
export det_divisor
Expand Down Expand Up @@ -464,7 +464,7 @@ export nullspace_right_rational
export number_field
export number_of_partitions
export numpart
#export numerator! # not exported for now to avoid clash with Hecke
export numerator!
export oct
export one
export one!
Expand Down
10 changes: 7 additions & 3 deletions src/flint/fmpq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1059,13 +1059,17 @@ function set!(c::QQFieldElemOrPtr, a::Union{Integer,ZZRingElemOrPtr})
return c
end

function numerator!(z::ZZRingElem, y::QQFieldElem)
@ccall libflint.fmpq_numerator(z::Ref{ZZRingElem}, y::Ref{QQFieldElem})::Nothing
function numerator!(z::ZZRingElemOrPtr, y::QQFieldElemOrPtr)
GC.@preserve y begin
set!(z, _num_ptr(y))
end
return z
end

function denominator!(z::ZZRingElemOrPtr, y::QQFieldElemOrPtr)
@ccall libflint.fmpq_denominator(z::Ref{ZZRingElem}, y::Ref{QQFieldElem})::Nothing
GC.@preserve y begin
set!(z, _den_ptr(y))
end
return z
end

Expand Down
8 changes: 8 additions & 0 deletions test/flint/fmpq-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ end

@test denominator(QQFieldElem(2, 3)) == 3

z = ZZRingElem()
@test numerator!(z, QQFieldElem(2, 3)) == 2
@test z == 2

z = ZZRingElem()
@test denominator!(z, QQFieldElem(2, 3)) == 3
@test z == 3

@test characteristic(R) == 0

@test nbits(QQFieldElem(12, 1)) == 5
Expand Down

0 comments on commit 3490994

Please sign in to comment.