Skip to content

Commit 02f3394

Browse files
committed
update docs
1 parent 8bb35d9 commit 02f3394

1 file changed

Lines changed: 30 additions & 31 deletions

File tree

docs/src/user_interface/truncations.md

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ Truncation strategies allow you to control which eigenvalues or singular values
1212
Truncation strategies can be used with truncated decomposition functions in two ways, as illustrated below.
1313
For concreteness, we use the following matrix as an example:
1414

15-
```jldoctest truncations
15+
```jldoctest truncations; output=false
1616
using MatrixAlgebraKit
1717
using MatrixAlgebraKit: diagview
1818
1919
A = [2 1 0; 1 3 1; 0 1 4];
2020
D, V = eigh_full(A);
21-
2221
diagview(D) ≈ [3 - √3, 3, 3 + √3]
2322
2423
# output
25-
2624
true
2725
```
2826

@@ -31,38 +29,35 @@ true
3129
The simplest approach is to pass a `NamedTuple` with the truncation parameters.
3230
For example, keeping only the largest 2 eigenvalues:
3331

34-
```jldoctest truncations
35-
Dtrunc, Vtrunc = eigh_trunc(A; trunc = (maxrank = 2,));
32+
```jldoctest truncations; output=false
33+
Dtrunc, Vtrunc, ϵ = eigh_trunc(A; trunc = (maxrank = 2,));
3634
size(Dtrunc, 1) <= 2
3735
3836
# output
39-
4037
true
4138
```
4239

4340
Note however that there are no guarantees on the order of the output values:
4441

45-
```jldoctest truncations
42+
```jldoctest truncations; output=false
4643
diagview(Dtrunc) ≈ diagview(D)[[3, 2]]
4744
4845
# output
49-
5046
true
5147
```
5248

5349
You can also use tolerance-based truncation or combine multiple criteria:
5450

55-
```jldoctest truncations
56-
Dtrunc, Vtrunc = eigh_trunc(A; trunc = (atol = 2.9,));
51+
```jldoctest truncations; output=false
52+
Dtrunc, Vtrunc, ϵ = eigh_trunc(A; trunc = (atol = 2.9,));
5753
all(>(2.9), diagview(Dtrunc))
5854
5955
# output
60-
6156
true
6257
```
6358

64-
```jldoctest truncations
65-
Dtrunc, Vtrunc = eigh_trunc(A; trunc = (maxrank = 2, atol = 2.9));
59+
```jldoctest truncations; output=false
60+
Dtrunc, Vtrunc, ϵ = eigh_trunc(A; trunc = (maxrank = 2, atol = 2.9));
6661
size(Dtrunc, 1) <= 2 && all(>(2.9), diagview(Dtrunc))
6762
6863
# output
@@ -72,7 +67,7 @@ true
7267
In general, the keyword arguments that are supported can be found in the `TruncationStrategy` docstring:
7368

7469
```@docs; canonical = false
75-
TruncationStrategy
70+
TruncationStrategy()
7671
```
7772

7873

@@ -81,33 +76,22 @@ TruncationStrategy
8176
For more control, you can construct [`TruncationStrategy`](@ref) objects directly.
8277
This is also what the previous syntax will end up calling.
8378

84-
```jldoctest truncations
79+
```jldoctest truncations; output=false
8580
Dtrunc, Vtrunc = eigh_trunc(A; trunc = truncrank(2))
8681
size(Dtrunc, 1) <= 2
8782
8883
# output
89-
9084
true
9185
```
9286

93-
```jldoctest truncations
94-
Dtrunc, Vtrunc = eigh_trunc(A; trunc = truncrank(2) & trunctol(; atol = 2.9))
87+
```jldoctest truncations; output=false
88+
Dtrunc, Vtrunc, ϵ = eigh_trunc(A; trunc = truncrank(2) & trunctol(; atol = 2.9))
9589
size(Dtrunc, 1) <= 2 && all(>(2.9), diagview(Dtrunc))
9690
9791
# output
9892
true
9993
```
10094

101-
## Truncation with SVD vs Eigenvalue Decompositions
102-
103-
When using truncations with different decomposition types, keep in mind:
104-
105-
- **`svd_trunc`**: Singular values are always real and non-negative, sorted in descending order. Truncation by value typically keeps the largest singular values.
106-
107-
- **`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).
108-
109-
- **`eig_trunc`**: For general (non-symmetric) matrices, eigenvalues can be complex. Truncation by absolute value considers the complex magnitude.
110-
11195
## Truncation Strategies
11296

11397
MatrixAlgebraKit provides several built-in truncation strategies:
@@ -136,7 +120,22 @@ For the case of `eig_trunc`, this interpretation does not hold because the norm
136120

137121

138122
For example:
139-
```julia
140-
U, S, Vᴴ, ϵ = svd_trunc(A; trunc=truncrank(10))
141-
# ϵ is the 2-norm of the discarded singular values
123+
```jldoctest truncations; output=false
124+
using LinearAlgebra: norm
125+
U, S, Vᴴ, ϵ = svd_trunc(A; trunc=truncrank(2))
126+
norm(A - U * S * Vᴴ) ≈ ϵ # ϵ is the 2-norm of the discarded singular values
127+
128+
# output
129+
true
142130
```
131+
132+
### Truncation with SVD vs Eigenvalue Decompositions
133+
134+
When using truncations with different decomposition types, keep in mind:
135+
136+
- **[`svd_trunc`](@ref)**: Singular values are always real and non-negative, sorted in descending order. Truncation by value typically keeps the largest singular values. The truncation error gives the 2-norm difference between the original and the truncated matrix.
137+
138+
- **[`eigh_trunc`](@ref)**: Eigenvalues are real but can be negative for symmetric matrices. By default, eigenvalues are treated by absolute value, e.g. `truncrank(k)` keeps the `k` eigenvalues with largest magnitude (positive or negative). The truncation error gives the 2-norm difference between the original and the truncated matrix.
139+
140+
- **[`eig_trunc`](@ref)**: For general (non-symmetric) matrices, eigenvalues can be complex. By default, eigenvalues are treated by absolute value. The truncation error gives an indication of the magnitude of discarded values, but is not directly related to the 2-norm difference between the original and the truncated matrix.
141+

0 commit comments

Comments
 (0)