Skip to content

Add fraction_field_type and define fraction_field method for fields #2132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions src/Fraction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ function //(x::T, y::T) where {T <: RingElem}
iszero(y) && throw(DivideError())
g = gcd(x, y)
z = Generic.FracFieldElem{T}(divexact(x, g), divexact(y, g))
try
z.parent = Generic.FracDict[R]
catch
z.parent = Generic.fraction_field(R)
z.parent = get(Generic.FracDict, R) do
return Generic.fraction_field(R)
end
return z
end
Expand Down Expand Up @@ -826,6 +824,42 @@ function fraction_field(R::Ring; cached::Bool=true)
return Generic.fraction_field(R; cached=cached)
end

function fraction_field(F::Field; cached::Bool=true)
return F
end


@doc raw"""
fraction_field_type(a)

Return the type of the base ring of the given element, element type, parent or parent type $a$.

# Examples
```jldoctest
julia> R, x = polynomial_ring(ZZ, :x)
(Univariate polynomial ring in x over integers, x)

julia> fraction_field_type(R) == typeof(fraction_field(R))
true

julia> fraction_field_type(zero(R)) == typeof(fraction_field(R))
true

julia> fraction_field_type(typeof(R)) == typeof(fraction_field(R))
true

julia> fraction_field_type(typeof(zero(R))) == typeof(fraction_field(R))
true
```
"""
fraction_field_type(x) = fraction_field_type(typeof(x))
fraction_field_type(x::Type{<:RingElement}) = fraction_field_type(parent_type(x))
fraction_field_type(T::DataType) = throw(MethodError(fraction_field_type, (T,)))

fraction_field_type(::Type{T}) where {T <: Field} = T
fraction_field_type(::Type{T}) where {T <: Ring} = AbstractAlgebra.Generic.FracField{elem_type(T)}


