From 90567a05bb7a52fb7f50bd0f08197dd75d497643 Mon Sep 17 00:00:00 2001 From: ArnoStrouwen Date: Sat, 5 Oct 2024 16:48:26 +0200 Subject: [PATCH] move symbolic details to Symbolics docs --- docs/pages.jl | 1 - docs/src/examples/parsing.md | 33 -------------- .../tutorials/programmatically_generating.md | 44 ++----------------- 3 files changed, 3 insertions(+), 75 deletions(-) delete mode 100644 docs/src/examples/parsing.md diff --git a/docs/pages.jl b/docs/pages.jl index f92f869def..d772c32471 100644 --- a/docs/pages.jl +++ b/docs/pages.jl @@ -17,7 +17,6 @@ pages = [ "Basic Examples" => Any["examples/higher_order.md", "examples/spring_mass.md", "examples/modelingtoolkitize_index_reduction.md", - "examples/parsing.md", "examples/remake.md"], "Advanced Examples" => Any["examples/tearing_parallelism.md", "examples/sparse_jacobians.md", diff --git a/docs/src/examples/parsing.md b/docs/src/examples/parsing.md deleted file mode 100644 index 66a4e4d82f..0000000000 --- a/docs/src/examples/parsing.md +++ /dev/null @@ -1,33 +0,0 @@ -# Parsing Expressions into Solvable Systems - -Many times when creating DSLs or creating ModelingToolkit extensions to read new file formats, -it can become imperative to parse expressions. In many cases, it can be easy to use `Base.parse` -to take things to standard Julia expressions, but how can you take a `Base.Expr` and generate -symbolic forms from that? For example, say we had the following system we wanted to solve: - -```@example parsing -ex = [:(y ~ x) - :(y ~ -2x + 3 / z) - :(z ~ 2)] -``` - -We can use the function `parse_expr_to_symbolic` from Symbolics.jl to generate the symbolic -form of the expression: - -```@example parsing -using Symbolics -eqs = parse_expr_to_symbolic.(ex, (Main,)) -``` - -From there, we can use ModelingToolkit to transform the symbolic equations into a numerical -nonlinear solve: - -```@example parsing -using ModelingToolkit, SymbolicIndexingInterface, NonlinearSolve -vars = union(ModelingToolkit.vars.(eqs)...) -@mtkbuild ns = NonlinearSystem(eqs, vars, []) - -varmap = Dict(SymbolicIndexingInterface.getname.(vars) .=> vars) -prob = NonlinearProblem(ns, [varmap[:x] => 1.0, varmap[:y] => 1.0, varmap[:z] => 1.0]) -sol = solve(prob, NewtonRaphson()) -``` diff --git a/docs/src/tutorials/programmatically_generating.md b/docs/src/tutorials/programmatically_generating.md index bd1d2359f1..76d12dba2a 100644 --- a/docs/src/tutorials/programmatically_generating.md +++ b/docs/src/tutorials/programmatically_generating.md @@ -59,51 +59,13 @@ plot(sol) As you can see, generating an ODESystem is as simple as creating an array of equations and passing it to the `ODESystem` constructor. -## Understanding the Difference Between the Julia Variable and the Symbolic Variable - -In the most basic usage of ModelingToolkit and Symbolics, the name of the Julia variable -and the symbolic variable are the same. For example, when we do: - -```@example scripting -@variables a -``` - -the name of the symbolic variable is `a` and same with the Julia variable. However, we can -de-couple these by setting `a` to a new symbolic variable, for example: - -```@example scripting -b = only(@variables(a)) -``` - -Now the Julia variable `b` refers to the variable named `a`. However, the downside of this current -approach is that it requires that the user writing the script knows the name `a` that they want to -place to the variable. But what if for example we needed to get the variable's name from a file? - -To do this, one can interpolate a symbol into the `@variables` macro using `$`. For example: - -```@example scripting -a = :c -b = only(@variables($a)) -``` - -In this example, `@variables($a)` created a variable named `c`, and set this variable to `b`. - -Variables are not the only thing with names. For example, when you build a system, it knows its name -that name is used in the namespacing. In the standard usage, again the Julia variable and the -symbolic name are made the same via: - -```@example scripting -@named fol_model = ODESystem(eqs, t) -``` - -However, one can decouple these two properties by noting that `@named` is simply shorthand for the -following: +`@named` automatically gives a name to the `ODESystem`, and is shorthand for ```@example scripting -fol_model = ODESystem(eqs, t; name = :fol_model) +fol_model = ODESystem(eqs, t; name = :fol_model) # @named fol_model = ODESystem(eqs, t) ``` -Thus if we had read a name from a file and wish to populate an `ODESystem` with said name, we could do: +Thus, if we had read a name from a file and wish to populate an `ODESystem` with said name, we could do: ```@example scripting namesym = :name_from_file