@@ -53,7 +53,9 @@ This can then generate the function. For example, we can see the
53
53
generated code via:
54
54
55
55
``` 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
+
57
59
58
60
# # Which returns:
59
61
:((# #363, u, p, t)->begin
@@ -86,27 +88,40 @@ eqs = [0 ~ σ*(y-x),
86
88
0 ~ x* (ρ- z)- y,
87
89
0 ~ x* y - β* z]
88
90
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
90
92
```
91
93
92
94
which generates:
93
95
94
96
``` 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
102
109
```
103
110
104
111
We can use this to build a nonlinear function for use with NLsolve.jl:
105
112
106
113
``` 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
+ =#
110
125
```
111
126
112
127
### Example: Arrays of variables
0 commit comments