From 92d86ce1bb28d383a8eed63d50e58451a0636e0c Mon Sep 17 00:00:00 2001 From: Max Horn Date: Thu, 5 Dec 2024 20:30:17 +0100 Subject: [PATCH] Add some convert method to/from QQBarFieldElem --- src/calcium/qqbar.jl | 11 +++++++++++ test/calcium/qqbar-test.jl | 31 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/calcium/qqbar.jl b/src/calcium/qqbar.jl index 30bc1e75e..7998915b6 100644 --- a/src/calcium/qqbar.jl +++ b/src/calcium/qqbar.jl @@ -1414,6 +1414,17 @@ function (::Type{Float64})(a::QQBarFieldElem) return Float64(x) end +function (::Type{T})(a::QQBarFieldElem) where {T <: Integer} + return T(ZZRingElem(a)) +end + +function (::Type{T})(a::QQBarFieldElem) where {T <: Rational} + return T(QQFieldElem(a)) +end + +convert(::Type{QQBarFieldElem}, a::RationalUnion) = QQBarFieldElem(a) +convert(::Type{T}, a::QQBarFieldElem) where {T <: RationalUnion} = T(a) + ############################################################################### # # Unsafe functions diff --git a/test/calcium/qqbar-test.jl b/test/calcium/qqbar-test.jl index d435c5a14..1590fd1c2 100644 --- a/test/calcium/qqbar-test.jl +++ b/test/calcium/qqbar-test.jl @@ -418,4 +418,35 @@ end x = sqrt(R(-2)) @test_throws InexactError Float64(x) @test isapprox(ComplexF64(x),sqrt(complex(-2))) + + + x = R(2) + for T in [Int16, UInt32, Int, BigInt, Rational{Int}, Rational{BigInt}, ZZRingElem, QQFieldElem] + y = convert(T, x) + @test y::T == 2 + + y = T(x) + @test y::T == 2 + + z = convert(QQBarFieldElem, y) + @test z::QQBarFieldElem == x + + z = QQBarFieldElem(y) + @test z::QQBarFieldElem == x + end + + x = R(2//3) + for T in [Rational{Int}, Rational{BigInt}, QQFieldElem] + y = convert(T, x) + @test y::T == 2//3 + + y = T(x) + @test y::T == 2//3 + + z = convert(QQBarFieldElem, y) + @test z::QQBarFieldElem == x + + z = QQBarFieldElem(y) + @test z::QQBarFieldElem == x + end end