Skip to content

Commit fb1f559

Browse files
Update README.md
1 parent ab843c4 commit fb1f559

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

README.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ This can then generate the function. For example, we can see the
5353
generated code via:
5454

5555
```julia
56-
generate_function(de, [x,y,z], [σ,ρ,β])
56+
using MacroTools
57+
MacroTools.striplines(generate_function(de, [x,y,z], [σ,ρ,β])[2]) # second one is the in-place function
58+
5759

5860
## Which returns:
5961
:((##363, u, p, t)->begin
@@ -86,27 +88,40 @@ eqs = [0 ~ σ*(y-x),
8688
0 ~ x*-z)-y,
8789
0 ~ x*y - β*z]
8890
ns = NonlinearSystem(eqs, [x,y,z])
89-
nlsys_func = generate_function(ns, [x,y,z], [σ,ρ,β])
91+
nlsys_func = generate_function(ns, [x,y,z], [σ,ρ,β])[2] # second is the inplace version
9092
```
9193
9294
which generates:
9395
9496
```julia
95-
:((##364, u, p)->begin
96-
let (x, y, z, σ, ρ, β) = (u[1], u[2], u[3], p[1], p[2], p[3])
97-
##364[1] = σ * (y - x)
98-
##364[2] = x * (ρ - z) - y
99-
##364[3] = x * y - β * z
100-
end
101-
end)
97+
(var"##MTIIPVar#405", u, p)->begin
98+
@inbounds begin
99+
@inbounds begin
100+
let (x, y, z, σ, ρ, β) = (u[1], u[2], u[3], p[1], p[2], p[3])
101+
var"##MTIIPVar#405"[1] = (*)(σ, (-)(y, x))
102+
var"##MTIIPVar#405"[2] = (-)((*)(x, (-)(ρ, z)), y)
103+
var"##MTIIPVar#405"[3] = (-)((*)(x, y), (*)(β, z))
104+
end
105+
end
106+
end
107+
nothing
108+
end
102109
```
103110
104111
We can use this to build a nonlinear function for use with NLsolve.jl:
105112
106113
```julia
107-
f = @eval eval(nlsys_func)
108-
# Make a closure over the parameters for for NLsolve.jl
109-
f2 = (du,u) -> f(du,u,(10.0,26.0,2.33))
114+
f = eval(nlsys_func)
115+
du = zeros(3); u = ones(3)
116+
f(du,u,(10.0,26.0,2.33))
117+
du
118+
119+
#=
120+
3-element Array{Float64,1}:
121+
0.0
122+
24.0
123+
-1.33
124+
=#
110125
```
111126
112127
### Example: Arrays of variables

0 commit comments

Comments
 (0)