@doc raw"""
factored_fraction_field(R::Ring; cached::Bool=true)

Expand Down
1 change: 1 addition & 0 deletions src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export find_pivot_popov
export finish
export fit!
export fraction_field
export fraction_field_type
export free_associative_algebra
export free_associative_algebra_type
export free_module
Expand Down
2 changes: 1 addition & 1 deletion src/generic/FunctionField.jl
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ end
# a numerator and denominator
function _rat_poly(p::Poly{RationalFunctionFieldElem{T, U}}, var=parent(p).S; cached::Bool=true) where {T <: FieldElement, U <: PolyRingElem}
K = base_ring(p)
R = base_ring(fraction_field(K))
R = base_ring(underlying_fraction_field(K))
S = elem_type(R)

par = PolyRing{S}(R, var, cached)
Expand Down
36 changes: 17 additions & 19 deletions src/generic/RationalFunctionField.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ parent(a::RationalFunctionFieldElem) = a.parent

data(x::RationalFunctionFieldElem{T, U}) where {T <: FieldElement, U <: Union{PolyRingElem, MPolyRingElem}} = x.d::FracFieldElem{U}

function fraction_field(a::RationalFunctionField{T, U}) where {T <: FieldElement, U <: Union{PolyRingElem, MPolyRingElem}}
return a.fraction_field::Union{FracField{U}}
function underlying_fraction_field(a::RationalFunctionField{T, U}) where {T <: FieldElement, U <: Union{PolyRingElem, MPolyRingElem}}
return a.fraction_field::FracField{U}
end

function is_domain_type(::Type{S}) where {T <: FieldElement, U <: Union{PolyRingElem, MPolyRingElem}, S <: RationalFunctionFieldElem{T, U}}
Expand Down Expand Up @@ -119,13 +119,13 @@ iszero(a::RationalFunctionFieldElem) = iszero(data(a))

isone(a::RationalFunctionFieldElem) = isone(data(a))

gen(R::RationalFunctionField) = R(gen(base_ring(R.fraction_field)))
gen(R::RationalFunctionField) = R(gen(base_ring(underlying_fraction_field(R))))

gen(R::RationalFunctionField, i::Int) = R(gen(base_ring(R.fraction_field), i))
gen(R::RationalFunctionField, i::Int) = R(gen(base_ring(underlying_fraction_field(R)), i))

gens(R::RationalFunctionField) = R.(gens(base_ring(R.fraction_field)))
gens(R::RationalFunctionField) = R.(gens(base_ring(underlying_fraction_field(R))))

number_of_generators(R::RationalFunctionField) = number_of_generators(base_ring(R.fraction_field))
number_of_generators(R::RationalFunctionField) = number_of_generators(base_ring(underlying_fraction_field(R)))

function deepcopy_internal(a::RationalFunctionFieldElem, dict::IdDict)
R = parent(a)
Expand All @@ -136,7 +136,7 @@ function characteristic(R::RationalFunctionField)
return characteristic(base_ring(R))
end

is_finite(R::RationalFunctionField) = is_finite(base_ring(R.fraction_field))
is_finite(R::RationalFunctionField) = is_finite(base_ring(underlying_fraction_field(R)))

function is_perfect(R::RationalFunctionField)
if characteristic(R) == 0
Expand Down Expand Up @@ -525,7 +525,7 @@ mul!(c::T, a::T, b::T) where {T <: RationalFunctionFieldElem} =
RandomExtensions.maketype(R::RationalFunctionField, _) = elem_type(R)

function RandomExtensions.make(S::RationalFunctionField, vs...)
R = base_ring(fraction_field(S))
R = base_ring(underlying_fraction_field(S))
if length(vs) == 1 && elem_type(R) == Random.gentype(vs[1])
Make(S, vs[1]) # forward to default Make constructor
else
Expand All @@ -536,13 +536,13 @@ end
function rand(rng::AbstractRNG,
sp::SamplerTrivial{<:Make2{<:FieldElement, <:RationalFunctionField}})
S, v = sp[][1:end]
R = base_ring(fraction_field(S))
R = base_ring(underlying_fraction_field(S))
n = rand(rng, v)
d = R()
while iszero(d)
d = rand(rng, v)
end
return S(fraction_field(S)(n, d; reduce = true))
return S(underlying_fraction_field(S)(n, d; reduce = true))
end

rand(rng::AbstractRNG, S::RationalFunctionField, v...) =
Expand Down Expand Up @@ -581,13 +581,13 @@ end
###############################################################################

function (a::RationalFunctionField{T, U})() where {T <: FieldElement, U <: Union{PolyRingElem, MPolyRingElem}}
K = fraction_field(a)
K = underlying_fraction_field(a)
z = RationalFunctionFieldElem{T, U}(K(), a)
return z
end

function (a::RationalFunctionField{T, U})(b::FracFieldElem{U}) where {T <: FieldElement, U <: Union{PolyRingElem{T}, MPolyRingElem{T}}}
K = fraction_field(a)
K = underlying_fraction_field(a)
parent(b) != K && error("Unable to coerce rational function")
z = RationalFunctionFieldElem{T, U}(b, a)
return z::RationalFunctionFieldElem{T, U}
Expand All @@ -601,10 +601,8 @@ function (a::RationalFunctionField{T, U})(n::U, d::U) where {T <: FieldElement,
d = divexact(d, g)
end
r = FracFieldElem{U}(n, d)
try
r.parent = FracDict[R]
catch
r.parent = fraction_field(R)
r.parent = get(FracDict, R) do
return underlying_fraction_field(R)
end
return a(r)
end
Expand All @@ -615,19 +613,19 @@ function (a::RationalFunctionField{T, U})(b::RationalFunctionFieldElem{T, U}) wh
end

function (a::RationalFunctionField{T, U})(b::Integer) where {T <: FieldElement, U <: Union{PolyRingElem, MPolyRingElem}}
K = fraction_field(a)
K = underlying_fraction_field(a)
z = RationalFunctionFieldElem{T, U}(K(b), a)
return z
end

function (a::RationalFunctionField{T, U})(b::Rational{<:Integer}) where {T <: FieldElement, U <: Union{PolyRingElem, MPolyRingElem}}
K = fraction_field(a)
K = underlying_fraction_field(a)
z = RationalFunctionFieldElem{T, U}(K(b), a)
return z
end

function (a::RationalFunctionField)(b::RingElem)
return a(fraction_field(a)(b))
return a(underlying_fraction_field(a)(b))
end

###############################################################################
Expand Down
Loading