Skip to content

Commit 91f697c

Browse files
kshyattfredrikekre
authored andcommitted
More doctests and clarifications for factorizations etc (#24696)
1 parent b41a7e2 commit 91f697c

File tree

3 files changed

+145
-1
lines changed

3 files changed

+145
-1
lines changed

base/linalg/bunchkaufman.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,28 @@ The following functions are available for
5454
5555
[^Bunch1977]: J R Bunch and L Kaufman, Some stable methods for calculating inertia and solving symmetric linear systems, Mathematics of Computation 31:137 (1977), 163-179. [url](http://www.ams.org/journals/mcom/1977-31-137/S0025-5718-1977-0428694-0/).
5656
57+
# Examples
58+
```jldoctest
59+
julia> A = [1 2; 2 3]
60+
2×2 Array{Int64,2}:
61+
1 2
62+
2 3
63+
64+
julia> bkfact(A)
65+
Base.LinAlg.BunchKaufman{Float64,Array{Float64,2}}
66+
D factor:
67+
2×2 Tridiagonal{Float64,Array{Float64,1}}:
68+
-0.333333 0.0
69+
0.0 3.0
70+
U factor:
71+
2×2 Base.LinAlg.UnitUpperTriangular{Float64,Array{Float64,2}}:
72+
1.0 0.666667
73+
0.0 1.0
74+
permutation:
75+
2-element Array{Int64,1}:
76+
1
77+
2
78+
```
5779
"""
5880
bkfact(A::AbstractMatrix{T}, rook::Bool=false) where {T} =
5981
bkfact!(copy_oftype(A, typeof(sqrt(one(T)))), rook)

base/linalg/eigen.jl

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,28 @@ Same as [`eigvals`](@ref), but saves space by overwriting the input `A`, instead
164164
The option `permute=true` permutes the matrix to become
165165
closer to upper triangular, and `scale=true` scales the matrix by its diagonal elements to
166166
make rows and columns more equal in norm.
167+
168+
!!! note
169+
The input matrix `A` will not contain its eigenvalues after `eigvals!` is
170+
called on it - `A` is used as a workspace.
171+
172+
# Examples
173+
```jldoctest
174+
julia> A = [1. 2.; 3. 4.]
175+
2×2 Array{Float64,2}:
176+
1.0 2.0
177+
3.0 4.0
178+
179+
julia> eigvals!(A)
180+
2-element Array{Float64,1}:
181+
-0.3722813232690143
182+
5.372281323269014
183+
184+
julia> A
185+
2×2 Array{Float64,2}:
186+
-0.372281 -1.0
187+
0.0 5.37228
188+
```
167189
"""
168190
function eigvals!(A::StridedMatrix{<:BlasReal}; permute::Bool=true, scale::Bool=true)
169191
issymmetric(A) && return eigvals!(Symmetric(A))
@@ -185,6 +207,19 @@ before the eigenvalue calculation. The option `permute=true` permutes the matrix
185207
become closer to upper triangular, and `scale=true` scales the matrix by its diagonal
186208
elements to make rows and columns more equal in norm. The default is `true` for both
187209
options.
210+
211+
# Examples
212+
```jldoctest
213+
julia> diag_matrix = [1 0; 0 4]
214+
2×2 Array{Int64,2}:
215+
1 0
216+
0 4
217+
218+
julia> eigvals(diag_matrix)
219+
2-element Array{Float64,1}:
220+
1.0
221+
4.0
222+
```
188223
"""
189224
function eigvals(A::StridedMatrix{T}; permute::Bool=true, scale::Bool=true) where T
190225
S = promote_type(Float32, typeof(one(T)/norm(one(T))))
@@ -317,6 +352,31 @@ Computes the generalized eigenvalue decomposition of `A` and `B`, returning a
317352
`GeneralizedEigen` factorization object `F` which contains the generalized eigenvalues in
318353
`F[:values]` and the generalized eigenvectors in the columns of the matrix `F[:vectors]`.
319354
(The `k`th generalized eigenvector can be obtained from the slice `F[:vectors][:, k]`.)
355+
356+
# Examples
357+
```jldoctest
358+
julia> A = [1 0; 0 -1]
359+
2×2 Array{Int64,2}:
360+
1 0
361+
0 -1
362+
363+
julia> B = [0 1; 1 0]
364+
2×2 Array{Int64,2}:
365+
0 1
366+
1 0
367+
368+
julia> F = eigfact(A, B);
369+
370+
julia> F[:values]
371+
2-element Array{Complex{Float64},1}:
372+
0.0 + 1.0im
373+
0.0 - 1.0im
374+
375+
julia> F[:vectors]
376+
2×2 Array{Complex{Float64},2}:
377+
0.0-1.0im 0.0+1.0im
378+
-1.0-0.0im -1.0+0.0im
379+
```
320380
"""
321381
function eigfact(A::AbstractMatrix{TA}, B::AbstractMatrix{TB}) where {TA,TB}
322382
S = promote_type(Float32, typeof(one(TA)/norm(one(TA))),TB)
@@ -361,7 +421,40 @@ end
361421
"""
362422
eigvals!(A, B) -> values
363423
364-
Same as [`eigvals`](@ref), but saves space by overwriting the input `A` (and `B`), instead of creating copies.
424+
Same as [`eigvals`](@ref), but saves space by overwriting the input `A` (and `B`),
425+
instead of creating copies.
426+
427+
!!! note
428+
The input matrices `A` and `B` will not contain their eigenvalues after
429+
`eigvals!` is called. They are used as workspaces.
430+
431+
# Examples
432+
```jldoctest
433+
julia> A = [1. 0.; 0. -1.]
434+
2×2 Array{Float64,2}:
435+
1.0 0.0
436+
0.0 -1.0
437+
438+
julia> B = [0. 1.; 1. 0.]
439+
2×2 Array{Float64,2}:
440+
0.0 1.0
441+
1.0 0.0
442+
443+
julia> eigvals!(A, B)
444+
2-element Array{Complex{Float64},1}:
445+
0.0 + 1.0im
446+
0.0 - 1.0im
447+
448+
julia> A
449+
2×2 Array{Float64,2}:
450+
-0.0 -1.0
451+
1.0 -0.0
452+
453+
julia> B
454+
2×2 Array{Float64,2}:
455+
1.0 0.0
456+
0.0 1.0
457+
```
365458
"""
366459
function eigvals!(A::StridedMatrix{T}, B::StridedMatrix{T}) where T<:BlasReal
367460
issymmetric(A) && isposdef(B) && return eigvals!(Symmetric(A), Symmetric(B))

base/linalg/lu.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,35 @@ end
3131
input `A`, instead of creating a copy. An [`InexactError`](@ref)
3232
exception is thrown if the factorization produces a number not representable by the
3333
element type of `A`, e.g. for integer types.
34+
35+
# Examples
36+
```jldoctest
37+
julia> A = [4. 3.; 6. 3.]
38+
2×2 Array{Float64,2}:
39+
6.0 3.0
40+
4.0 3.0
41+
42+
julia> F = lufact!(A)
43+
Base.LinAlg.LU{Float64,Array{Float64,2}}
44+
L factor:
45+
2×2 Array{Float64,2}:
46+
1.0 0.0
47+
0.666667 1.0
48+
U factor:
49+
2×2 Array{Float64,2}:
50+
6.0 3.0
51+
0.0 1.0
52+
53+
julia> iA = [4 3; 6 3]
54+
2×2 Array{Int64,2}:
55+
4 3
56+
6 3
57+
58+
julia> lufact!(iA)
59+
ERROR: InexactError: convert(Int64, 0.6666666666666666)
60+
Stacktrace:
61+
[...]
62+
```
3463
"""
3564
lufact!(A::StridedMatrix, pivot::Union{Val{false}, Val{true}} = Val(true)) = generic_lufact!(A, pivot)
3665
function generic_lufact!(A::StridedMatrix{T}, ::Val{Pivot} = Val(true)) where {T,Pivot}

0 commit comments

Comments
 (0)