/
direct.jl
286 lines (249 loc) · 8.08 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
274
275
276
277
278
279
280
281
282
283
284
285
286
"""
```julia
derivative(O, v; simplify=false)
```
A helper function for computing the derivative of an expression with respect to
`var`.
"""
function derivative(O, v; simplify=false)
if O isa AbstractArray
Num[Num(expand_derivatives(Differential(v)(value(o)), simplify)) for o in O]
else
Num(expand_derivatives(Differential(v)(value(O)), simplify))
end
end
"""
```julia
gradient(O, vars::AbstractVector; simplify=false)
```
A helper function for computing the gradient of an expression with respect to
an array of variable expressions.
"""
function gradient(O, vars::AbstractVector; simplify=false)
Num[Num(expand_derivatives(Differential(v)(value(O)),simplify)) for v in vars]
end
"""
```julia
jacobian(ops::AbstractVector, vars::AbstractVector; simplify=false)
```
A helper function for computing the Jacobian of an array of expressions with respect to
an array of variable expressions.
"""
function jacobian(ops::AbstractVector, vars::AbstractVector; simplify=false)
Num[Num(expand_derivatives(Differential(value(v))(value(O)),simplify)) for O in ops, v in vars]
end
"""
```julia
sparsejacobian(ops::AbstractVector, vars::AbstractVector; simplify=false)
```
A helper function for computing the sparse Jacobian of an array of expressions with respect to
an array of variable expressions.
"""
function sparsejacobian(ops::AbstractVector, vars::AbstractVector; simplify=false)
I = Int[]
J = Int[]
du = Num[]
sp = jacobian_sparsity(ops, vars)
I,J,_ = findnz(sp)
exprs = Num[]
for (i,j) in zip(I, J)
push!(exprs, Num(expand_derivatives(Differential(vars[j])(ops[i]), simplify)))
end
sparse(I,J, exprs, length(ops), length(vars))
end
"""
```julia
jacobian_sparsity(ops::AbstractVector, vars::AbstractVector)
```
Return the sparsity pattern of the Jacobian of an array of expressions with respect to
an array of variable expressions.
"""
function jacobian_sparsity(du, u)
du = map(value, du)
u = map(value, u)
dict = Dict(zip(u, 1:length(u)))
i = Ref(1)
I = Int[]
J = Int[]
# This rewriter notes down which u's appear in a
# given du (whose index is stored in the `i` Ref)
r = [@rule ~x::(x->haskey(dict, x)) => begin
push!(I, i[])
push!(J, dict[~x])
nothing
end] |> Rewriters.Chain |> Rewriters.Postwalk
for ii = 1:length(du)
i[] = ii
r(du[ii])
end
sparse(I, J, true, length(du), length(u))
end
"""
exprs_occur_in(exprs::Vector, expr)
Return an array of booleans `finds` where `finds[i]` is true if `exprs[i]` occurs in `expr`
false otherwise.
"""
function exprs_occur_in(exprs, expr)
vec(jacobian_sparsity([expr], exprs))
end
"""
```julia
hessian(O, vars::AbstractVector; simplify=false)
```
A helper function for computing the Hessian of an expression with respect to
an array of variable expressions.
"""
function hessian(O, vars::AbstractVector; simplify=false)
vars = map(value, vars)
first_derivs = map(value, vec(jacobian([values(O)], vars, simplify=simplify)))
n = length(vars)
H = Array{Num, 2}(undef,(n, n))
fill!(H, 0)
for i=1:n
for j=1:i
H[j, i] = H[i, j] = expand_derivatives(Differential(vars[i])(first_derivs[j]))
end
end
H
end
isidx(x) = x isa TermCombination
"""
```julia
hessian_sparsity(ops::AbstractVector, vars::AbstractVector)
```
Return the sparsity pattern of the Hessian of an array of expressions with respect to
an array of variable expressions.
"""
function hessian_sparsity end
let
# we do this in a let block so that Revise works on the list of rules
_scalar = one(TermCombination)
simterm(t, f, args) = Term{Any}(f, args)
linearity_rules = [
@rule +(~~xs) => reduce(+, filter(isidx, ~~xs), init=_scalar)
@rule *(~~xs) => reduce(*, filter(isidx, ~~xs), init=_scalar)
@rule (~f)(~x::(!isidx)) => _scalar
@rule (~f)(~x::isidx) => if haslinearity_1(~f)
combine_terms_1(linearity_1(~f), ~x)
else
error("Function of unknown linearity used: ", ~f)
end
@rule (^)(~x::isidx, ~y) => ~y isa Number && isone(~y) ? ~x : (~x) * (~x)
@rule (~f)(~x, ~y) => begin
if haslinearity_2(~f)
a = isidx(~x) ? ~x : _scalar
b = isidx(~y) ? ~y : _scalar
combine_terms_2(linearity_2(~f), a, b)
else
error("Function of unknown linearity used: ", ~f)
end
end]
linearity_propagator = Fixpoint(Postwalk(Chain(linearity_rules); similarterm=simterm))
global hessian_sparsity
"""
```julia
hessian_sparsity(ops::AbstractVector, vars::AbstractVector)
```
Return the sparsity pattern of the Hessian of an array of expressions with respect to
an array of variable expressions.
"""
function hessian_sparsity(f, u)
@assert !(f isa AbstractArray)
f = value(f)
u = map(value, u)
idx(i) = TermCombination(Set([Dict(i=>1)]))
dict = Dict(u .=> idx.(1:length(u)))
f = Rewriters.Prewalk(x->haskey(dict, x) ? dict[x] : x; similarterm=simterm)(f)
lp = linearity_propagator(f)
_sparse(lp, length(u))
end
end
"""
```julia
islinear(ex, u)
```
Check if an expression is linear with respect to a list of variable expressions.
"""
function islinear(ex, u)
isempty(hessian_sparsity(ex, u).nzval)
end
"""
```julia
sparsehessian(O, vars::AbstractVector; simplify=false)
```
A helper function for computing the sparse Hessian of an expression with respect to
an array of variable expressions.
"""
function sparsehessian(O, vars::AbstractVector; simplify=false)
O = value(O)
vars = map(value, vars)
S = hessian_sparsity(O, vars)
I, J, _ = findnz(S)
exprs = Array{Num}(undef, length(I))
fill!(exprs, 0)
prev_j = 0
d = nothing
for (k, (i, j)) in enumerate(zip(I, J))
j > i && continue
if j != prev_j
d = expand_derivatives(Differential(vars[j])(O), false)
end
expr = expand_derivatives(Differential(vars[i])(d), simplify)
exprs[k] = expr
prev_j = j
end
H = sparse(I, J, exprs, length(vars), length(vars))
for (i, j) in zip(I, J)
j > i && (H[i, j] = H[j, i])
end
return H
end
# `_toexpr` is only used for latexify
function _toexpr(O; canonicalize=true)
if canonicalize
canonical, O = canonicalexpr(O)
canonical && return O
else
!istree(O) && return O
end
op = operation(O)
args = arguments(O)
if op isa Differential
ex = _toexpr(args[1]; canonicalize=canonicalize)
wrt = _toexpr(op.x; canonicalize=canonicalize)
return :(_derivative($ex, $wrt))
elseif op isa Sym
isempty(args) && return nameof(op)
return Expr(:call, _toexpr(op; canonicalize=canonicalize), _toexpr(args; canonicalize=canonicalize)...)
end
return Expr(:call, op, _toexpr(args; canonicalize=canonicalize)...)
end
_toexpr(s::Sym; kw...) = nameof(s)
function canonicalexpr(O)
!istree(O) && return true, O
op = operation(O)
args = arguments(O)
if op === (^)
if length(args) == 2 && args[2] isa Number && args[2] < 0
ex = _toexpr(args[1])
if args[2] == -1
expr = Expr(:call, inv, ex)
else
expr = Expr(:call, ^, Expr(:call, inv, ex), -args[2])
end
return true, expr
end
end
return false, O
end
for fun in [:toexpr, :_toexpr]
@eval begin
function $fun(eq::Equation; kw...)
Expr(:(=), $fun(eq.lhs; kw...), $fun(eq.rhs; kw...))
end
$fun(eqs::AbstractArray; kw...) = map(eq->$fun(eq; kw...), eqs)
$fun(x::Integer; kw...) = x
$fun(x::AbstractFloat; kw...) = x
$fun(x::Num; kw...) = $fun(value(x); kw...)
end
end