Skip to content

Commit 262b06f

Browse files
Merge pull request #201 from ChrisRackauckas-Claude/add-comprehensive-docstrings
Add comprehensive API documentation and restructure docs organization
2 parents 1161d80 + 422200c commit 262b06f

File tree

17 files changed

+1447
-575
lines changed

17 files changed

+1447
-575
lines changed

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ open(joinpath(@__DIR__, "src", "index.md"), "w") do io
2323
for line in eachline(joinpath(dirname(@__DIR__), "README.md"))
2424
println(io, line)
2525
end
26+
2627
for line in eachline(joinpath(@__DIR__, "src", "reproducibility.md"))
2728
println(io, line)
2829
end

docs/pages.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
# Put in a separate page so it can be used by SciMLDocs.jl
22

3-
pages = ["Home" => "index.md", "tutorials.md", "api.md"]
3+
pages = [
4+
"Home" => "index.md",
5+
"Tutorials" => "tutorials.md",
6+
"Derivatives" => "derivatives.md",
7+
"Gradients" => "gradients.md",
8+
"Jacobians" => "jacobians.md",
9+
"Hessians" => "hessians.md",
10+
"Jacobian-Vector Products" => "jvp.md",
11+
"Step Size Selection" => "epsilons.md",
12+
"Internal Utilities" => "utilities.md"
13+
]

docs/src/api.md

Lines changed: 0 additions & 58 deletions
This file was deleted.

docs/src/derivatives.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Derivatives
2+
3+
Functions for computing derivatives of scalar-valued functions.
4+
5+
## Overview
6+
7+
Derivatives are computed for scalar→scalar maps `f(x)` where `x` can be a single point or a collection of points. The derivative functions support:
8+
9+
- **Forward differences**: `O(1)` function evaluation per point, `O(h)` accuracy
10+
- **Central differences**: `O(2)` function evaluations per point, `O(h²)` accuracy
11+
- **Complex step**: `O(1)` function evaluation per point, machine precision accuracy
12+
13+
For optimal performance with repeated computations, use the cached versions with `DerivativeCache`.
14+
15+
## Functions
16+
17+
```@docs
18+
FiniteDiff.finite_difference_derivative
19+
FiniteDiff.finite_difference_derivative!
20+
```
21+
22+
## Cache
23+
24+
```@docs
25+
FiniteDiff.DerivativeCache
26+
```

docs/src/epsilons.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Step Size Selection (Epsilons)
2+
3+
Functions and theory for computing optimal step sizes in finite difference approximations.
4+
5+
## Theory
6+
7+
The choice of step size (epsilon) in finite difference methods is critical for accuracy. Too large a step leads to truncation error, while too small a step leads to round-off error. The optimal step size balances these two sources of error.
8+
9+
### Error Analysis
10+
11+
For a function `f` with bounded derivatives, the total error in finite difference approximations consists of:
12+
13+
1. **Truncation Error**: Comes from the finite difference approximation itself
14+
- Forward differences: `O(h)` where `h` is the step size
15+
- Central differences: `O(h²)`
16+
- Hessian central differences: `O(h²)` for second derivatives
17+
18+
2. **Round-off Error**: Comes from floating-point arithmetic
19+
- Forward differences: `O(eps/h)` where `eps` is machine epsilon
20+
- Central differences: `O(eps/h)`
21+
22+
### Optimal Step Sizes
23+
24+
Minimizing the total error `truncation + round-off` gives optimal step sizes:
25+
26+
- **Forward differences**: `h* = sqrt(eps)` - balances `O(h)` truncation with `O(eps/h)` round-off
27+
- **Central differences**: `h* = eps^(1/3)` - balances `O(h²)` truncation with `O(eps/h)` round-off
28+
- **Hessian central**: `h* = eps^(1/4)` - balances `O(h²)` truncation for mixed derivatives
29+
- **Complex step**: `h* = eps` - no subtractive cancellation, only limited by machine precision
30+
31+
## Adaptive Step Sizing
32+
33+
The step size computation uses both relative and absolute components:
34+
35+
```julia
36+
epsilon = max(relstep * abs(x), absstep) * dir
37+
```
38+
39+
This ensures:
40+
- **Large values**: Use relative step `relstep * |x|` for scale-invariant accuracy
41+
- **Small values**: Use absolute step `absstep` to avoid underflow
42+
- **Direction**: Multiply by `dir` (±1) for forward differences
43+
44+
## Implementation
45+
46+
The step size computation is handled by internal functions:
47+
48+
- **`compute_epsilon(fdtype, x, relstep, absstep, dir)`**: Computes the actual step size for a given finite difference method and input value
49+
- **`default_relstep(fdtype, T)`**: Returns the optimal relative step size for a given method and numeric type
50+
51+
These functions are called automatically by all finite difference routines, but understanding their behavior can help with custom implementations or debugging numerical issues.
52+
53+
## Special Cases
54+
55+
### Complex Step Differentiation
56+
57+
For complex step differentiation, the step size is simply machine epsilon since this method avoids subtractive cancellation entirely:
58+
59+
⚠️ **Important**: The function `f` must be complex analytic when the input is complex!
60+
61+
### Sparse Jacobians
62+
63+
When computing sparse Jacobians with graph coloring, the step size is computed based on the norm of the perturbation vector to ensure balanced accuracy across all columns in the same color group.
64+
65+
## Practical Considerations
66+
67+
- **Default step sizes** are optimal for most smooth functions
68+
- **Custom step sizes** may be needed for functions with unusual scaling or near-discontinuities
69+
- **Relative steps** should scale with the magnitude of the input
70+
- **Absolute steps** provide a fallback for inputs near zero
71+
- **Direction parameter** allows for one-sided differences when needed (e.g., at domain boundaries)

