forked from SciML/ModelingToolkit.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirect.jl
273 lines (224 loc) · 7.39 KB
/
direct.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using ModelingToolkit, StaticArrays, LinearAlgebra, SparseArrays
using DiffEqBase
using Test
canonequal(a, b) = isequal(simplify(a), simplify(b))
# Calculus
@parameters t σ ρ β
@variables x y z
@test isequal((Differential(z) * Differential(y) * Differential(x))(t),
Differential(z)(Differential(y)(Differential(x)(t))))
@test canonequal(ModelingToolkit.derivative(sin(cos(x)), x),
-sin(x) * cos(cos(x)))
@register_symbolic no_der(x)
@test canonequal(ModelingToolkit.derivative([sin(cos(x)), hypot(x, no_der(x))], x),
[
-sin(x) * cos(cos(x)),
x / hypot(x, no_der(x)) +
no_der(x) * Differential(x)(no_der(x)) / hypot(x, no_der(x)),
])
@register_symbolic intfun(x)::Int
@test ModelingToolkit.symtype(intfun(x)) === Int
eqs = [σ * (y - x),
x * (ρ - z) - y,
x * y - β * z]
simpexpr = [:($(*)(σ, $(+)(y, $(*)(-1, x))))
:($(+)($(*)(x, $(+)(ρ, $(*)(-1, z))), $(*)(-1, y)))
:($(+)($(*)(x, y), $(*)(-1, z, β)))]
σ, β, ρ = 2 // 3, 3 // 4, 4 // 5
x, y, z = 6 // 7, 7 // 8, 8 // 9
for i in 1:3
@test eval(ModelingToolkit.toexpr.(eqs)[i]) == eval(simpexpr[i])
@test eval(ModelingToolkit.toexpr.(eqs)[i]) == eval(simpexpr[i])
end
@parameters t σ ρ β
@variables x y z
∂ = ModelingToolkit.jacobian(eqs, [x, y, z])
for i in 1:3
∇ = ModelingToolkit.gradient(eqs[i], [x, y, z])
@test canonequal(∂[i, :], ∇)
end
@test all(canonequal.(ModelingToolkit.gradient(eqs[1], [x, y, z]), [σ * -1, σ, 0]))
@test all(canonequal.(ModelingToolkit.hessian(eqs[1], [x, y, z]), 0))
du = [x^2, y^3, x^4, sin(y), x + y, x + z^2, z + x, x + y^2 + sin(z)]
reference_jac = sparse(ModelingToolkit.jacobian(du, [x, y, z]))
@test findnz(ModelingToolkit.jacobian_sparsity(du, [x, y, z]))[[1, 2]] ==
findnz(reference_jac)[[1, 2]]
let
@variables t x(t) y(t) z(t)
@test ModelingToolkit.exprs_occur_in([x, y, z], x^2 * y) == [true, true, false]
end
@test isequal(ModelingToolkit.sparsejacobian(du, [x, y, z]), reference_jac)
using ModelingToolkit
rosenbrock(X) =
sum(1:(length(X) - 1)) do i
100 * (X[i + 1] - X[i]^2)^2 + (1 - X[i])^2
end
@variables a, b
X = [a, b]
spoly(x) = simplify(x, expand = true)
rr = rosenbrock(X)
reference_hes = ModelingToolkit.hessian(rr, X)
@test findnz(sparse(reference_hes))[1:2] ==
findnz(ModelingToolkit.hessian_sparsity(rr, X))[1:2]
sp_hess = ModelingToolkit.sparsehessian(rr, X)
@test findnz(sparse(reference_hes))[1:2] == findnz(sp_hess)[1:2]
@test isequal(map(spoly, findnz(sparse(reference_hes))[3]), map(spoly, findnz(sp_hess)[3]))
Joop, Jiip = eval.(ModelingToolkit.build_function(∂, [x, y, z], [σ, ρ, β], t))
J = Joop([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 1.0)
@test J isa Matrix
J2 = copy(J)
Jiip(J2, [1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 1.0)
@test J2 == J
Joop, Jiip = eval.(ModelingToolkit.build_function(vcat(∂, ∂), [x, y, z], [σ, ρ, β], t))
J = Joop([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 1.0)
@test J isa Matrix
J2 = copy(J)
Jiip(J2, [1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 1.0)
@test J2 == J
Joop, Jiip = eval.(ModelingToolkit.build_function(hcat(∂, ∂), [x, y, z], [σ, ρ, β], t))
J = Joop([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 1.0)
@test J isa Matrix
J2 = copy(J)
Jiip(J2, [1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 1.0)
@test J2 == J
∂3 = cat(∂, ∂, dims = 3)
Joop, Jiip = eval.(ModelingToolkit.build_function(∂3, [x, y, z], [σ, ρ, β], t))
J = Joop([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 1.0)
@test size(J) == (3, 3, 2)
J2 = copy(J)
Jiip(J2, [1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 1.0)
@test J2 == J
s∂ = sparse(∂)
@test nnz(s∂) == 8
Joop, Jiip = eval.(ModelingToolkit.build_function(s∂, [x, y, z], [σ, ρ, β], t,
linenumbers = true))
J = Joop([1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 1.0)
@test length(nonzeros(s∂)) == 8
J2 = copy(J)
Jiip(J2, [1.0, 2.0, 3.0], [1.0, 2.0, 3.0], 1.0)
@test J2 == J
# Function building
@parameters σ ρ β
@variables x y z
eqs = [σ * (y - x),
x * (ρ - z) - y,
x * y - β * z]
f1, f2 = ModelingToolkit.build_function(eqs, [x, y, z], [σ, ρ, β])
f = eval(f1)
out = [1.0, 2, 3]
o1 = f([1.0, 2, 3], [1.0, 2, 3])
f = eval(f2)
f(out, [1.0, 2, 3], [1.0, 2, 3])
@test all(o1 .== out)
function test_worldage()
@parameters σ ρ β
@variables x y z
eqs = [σ * (y - x),
x * (ρ - z) - y,
x * y - β * z]
f, f_iip = ModelingToolkit.build_function(eqs, [x, y, z], [σ, ρ, β];
expression = Val{false})
out = [1.0, 2, 3]
o1 = f([1.0, 2, 3], [1.0, 2, 3])
f_iip(out, [1.0, 2, 3], [1.0, 2, 3])
end
test_worldage()
## No parameters
@variables x y z
eqs = [(y - x)^2,
x * (x - z) - y,
x * y - y * z]
f1, f2 = ModelingToolkit.build_function(eqs, [x, y, z])
f = eval(f1)
out = zeros(3)
o1 = f([1.0, 2, 3])
f = eval(f2)
f(out, [1.0, 2, 3])
@test all(out .== o1)
# y ^ -1 test
g = let
f(x, y) = x / y
@variables x y
ex = expand_derivatives(Differential(x)(f(x, y)))
func_ex = build_function(ex, x, y)
eval(func_ex)
end
@test g(42, 4) == 1 / 4
function test_worldage()
@variables x y z
eqs = [(y - x)^2,
x * (x - z) - y,
x * y - y * z]
f, f_iip = ModelingToolkit.build_function(eqs, [x, y, z]; expression = Val{false})
out = zeros(3)
o1 = f([1.0, 2, 3])
f_iip(out, [1.0, 2, 3])
end
test_worldage()
@test_nowarn muladd(x, y, 0)
@test promote(x, 0) == (x, identity(0))
@test_nowarn [x, y, z]'
let
@register_symbolic foo(x)
@variables t
D = Differential(t)
@test isequal(expand_derivatives(D(foo(t))), D(foo(t)))
@test isequal(expand_derivatives(D(sin(t) * foo(t))),
cos(t) * foo(t) + sin(t) * D(foo(t)))
end
foo(; kw...) = kw
foo(args...; kw...) = args, kw
pp = :name => :cool_name
@named cool_name = foo()
@test collect(cool_name) == [pp]
@named cool_name = foo(42)
@test cool_name[1] == (42,)
@test collect(cool_name[2]) == [pp]
@named cool_name = foo(42; a = 2)
@test cool_name[1] == (42,)
@test collect(cool_name[2]) == [pp; :a => 2]
@named cool_name = foo(a = 2)
@test collect(cool_name) == [pp; :a => 2]
@named cool_name = foo(; a = 2)
@test collect(cool_name) == [pp; :a => 2]
@named cool_name = foo(name = 2)
@test collect(cool_name) == [:name => 2]
@named cool_name = foo(42; name = 3)
@test cool_name[1] == (42,)
@test collect(cool_name[2]) == [:name => 3]
kwargs = (; name = 3)
@named cool_name = foo(42; kwargs...)
@test cool_name[1] == (42,)
@test collect(cool_name[2]) == [:name => 3]
if VERSION >= v"1.5"
name = 3
@named cool_name = foo(42; name)
@test cool_name[1] == (42,)
@test collect(cool_name[2]) == [:name => name]
@named cool_name = foo(; name)
@test collect(cool_name) == [:name => name]
ff = 3
@named cool_name = foo(42; ff)
@test cool_name[1] == (42,)
@test collect(cool_name[2]) == [pp; :ff => ff]
@named cool_name = foo(; ff)
@test collect(cool_name) == [pp; :ff => ff]
end
foo(i; name) = (; i, name)
@named goo[1:3] = foo(10)
@test isequal(goo, [(i = 10, name = Symbol(:goo_, i)) for i in 1:3])
@named koo 1:3 i->foo(10i)
@test isequal(koo, [(i = 10i, name = Symbol(:koo_, i)) for i in 1:3])
xys = @named begin
x = foo(12)
y[1:3] = foo(13)
end
@test isequal(x, (i = 12, name = :x))
@test isequal(y, [(i = 13, name = Symbol(:y_, i)) for i in 1:3])
@test isequal(xys, [x; y])
@variables x [misc = "wow"]
@test SymbolicUtils.getmetadata(Symbolics.unwrap(x), ModelingToolkit.VariableMisc,
nothing) == "wow"
@parameters x [misc = "wow"]
@test SymbolicUtils.getmetadata(Symbolics.unwrap(x), ModelingToolkit.VariableMisc,
nothing) == "wow"