Skip to content

Commit 74fc3dc

Browse files
Merge pull request #246 from JuliaDiffEq/latexify_ode
Add Latexify for ODESystem
2 parents 9fc1856 + 01c95a0 commit 74fc3dc

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

src/ModelingToolkit.jl

+1-1
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

+25-1
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 ? Latexify.LaTeXString("\\frac{d$(Latexify.latexraw(var[i]))}{d$iv}") : LaTeXString("\\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+
return latexify(sys.eqs; iv=iv)
47+
end

test/latexify.jl

+38
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,41 @@ 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+
@test latexify(eqs) ==
47+
raw"\begin{align}
48+
\mathrm{derivative}\left( \mathrm{u_1}\left( t \right), t \right) =& p_3 \cdot \left( \mathrm{u_2}\left( t \right) - \mathrm{u_1}\left( t \right) \right) \\
49+
0 =& \frac{p_2 \cdot p_3 \cdot \mathrm{u_1}\left( t \right) \cdot \left( p_1 - \mathrm{u_1}\left( t \right) \right)}{10} - \mathrm{u_2}\left( t \right) \\
50+
\mathrm{derivative}\left( \mathrm{u_3}\left( t \right), t \right) =& \mathrm{u_1}\left( t \right) \cdot \left( \mathrm{u_2}\left( t \right) \right)^{\frac{2}{3}} - p_3 \cdot \mathrm{u_3}\left( t \right)
51+
\end{align}
52+
"
53+
54+
eqs = [D(u[1]) ~ p[3]*(u[2]-u[1]),
55+
D(u[2]) ~ p[2]*p[3]*u[1]*(p[1]-u[1])/10-u[2],
56+
D(u[3]) ~ u[1]*u[2]^(2//3) - p[3]*u[3]]
57+
58+
sys = ODESystem(eqs)
59+
60+
@test latexify(sys.eqs) ==
61+
raw"\begin{align}
62+
\frac{du_{1}}{dt} =& p_3 \cdot \left( \mathrm{u_2}\left( t \right) - \mathrm{u_1}\left( t \right) \right) \\
63+
\frac{du_{2}}{dt} =& \frac{p_2 \cdot p_3 \cdot \mathrm{u_1}\left( t \right) \cdot \left( p_1 - \mathrm{u_1}\left( t \right) \right)}{10} - \mathrm{u_2}\left( t \right) \\
64+
\frac{du_{3}}{dt} =& \mathrm{u_1}\left( t \right) \cdot \left( \mathrm{u_2}\left( t \right) \right)^{\frac{2}{3}} - p_3 \cdot \mathrm{u_3}\left( t \right)
65+
\end{align}
66+
"
67+
68+
@test latexify(sys) ==
69+
raw"$\begin{align}
70+
\frac{du_{1}}{dt} =& p_3 \cdot \left( \mathrm{u_2}\left( t \right) - \mathrm{u_1}\left( t \right) \right) \\
71+
\frac{du_{2}}{dt} =& \frac{p_2 \cdot p_3 \cdot \mathrm{u_1}\left( t \right) \cdot \left( p_1 - \mathrm{u_1}\left( t \right) \right)}{10} - \mathrm{u_2}\left( t \right) \\
72+
\frac{du_{3}}{dt} =& \mathrm{u_1}\left( t \right) \cdot \left( \mathrm{u_2}\left( t \right) \right)^{\frac{2}{3}} - p_3 \cdot \mathrm{u_3}\left( t \right)
73+
\end{align}
74+
$"

0 commit comments

Comments
 (0)