-
Notifications
You must be signed in to change notification settings - Fork 12
/
sparsity_pattern.jl
53 lines (47 loc) · 1.15 KB
/
sparsity_pattern.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
"""
compute_jacobian_sparsity(c, x0; detector)
compute_jacobian_sparsity(c!, cx, x0; detector)
Return a sparse boolean matrix that represents the adjacency matrix of the Jacobian of c(x).
"""
function compute_jacobian_sparsity end
function compute_jacobian_sparsity(
c,
x0;
detector::AbstractSparsityDetector = TracerSparsityDetector(),
)
S = ADTypes.jacobian_sparsity(c, x0, detector)
return S
end
function compute_jacobian_sparsity(
c!,
cx,
x0;
detector::AbstractSparsityDetector = TracerSparsityDetector(),
)
S = ADTypes.jacobian_sparsity(c!, cx, x0, detector)
return S
end
"""
compute_hessian_sparsity(f, nvar, c!, ncon; detector)
Return a sparse boolean matrix that represents the adjacency matrix of the Hessian of f(x) + λᵀc(x).
"""
function compute_hessian_sparsity(
f,
nvar,
c!,
ncon;
detector::AbstractSparsityDetector = TracerSparsityDetector(),
)
function lagrangian(x)
if ncon == 0
return f(x)
else
cx = zeros(eltype(x), ncon)
y0 = rand(ncon)
return f(x) + dot(c!(cx, x), y0)
end
end
x0 = rand(nvar)
S = ADTypes.hessian_sparsity(lagrangian, x0, detector)
return S
end