Skip to content

Commit 49de8c6

Browse files
Add Latexify for ODESystem
1 parent 9fc1856 commit 49de8c6

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/ModelingToolkit.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ include("simplify.jl")
9191
include("utils.jl")
9292
include("direct.jl")
9393
include("domains.jl")
94-
include("latexify_recipes.jl")
9594
include("systems/diffeqs/diffeqsystem.jl")
9695
include("systems/diffeqs/first_order_transform.jl")
9796
include("systems/nonlinear/nonlinear_system.jl")
9897
include("systems/pde/pdesystem.jl")
98+
include("latexify_recipes.jl")
9999

100100
end # module

src/latexify_recipes.jl

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Set default option values.
33
env --> :align
44

5-
# Convert both the left and right hand side to expressions of basic types
5+
# Convert both the left and right hand side to expressions of basic types
66
# that latexify can deal with.
77

88
rhs = getfield.(eqs, :rhs)
@@ -21,3 +21,27 @@
2121

2222
return lhs, rhs
2323
end
24+
25+
@latexrecipe function f(eqs::Vector{ModelingToolkit.DiffEq}; iv=:t)
26+
# Set default option values.
27+
env --> :align
28+
29+
# Convert both the left and right hand side to expressions of basic types
30+
# that latexify can deal with.
31+
32+
rhs = getfield.(eqs, :rhs)
33+
rhs = convert.(Expr, rhs)
34+
rhs = [postwalk(x -> x isa ModelingToolkit.Constant ? x.value : x, eq) for eq in rhs]
35+
rhs = [postwalk(x -> x isa Expr && length(x.args) == 1 ? x.args[1] : x, eq) for eq in rhs]
36+
rhs = [postwalk(x -> x isa Expr && x.args[1] == :Differential && length(x.args[2].args) == 2 ? :($(Symbol(:d, x.args[2]))/($(Symbol(:d, x.args[2].args[2])))) : x, eq) for eq in rhs]
37+
rhs = [postwalk(x -> x isa Expr && x.args[1] == :Differential ? "\\frac{d\\left($(Latexify.latexraw(x.args[2]))\\right)}{d$iv}" : x, eq) for eq in rhs]
38+
39+
var = getfield.(getfield.(eqs, :x),:name)
40+
ns = getfield.(eqs, :n)
41+
lhs = [ns[i] == 1 ? "\\frac{d$(Latexify.latexraw(var[i]))}{d$iv}" : "\\frac{d^{$(ns[i])}$(Latexify.latexraw(var[i]))}{d$iv^{$(ns[i])}}" for i in 1:length(var)]
42+
return lhs, rhs
43+
end
44+
45+
@latexrecipe function f(sys::ModelingToolkit.ODESystem; iv=:t)
46+
latexify(sys.eqs)
47+
end

test/latexify.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,20 @@ raw"\begin{align}
3434
\mathrm{derivative}\left( \mathrm{z}\left( t \right), t \right) =& \mathrm{x}\left( t \right) \cdot \left( \mathrm{y}\left( t \right) \right)^{\frac{2}{3}} - \beta \cdot \mathrm{z}\left( t \right)
3535
\end{align}
3636
"
37+
38+
@parameters t p[1:3]
39+
@variables u[1:3](t)
40+
@derivatives D'~t
41+
42+
eqs = [D(u[1]) ~ p[3]*(u[2]-u[1]),
43+
0 ~ p[2]*p[3]*u[1]*(p[1]-u[1])/10-u[2],
44+
D(u[3]) ~ u[1]*u[2]^(2//3) - p[3]*u[3]]
45+
46+
latexify(eqs)
47+
48+
eqs = [D(u[1]) ~ p[3]*(u[2]-u[1]),
49+
D(u[2]) ~ p[2]*p[3]*u[1]*(p[1]-u[1])/10-u[2],
50+
D(u[3]) ~ u[1]*u[2]^(2//3) - p[3]*u[3]]
51+
52+
sys = ODESystem(eqs)
53+
latexify(sys)

0 commit comments

Comments
 (0)