Skip to content

Substituting a constant = 1 seems to mutate variable type of certain types of parameters #3487

New issue

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

By clicking “No 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? No Sign in to your account

Closed
vyudu opened this issue Mar 22, 2025 · 2 comments · Fixed by JuliaSymbolics/SymbolicUtils.jl#734
Assignees
Labels
bug Something isn't working

Comments

@vyudu
Copy link
Member

vyudu commented Mar 22, 2025

I'm really not sure what's going on here, but the variable type of Initial(p) seems to be silently changed to variable instead of parameter when substituting h = 1. This doesn't happen when any number other than 1 is substituted, or for normal parameters.

@constants h = 1
@variables x
eq = x ~ h * Initial(x)
dvs = Set(); ps = Set()
ModelingToolkit.collect_vars!(dvs, ps, eq, nothing; op = Initial) # dvs = Set(x), ps = Set(Initial(x))
empty!(dvs); empty!(ps)

eq = Symbolics.substitute(eq, h => 1) # eq = x ~ Initial(x)
ModelingToolkit.collect_vars!(dvs, ps, eq, nothing; op = Initial) # dvs = Set(x, Initial(x)), ps = Set()
ModelingToolkit.isparameter(eq.rhs) # false

on 9.65.0

@AayushSabharwal
Copy link
Member

This isn't hashconsing, it's a bug in substitute. When substituting h => 1, it ends up in a state where

expr = h * Initial(x)
op = *
args = [1, Initial(x)] # values after substitution into `expr`

It then calls maketerm(typeof(expr), op, args, metadata(expr)). The problem is that this turns 1 * Initial(x) into Initial(x), then sets the metadata of the result to metadata(expr), which erases the parameter metadata from Initial(x).

@vyudu
Copy link
Member Author

vyudu commented Apr 1, 2025

Thanks so much Aayush!

No Sign up for free to join this conversation on GitHub. Already have an account? No Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants