Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit ffdc07d

Browse files
Include sparsity detection in the README
1 parent 3cabdfe commit ffdc07d

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

README.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,35 @@ end
2929
```
3030

3131
For this function, we know that the sparsity pattern of the Jacobian is a
32-
`Tridiagonal` matrix. We represent our sparsity by that matrix:
32+
`Tridiagonal` matrix. However, if we didn't know the sparsity pattern for
33+
the Jacobian, we could use the `sparsity!` function to automatically
34+
detect the sparsity pattern. We declare that it outputs a length 30 vector
35+
and takes in a length 30 vector, and it spits out a `Sparsity` object
36+
which we can turn into a `SparseMatrixCSC`:
3337

3438
```julia
35-
sparsity_pattern = Tridiagonal(ones(29),ones(30),ones(29))
39+
sparsity_pattern = sparsity!(f,output,input)
40+
jac = Float64.(sparse(sparsity_pattern))
3641
```
3742

3843
Now we call `matrix_colors` to get the color vector for that matrix:
3944

4045
```julia
41-
colors = matrix_colors(sparsity_pattern)
46+
colors = matrix_colors(jac)
4247
```
4348

4449
Since `maximum(colors)` is 3, this means that finite differencing can now
4550
compute the Jacobian in just 4 `f`-evaluations:
4651

4752
```julia
48-
J = DiffEqDiffTools.finite_difference_jacobian(f, rand(30), color=colors)
53+
DiffEqDiffTools.finite_difference_jacobian!(jac, f, rand(30), color=colors)
4954
@show fcalls # 4
5055
```
5156

5257
In addition, a faster forward-mode autodiff call can be utilized as well:
5358

5459
```julia
55-
forwarddiff_color_jacobian!(sparsity_pattern, f, x, color = colors)
60+
forwarddiff_color_jacobian!(jac, f, x, color = colors)
5661
```
5762

5863
If one only need to compute products, one can use the operators. For example,
@@ -83,6 +88,36 @@ gmres!(res,J,v)
8388

8489
## Documentation
8590

91+
### Automated Sparsity Detection
92+
93+
Automated sparsity detection is provided by the `sparsity!` function whose
94+
syntax is:
95+
96+
```julia
97+
`sparsity!(f, Y, X, args...; sparsity=Sparsity(length(X), length(Y)), verbose=true)`
98+
```
99+
100+
The arguments are:
101+
102+
- `f`: the function
103+
- `Y`: the output array
104+
- `X`: the input array
105+
- `args`: trailing arguments to `f`. They are considered subject to change, unless wrapped as `Fixed(arg)`
106+
- `S`: (optional) the sparsity pattern
107+
- `verbose`: (optional) whether to describe the paths taken by the sparsity detection.
108+
109+
The function `f` is assumed to take arguments of the form `f(dx,x,args...)`.
110+
`sparsity!` returns a `Sparsity` object which describes where the non-zeros
111+
of the Jacobian occur. `sparse(::Sparsity)` transforms the pattern into
112+
a sparse matrix.
113+
114+
This function utilizes non-standard interpretation, which we denote
115+
combinatoric concolic analysis, to directly realize the sparsity pattern from the program's AST. It requires that the function `f` is a Julia function. It does not
116+
work numerically, meaning that it is not prone to floating point error or
117+
cancelation. It allows for branching and will automatically check all of the
118+
branches. However, a while loop of indeterminate length which is dependent
119+
on the input argument is not allowed.
120+
86121
### Matrix Coloring
87122

88123
Matrix coloring allows you to reduce the number of times finite differencing

src/program_sparsity/program_sparsity.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ struct Fixed
33
end
44

55
"""
6-
`sparsity!(f, Y, X, args...; sparsity=Sparsity(length(X), length(Y)), verboase=true)`
6+
`sparsity!(f, Y, X, args...; sparsity=Sparsity(length(X), length(Y)), verbose=true)`
77
88
Execute the program that figures out the sparsity pattern of
99
the jacobian of the function `f`.

test/test_integration.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ function second_derivative_stencil(N)
2323
A
2424
end
2525

26-
sparsity_pattern = sparsity!(f,ones(30),ones(30))
26+
output = ones(30); input = ones(30)
27+
sparsity_pattern = sparsity!(f,output,input)
2728
true_jac = Float64.(sparse(sparsity_pattern))
2829
colors = matrix_colors(true_jac)
2930
@test colors == repeat(1:3,10)

0 commit comments

Comments
 (0)