Skip to content

Commit

Permalink
More conversions between QQFieldElem and Rational (#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin authored Nov 2, 2023
1 parent 11fcb90 commit 1fcc3b5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/flint/fmpq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,8 @@ end

convert(::Type{QQFieldElem}, a::Integer) = QQFieldElem(a)

convert(::Type{QQFieldElem}, a::Rational) = QQFieldElem(a)

convert(::Type{QQFieldElem}, a::ZZRingElem) = QQFieldElem(a)

Base.promote_rule(::Type{QQFieldElem}, ::Type{T}) where {T <: Integer} = QQFieldElem
Expand All @@ -1178,15 +1180,13 @@ promote_rule(::Type{QQFieldElem}, ::Type{Rational{T}} where {T <: Integer}) = QQ
Base.promote_rule(::Type{QQFieldElem}, ::Type{Rational{T}}) where {T <: Integer} = QQFieldElem

function Base.convert(::Type{Rational{T}}, a::QQFieldElem) where T <: Integer
return Rational{T}(convert(T, numerator(a)), convert(T, denominator(a)))
return Rational{T}(a)
end

function Base.convert(::Type{QQFieldElem}, a::Rational{T}) where T <: Integer
return convert(ZZRingElem, numerator(a))//convert(ZZRingElem, denominator(a))
function Base.Rational{T}(z::QQFieldElem) where T <: Integer
return Rational{T}(T(numerator(z)), T(denominator(z)))
end

convert(::Type{Rational{BigInt}}, a::QQFieldElem) = Rational(a)

function Base.Rational{BigInt}(z::QQFieldElem)
r = Rational{BigInt}(0)
ccall((:fmpq_get_mpz_frac, libflint), Nothing,
Expand All @@ -1196,8 +1196,8 @@ end

Rational(z::QQFieldElem) = Rational{BigInt}(z)

function Base.Rational{BigInt}(z::ZZRingElem)
return Rational{BigInt}(BigInt(z))
function Base.Rational{T}(z::ZZRingElem) where T <: Integer
return Rational{T}(T(z))
end

Rational(z::ZZRingElem) = Rational{BigInt}(z)
Expand Down
10 changes: 10 additions & 0 deletions test/flint/fmpq-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,19 @@ end
end

@testset "QQFieldElem.conversions" begin
@test convert(Rational{Int}, QQFieldElem(3, 7)) == 3//7
@test convert(Rational{BigInt}, QQFieldElem(3, 7)) == 3//7

@test convert(QQFieldElem, 3) == QQFieldElem(3)
@test convert(QQFieldElem, 3//7) == QQFieldElem(3, 7)

@test Rational(ZZRingElem(12)) == 12
@test Rational{Int}(ZZRingElem(12)) == 12
@test Rational{BigInt}(ZZRingElem(12)) == 12

@test Rational(QQFieldElem(3, 7)) == 3//7
@test Rational{Int}(QQFieldElem(3, 7)) == 3//7
@test Rational{BigInt}(QQFieldElem(3, 7)) == 3//7

@test ZZ(QQFieldElem(3)) isa ZZRingElem
@test_throws Exception ZZ(QQFieldElem(3, 2))
Expand Down

0 comments on commit 1fcc3b5

Please sign in to comment.