JuliaSymbolics / Symbolics.jl

Symbolic programming for the next generation of numerical software
https://docs.sciml.ai/Symbolics/stable/
Other
1.37k stars 154 forks source link

Simplified API functions for creating Variables #23

Closed isaacsas closed 3 years ago

isaacsas commented 4 years ago

Just a proposal; from the perspective of questions like https://github.com/SciML/Catalyst.jl/issues/281 and my own the other day, the current notation for building Syms and Terms programmatically seems complicated:

t = Num(Variable{ModelingToolkit.Parameter{Real}}(:t))  # t
x = Num(Variable{ModelingToolkit.FnType{Tuple{Any},Real}}(Symbol("x"),1))(t) # x_1(t)

Would there be objections to adding a few helper functions to simplify this for common use cases? I know @ChrisRackauckas prefers to keep the API smaller, but I suspect this is going to lead to many user questions in the future as understanding the typing here requires going in and reading the SymbolicUtils docs too. (Not to mention it is much more complicated than just remembering Operation and Variable with appropriate args.) Any thoughts?

YingboMa commented 3 years ago

We can make the @variables macro take the runtime value when write it like @variables $t.

ChrisRackauckas commented 3 years ago

I really like that proposal.

paulflang commented 2 years ago

How would I do that in a loop?

term = "par1 + par2/par1"
pars = ["par1", "par2"]
for par in pars
    sym = Symbol(par)
    @variables $sym
end

ex = eval(Meta.parse(term))  # UndefVarError: par2 not defined

(cc @axla-io)

paulflang commented 2 years ago

Sorted. Variables can be defined in a custom module to evaluate symbolic expression as is done in ReactionNetworkImporters.jl here.