From 2515d76608f4460372c647b347b69efcf06cccb1 Mon Sep 17 00:00:00 2001 From: Alexander Demin Date: Wed, 4 Nov 2026 22:00:15 +0100 Subject: [PATCH 1/9] better reparametrize interface --- src/parametrizations.jl | 146 ++++++++++++++++++++++++++-------- test/bodies/paradigm_shift.jl | 5 +- 2 files changed, 115 insertions(+), 36 deletions(-) diff --git a/src/parametrizations.jl b/src/parametrizations.jl index b16208efc..24db59cf1 100644 --- a/src/parametrizations.jl +++ b/src/parametrizations.jl @@ -16,6 +16,62 @@ function vector_field_along(derivation::Dict{T, U}, directions::AbstractVector) return new_vector_field end +function suggest_variable_names(ode, new_states, new_params) + @assert allunique(map(string, gens(parent(ode)))) + names = String[] + + for (i, def) in enumerate(new_states) + num, den = unpack_fraction(def) + name = nothing + + # x --> x + if def in ode.x_vars + name == nothing && (name = var_to_str(num)) + end + + # a*x+b --> x̃ + tildify(c::Char) = string(c, '\u0303') + tildify(s) = tildify(first(s)) * s[nextind(s, firstindex(s)):end] + states = intersect(vars(def), ode.x_vars) + if length(states) == 1 + name_orig = var_to_str(states[1]) + name == nothing && (name = tildify(name_orig)) + # resolve collision a*x -> x̃ and b*x -> x̃ + j = 1 + @info "" name + while name in names + name = chopsuffix(chopsuffix(name, "(t)"), string(j-1)) + name = string(name, j, "(t)") + j += 1 + end + end + + # f(x1,...,xn) --> X_i + name == nothing && (name = "X$i(t)") + + push!(names, name) + end + + for (i, def) in enumerate(new_params) + num, den = unpack_fraction(def) + name = nothing + + # x --> x + if def in ode.parameters + name == nothing && (name = var_to_str(num)) + end + + # f(x) --> ai + name == nothing && (name = "a$i") + + push!(names, name) + end + + append!(names, string.(ode.u_vars), string.(ode.y_vars)) + + names +end + """ reparametrize_with_respect_to(ode, new_vars) @@ -31,15 +87,15 @@ function reparametrize_with_respect_to(ode, new_vars) contains_states(func) = contains_states(numerator(func)) || contains_states(denominator(func)) new_vars_contains_states = filter(contains_states, new_vars) @info """ - Original states: $(ode.x_vars) - Original params: $(ode.parameters) - New variables that contain states: $(new_vars_contains_states)""" + Original variables: + States: $(join(map(string, ode.x_vars), ", ")) + Params: $(join(map(string, ode.parameters), ", "))""" new_states = new_vars_contains_states new_params = setdiff(new_vars, new_vars_contains_states) @info """ Reparametrizing with respect to: - New states: $new_states - New params: $new_params""" + New states: $(join(map(string, new_states), ", ")) + New params: $(join(map(string, new_params), ", "))""" return reparametrize_with_respect_to(ode, new_states, new_params) end @@ -57,6 +113,9 @@ function reparametrize_with_respect_to(ode, new_states, new_params) new_vector_field = vector_field_along(ode.x_equations, new_states) @debug "New vector field:\n$new_vector_field" new_states = collect(keys(new_vector_field)) + # Order by the first original state occurring in each new state. + # TODO: might be too invasive if we do this here instead of the outer function + sort!(new_states, by = f -> something(findfirst(in(vars(f)), ode.x_vars), length(ode.x_vars) + 1)) new_dynamics = [new_vector_field[new_state] for new_state in new_states] # Express the new dynamics in terms of new states and new parameters. outputs = [ode.y_equations[output] for output in ode.y_vars] @@ -74,7 +133,7 @@ function reparametrize_with_respect_to(ode, new_states, new_params) gen_tag_names(length(ode.u_vars), "Input"), gen_tag_names(length(ode.y_vars), "Output"), ) - @info """ + @debug """ Tag names: $tag_names Generating functions: @@ -82,12 +141,14 @@ function reparametrize_with_respect_to(ode, new_states, new_params) Functions to be reduced: $to_be_reduced_funcs """ - membership, new_dynamics_all, implicit_relations, new_vars = + @info "Constructing equations of new ODE" + mem_time = @elapsed membership, new_dynamics_all, implicit_relations, new_vars = check_constructive_field_membership( RationalFunctionField(generating_funcs), to_be_reduced_funcs; tag_names = tag_names, ) + @info "Constructed equations of new ODE in $mem_time seconds" @assert all(membership) ring_of_tags = parent(first(keys(new_vars))) tags = gens(ring_of_tags) @@ -103,7 +164,7 @@ function reparametrize_with_respect_to(ode, new_states, new_params) if !isempty(ode.u_vars) new_inputs = tag_inputs end - @info """ + @debug """ New state dynamics: $new_dynamics_states New output dynamics: @@ -117,19 +178,11 @@ function reparametrize_with_respect_to(ode, new_states, new_params) new_vars_vector_field[state] = new_dynamics_states[i] end @info "Converting variable names to human-readable ones" - internal_variable_names = map(i -> "X$i(t)", 1:length(new_states)) - parameter_variable_names = map(i -> "a$i", 1:length(new_params)) - input_variable_names = map(i -> "u$i(t)", 1:length(tag_inputs)) - output_variable_names = map(i -> "y$i(t)", 1:length(tag_outputs)) - all_variable_names = vcat( - internal_variable_names, - parameter_variable_names, - input_variable_names, - output_variable_names, - ) + new_names = suggest_variable_names(ode, new_states, new_params) + @assert allunique(new_names) ring_output, _ = polynomial_ring( base_ring(ring_of_tags), - all_variable_names, + new_names, internal_ordering = Nemo.internal_ordering(ring_of_tags), ) new_vars_vector_field = Dict( @@ -146,9 +199,9 @@ function reparametrize_with_respect_to(ode, new_states, new_params) parent_ring_change(var_expr, ring_output, matching = :byindex) for (var_old, var_expr) in new_outputs ) - new_vars = Dict( - parent_ring_change(var_old, ring_output, matching = :byindex) => var_expr for - (var_old, var_expr) in new_vars + new_vars = OrderedDict( + parent_ring_change(var_old, ring_output, matching = :byindex) => new_vars[var_old] for + var_old in gens(ring_of_tags) ) implicit_relations = map( var_old -> parent_ring_change(var_old, ring_output, matching = :byindex), @@ -163,6 +216,28 @@ function reparametrize_with_respect_to(ode, new_states, new_params) return new_vars_vector_field, new_inputs, new_outputs, new_vars, implicit_relations end +function show_reparametrization(out, reparam) + # eval(ode_repr(ode)) ≈ ode + ode_repr(ode) = begin + s = sprint(show, reparam.new_ode) + s = join(map(line -> string(" "^2, line), split(rstrip(s, '\n'), "\n")), ",\n") + string("ode = @ODEmodel(\n", s, "\n)") + end + + println(out, "\n", "• Reparametrized ODE:\n") + println(out, ode_repr(reparam.new_ode), "\n") + + println(out, "• Definitions:", "\n") + println(out, join(map(p -> replace(string(" "^2, p), "=>" => ":="), collect(reparam.new_vars)), "\n"), "\n") + + if !isempty(reparam.implicit_relations) + participants = mapreduce(vars, union, reparam.implicit_relations) + println(out, "• These variables are not independent: $(join(map(string, participants), ", "))", "\n") + end + + println(out, "• For more info, run\n\njulia> @doc reparametrize_global\n") +end + """ reparametrize_global(ode, options...) @@ -211,18 +286,18 @@ Then, we have the following: ``` # new_ode -X2'(t) = X1(t)*X2(t)*a2 - X2(t)*a1 -X1'(t) = -X1(t)*X2(t) + X1(t)*a3 -y1(t) = X1(t) +x1'(t) = a*x1(t) - x1(t)*x̃2(t) +x̃2'(t) = d*x1(t)*x̃2(t) - c*x̃2(t) +y(t) = x1(t) # new_vars Dict{Nemo.QQMPolyRingElem, AbstractAlgebra.Generic.FracFieldElem{Nemo.QQMPolyRingElem}} with 6 entries: - X2 => b*x2 - y1 => y - X1 => x1 - a2 => d - a3 => a - a1 => c + x1(t) => x1(t) + x̃2(t) => b*x2(t) + y(t) => y(t) + d => d + a => a + c => c ``` Notice that the `new_ode` is fully identifiabile, and has `1` less parameter @@ -256,6 +331,11 @@ function _reparametrize_global(ode::ODE{P}; prob_threshold = 0.99, seed = 42, cm new_vector_field, new_inputs, new_outputs, new_vars, implicit_relations = reparametrize_with_respect_to(ode, id_funcs) new_ring = parent(first(keys(new_vector_field))) - new_ode = ODE{P}(new_vector_field, new_outputs, new_inputs) - return (new_ode = new_ode, new_vars = new_vars, implicit_relations = implicit_relations) + # Fix the order of variables in the new ODE + new_x_vars = filter(v -> haskey(new_vector_field, v), gens(new_ring)) + new_y_vars = filter(v -> haskey(new_outputs, v), gens(new_ring)) + new_ode = ODE{P}(new_x_vars, new_y_vars, new_vector_field, new_outputs, new_inputs) + reparam = (new_ode = new_ode, new_vars = new_vars, implicit_relations = implicit_relations) + show_reparametrization(stdout, reparam) + reparam end diff --git a/test/bodies/paradigm_shift.jl b/test/bodies/paradigm_shift.jl index a8a686556..0d74eacf2 100644 --- a/test/bodies/paradigm_shift.jl +++ b/test/bodies/paradigm_shift.jl @@ -3,9 +3,8 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) ≡(A, B) = A ⊆ B && B ⊆ A function test_reparametrization(old_ode, new_ode, var_mapping, implicit_relations) - # NOTE: should this be strengthened to == ? - # @test map(string, old_ode.u_vars) ≡ map(string, new_ode.u_vars) - # @test map(string, old_ode.y_vars) ≡ map(string, new_ode.y_vars) + @test map(var_to_str, old_ode.u_vars) ≡ map(var_to_str, new_ode.u_vars) + @test map(var_to_str, old_ode.y_vars) ≡ map(var_to_str, new_ode.y_vars) @test length(old_ode.y_vars) == length(new_ode.y_vars) @test length(old_ode.u_vars) == length(new_ode.u_vars) @test base_ring(parent(first(values(var_mapping)))) == parent(old_ode) From b0dc2e3b6deb0349e732d21d03cfa9bb5c573e1d Mon Sep 17 00:00:00 2001 From: Alexander Demin Date: Wed, 4 Nov 2026 22:05:50 +0100 Subject: [PATCH 2/9] remove debug --- src/parametrizations.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/parametrizations.jl b/src/parametrizations.jl index 24db59cf1..6b5c6260b 100644 --- a/src/parametrizations.jl +++ b/src/parametrizations.jl @@ -38,7 +38,6 @@ function suggest_variable_names(ode, new_states, new_params) name == nothing && (name = tildify(name_orig)) # resolve collision a*x -> x̃ and b*x -> x̃ j = 1 - @info "" name while name in names name = chopsuffix(chopsuffix(name, "(t)"), string(j-1)) name = string(name, j, "(t)") From b5a381564897b11a298310e1929ff706a5ecd0e0 Mon Sep 17 00:00:00 2001 From: pogudingleb Date: Thu, 5 Nov 2026 07:56:35 +0100 Subject: [PATCH 3/9] restoring the interface --- src/io_equation.jl | 10 ++++++---- test/bodies/common_ring.jl | 4 ++-- test/bodies/diffreduction.jl | 6 +++--- test/bodies/find_leader.jl | 2 +- test/bodies/io_cases.jl | 2 +- test/bodies/pb_representation.jl | 4 ++-- test/bodies/raw_generators.jl | 2 +- test/bodies/state_generators.jl | 4 ++-- 8 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/io_equation.jl b/src/io_equation.jl index b0b671059..4bced71ff 100644 --- a/src/io_equation.jl +++ b/src/io_equation.jl @@ -282,9 +282,7 @@ Input: - `var_change_policy` - whether to perform automatic variable change, can be one of `:default`, `:yes`, `:no` - `loglevel` - logging level (default: Logging.Info) -Output: -- a dictionary from “leaders” to the corresponding input-output equations; -- boolean value determining if the ideal is prime (if `true` definitely prime, if `false` may still be prime) +Output: a dictionary from “leaders” to the corresponding input-output equations """ @timeit _to function find_ioequations( ode::ODE{P}; @@ -294,7 +292,11 @@ Output: restart_logging(loglevel = loglevel) reset_timings() with_logger(_si_logger[]) do - return _find_ioequations(ode, var_change_policy = var_change_policy) + ioeqs, is_prime = _find_ioequations(ode, var_change_policy = var_change_policy) + if !is_prime + @warn "The computed set of IO-projections may not define a prime ideal (in particular, the coefficients may not generate all identifiable functions)" + end + return ioeqs end end diff --git a/test/bodies/common_ring.jl b/test/bodies/common_ring.jl index 14d0da3b1..37111cd1f 100644 --- a/test/bodies/common_ring.jl +++ b/test/bodies/common_ring.jl @@ -3,7 +3,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) if GROUP == "All" || GROUP == "Core" @testset "Computing common ring for the PB-reduction" begin ode = @ODEmodel(x1'(t) = x2(t), x2'(t) = a * x1(t), y(t) = x1(t)) - ioeqs = find_ioequations(ode)[1] + ioeqs = find_ioequations(ode) pbr = PBRepresentation(ode, ioeqs) R, (y_2, y_5, c) = Nemo.polynomial_ring(Nemo.QQ, ["y(t)_2", "y(t)_5", "c"]) p = y_2^2 + c * y_5 @@ -18,7 +18,7 @@ if GROUP == "All" || GROUP == "Core" y1(t) = x1(t), y2(t) = x2(t) + u(t) ) - ioeqs = find_ioequations(ode)[1] + ioeqs = find_ioequations(ode) pbr = PBRepresentation(ode, ioeqs) R, (y1_0, y2_3, u_3) = Nemo.polynomial_ring(Nemo.QQ, ["y1(t)_0", "y2(t)_3", "u(t)_3"]) diff --git a/test/bodies/diffreduction.jl b/test/bodies/diffreduction.jl index 300d0c5ff..b82c24375 100644 --- a/test/bodies/diffreduction.jl +++ b/test/bodies/diffreduction.jl @@ -2,7 +2,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) @testset "Differential reduction" begin ode = @ODEmodel(x'(t) = a * x(t), y(t) = x(t)) - pbr = PBRepresentation(ode, find_ioequations(ode)[1]) + pbr = PBRepresentation(ode, find_ioequations(ode)) R, (y_5, y_4) = Nemo.polynomial_ring(Nemo.QQ, ["y(t)_5", "y(t)_4"]) res = diffreduce(y_5, pbr) @test res == str_to_var("a", parent(res))^5 * str_to_var("y(t)_0", parent(res)) @@ -12,7 +12,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) @test res == str_to_var("a", parent(res))^8 * str_to_var("y(t)_0", parent(res))^2 ode = @ODEmodel(x1'(t) = x2(t), x2'(t) = -x1(t), y(t) = x1(t) + u(t)) - pbr = PBRepresentation(ode, find_ioequations(ode)[1]) + pbr = PBRepresentation(ode, find_ioequations(ode)) R, (y_5, y_4, u_10) = Nemo.polynomial_ring(Nemo.QQ, ["y(t)_5", "y(t)_4", "u(t)_10"]) res = diffreduce(y_4 + u_10, pbr) @test res == @@ -128,7 +128,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) x2'(t) = x2(t) + 3 * x1(t) * x2(t), y(t) = x1(t) ) - pbr = PBRepresentation(ode, find_ioequations(ode)[1]) + pbr = PBRepresentation(ode, find_ioequations(ode)) R, (y_0, y_1, y_2, y_3, y_4, u_0, u_3) = Nemo.polynomial_ring( Nemo.QQ, ["y(t)_0", "y(t)_1", "y(t)_2", "y(t)_3", "y(t)_4", "u(t)_0", "u(t)_3"], diff --git a/test/bodies/find_leader.jl b/test/bodies/find_leader.jl index db9687860..28ba0f4e5 100644 --- a/test/bodies/find_leader.jl +++ b/test/bodies/find_leader.jl @@ -8,7 +8,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) y1(t) = x1(t), y2(t) = x2(t) + u(t) ) - ioeqs = find_ioequations(ode)[1] + ioeqs = find_ioequations(ode) println("IOEQS: ", ioeqs) pbr = PBRepresentation(ode, ioeqs) diff --git a/test/bodies/io_cases.jl b/test/bodies/io_cases.jl index da025ec0b..39734c360 100644 --- a/test/bodies/io_cases.jl +++ b/test/bodies/io_cases.jl @@ -168,7 +168,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) for case in test_cases ode = case[:ode] - ioeq_dict, is_prime = find_ioequations(ode) + ioeq_dict, is_prime = _find_ioequations(ode) ring = parent(first(values(case[:correct][1]))) for (lead, ioeq) in ioeq_dict lead = parent_ring_change(lead, ring) diff --git a/test/bodies/pb_representation.jl b/test/bodies/pb_representation.jl index 41a94732c..e0459b4ef 100644 --- a/test/bodies/pb_representation.jl +++ b/test/bodies/pb_representation.jl @@ -2,7 +2,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) @testset "PB-representation - creation" begin ode = @ODEmodel(x1'(t) = x2(t), x2'(t) = a * x1(t), y(t) = x1(t)) - ioeqs = find_ioequations(ode)[1] + ioeqs = find_ioequations(ode) pbr = PBRepresentation(ode, ioeqs) @test pbr.profile == Dict("y(t)" => 2) @test pbr.u_names == Array{String, 1}() @@ -10,7 +10,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) @test pbr.param_names == ["a"] ode = @ODEmodel(x1'(t) = x1(t), x2'(t) = a * x2(t), y(t) = x1(t), y2(t) = x2(t) + u(t)) - ioeqs = find_ioequations(ode)[1] + ioeqs = find_ioequations(ode) pbr = PBRepresentation(ode, ioeqs) @test pbr.profile == Dict("y(t)" => 1, "y2(t)" => 1) @test pbr.u_names == ["u(t)"] diff --git a/test/bodies/raw_generators.jl b/test/bodies/raw_generators.jl index 92bd8525e..d3633cd2a 100644 --- a/test/bodies/raw_generators.jl +++ b/test/bodies/raw_generators.jl @@ -14,7 +14,7 @@ import Groebner y(t) = k * I(t) ) - io_eqs = StructuralIdentifiability.find_ioequations(ode)[1] + io_eqs = StructuralIdentifiability.find_ioequations(ode) id_funcs, bring = StructuralIdentifiability.extract_identifiable_functions_raw( io_eqs, ode, diff --git a/test/bodies/state_generators.jl b/test/bodies/state_generators.jl index 9001b4411..d0ced1874 100644 --- a/test/bodies/state_generators.jl +++ b/test/bodies/state_generators.jl @@ -7,7 +7,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) y(t) = x1(t) ) - sg = states_generators(ode, find_ioequations(ode)[1]) + sg = states_generators(ode, find_ioequations(ode)) @test sg == [ x1 // 1, (a * x1 - b * x1 * x2) // 1, @@ -21,7 +21,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) x4'(t) = 1 // x2(t), y(t) = x1(t) ) - sg = states_generators(ode, find_ioequations(ode)[1]) + sg = states_generators(ode, find_ioequations(ode)) @test sg == [x1 // 1, x2 // 1, x3 // 1, x4 // 1, x2 // 1] ode = @ODEmodel( From d75827bf42b11470bf01807e27f5666d13f1db02 Mon Sep 17 00:00:00 2001 From: pogudingleb Date: Thu, 5 Nov 2026 07:58:12 +0100 Subject: [PATCH 4/9] Revert "restoring the interface" This reverts commit b5a381564897b11a298310e1929ff706a5ecd0e0. --- src/io_equation.jl | 10 ++++------ test/bodies/common_ring.jl | 4 ++-- test/bodies/diffreduction.jl | 6 +++--- test/bodies/find_leader.jl | 2 +- test/bodies/io_cases.jl | 2 +- test/bodies/pb_representation.jl | 4 ++-- test/bodies/raw_generators.jl | 2 +- test/bodies/state_generators.jl | 4 ++-- 8 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/io_equation.jl b/src/io_equation.jl index 4bced71ff..b0b671059 100644 --- a/src/io_equation.jl +++ b/src/io_equation.jl @@ -282,7 +282,9 @@ Input: - `var_change_policy` - whether to perform automatic variable change, can be one of `:default`, `:yes`, `:no` - `loglevel` - logging level (default: Logging.Info) -Output: a dictionary from “leaders” to the corresponding input-output equations +Output: +- a dictionary from “leaders” to the corresponding input-output equations; +- boolean value determining if the ideal is prime (if `true` definitely prime, if `false` may still be prime) """ @timeit _to function find_ioequations( ode::ODE{P}; @@ -292,11 +294,7 @@ Output: a dictionary from “leaders” to the corresponding input-output equati restart_logging(loglevel = loglevel) reset_timings() with_logger(_si_logger[]) do - ioeqs, is_prime = _find_ioequations(ode, var_change_policy = var_change_policy) - if !is_prime - @warn "The computed set of IO-projections may not define a prime ideal (in particular, the coefficients may not generate all identifiable functions)" - end - return ioeqs + return _find_ioequations(ode, var_change_policy = var_change_policy) end end diff --git a/test/bodies/common_ring.jl b/test/bodies/common_ring.jl index 37111cd1f..14d0da3b1 100644 --- a/test/bodies/common_ring.jl +++ b/test/bodies/common_ring.jl @@ -3,7 +3,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) if GROUP == "All" || GROUP == "Core" @testset "Computing common ring for the PB-reduction" begin ode = @ODEmodel(x1'(t) = x2(t), x2'(t) = a * x1(t), y(t) = x1(t)) - ioeqs = find_ioequations(ode) + ioeqs = find_ioequations(ode)[1] pbr = PBRepresentation(ode, ioeqs) R, (y_2, y_5, c) = Nemo.polynomial_ring(Nemo.QQ, ["y(t)_2", "y(t)_5", "c"]) p = y_2^2 + c * y_5 @@ -18,7 +18,7 @@ if GROUP == "All" || GROUP == "Core" y1(t) = x1(t), y2(t) = x2(t) + u(t) ) - ioeqs = find_ioequations(ode) + ioeqs = find_ioequations(ode)[1] pbr = PBRepresentation(ode, ioeqs) R, (y1_0, y2_3, u_3) = Nemo.polynomial_ring(Nemo.QQ, ["y1(t)_0", "y2(t)_3", "u(t)_3"]) diff --git a/test/bodies/diffreduction.jl b/test/bodies/diffreduction.jl index b82c24375..300d0c5ff 100644 --- a/test/bodies/diffreduction.jl +++ b/test/bodies/diffreduction.jl @@ -2,7 +2,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) @testset "Differential reduction" begin ode = @ODEmodel(x'(t) = a * x(t), y(t) = x(t)) - pbr = PBRepresentation(ode, find_ioequations(ode)) + pbr = PBRepresentation(ode, find_ioequations(ode)[1]) R, (y_5, y_4) = Nemo.polynomial_ring(Nemo.QQ, ["y(t)_5", "y(t)_4"]) res = diffreduce(y_5, pbr) @test res == str_to_var("a", parent(res))^5 * str_to_var("y(t)_0", parent(res)) @@ -12,7 +12,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) @test res == str_to_var("a", parent(res))^8 * str_to_var("y(t)_0", parent(res))^2 ode = @ODEmodel(x1'(t) = x2(t), x2'(t) = -x1(t), y(t) = x1(t) + u(t)) - pbr = PBRepresentation(ode, find_ioequations(ode)) + pbr = PBRepresentation(ode, find_ioequations(ode)[1]) R, (y_5, y_4, u_10) = Nemo.polynomial_ring(Nemo.QQ, ["y(t)_5", "y(t)_4", "u(t)_10"]) res = diffreduce(y_4 + u_10, pbr) @test res == @@ -128,7 +128,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) x2'(t) = x2(t) + 3 * x1(t) * x2(t), y(t) = x1(t) ) - pbr = PBRepresentation(ode, find_ioequations(ode)) + pbr = PBRepresentation(ode, find_ioequations(ode)[1]) R, (y_0, y_1, y_2, y_3, y_4, u_0, u_3) = Nemo.polynomial_ring( Nemo.QQ, ["y(t)_0", "y(t)_1", "y(t)_2", "y(t)_3", "y(t)_4", "u(t)_0", "u(t)_3"], diff --git a/test/bodies/find_leader.jl b/test/bodies/find_leader.jl index 28ba0f4e5..db9687860 100644 --- a/test/bodies/find_leader.jl +++ b/test/bodies/find_leader.jl @@ -8,7 +8,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) y1(t) = x1(t), y2(t) = x2(t) + u(t) ) - ioeqs = find_ioequations(ode) + ioeqs = find_ioequations(ode)[1] println("IOEQS: ", ioeqs) pbr = PBRepresentation(ode, ioeqs) diff --git a/test/bodies/io_cases.jl b/test/bodies/io_cases.jl index 39734c360..da025ec0b 100644 --- a/test/bodies/io_cases.jl +++ b/test/bodies/io_cases.jl @@ -168,7 +168,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) for case in test_cases ode = case[:ode] - ioeq_dict, is_prime = _find_ioequations(ode) + ioeq_dict, is_prime = find_ioequations(ode) ring = parent(first(values(case[:correct][1]))) for (lead, ioeq) in ioeq_dict lead = parent_ring_change(lead, ring) diff --git a/test/bodies/pb_representation.jl b/test/bodies/pb_representation.jl index e0459b4ef..41a94732c 100644 --- a/test/bodies/pb_representation.jl +++ b/test/bodies/pb_representation.jl @@ -2,7 +2,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) @testset "PB-representation - creation" begin ode = @ODEmodel(x1'(t) = x2(t), x2'(t) = a * x1(t), y(t) = x1(t)) - ioeqs = find_ioequations(ode) + ioeqs = find_ioequations(ode)[1] pbr = PBRepresentation(ode, ioeqs) @test pbr.profile == Dict("y(t)" => 2) @test pbr.u_names == Array{String, 1}() @@ -10,7 +10,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) @test pbr.param_names == ["a"] ode = @ODEmodel(x1'(t) = x1(t), x2'(t) = a * x2(t), y(t) = x1(t), y2(t) = x2(t) + u(t)) - ioeqs = find_ioequations(ode) + ioeqs = find_ioequations(ode)[1] pbr = PBRepresentation(ode, ioeqs) @test pbr.profile == Dict("y(t)" => 1, "y2(t)" => 1) @test pbr.u_names == ["u(t)"] diff --git a/test/bodies/raw_generators.jl b/test/bodies/raw_generators.jl index d3633cd2a..92bd8525e 100644 --- a/test/bodies/raw_generators.jl +++ b/test/bodies/raw_generators.jl @@ -14,7 +14,7 @@ import Groebner y(t) = k * I(t) ) - io_eqs = StructuralIdentifiability.find_ioequations(ode) + io_eqs = StructuralIdentifiability.find_ioequations(ode)[1] id_funcs, bring = StructuralIdentifiability.extract_identifiable_functions_raw( io_eqs, ode, diff --git a/test/bodies/state_generators.jl b/test/bodies/state_generators.jl index d0ced1874..9001b4411 100644 --- a/test/bodies/state_generators.jl +++ b/test/bodies/state_generators.jl @@ -7,7 +7,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) y(t) = x1(t) ) - sg = states_generators(ode, find_ioequations(ode)) + sg = states_generators(ode, find_ioequations(ode)[1]) @test sg == [ x1 // 1, (a * x1 - b * x1 * x2) // 1, @@ -21,7 +21,7 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) x4'(t) = 1 // x2(t), y(t) = x1(t) ) - sg = states_generators(ode, find_ioequations(ode)) + sg = states_generators(ode, find_ioequations(ode)[1]) @test sg == [x1 // 1, x2 // 1, x3 // 1, x4 // 1, x2 // 1] ode = @ODEmodel( From ef41ee2d8d1b58bb5e4b27fe0bbb89db83ad006e Mon Sep 17 00:00:00 2001 From: Alexander Demin Date: Fri, 6 Nov 2026 21:24:14 +0100 Subject: [PATCH 5/9] more compact logging --- src/parametrizations.jl | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/parametrizations.jl b/src/parametrizations.jl index 6b5c6260b..a6bc69c5c 100644 --- a/src/parametrizations.jl +++ b/src/parametrizations.jl @@ -85,13 +85,14 @@ function reparametrize_with_respect_to(ode, new_vars) contains_states(poly::MPolyRingElem) = any(x -> degree(poly, x) > 0, ode.x_vars) contains_states(func) = contains_states(numerator(func)) || contains_states(denominator(func)) new_vars_contains_states = filter(contains_states, new_vars) - @info """ - Original variables: + @info "Original variables: $(length(ode.x_vars)) states and $(length(ode.parameters)) parameters" + @debug """Original variables States: $(join(map(string, ode.x_vars), ", ")) Params: $(join(map(string, ode.parameters), ", "))""" new_states = new_vars_contains_states new_params = setdiff(new_vars, new_vars_contains_states) - @info """ + @info "New macro variables: $(length(new_states)) states and $(length(new_params)) parameters" + @debug """ Reparametrizing with respect to: New states: $(join(map(string, new_states), ", ")) New params: $(join(map(string, new_params), ", "))""" @@ -226,7 +227,7 @@ function show_reparametrization(out, reparam) println(out, "\n", "• Reparametrized ODE:\n") println(out, ode_repr(reparam.new_ode), "\n") - println(out, "• Definitions:", "\n") + println(out, "• Definitions of new variables:", "\n") println(out, join(map(p -> replace(string(" "^2, p), "=>" => ":="), collect(reparam.new_vars)), "\n"), "\n") if !isempty(reparam.implicit_relations) From d2da4a9de0731e408ae5e58dd3b8576313ee0063 Mon Sep 17 00:00:00 2001 From: Alexander Demin <60229118+sumiya11@users.noreply.github.com> Date: Fri, 6 Nov 2026 21:34:13 +0100 Subject: [PATCH 6/9] Update src/parametrizations.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/parametrizations.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parametrizations.jl b/src/parametrizations.jl index a6bc69c5c..7a79c1290 100644 --- a/src/parametrizations.jl +++ b/src/parametrizations.jl @@ -337,5 +337,5 @@ function _reparametrize_global(ode::ODE{P}; prob_threshold = 0.99, seed = 42, cm new_ode = ODE{P}(new_x_vars, new_y_vars, new_vector_field, new_outputs, new_inputs) reparam = (new_ode = new_ode, new_vars = new_vars, implicit_relations = implicit_relations) show_reparametrization(stdout, reparam) - reparam + return reparam end From 613fd2701109f8d521e13e0700241adad1857351 Mon Sep 17 00:00:00 2001 From: Alexander Demin <60229118+sumiya11@users.noreply.github.com> Date: Fri, 6 Nov 2026 21:34:41 +0100 Subject: [PATCH 7/9] Update src/parametrizations.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/parametrizations.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parametrizations.jl b/src/parametrizations.jl index 7a79c1290..f0d25948f 100644 --- a/src/parametrizations.jl +++ b/src/parametrizations.jl @@ -39,7 +39,7 @@ function suggest_variable_names(ode, new_states, new_params) # resolve collision a*x -> x̃ and b*x -> x̃ j = 1 while name in names - name = chopsuffix(chopsuffix(name, "(t)"), string(j-1)) + name = chopsuffix(chopsuffix(name, "(t)"), string(j - 1)) name = string(name, j, "(t)") j += 1 end From e86ab32ef163f2979f4134044a12dceec7404c8a Mon Sep 17 00:00:00 2001 From: Alexander Demin Date: Fri, 6 Nov 2026 23:48:55 +0100 Subject: [PATCH 8/9] resolve nasty collisions --- Project.toml | 2 + src/ODE.jl | 4 +- src/StructuralIdentifiability.jl | 1 + src/parametrizations.jl | 55 ++++++++++++++++++-------- test/bodies/ode.jl | 66 ++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 18 deletions(-) diff --git a/Project.toml b/Project.toml index 1af4ff4a1..2f14b2927 100644 --- a/Project.toml +++ b/Project.toml @@ -20,6 +20,7 @@ Primes = "27ebfcd6-29c5-5fa9-bf4b-fb8fc14df3ae" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" RationalFunctionFields = "73480bc8-48a2-41cc-880f-208b490ccf65" TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f" +Unicode = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" [weakdeps] ModelingToolkitBase = "7771a370-6774-4173-bd38-47e70ca0b839" @@ -57,6 +58,7 @@ Symbolics = "7" Test = "1.10, 1.11" TestSetExtensions = "4" TimerOutputs = "0.5.10, 1.0" +Unicode = "1.10, 1.11" julia = "1.10.4, 1.11.2" [extras] diff --git a/src/ODE.jl b/src/ODE.jl index f2a8e4fb4..4cd0524ae 100644 --- a/src/ODE.jl +++ b/src/ODE.jl @@ -258,8 +258,8 @@ function rename_variables( for (i, old_name) in enumerate(new_varnames) if old_name in keys(transform) new_varnames[i] = transform[old_name] - elseif endswith(old_name, "(t)") && old_name[1:(end - 3)] in keys(transform) - new_varnames[i] = transform[old_name[1:(end - 3)]] * "(t)" + elseif endswith(old_name, "(t)") && chopsuffix(old_name, "(t)") in keys(transform) + new_varnames[i] = transform[chopsuffix(old_name, "(t)")] * "(t)" end end diff --git a/src/StructuralIdentifiability.jl b/src/StructuralIdentifiability.jl index f5dfffd93..ebf073d4d 100644 --- a/src/StructuralIdentifiability.jl +++ b/src/StructuralIdentifiability.jl @@ -15,6 +15,7 @@ import Primes import Random import TimerOutputs using TimerOutputs: @timeit, disable_timer!, enable_timer! +import Unicode # Algebra packages import AbstractAlgebra diff --git a/src/parametrizations.jl b/src/parametrizations.jl index a6bc69c5c..23d96fcb6 100644 --- a/src/parametrizations.jl +++ b/src/parametrizations.jl @@ -16,9 +16,14 @@ function vector_field_along(derivation::Dict{T, U}, directions::AbstractVector) return new_vector_field end -function suggest_variable_names(ode, new_states, new_params) - @assert allunique(map(string, gens(parent(ode)))) +function suggest_names(ode, new_states, new_params) + unorm(s) = Unicode.normalize(s; chartransform = Unicode.julia_chartransform) + original_names = unorm.(string.(gens(parent(ode)))) + + @assert allunique(original_names) names = String[] + tildify(c::Char) = string(c, '\u0303') + tildify(s) = tildify(first(s)) * s[nextind(s, firstindex(s)):end] for (i, def) in enumerate(new_states) num, den = unpack_fraction(def) @@ -30,19 +35,10 @@ function suggest_variable_names(ode, new_states, new_params) end # a*x+b --> x̃ - tildify(c::Char) = string(c, '\u0303') - tildify(s) = tildify(first(s)) * s[nextind(s, firstindex(s)):end] states = intersect(vars(def), ode.x_vars) if length(states) == 1 name_orig = var_to_str(states[1]) name == nothing && (name = tildify(name_orig)) - # resolve collision a*x -> x̃ and b*x -> x̃ - j = 1 - while name in names - name = chopsuffix(chopsuffix(name, "(t)"), string(j-1)) - name = string(name, j, "(t)") - j += 1 - end end # f(x1,...,xn) --> X_i @@ -60,6 +56,12 @@ function suggest_variable_names(ode, new_states, new_params) name == nothing && (name = var_to_str(num)) end + # f(k) --> k̃ + params = intersect(vars(def), ode.parameters) + if length(params) == 1 + name == nothing && (name = tildify(var_to_str(params[1]))) + end + # f(x) --> ai name == nothing && (name = "a$i") @@ -68,6 +70,19 @@ function suggest_variable_names(ode, new_states, new_params) append!(names, string.(ode.u_vars), string.(ode.y_vars)) + # Resolve collisions + reserved, used = Set(original_names), Set{String}() + for (i, def) in enumerate(vcat(new_states, new_params, ode.u_vars, ode.y_vars)) + base = names[i] + suffix = endswith(base, "(t)") ? "(t)" : "" + j = 2 + while unorm(names[i]) in used || (names[i] != string(def) && unorm(names[i]) in reserved) + names[i] = string(chopsuffix(base, suffix), "_", j, suffix) + j += 1 + end + push!(used, unorm(names[i])) + end + names end @@ -178,7 +193,7 @@ function reparametrize_with_respect_to(ode, new_states, new_params) new_vars_vector_field[state] = new_dynamics_states[i] end @info "Converting variable names to human-readable ones" - new_names = suggest_variable_names(ode, new_states, new_params) + new_names = suggest_names(ode, new_states, new_params) @assert allunique(new_names) ring_output, _ = polynomial_ring( base_ring(ring_of_tags), @@ -227,12 +242,18 @@ function show_reparametrization(out, reparam) println(out, "\n", "• Reparametrized ODE:\n") println(out, ode_repr(reparam.new_ode), "\n") - println(out, "• Definitions of new variables:", "\n") - println(out, join(map(p -> replace(string(" "^2, p), "=>" => ":="), collect(reparam.new_vars)), "\n"), "\n") + is_new = [string(v) != string(f) for (v, f) in reparam.new_vars] + println(out, "• Variable definitions ($(count(is_new)) new):\n") + for ((v, f), new) in zip(reparam.new_vars, is_new) + print(out, " ", v, " := ", f) + new && printstyled(out, " (new)"; color = :green) + println(out) + end + println(out) if !isempty(reparam.implicit_relations) participants = mapreduce(vars, union, reparam.implicit_relations) - println(out, "• These variables are not independent: $(join(map(string, participants), ", "))", "\n") + println(out, "• These variables are not independent:\n $(join(map(string, participants), ", "))", "\n") end println(out, "• For more info, run\n\njulia> @doc reparametrize_global\n") @@ -336,6 +357,8 @@ function _reparametrize_global(ode::ODE{P}; prob_threshold = 0.99, seed = 42, cm new_y_vars = filter(v -> haskey(new_outputs, v), gens(new_ring)) new_ode = ODE{P}(new_x_vars, new_y_vars, new_vector_field, new_outputs, new_inputs) reparam = (new_ode = new_ode, new_vars = new_vars, implicit_relations = implicit_relations) - show_reparametrization(stdout, reparam) + # if Logging.min_enabled_level(current_logger()) < Logging.Warn + @info (show_reparametrization(stdout, reparam); "Done!") + # end reparam end diff --git a/test/bodies/ode.jl b/test/bodies/ode.jl index 51b3a9ae9..d70cfea28 100644 --- a/test/bodies/ode.jl +++ b/test/bodies/ode.jl @@ -29,6 +29,72 @@ include(joinpath(@__DIR__, "..", "shared", "test_setup.jl")) ) end +# Some nasty corner cases +@testset "Reparametrization names" begin + suggest_names = StructuralIdentifiability.suggest_names + ode = @ODEmodel( + x10'(t) = a1 * x10(t) + b * z(t) + x̃10(t), + z'(t) = 0, + X3(t) = x10(t), + ) + @test suggest_names(ode, ode.x_vars, ode.parameters) == + string.(vcat(ode.x_vars, ode.parameters, ode.u_vars, ode.y_vars)) + + # Preserve indices and reserve names of parameters, inputs, and outputs. + @test suggest_names( + ode, [a1 * x10, b * x10, x10 + z, x10] .// one(parent(ode)), + [a1 + b, a1] .// one(parent(ode)), + ) == ["x̃10_2(t)", "x̃10_3(t)", "X3_2(t)", "x10(t)", "a1_2", "a1", "x̃10(t)", "X3(t)"] + @test suggest_names(ode, [x10], [a1, a1]) == + ["x10(t)", "a1", "a1_2", "x̃10(t)", "X3(t)"] + + # An unchanged name always wins. + ode = @ODEmodel(n'(t) = a * n(t), ñ'(t) = 0, y(t) = n(t)) + for states in ([a * n, ñ], [ñ, a * n]) + names = suggest_names(ode, states, [a]) + @test Dict(names[1:2] .=> states) == Dict("ñ(t)" => ñ, "ñ_2(t)" => a * n) + end + + R, (x0, a, y) = polynomial_ring(QQ, ["x0", "a", "y"]) + ode = ODE{typeof(x0)}(Dict(x0 => a * x0), Dict(y => x0), typeof(x0)[]) + @test suggest_names(ode, [a * x0, 2 * a * x0], [a]) == ["x̃0", "x̃0_2", "a", "y"] + + ode = @ODEmodel(x'(t) = (k8 + k9 + k10) * x(t), y(t) = x(t)) + @test suggest_names(ode, [x], [k9^2]) == ["x(t)", "k̃9", "y(t)"] + @test suggest_names(ode, [x], [k9^2, 1 // k9, k9]) == ["x(t)", "k̃9", "k̃9_2", "k9", "y(t)"] + @test suggest_names(ode, [x], [k10 + 2 * k8, k10 * k9]) == ["x(t)", "a1", "a2", "y(t)"] + @test suggest_names(ode, empty(ode.x_vars), [k9^2]) == ["k̃9", "y(t)"] +end + +@testset "Rename variables" begin + ode = @ODEmodel( + x'(t) = (a * x(t) + u(t)) // (b + x(t)), + z'(t) = x(t) - z(t), + y(t) = x(t) + z(t), + ) + original = sprint(show, ode) + @test sprint(show, rename_variables(ode, Dict{String, String}())) == original + for transform in ( + Dict("x" => "χ", "a" => "α", "u" => "signal", "y" => "output"), + Dict("x" => "ignored", "x(t)" => "χ(t)", "a" => "α", "u(t)" => "signal(t)", "y(t)" => "output(t)"), + ) + renamed = rename_variables(ode, transform) + χ, z, α, b, signal, output = str_to_var.( + ["χ(t)", "z(t)", "α", "b", "signal(t)", "output(t)"], Ref(parent(renamed)), + ) + @test renamed.x_vars == [χ, z] + @test renamed.u_vars == [signal] + @test renamed.y_vars == [output] + @test Set(string.(renamed.parameters)) == Set(["α", "b"]) + @test renamed.x_equations[χ] == (α * χ + signal) // (b + χ) + @test renamed.x_equations[z] == χ - z + @test renamed.y_equations[output] == χ + z + restored = rename_variables(renamed, Dict("χ" => "x", "α" => "a", "signal" => "u", "output" => "y")) + @test sprint(show, restored) == original + @test sprint(show, ode) == original + end +end + @testset "ODE/DDE unicode" begin ode = StructuralIdentifiability.@ODEmodel( 🐁'(t) = a * 🐁 - b * 🐁 * 🦉, From 80dec24c10be71a1c30583706eb9345216dd399d Mon Sep 17 00:00:00 2001 From: Alexander Demin Date: Fri, 6 Nov 2026 23:53:52 +0100 Subject: [PATCH 9/9] clean docstring --- src/identifiable_functions.jl | 2 +- src/parametrizations.jl | 12 ++---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/identifiable_functions.jl b/src/identifiable_functions.jl index 84589a7e1..390c100a0 100644 --- a/src/identifiable_functions.jl +++ b/src/identifiable_functions.jl @@ -40,7 +40,7 @@ julia> ode = @ODEmodel( y(t) = x0(t) ); -julia> identifiable_functions = find_identifiable_functions(ode; loglevel = Logging.Error); +julia> identifiable_functions = find_identifiable_functions(ode); julia> length(identifiable_functions) 2 diff --git a/src/parametrizations.jl b/src/parametrizations.jl index 530f5a775..161dd2ba9 100644 --- a/src/parametrizations.jl +++ b/src/parametrizations.jl @@ -283,24 +283,16 @@ The function accepts the following optional arguments. ## Example -```jldoctest +```jldoctest; setup = :(using Logging; Logging.disable_logging(Logging.Info);) julia> using StructuralIdentifiability -julia> using Logging - -julia> Logging.disable_logging(Logging.Info); - julia> ode = @ODEmodel( x1'(t) = a * x1(t) - b*x1(t)*x2(t), x2'(t) = -c * x2(t) + d*x1(t)*x2(t), y(t) = x1(t) ); -julia> Logging.disable_logging(Logging.BelowMinLevel); - -julia> new_ode, new_vars, relations = reparametrize_global( - ode; loglevel = Logging.Error - ); +julia> new_ode, new_vars, relations = reparametrize_global(ode); ``` Then, we have the following: