Skip to content

Commit a6cec2d

Browse files
Copilotlkdvos
andcommitted
Rewrite Truncations documentation with comprehensive examples and usage guide
Co-authored-by: lkdvos <[email protected]>
1 parent e3e348f commit a6cec2d

File tree

1 file changed

+121
-6
lines changed

1 file changed

+121
-6
lines changed

docs/src/user_interface/truncations.md

Lines changed: 121 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ CollapsedDocStrings = true
55

66
# Truncations
77

8-
Currently, truncations are supported through the following different methods:
8+
Truncation strategies allow you to control which eigenvalues or singular values to keep when computing partial or truncated decompositions. These strategies are used in functions like [`eigh_trunc`](@ref), [`eig_trunc`](@ref), and [`svd_trunc`](@ref) to reduce the size of the decomposition while retaining the most important information.
9+
10+
## Truncation Strategies
11+
12+
MatrixAlgebraKit provides several built-in truncation strategies:
913

1014
```@docs; canonical=false
1115
notrunc
@@ -15,11 +19,122 @@ truncfilter
1519
truncerror
1620
```
1721

18-
It is additionally possible to combine truncation strategies by making use of the `&` operator.
19-
For example, truncating to a maximal dimension `10`, and discarding all values below `1e-6` would be achieved by:
22+
## Combining Strategies
23+
24+
Truncation strategies can be combined using the `&` operator to create intersection-based truncation.
25+
When strategies are combined, only the values that satisfy all conditions are kept.
26+
27+
For example, to keep at most 10 eigenvalues while also discarding all values below `1e-6`:
28+
29+
```julia
30+
combined_trunc = truncrank(10) & trunctol(; atol = 1e-6)
31+
```
32+
33+
## Using Truncations in Decompositions
34+
35+
Truncation strategies can be used with truncated decomposition functions in two ways:
36+
37+
### 1. Using the `trunc` keyword with a `NamedTuple`
38+
39+
The simplest approach is to pass a `NamedTuple` with the truncation parameters:
2040

2141
```julia
22-
maxdim = 10
23-
atol = 1e-6
24-
combined_trunc = truncrank(maxdim) & trunctol(; atol)
42+
using MatrixAlgebraKit
43+
44+
# Create a symmetric matrix
45+
A = randn(100, 100)
46+
A = A + A' # Make symmetric
47+
48+
# Keep only the 10 largest eigenvalues
49+
D, V = eigh_trunc(A; trunc = (maxrank = 10,))
50+
51+
# Keep eigenvalues with absolute value above tolerance
52+
D, V = eigh_trunc(A; trunc = (atol = 1e-6,))
53+
54+
# Combine multiple criteria
55+
D, V = eigh_trunc(A; trunc = (maxrank = 20, atol = 1e-10, rtol = 1e-8))
56+
```
57+
58+
### 2. Using explicit `TruncationStrategy` objects
59+
60+
For more control, you can construct `TruncationStrategy` objects directly:
61+
62+
```julia
63+
# Keep the 5 largest eigenvalues
64+
strategy = truncrank(5)
65+
D, V = eigh_trunc(A; trunc = strategy)
66+
67+
# Keep eigenvalues above an absolute tolerance
68+
strategy = trunctol(; atol = 1e-6)
69+
D, V = eigh_trunc(A; trunc = strategy)
70+
71+
# Combine strategies: keep at most 10 eigenvalues, all above 1e-8
72+
strategy = truncrank(10) & trunctol(; atol = 1e-8)
73+
D, V = eigh_trunc(A; trunc = strategy)
74+
```
75+
76+
## Complete Example
77+
78+
Here's a complete example demonstrating different truncation approaches:
79+
80+
```julia
81+
using MatrixAlgebraKit
82+
using LinearAlgebra
83+
84+
# Generate a test matrix with known spectrum
85+
n = 50
86+
A = randn(n, n)
87+
A = A + A' # Make symmetric
88+
89+
# 1. No truncation - keep all eigenvalues
90+
D_full, V_full = eigh_trunc(A; trunc = nothing)
91+
@assert size(D_full) == (n, n)
92+
93+
# 2. Keep only the 10 largest eigenvalues
94+
D_rank, V_rank = eigh_trunc(A; trunc = (maxrank = 10,))
95+
@assert size(D_rank) == (10, 10)
96+
@assert size(V_rank) == (n, 10)
97+
98+
# 3. Keep eigenvalues with absolute value above a threshold
99+
D_tol, V_tol = eigh_trunc(A; trunc = (atol = 1e-6,))
100+
println("Kept $(size(D_tol, 1)) eigenvalues above tolerance")
101+
102+
# 4. Combine rank and tolerance truncation
103+
strategy = truncrank(15) & trunctol(; atol = 1e-8)
104+
D_combined, V_combined = eigh_trunc(A; trunc = strategy)
105+
println("Kept $(size(D_combined, 1)) eigenvalues (max 15, all above 1e-8)")
106+
107+
# 5. Truncated SVD example
108+
B = randn(100, 80)
109+
U, S, Vh = svd_trunc(B; trunc = (maxrank = 20,))
110+
@assert size(S) == (20, 20)
111+
@assert size(U) == (100, 20)
112+
@assert size(Vh) == (20, 80)
113+
114+
# Verify the truncated decomposition is accurate
115+
@assert norm(B - U * S * Vh) ≈ norm(svd(B).S[21:end])
116+
```
117+
118+
## Truncation with SVD vs Eigenvalue Decompositions
119+
120+
When using truncations with different decomposition types, keep in mind:
121+
122+
- **`svd_trunc`**: Singular values are always real and non-negative, sorted in descending order. Truncation by value typically keeps the largest singular values.
123+
124+
- **`eigh_trunc`**: Eigenvalues are real but can be negative for symmetric matrices. By default, `truncrank` sorts by absolute value, so `truncrank(k)` keeps the `k` eigenvalues with largest magnitude (positive or negative).
125+
126+
- **`eig_trunc`**: For general (non-symmetric) matrices, eigenvalues can be complex. Truncation by absolute value considers the complex magnitude.
127+
128+
## Advanced: Custom Truncation Filters
129+
130+
For specialized needs, you can use [`truncfilter`](@ref) to define custom selection criteria:
131+
132+
```julia
133+
# Keep only positive eigenvalues
134+
strategy = truncfilter(x -> x > 0)
135+
D_positive, V_positive = eigh_trunc(A; trunc = strategy)
136+
137+
# Keep eigenvalues in a specific range
138+
strategy = truncfilter(x -> 0.1 < abs(x) < 10.0)
139+
D_range, V_range = eigh_trunc(A; trunc = strategy)
25140
```

0 commit comments

Comments
 (0)