docs/src/gradients.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Gradients
2+
3+
Functions for computing gradients of scalar-valued functions with respect to vector inputs.
4+
5+
## Function Types
6+
7+
Gradients support two types of function mappings:
8+
9+
- **Vector→scalar**: `f(x)` where `x` is a vector and `f` returns a scalar
10+
- **Scalar→vector**: `f(fx, x)` for in-place evaluation or `fx = f(x)` for out-of-place
11+
12+
## Performance Notes
13+
14+
- **Forward differences**: `O(n)` function evaluations, `O(h)` accuracy
15+
- **Central differences**: `O(2n)` function evaluations, `O(h²)` accuracy
16+
- **Complex step**: `O(n)` function evaluations, machine precision accuracy
17+
18+
## Cache Management
19+
20+
When using `GradientCache` with pre-computed function values:
21+
22+
- If you provide `fx`, then `fx` will be used in forward differencing to skip a function call
23+
- You must update `cache.fx` before each call to `finite_difference_gradient!`
24+
- For immutable types (scalars, `StaticArray`), use `@set` from [Setfield.jl](https://github.com/jw3126/Setfield.jl)
25+
- Consider aliasing existing arrays into the cache for memory efficiency
26+
27+
## Functions
28+
29+
```@docs
30+
FiniteDiff.finite_difference_gradient
31+
FiniteDiff.finite_difference_gradient!
32+
```
33+
34+
## Cache
35+
36+
```@docs
37+
FiniteDiff.GradientCache
38+
```

docs/src/hessians.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Hessians
2+
3+
Functions for computing Hessian matrices of scalar-valued functions.
4+
5+
## Function Requirements
6+
7+
Hessian functions are designed for scalar-valued functions `f(x)` where:
8+
9+
- `x` is a vector of parameters
10+
- `f(x)` returns a scalar value
11+
- The Hessian `H[i,j] = ∂²f/(∂x[i]∂x[j])` is automatically symmetrized
12+
13+
## Mathematical Background
14+
15+
For a scalar function `f: ℝⁿ → ℝ`, the Hessian central difference approximation is:
16+
17+
```
18+
H[i,j] ≈ (f(x + eᵢhᵢ + eⱼhⱼ) - f(x + eᵢhᵢ - eⱼhⱼ) - f(x - eᵢhᵢ + eⱼhⱼ) + f(x - eᵢhᵢ - eⱼhⱼ)) / (4hᵢhⱼ)
19+
```
20+
21+
where `eᵢ` is the i-th unit vector and `hᵢ` is the step size in dimension i.
22+
23+
## Performance Considerations
24+
25+
- **Complexity**: Requires `O(n²)` function evaluations for an n-dimensional input
26+
- **Accuracy**: Central differences provide `O(h²)` accuracy for second derivatives
27+
- **Memory**: The result is returned as a `Symmetric` matrix view
28+
- **Alternative**: For large problems, consider computing the gradient twice instead
29+
30+
## StaticArrays Support
31+
32+
The cache constructor automatically detects `StaticArray` types and adjusts the `inplace` parameter accordingly for optimal performance.
33+
34+
## Functions
35+
36+
```@docs
37+
FiniteDiff.finite_difference_hessian
38+
FiniteDiff.finite_difference_hessian!
39+
```
40+
41+
## Cache
42+
43+
```@docs
44+
FiniteDiff.HessianCache
45+
```

docs/src/jacobians.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Jacobians
2+
3+
Functions for computing Jacobian matrices of vector-valued functions.
4+
5+
## Function Types
6+
7+
Jacobians support the following function signatures:
8+
9+
- **Out-of-place**: `fx = f(x)` where both `x` and `fx` are vectors
10+
- **In-place**: `f!(fx, x)` where `f!` modifies `fx` in-place
11+
12+
## Sparse Jacobians
13+
14+
FiniteDiff.jl provides efficient sparse Jacobian computation using graph coloring:
15+
16+
- Pass a `colorvec` of matrix colors to enable column compression
17+
- Provide `sparsity` as a sparse or structured matrix (`Tridiagonal`, `Banded`, etc.)
18+
- Supports automatic sparsity pattern detection via ArrayInterfaceCore.jl
19+
- Results are automatically decompressed unless `sparsity=nothing`
20+
21+
## Performance Notes
22+
23+
- **Forward differences**: `O(n)` function evaluations, `O(h)` accuracy
24+
- **Central differences**: `O(2n)` function evaluations, `O(h²)` accuracy
25+
- **Complex step**: `O(n)` function evaluations, machine precision accuracy
26+
- **Sparse Jacobians**: Use graph coloring to reduce function evaluations significantly
27+
28+
For non-square Jacobians, specify the output vector `fx` when creating the cache to ensure proper sizing.
29+
30+
## Functions
31+
32+
```@docs
33+
FiniteDiff.finite_difference_jacobian
34+
FiniteDiff.finite_difference_jacobian!
35+
```
36+
37+
## Cache
38+
39+
```@docs
40+
FiniteDiff.JacobianCache
41+
```

docs/src/jvp.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Jacobian-Vector Products (JVP)
2+
3+
Functions for computing Jacobian-vector products efficiently without forming the full Jacobian matrix.
4+
5+
## Mathematical Background
6+
7+
The JVP computes `J(x) * v` where `J(x)` is the Jacobian of function `f` at point `x` and `v` is a direction vector. This is computed using finite difference approximations:
8+
9+
- **Forward**: `J(x) * v ≈ (f(x + h*v) - f(x)) / h`
10+
- **Central**: `J(x) * v ≈ (f(x + h*v) - f(x - h*v)) / (2h)`
11+
12+
where `h` is the step size and `v` is the direction vector.
13+
14+
## Performance Benefits
15+
16+
JVP functions are particularly efficient when you only need directional derivatives:
17+
18+
- **Function evaluations**: Only 2 function evaluations (vs `O(n)` for full Jacobian)
19+
- **Forward differences**: 2 function evaluations, `O(h)` accuracy
20+
- **Central differences**: 2 function evaluations, `O(h²)` accuracy
21+
- **Memory efficient**: No need to store the full Jacobian matrix
22+
23+
## Use Cases
24+
25+
JVP is particularly useful for:
26+
27+
- **Optimization**: Computing directional derivatives along search directions
28+
- **Sparse directions**: When `v` has few non-zero entries
29+
- **Memory constraints**: Avoiding storage of large Jacobian matrices
30+
- **Newton methods**: Computing Newton steps `J⁻¹ * v` iteratively
31+
32+
## Limitations
33+
34+
- **Complex step**: JVP does not currently support complex step differentiation (`Val(:complex)`)
35+
- **In-place functions**: For in-place function evaluation, ensure proper cache sizing
36+
37+
## Functions
38+
39+
```@docs
40+
FiniteDiff.finite_difference_jvp
41+
FiniteDiff.finite_difference_jvp!
42+
```
43+
44+
## Cache
45+
46+
```@docs
47+
FiniteDiff.JVPCache
48+
```

0 commit comments

Comments
 (0)