-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic latexify support for Equation arrays.
- Loading branch information
Niklas Korsbo
committed
Oct 2, 2019
1 parent
870f80f
commit b166fc0
Showing
5 changed files
with
93 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
name = "ModelingToolkit" | ||
uuid = "961ee093-0014-501f-94e3-6117800e7a78" | ||
authors = ["Chris Rackauckas <accounts@chrisrackauckas.com>"] | ||
version = "0.8.0" | ||
|
||
[deps] | ||
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" | ||
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b" | ||
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" | ||
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" | ||
GeneralizedGenerated = "6b9d7cbe-bcb9-11e9-073f-15a7a543e2eb" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" | ||
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" | ||
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" | ||
|
||
[compat] | ||
julia = "1" | ||
|
||
[extras] | ||
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["OrdinaryDiffEq", "Test"] | ||
name = "ModelingToolkit" | ||
uuid = "961ee093-0014-501f-94e3-6117800e7a78" | ||
authors = ["Chris Rackauckas <accounts@chrisrackauckas.com>"] | ||
version = "0.8.0" | ||
|
||
[deps] | ||
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e" | ||
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b" | ||
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" | ||
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" | ||
GeneralizedGenerated = "6b9d7cbe-bcb9-11e9-073f-15a7a543e2eb" | ||
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" | ||
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" | ||
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" | ||
|
||
[compat] | ||
julia = "1" | ||
Latexify = "0.11, 2" | ||
|
||
[extras] | ||
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["OrdinaryDiffEq", "Test"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
@latexrecipe function f(eqs::Vector{ModelingToolkit.Equation}; iv=:t) | ||
# Set default option values. | ||
env --> :align | ||
|
||
# Convert both the left and right hand side to expressions of basic types | ||
# that latexify can deal with. | ||
|
||
rhs = getfield.(eqs, :rhs) | ||
rhs = convert.(Expr, rhs) | ||
rhs = [postwalk(x -> x isa ModelingToolkit.Constant ? x.value : x, eq) for eq in rhs] | ||
rhs = [postwalk(x -> x isa Expr && length(x.args) == 1 ? x.args[1] : x, eq) for eq in rhs] | ||
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] | ||
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] | ||
|
||
lhs = getfield.(eqs, :lhs) | ||
lhs = convert.(Expr, lhs) | ||
lhs = [postwalk(x -> x isa ModelingToolkit.Constant ? x.value : x, eq) for eq in lhs] | ||
lhs = [postwalk(x -> x isa Expr && length(x.args) == 1 ? x.args[1] : x, eq) for eq in lhs] | ||
lhs = [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 lhs] | ||
lhs = [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 lhs] | ||
|
||
return lhs, rhs | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Test | ||
using Latexify | ||
using ModelingToolkit | ||
|
||
### Tips for generating latex tests: | ||
### Latexify has an unexported macro: | ||
### | ||
### Latexify.@generate_test | ||
### | ||
### which generates a test using a given latexify function. | ||
### For example: | ||
### | ||
### Latexify.@generate_test latexify([1, 2, 3], [4, 5, 6]; env=:mdtable) | ||
### | ||
### This puts a ready-made test in your clipboard which you can paste into the | ||
### test file. | ||
### | ||
### Just be sure to remove all such macros before you commit a change since it | ||
### will cause issues with Travis. | ||
|
||
@parameters t σ ρ β | ||
@variables x(t) y(t) z(t) | ||
@derivatives D'~t | ||
|
||
eqs = [D(x) ~ σ*(y-x)*D(x-y)/D(z), | ||
0 ~ σ*x*(ρ-z)/10-y, | ||
D(z) ~ x*y - β*z] | ||
|
||
|
||
@test latexify(eqs) == | ||
raw"\begin{align} | ||
\frac{dx(t)}{dt} =& \frac{\sigma \cdot \left( \mathrm{y}\left( t \right) - \mathrm{x}\left( t \right) \right) \cdot \frac{d\left(\mathrm{x}\left( t \right) - \mathrm{y}\left( t \right)\right)}{dt}}{\frac{dz(t)}{dt}} \\ | ||
0 =& \frac{\sigma \cdot \mathrm{x}\left( t \right) \cdot \left( \rho - \mathrm{z}\left( t \right) \right)}{10} - \mathrm{y}\left( t \right) \\ | ||
\frac{dz(t)}{dt} =& \mathrm{x}\left( t \right) \cdot \mathrm{y}\left( t \right) - \beta \cdot \mathrm{z}\left( t \right) | ||
\end{align} | ||
" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters