Skip to content

Commit

Permalink
fixing Nemo and AA depreciation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
pogudingleb committed Jan 21, 2024
1 parent afb9b21 commit 0f79ab9
Show file tree
Hide file tree
Showing 41 changed files with 302 additions and 275 deletions.
22 changes: 11 additions & 11 deletions benchmarking/IdentifiableFunctions/param.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function check_constructive_field_membership(generators::AbstractVector, to_be_r
$(join(map(x -> string(x[1]) * " -> " * string(x[2]), zip(fracs_gen, tag_strings)), "\t\n"))
"""
var_strings = vcat(sat_string, map(string, gens(ring)), tag_strings)
ring_tag, xs_tag = PolynomialRing(K, var_strings, ordering = Nemo.ordering(ring))
ring_tag, xs_tag = polynomial_ring(K, var_strings, ordering = Nemo.ordering(ring))
orig_vars = xs_tag[2:(nvars(ring) + 1)]
tag_vars = xs_tag[(nvars(ring) + 2):end]
sat_var = xs_tag[1]
Expand Down Expand Up @@ -121,12 +121,12 @@ rem_tags, tag_to_gen = check_constructive_field_membership(fracs_generators, to_
#=
┌ Info:
│ rem_tags =
│ 3-element Vector{AbstractAlgebra.Generic.Frac{fmpq_mpoly}}:
│ 3-element Vector{AbstractAlgebra.Generic.Frac{QQMPolyRingElem}}:
│ T1^2
│ -5*T1 + T2
│ T2//T1^10
│ tag_to_gen =
│ Dict{fmpq_mpoly, AbstractAlgebra.Generic.Frac{fmpq_mpoly}} with 2 entries:
│ Dict{QQMPolyRingElem, AbstractAlgebra.Generic.Frac{QQMPolyRingElem}} with 2 entries:
│ T1 => a^2
└ T2 => (a + b)//b
=#
Expand Down Expand Up @@ -205,10 +205,10 @@ new_vector_field, new_outputs, new_vars =
│ Dict{Any, Any} with 1 entry:
│ T1 => 2*T1 + T2
│ new_outputs =
│ Dict{fmpq_mpoly, AbstractAlgebra.Generic.Frac{fmpq_mpoly}} with 1 entry:
│ Dict{QQMPolyRingElem, AbstractAlgebra.Generic.Frac{QQMPolyRingElem}} with 1 entry:
│ y => T1
│ new_vars =
│ Dict{fmpq_mpoly, AbstractAlgebra.Generic.Frac{fmpq_mpoly}} with 2 entries:
│ Dict{QQMPolyRingElem, AbstractAlgebra.Generic.Frac{QQMPolyRingElem}} with 2 entries:
│ T2 => a + b
└ T1 => x2 + x1
=#
Expand All @@ -230,7 +230,7 @@ id_funcs = StructuralIdentifiability.find_identifiable_functions(
#=
┌ Info:
│ id_funcs =
│ 2-element Vector{AbstractAlgebra.Generic.Frac{fmpq_mpoly}}:
│ 2-element Vector{AbstractAlgebra.Generic.Frac{QQMPolyRingElem}}:
│ x2*x1
└ a + b
=#
Expand All @@ -246,10 +246,10 @@ new_vector_field, new_outputs, new_vars = reparametrize_with_respect_to(ode, new
│ Dict{Any, Any} with 1 entry:
│ T1 => T1*T2
│ new_outputs =
│ Dict{fmpq_mpoly, AbstractAlgebra.Generic.Frac{fmpq_mpoly}} with 1 entry:
│ Dict{QQMPolyRingElem, AbstractAlgebra.Generic.Frac{QQMPolyRingElem}} with 1 entry:
│ y => T1
│ new_vars =
│ Dict{fmpq_mpoly, AbstractAlgebra.Generic.Frac{fmpq_mpoly}} with 2 entries:
│ Dict{QQMPolyRingElem, AbstractAlgebra.Generic.Frac{QQMPolyRingElem}} with 2 entries:
│ T2 => a + b
└ T1 => x2*x1
=#
Expand All @@ -271,7 +271,7 @@ id_funcs = StructuralIdentifiability.find_identifiable_functions(
#=
┌ Info:
│ id_funcs =
│ 5-element Vector{AbstractAlgebra.Generic.Frac{fmpq_mpoly}}:
│ 5-element Vector{AbstractAlgebra.Generic.Frac{QQMPolyRingElem}}:
│ x2*x1
│ a*b
│ x2 + x1
Expand All @@ -292,10 +292,10 @@ new_vector_field, new_iutputs, new_vars = reparametrize_with_respect_to(ode, new
│ T2 => T2*T4
│ T3 => -1//2*T1*T4^2 + 2*T1*T5 + 1//2*T3*T4
│ new_outputs =
│ Dict{fmpq_mpoly, AbstractAlgebra.Generic.Frac{fmpq_mpoly}} with 1 entry:
│ Dict{QQMPolyRingElem, AbstractAlgebra.Generic.Frac{QQMPolyRingElem}} with 1 entry:
│ y => T1
│ new_vars =
│ Dict{fmpq_mpoly, AbstractAlgebra.Generic.Frac{fmpq_mpoly}} with 5 entries:
│ Dict{QQMPolyRingElem, AbstractAlgebra.Generic.Frac{QQMPolyRingElem}} with 5 entries:
│ T1 => x2 + x1
│ T2 => x2*x1
│ T3 => a*x2 - a*x1 - b*x2 + b*x1
Expand Down
4 changes: 2 additions & 2 deletions ext/ModelingToolkitExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function __mtk_to_si(
de::ModelingToolkit.AbstractTimeDependentSystem,
measured_quantities::Array{<:Tuple{String, <:SymbolicUtils.BasicSymbolic}},
)
polytype = StructuralIdentifiability.Nemo.fmpq_mpoly
polytype = StructuralIdentifiability.Nemo.QQMPolyRingElem
fractype = StructuralIdentifiability.Nemo.Generic.Frac{polytype}
diff_eqs =
filter(eq -> !(ModelingToolkit.isoutput(eq.lhs)), ModelingToolkit.equations(de))
Expand Down Expand Up @@ -197,7 +197,7 @@ function __mtk_to_si(
input_symbols = vcat(state_vars, inputs, params)
generators = vcat(string.(input_symbols), [e[1] for e in measured_quantities])
generators = map(g -> replace(g, "(t)" => ""), generators)
R, gens_ = Nemo.PolynomialRing(Nemo.QQ, generators)
R, gens_ = Nemo.polynomial_ring(Nemo.QQ, generators)
y_vars = Vector{polytype}([str_to_var(e[1], R) for e in measured_quantities])
symb2gens = Dict(input_symbols .=> gens_[1:length(input_symbols)])

Expand Down
68 changes: 37 additions & 31 deletions src/ODE.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct ODE{P} # P is the type of polynomials in the rhs of the ODE system
x_eqs::Dict{P, <:Union{P, Generic.Frac{P}}},
y_eqs::Dict{P, <:Union{P, Generic.Frac{P}}},
inputs::Array{P, 1},
) where {P <: MPolyElem{<:FieldElem}}
) where {P <: MPolyRingElem{<:FieldElem}}
# Initialize ODE
# x_eqs is a dictionary x_i => f_i(x, u, params)
# y_eqs is a dictionary y_i => g_i(x, u, params)
Expand All @@ -40,7 +40,7 @@ struct ODE{P} # P is the type of polynomials in the rhs of the ODE system
x_eqs::Dict{P, <:Union{P, Generic.Frac{P}}},
y_eqs::Dict{P, <:Union{P, Generic.Frac{P}}},
inputs::Array{P, 1},
) where {P <: MPolyElem{<:FieldElem}}
) where {P <: MPolyRingElem{<:FieldElem}}
x_vars = collect(keys(x_eqs))
y_vars = collect(keys(y_eqs))
return ODE{P}(x_vars, y_vars, x_eqs, y_eqs, inputs)
Expand All @@ -53,10 +53,13 @@ end

#------------------------------------------------------------------------------

function add_outputs(ode::ODE{P}, extra_y::Dict{String, <:RingElem}) where {P <: MPolyElem}
function add_outputs(
ode::ODE{P},
extra_y::Dict{String, <:RingElem},
) where {P <: MPolyRingElem}
new_var_names =
vcat(collect(map(var_to_str, gens(ode.poly_ring))), collect(keys(extra_y)))
new_ring, new_vars = Nemo.PolynomialRing(base_ring(ode.poly_ring), new_var_names)
new_ring, new_vars = Nemo.polynomial_ring(base_ring(ode.poly_ring), new_var_names)

new_x = Array{P, 1}([parent_ring_change(x, new_ring) for x in ode.x_vars])
new_x_eqs = Dict{P, Union{P, Generic.Frac{P}}}(
Expand Down Expand Up @@ -94,10 +97,10 @@ Output:
function set_parameter_values(
ode::ODE{P},
param_values::Dict{P, T},
) where {T <: FieldElem, P <: MPolyElem{T}}
) where {T <: FieldElem, P <: MPolyRingElem{T}}
new_vars =
map(var_to_str, [v for v in gens(ode.poly_ring) if !(v in keys(param_values))])
small_ring, small_vars = Nemo.PolynomialRing(base_ring(ode.poly_ring), new_vars)
small_ring, small_vars = Nemo.polynomial_ring(base_ring(ode.poly_ring), new_vars)
eval_dict =
Dict(str_to_var(v, ode.poly_ring) => str_to_var(v, small_ring) for v in new_vars)
merge!(eval_dict, Dict(p => small_ring(val) for (p, val) in param_values))
Expand Down Expand Up @@ -130,8 +133,8 @@ end
function set_parameter_values(
ode::ODE{P},
param_values::Dict{P, <:Number},
) where {P <: MPolyElem}
new_values = Dict{P, fmpq}(x => _to_rational(v) for (x, v) in param_values)
) where {P <: MPolyRingElem}
new_values = Dict{P, QQFieldElem}(x => _to_rational(v) for (x, v) in param_values)
return set_parameter_values(ode, new_values)
end

Expand All @@ -156,11 +159,11 @@ function power_series_solution(
initial_conditions::Dict{P, T},
input_values::Dict{P, Array{T, 1}},
prec::Int,
) where {T <: FieldElem, P <: MPolyElem{T}}
) where {T <: FieldElem, P <: MPolyRingElem{T}}
new_varnames = map(var_to_str, vcat(ode.x_vars, ode.u_vars))
append!(new_varnames, map(v -> var_to_str(v) * "_dot", ode.x_vars))

new_ring, new_vars = Nemo.PolynomialRing(base_ring(ode.poly_ring), new_varnames)
new_ring, new_vars = Nemo.polynomial_ring(base_ring(ode.poly_ring), new_varnames)
equations = Array{P, 1}()
evaluation = Dict(k => new_ring(v) for (k, v) in param_values)
for v in vcat(ode.x_vars, ode.u_vars)
Expand Down Expand Up @@ -196,7 +199,7 @@ function power_series_solution(
initial_conditions::Dict{P, Int},
input_values::Dict{P, Array{Int, 1}},
prec::Int,
) where {P <: MPolyElem{<:FieldElem}}
) where {P <: MPolyRingElem{<:FieldElem}}
bring = base_ring(ode.poly_ring)
return power_series_solution(
ode,
Expand All @@ -213,7 +216,7 @@ end
Reduces a polynomial/rational function over Q modulo p
"""
function _reduce_mod_p(poly::fmpq_mpoly, p::Int)
function _reduce_mod_p(poly::QQMPolyRingElem, p::Int)
den = denominator(poly)
num = change_base_ring(Nemo.ZZ, den * poly)
if Nemo.Native.GF(p)(den) == 0
Expand All @@ -222,7 +225,7 @@ function _reduce_mod_p(poly::fmpq_mpoly, p::Int)
return change_base_ring(Nemo.Native.GF(p), num) * (1 // Nemo.Native.GF(p)(den))
end

function _reduce_mod_p(rat::Generic.Frac{fmpq_mpoly}, p::Int)
function _reduce_mod_p(rat::Generic.Frac{QQMPolyRingElem}, p::Int)
num, den = map(poly -> _reduce_mod_p(poly, p), [numerator(rat), denominator(rat)])
if den == 0
throw(Base.ArgumentError("Prime $p divides the denominator of $rat"))
Expand All @@ -238,9 +241,9 @@ end
Input: ode is an ODE over QQ, p is a prime number
Output: the reduction mod p, throws an exception if p divides one of the denominators
"""
function reduce_ode_mod_p(ode::ODE{<:MPolyElem{Nemo.fmpq}}, p::Int)
function reduce_ode_mod_p(ode::ODE{<:MPolyRingElem{Nemo.QQFieldElem}}, p::Int)
new_ring, new_vars =
Nemo.PolynomialRing(Nemo.Native.GF(p), map(var_to_str, gens(ode.poly_ring)))
Nemo.polynomial_ring(Nemo.Native.GF(p), map(var_to_str, gens(ode.poly_ring)))
new_type = typeof(new_vars[1])
new_inputs = map(u -> switch_ring(u, new_ring), ode.u_vars)
new_x = map(x -> switch_ring(x, new_ring), ode.x_vars)
Expand Down Expand Up @@ -375,7 +378,7 @@ macro ODEmodel(ex::Expr...)
R = gensym()
vars_aux = gensym()
exp_ring = :(
($R, $vars_aux) = StructuralIdentifiability.Nemo.PolynomialRing(
($R, $vars_aux) = StructuralIdentifiability.Nemo.polynomial_ring(
StructuralIdentifiability.Nemo.QQ,
map(string, $all_symb_with_t),
)
Expand All @@ -385,31 +388,33 @@ macro ODEmodel(ex::Expr...)
# setting x_vars and y_vars in the right order
vx = gensym()
vy = gensym()
x_var_expr = :($vx = Vector{StructuralIdentifiability.Nemo.fmpq_mpoly}([$(x_vars...)]))
y_var_expr = :($vy = Vector{StructuralIdentifiability.Nemo.fmpq_mpoly}([$(y_vars...)]))
x_var_expr =
:($vx = Vector{StructuralIdentifiability.Nemo.QQMPolyRingElem}([$(x_vars...)]))
y_var_expr =
:($vy = Vector{StructuralIdentifiability.Nemo.QQMPolyRingElem}([$(y_vars...)]))

# preparing equations
equations = map(macrohelper_clean, equations)
x_dict = gensym()
y_dict = gensym()
x_dict_create_expr = :(
$x_dict = Dict{
StructuralIdentifiability.Nemo.fmpq_mpoly,
StructuralIdentifiability.Nemo.QQMPolyRingElem,
Union{
StructuralIdentifiability.Nemo.fmpq_mpoly,
StructuralIdentifiability.Nemo.QQMPolyRingElem,
StructuralIdentifiability.AbstractAlgebra.Generic.Frac{
StructuralIdentifiability.Nemo.fmpq_mpoly,
StructuralIdentifiability.Nemo.QQMPolyRingElem,
},
},
}()
)
y_dict_create_expr = :(
$y_dict = Dict{
StructuralIdentifiability.Nemo.fmpq_mpoly,
StructuralIdentifiability.Nemo.QQMPolyRingElem,
Union{
StructuralIdentifiability.Nemo.fmpq_mpoly,
StructuralIdentifiability.Nemo.QQMPolyRingElem,
StructuralIdentifiability.AbstractAlgebra.Generic.Frac{
StructuralIdentifiability.Nemo.fmpq_mpoly,
StructuralIdentifiability.Nemo.QQMPolyRingElem,
},
},
}()
Expand Down Expand Up @@ -472,13 +477,14 @@ macro ODEmodel(ex::Expr...)
),
]
# creating the ode object
ode_expr = :(StructuralIdentifiability.ODE{StructuralIdentifiability.Nemo.fmpq_mpoly}(
$vx,
$vy,
$x_dict,
$y_dict,
Array{StructuralIdentifiability.Nemo.fmpq_mpoly}([$(u_vars...)]),
))
ode_expr =
:(StructuralIdentifiability.ODE{StructuralIdentifiability.Nemo.QQMPolyRingElem}(
$vx,
$vy,
$x_dict,
$y_dict,
Array{StructuralIdentifiability.Nemo.QQMPolyRingElem}([$(u_vars...)]),
))

result = Expr(
:block,
Expand Down
4 changes: 2 additions & 2 deletions src/ODEexport.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function print_for_maple(ode::ODE, package = :SIAN)
varstr =
Dict(x => var_to_str(x) * "(t)" for x in vcat(ode.x_vars, ode.u_vars, ode.y_vars))
merge!(varstr, Dict(p => var_to_str(p) for p in ode.parameters))
R_print, vars_print = Nemo.PolynomialRing(
R_print, vars_print = Nemo.polynomial_ring(

Check warning on line 13 in src/ODEexport.jl

View check run for this annotation

Codecov / codecov/patch

src/ODEexport.jl#L13

Added line #L13 was not covered by tests
base_ring(ode.poly_ring),
[varstr[v] for v in gens(ode.poly_ring)],
)
Expand Down Expand Up @@ -189,7 +189,7 @@ function print_for_COMBOS(ode::ODE)
merge!(varstr, Dict(u => "u" * string(ind) for (ind, u) in enumerate(ode.u_vars)))
merge!(varstr, Dict(y => "y" * string(ind) for (ind, y) in enumerate(ode.y_vars)))
merge!(varstr, Dict(p => var_to_str(p) for p in ode.parameters))
R_print, vars_print = Nemo.PolynomialRing(
R_print, vars_print = Nemo.polynomial_ring(

Check warning on line 192 in src/ODEexport.jl

View check run for this annotation

Codecov / codecov/patch

src/ODEexport.jl#L192

Added line #L192 was not covered by tests
base_ring(ode.poly_ring),
[varstr[v] for v in gens(ode.poly_ring)],
)
Expand Down
12 changes: 6 additions & 6 deletions src/RationalFunctionFields/IdealMQS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ mutable struct IdealMQS{T} <: AbstractBlackboxIdeal
end
varnames = append_at_index(ystrs, sat_var_index, sat_varname)
@debug "Saturating variable is $sat_varname, index is $sat_var_index"
R_sat, v_sat = Nemo.PolynomialRing(K, varnames, ordering = ordering)
R_sat, v_sat = Nemo.polynomial_ring(K, varnames, ordering = ordering)
# Saturation
t_sat = v_sat[sat_var_index]
den_lcm_orig = den_lcm
Expand Down Expand Up @@ -144,7 +144,7 @@ mutable struct IdealMQS{T} <: AbstractBlackboxIdeal
push!(nums_qq, num)
end
end
parent_ring_param, _ = PolynomialRing(ring, varnames, ordering = ordering)
parent_ring_param, _ = polynomial_ring(ring, varnames, ordering = ordering)
@debug "Constructed MQS ideal in $R_sat with $(length(nums_qq) + 1) elements"
@assert length(pivots_indices) == length(dens_indices) == length(dens_qq)
@assert length(pivots_indices) == length(funcs_den_nums)
Expand Down Expand Up @@ -202,7 +202,7 @@ function fractionfree_generators_raw(mqs::IdealMQS)
end
# NOTE: new variables go first!
big_ring, big_vars =
PolynomialRing(K, vcat(new_varnames, old_varnames), ordering = :lex)
polynomial_ring(K, vcat(new_varnames, old_varnames), ordering = :lex)
@info "$(mqs.sat_var_index) $(varnames) $ring_params $(parent(mqs.sat_qq))"
nums_qq, dens_qq, sat_qq = mqs.nums_qq, mqs.dens_qq, mqs.sat_qq
nums_y = map(num -> parent_ring_change(num, big_ring, matching = :byindex), nums_qq)
Expand All @@ -229,7 +229,7 @@ end
function ParamPunPam.reduce_mod_p!(
mqs::IdealMQS,
ff::Field,
) where {Field <: Union{Nemo.GaloisField, Nemo.GaloisFmpzField}}
) where {Field <: Union{Nemo.GaloisField, Nemo.FpField}}
@debug "Reducing MQS ideal modulo $(ff)"
# If there is a reduction modulo this field already,
if haskey(mqs.cached_nums_gf, ff)
Expand All @@ -251,7 +251,7 @@ function ParamPunPam.specialize_mod_p(
mqs::IdealMQS,
point::Vector{T};
saturated = true,
) where {T <: Union{gfp_elem, gfp_fmpz_elem}}
) where {T <: Union{fpFieldElem, gfp_fmpz_elem}}
K_1 = parent(first(point))
@debug "Evaluating MQS ideal over $K_1 at $point"
@assert haskey(mqs.cached_nums_gf, K_1)
Expand Down Expand Up @@ -283,7 +283,7 @@ function ParamPunPam.specialize_mod_p(
return polys
end

function specialize(mqs::IdealMQS, point::Vector{Nemo.fmpq}; saturated = true)
function specialize(mqs::IdealMQS, point::Vector{Nemo.QQFieldElem}; saturated = true)
@debug "Evaluating MQS ideal over QQ at $point"
nums_qq, dens_qq, sat_qq = mqs.nums_qq, mqs.dens_qq, mqs.sat_qq
dens_indices = mqs.dens_indices
Expand Down
6 changes: 3 additions & 3 deletions src/StructuralIdentifiability.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function assess_identifiability(
funcs_to_check = Vector(),
prob_threshold::Float64 = 0.99,
loglevel = Logging.Info,
) where {P <: MPolyElem{fmpq}}
) where {P <: MPolyRingElem{QQFieldElem}}
restart_logging(loglevel = loglevel)
reset_timings()
with_logger(_si_logger[]) do
Expand All @@ -115,7 +115,7 @@ function _assess_identifiability(
ode::ODE{P};
funcs_to_check = Vector(),
prob_threshold::Float64 = 0.99,
) where {P <: MPolyElem{fmpq}}
) where {P <: MPolyRingElem{QQFieldElem}}
p_glob = 1 - (1 - prob_threshold) * 0.9
p_loc = 1 - (1 - prob_threshold) * 0.1

Expand All @@ -124,7 +124,7 @@ function _assess_identifiability(
end

@info "Assessing local identifiability"
trbasis = Array{fmpq_mpoly, 1}()
trbasis = Array{QQMPolyRingElem, 1}()
runtime = @elapsed local_result = _assess_local_identifiability(
ode,
funcs_to_check = funcs_to_check,
Expand Down
Loading

0 comments on commit 0f79ab9

Please sign in to comment.