Skip to content

Commit

Permalink
tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
antonydellavecchia committed Nov 13, 2024
1 parent b41dee5 commit d758fad
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 173 deletions.
81 changes: 41 additions & 40 deletions src/Serialization/Fields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
################################################################################
# field of rationals (singleton type)
@register_serialization_type QQField
type_params(::QQField) = nothing
type_params(::fpField) = nothing
type_params(::FpField) = nothing
type_params(::QQBarField) = nothing
type_params(::PadicField) = nothing
# exclude from ring union definition
type_params(::QQField) = QQField, nothing
type_params(::fpField) = fpField, nothing
type_params(::FpField) = FpField, nothing
type_params(::QQBarField) = QQBarField, nothing
type_params(::PadicField) = PadicField, nothing

################################################################################
# type_params for field extension types
Expand All @@ -29,7 +30,7 @@ function load_object(s::DeserializerState, ::Type{fpField})
end

# elements
@register_serialization_type fpFieldElem uses_params
@register_serialization_type fpFieldElem

function save_object(s::SerializerState, elem::fpFieldElem)
save_data_basic(s, string(elem))
Expand All @@ -56,7 +57,7 @@ function load_object(s::DeserializerState, ::Type{FpField})
end

# elements
@register_serialization_type FpFieldElem uses_params
@register_serialization_type FpFieldElem

function save_object(s::SerializerState, elem::FpFieldElem)
save_data_basic(s, string(elem))
Expand All @@ -71,11 +72,11 @@ end
################################################################################
# SimpleNumField

@register_serialization_type Hecke.RelSimpleNumField uses_id uses_params
@register_serialization_type AbsSimpleNumField uses_id uses_params
@register_serialization_type Hecke.RelSimpleNumField uses_id
@register_serialization_type AbsSimpleNumField uses_id
const SimNumFieldTypeUnion = Union{AbsSimpleNumField, Hecke.RelSimpleNumField}

type_params(obj::SimpleNumField) = type_params(defining_polynomial(obj))
type_params(obj::T) where T <: SimpleNumField = T, type_params(defining_polynomial(obj))

function save_object(s::SerializerState, K::SimpleNumField)
save_data_dict(s) do
Expand All @@ -93,9 +94,9 @@ end

################################################################################
# FqNmodfinitefield
@register_serialization_type fqPolyRepField uses_id uses_params
@register_serialization_type fqPolyRepField uses_id

type_params(K::fqPolyRepField) = type_params(defining_polynomial(K))
type_params(K::fqPolyRepField) = fqPolyRepField, type_params(defining_polynomial(K))

function save_object(s::SerializerState, K::fqPolyRepField)
save_object(s, defining_polynomial(K))
Expand All @@ -108,9 +109,9 @@ function load_object(s::DeserializerState, ::Type{<: fqPolyRepField}, params::Po
end

#elements
@register_serialization_type fqPolyRepFieldElem uses_params
@register_serialization_type AbsSimpleNumFieldElem uses_params
@register_serialization_type Hecke.RelSimpleNumFieldElem uses_params
@register_serialization_type fqPolyRepFieldElem
@register_serialization_type AbsSimpleNumFieldElem
@register_serialization_type Hecke.RelSimpleNumFieldElem
const SimNumFieldElemTypeUnion = Union{AbsSimpleNumFieldElem, fqPolyRepFieldElem, Hecke.RelSimpleNumFieldElem}

function save_object(s::SerializerState, k::SimNumFieldElemTypeUnion)
Expand All @@ -133,12 +134,12 @@ end

################################################################################
# FqField
@register_serialization_type FqField uses_id uses_params
@register_serialization_type FqFieldElem uses_params
@register_serialization_type FqField uses_id
@register_serialization_type FqFieldElem

function type_params(K::FqField)
absolute_degree(K) == 1 && return nothing
return type_params(defining_polynomial(K))
absolute_degree(K) == 1 && return FqField, nothing
return FqField, type_params(defining_polynomial(K))
end

function save_object(s::SerializerState, K::FqField)
Expand Down Expand Up @@ -187,11 +188,11 @@ end
################################################################################
# Non Simple Extension

@register_serialization_type Hecke.RelNonSimpleNumField uses_id uses_params
@register_serialization_type AbsNonSimpleNumField uses_id uses_params
@register_serialization_type Hecke.RelNonSimpleNumField uses_id
@register_serialization_type AbsNonSimpleNumField uses_id
const NonSimFieldTypeUnion = Union{AbsNonSimpleNumField, RelNonSimpleNumField}

type_params(K::Union{AbsNonSimpleNumField, RelNonSimpleNumField}) = type_params(defining_polynomials(K)[1])
type_params(K::T) where T <: Union{AbsNonSimpleNumField, RelNonSimpleNumField} = T, type_params(defining_polynomials(K)[1])

function save_object(s::SerializerState, K::NonSimFieldTypeUnion)
save_data_dict(s) do
Expand All @@ -214,8 +215,8 @@ function load_object(s::DeserializerState,
end

#elements
@register_serialization_type Hecke.RelNonSimpleNumFieldElem uses_params
@register_serialization_type AbsNonSimpleNumFieldElem uses_params
@register_serialization_type Hecke.RelNonSimpleNumFieldElem
@register_serialization_type AbsNonSimpleNumFieldElem

function save_object(s::SerializerState, k::Union{AbsNonSimpleNumFieldElem, Hecke.RelNonSimpleNumFieldElem})
polynomial = Oscar.Hecke.data(k)
Expand All @@ -240,7 +241,7 @@ end
################################################################################
# FracField

@register_serialization_type FracField uses_id uses_params
@register_serialization_type FracField uses_id

const FracUnionTypes = Union{MPolyRingElem, PolyRingElem, UniversalPolyRingElem}
# we use the union to prevent QQField from using these save methods
Expand All @@ -255,7 +256,7 @@ load_object(s::DeserializerState, ::Type{<: FracField}, base::Ring) = fraction_f

# elements

@register_serialization_type FracElem{<: FracUnionTypes} "FracElem" uses_params
@register_serialization_type FracElem{<: FracUnionTypes} "FracElem"

function save_object(s::SerializerState, f::FracElem)
save_data_array(s) do
Expand All @@ -278,7 +279,7 @@ end
################################################################################
# RationalFunctionField

@register_serialization_type AbstractAlgebra.Generic.RationalFunctionField "RationalFunctionField" uses_id uses_params
@register_serialization_type AbstractAlgebra.Generic.RationalFunctionField "RationalFunctionField" uses_id

function save_object(s::SerializerState,
RF::AbstractAlgebra.Generic.RationalFunctionField)
Expand All @@ -302,7 +303,7 @@ function load_object(s::DeserializerState,
end

#elements
@register_serialization_type AbstractAlgebra.Generic.RationalFunctionFieldElem "RationalFunctionFieldElem" uses_params
@register_serialization_type AbstractAlgebra.Generic.RationalFunctionFieldElem "RationalFunctionFieldElem"

function save_object(s::SerializerState, f::AbstractAlgebra.Generic.RationalFunctionFieldElem)
save_data_array(s) do
Expand Down Expand Up @@ -332,7 +333,7 @@ end
################################################################################
# ArbField
@register_serialization_type ArbField
@register_serialization_type ArbFieldElem uses_params
@register_serialization_type ArbFieldElem

function save_object(s::SerializerState, RR::ArbField)
save_object(s, precision(RR))
Expand Down Expand Up @@ -365,7 +366,7 @@ end
################################################################################
# AcbField
@register_serialization_type AcbField
@register_serialization_type AcbFieldElem uses_params
@register_serialization_type AcbFieldElem

function save_object(s::SerializerState, CC::AcbField)
save_object(s, precision(CC))
Expand Down Expand Up @@ -396,20 +397,20 @@ end

const FieldEmbeddingTypes = Union{Hecke.AbsSimpleNumFieldEmbedding, Hecke.RelSimpleNumFieldEmbedding, Hecke.AbsNonSimpleNumFieldEmbedding, Hecke.RelNonSimpleNumFieldEmbedding}

@register_serialization_type Hecke.AbsNonSimpleNumFieldEmbedding uses_id uses_params
@register_serialization_type Hecke.RelNonSimpleNumFieldEmbedding uses_id uses_params
@register_serialization_type Hecke.AbsSimpleNumFieldEmbedding uses_id uses_params
@register_serialization_type Hecke.RelSimpleNumFieldEmbedding uses_id uses_params
@register_serialization_type Hecke.AbsNonSimpleNumFieldEmbedding uses_id
@register_serialization_type Hecke.RelNonSimpleNumFieldEmbedding uses_id
@register_serialization_type Hecke.AbsSimpleNumFieldEmbedding uses_id
@register_serialization_type Hecke.RelSimpleNumFieldEmbedding uses_id

function type_params(E::FieldEmbeddingTypes)
function type_params(E::T) where T <: FieldEmbeddingTypes
K = number_field(E)
base_K = base_field(K)
d = Dict(:num_field => type_params(K))

if !(base_field(K) isa QQField)
d[:base_field_emb] = type_params(restrict(E, base_K))
end
return d
return T, d
end

function save_object(s::SerializerState, E::FieldEmbeddingTypes)
Expand Down Expand Up @@ -450,9 +451,9 @@ function load_object(s::DeserializerState, ::Type{<:FieldEmbeddingTypes}, params
end
end

@register_serialization_type EmbeddedNumField uses_id uses_params
@register_serialization_type EmbeddedNumField uses_id

type_params(E::EmbeddedNumField) = Dict(
type_params(E::EmbeddedNumField) = EmbeddedNumField, Dict(
:num_field_params => type_params(number_field(E)),
:embedding_params => type_params(embedding(E))
)
Expand All @@ -474,7 +475,7 @@ function load_object(s::DeserializerState, ::Type{EmbeddedNumField}, params::Dic
return Hecke.embedded_field(K, e)[1]
end

@register_serialization_type EmbeddedNumFieldElem uses_params
@register_serialization_type EmbeddedNumFieldElem

function save_object(s::SerializerState, f::EmbeddedNumFieldElem)
save_object(s, data(f))
Expand Down Expand Up @@ -559,7 +560,7 @@ function load_object(s::DeserializerState, ::Type{PadicField})
end

#elements
@register_serialization_type PadicFieldElem uses_params
@register_serialization_type PadicFieldElem

function save_object(s::SerializerState, obj::PadicFieldElem)
# currently it seems PadicFieldElems do not store the underlying polynomial
Expand Down
2 changes: 1 addition & 1 deletion src/Serialization/PolyhedralGeometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ end
##############################################################################
# Abstract Polyhedral Object

type_params(obj::T) where T<: PolyhedralObject = coefficient_field(obj)
type_params(obj::T) where T<: PolyhedralObject = T, coefficient_field(obj)

function save_object(s::SerializerState, obj::PolyhedralObject{S}) where S <: Union{QQFieldElem, Float64}
save_object(s, pm_object(obj))
Expand Down
17 changes: 10 additions & 7 deletions src/Serialization/Rings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ const LaurentUnionType = Union{Generic.LaurentSeriesRing,
################################################################################
# type_params functions

type_params(x::T) where T <: RingMatElemUnion = parent(x)
type_params(R::T) where T <: RingMatSpaceUnion = base_ring(R)
type_params(::ZZRing) = nothing
type_params(::T) where T <: ModRingUnion = nothing
type_params(x::T) where T <: IdealOrdUnionType = base_ring(x)
type_params(x::T) where T <: RingMatElemUnion = T, parent(x)
type_params(R::T) where T <: RingMatSpaceUnion = T, base_ring(R)
type_params(x::T) where T <: IdealOrdUnionType = T, base_ring(x)
# exclude from ring union
type_params(::ZZRing) = ZZRing, nothing
type_params(::T) where T <: ModRingUnion = T, nothing


################################################################################
# ring of integers (singleton type)
Expand Down Expand Up @@ -114,8 +116,9 @@ function load_object(s::DeserializerState,
end

# with grading
type_params(R::MPolyDecRing) = Dict(:grading_group => type_params(_grading(R)),
:base_ring => type_params(forget_grading(R)))
type_params(R::MPolyDecRing) = MPolyDecRing, Dict(
:grading_group => type_params(_grading(R)),
:base_ring => type_params(forget_grading(R)))

function save_object(s::SerializerState, R::MPolyDecRing)
save_data_dict(s) do
Expand Down
1 change: 1 addition & 0 deletions src/Serialization/basic_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ function save_object(s::SerializerState, x::T) where T <: Union{BasicTypeUnion,
save_data_basic(s, x)
end

load_object(s::DeserializerState, T::Type, ::Nothing) = load_object(s, T)
################################################################################
# Bool
@register_serialization_type Bool
Expand Down
65 changes: 9 additions & 56 deletions src/Serialization/containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,13 @@ const MatVecType{T} = Union{Matrix{T}, Vector{T}, SRow{T}}

function type_params(obj::S) where {T, S <:MatVecType{T}}
if isempty(obj)
return nothing
return S, nothing
end

params = type_params.(obj)
params_all_equal = all(map(x -> isequal(first(params), x), params))
@req params_all_equal "Not all params of Vector or Matrix entries are the same, consider using a Tuple for serialization"

if isnothing(params[1])
return T
else
return Dict(
:entry_type => T,
:params => params[1]
)
end
return S, params[1]
end

function save_object(s::SerializerState, x::Vector)
Expand Down Expand Up @@ -122,7 +114,7 @@ end

function type_params(obj::T) where T <: Tuple
n = fieldcount(T)
return [
return T, [
isnothing(type_params(obj[i])) ? fieldtype(T, i) : type_params(obj[i])
for i in 1:n]
end
Expand All @@ -140,69 +132,30 @@ function save_object(s::SerializerState, obj::Tuple)
end
end

function load_object(s::DeserializerState, ::Type{<:Tuple}, params::Vector)
function load_object(s::DeserializerState, T::Type{<:Tuple}, params::Vector)
entries = load_array_node(s) do (i, entry)
if params[i] isa Type
if serialize_with_id(params[i])
S = fieldtype(T, i)
if serialize_with_id(S)
return load_ref(s)
else
return load_object(s, params[i])
end
else
return load_object(s, params[i][1], params[i][2])
return load_object(s, S, params[i])
end
end
return Tuple(entries)
end

################################################################################
# Saving and loading NamedTuple
@register_serialization_type NamedTuple uses_params
@register_serialization_type NamedTuple

function type_params(obj::T) where T <: NamedTuple
return Dict(:tuple_params => type_params.(values(obj)),
:names => keys(obj))
return T, NamedTuple(map(x -> x.first => type_params(x.second), collect(pairs(obj))))
end

#function save_type_params(s::SerializerState, obj::T) where T <: NamedTuple
# save_data_dict(s) do
# save_object(s, encode_type(NamedTuple), :name)
# save_data_dict(s, :params) do
# save_data_array(s, :tuple_params) do
# for (i, value) in enumerate(values(obj))
# U = fieldtype(T, i)
# if serialize_with_params(U)
# save_type_params(s, value)
# else
# save_object(s, encode_type(U))
# end
# end
# end
# save_object(s, keys(obj), :names)
# end
# end
#end

function save_object(s::SerializerState, obj::NamedTuple)
save_object(s, values(obj))
end

function load_type_params(s::DeserializerState, ::Type{<:NamedTuple})
loaded_params = Any[]
load_array_node(s, :tuple_params) do (_, param)
if param isa String
push!(loaded_params, decode_type(s))
else
T = decode_type(s)
params = load_params_node(s)
push!(loaded_params, (T, params))
end
end
load_node(s, :names) do names
return (names, loaded_params)
end
end

function load_object(s::DeserializerState, ::Type{<: NamedTuple}, params::Tuple)
keys, tuple_params = params
tuple = load_object(s, Tuple, tuple_params)
Expand Down
Loading

0 comments on commit d758fad

Please sign in to comment.