Skip to content

Commit a832fb2

Browse files
committed
Split up eigenfactorization docs
Make the docstrings accurately reflect the function signatures. Add doctests for the symmetric cases.
1 parent 2f04155 commit a832fb2

File tree

3 files changed

+146
-21
lines changed

3 files changed

+146
-21
lines changed

base/linalg/eigen.jl

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,14 @@ function eigfact!{T<:BlasComplex}(A::StridedMatrix{T}; permute::Bool=true, scale
5656
end
5757

5858
"""
59-
eigfact(A,[irange,][vl,][vu,][permute=true,][scale=true]) -> Eigen
59+
eigfact(A; permute::Bool=true, scale::Bool=true) -> Eigen
6060
6161
Computes the eigenvalue decomposition of `A`, returning an `Eigen` factorization object `F`
6262
which contains the eigenvalues in `F[:values]` and the eigenvectors in the columns of the
6363
matrix `F[:vectors]`. (The `k`th eigenvector can be obtained from the slice `F[:vectors][:, k]`.)
6464
6565
The following functions are available for `Eigen` objects: [`inv`](@ref), [`det`](@ref), and [`isposdef`](@ref).
6666
67-
If `A` is [`Symmetric`](@ref), [`Hermitian`](@ref) or
68-
[`SymTridiagonal`](@ref), it is possible to calculate only a subset of
69-
the eigenvalues by specifying either a `UnitRange` `irange` covering
70-
indices of the sorted eigenvalues or a pair `vl` and `vu` for the lower and upper boundaries
71-
of the eigenvalues.
72-
7367
For general nonsymmetric matrices it is possible to specify how the matrix is balanced
7468
before the eigenvector calculation. The option `permute=true` permutes the matrix to become
7569
closer to upper triangular, and `scale=true` scales the matrix by its diagonal elements to
@@ -106,11 +100,15 @@ function eig(A::Union{Number, StridedMatrix}; permute::Bool=true, scale::Bool=tr
106100
end
107101

108102
"""
109-
eig(A,[irange,][vl,][vu,][permute=true,][scale=true]) -> D, V
103+
eig(A::Union{SymTridiagonal, Hermitian, Symmetric}, irange::UnitRange) -> D, V
104+
eig(A::Union{SymTridiagonal, Hermitian, Symmetric}, vl::Real, vu::Real) -> D, V
105+
eig(A, permute::Bool=true, scale::Bool=true) -> D, V
110106
111107
Computes eigenvalues (`D`) and eigenvectors (`V`) of `A`.
112108
See [`eigfact`](@ref) for details on the
113109
`irange`, `vl`, and `vu` arguments
110+
(for [`SymTridiagonal`](@ref), `Hermitian`, and
111+
`Symmetric` matrices)
114112
and the `permute` and `scale` keyword arguments.
115113
The eigenvectors are returned columnwise.
116114
@@ -131,15 +129,12 @@ function eig(A::AbstractMatrix, args...)
131129
end
132130

133131
"""
134-
eigvecs(A, [eigvals,][permute=true,][scale=true]) -> Matrix
132+
eigvecs(A; permute::Bool=true, scale::Bool=true) -> Matrix
135133
136134
Returns a matrix `M` whose columns are the eigenvectors of `A`. (The `k`th eigenvector can
137135
be obtained from the slice `M[:, k]`.) The `permute` and `scale` keywords are the same as
138136
for [`eigfact`](@ref).
139137
140-
For [`SymTridiagonal`](@ref) matrices, if the optional vector of
141-
eigenvalues `eigvals` is specified, returns the specific corresponding eigenvectors.
142-
143138
# Example
144139
145140
```jldoctest
@@ -157,9 +152,12 @@ eigvecs{T,V,S,U}(F::Union{Eigen{T,V,S,U}, GeneralizedEigen{T,V,S,U}}) = F[:vecto
157152
eigvals{T,V,S,U}(F::Union{Eigen{T,V,S,U}, GeneralizedEigen{T,V,S,U}}) = F[:values]::U
158153

159154
"""
160-
eigvals!(A,[irange,][vl,][vu]) -> values
155+
eigvals!(A; permute::Bool=true, scale::Bool=true) -> values
161156
162157
Same as [`eigvals`](@ref), but saves space by overwriting the input `A`, instead of creating a copy.
158+
The option `permute=true` permutes the matrix to become
159+
closer to upper triangular, and `scale=true` scales the matrix by its diagonal elements to
160+
make rows and columns more equal in norm.
163161
"""
164162
function eigvals!{T<:BlasReal}(A::StridedMatrix{T}; permute::Bool=true, scale::Bool=true)
165163
issymmetric(A) && return eigvals!(Symmetric(A))
@@ -172,15 +170,12 @@ function eigvals!{T<:BlasComplex}(A::StridedMatrix{T}; permute::Bool=true, scale
172170
end
173171

174172
"""
175-
eigvals(A,[irange,][vl,][vu]) -> values
173+
eigvals(A; permute::Bool=true, scale::Bool=true) -> values
176174
177-
Returns the eigenvalues of `A`. If `A` is [`Symmetric`](@ref), [`Hermitian`](@ref) or [`SymTridiagonal`](@ref),
178-
it is possible to calculate only a subset of the eigenvalues by specifying either a
179-
`UnitRange` `irange` covering indices of the sorted eigenvalues, or a pair `vl` and `vu`
180-
for the lower and upper boundaries of the eigenvalues.
175+
Returns the eigenvalues of `A`.
181176
182177
For general non-symmetric matrices it is possible to specify how the matrix is balanced
183-
before the eigenvector calculation. The option `permute=true` permutes the matrix to
178+
before the eigenvalue calculation. The option `permute=true` permutes the matrix to
184179
become closer to upper triangular, and `scale=true` scales the matrix by its diagonal
185180
elements to make rows and columns more equal in norm. The default is `true` for both
186181
options.

base/linalg/symmetric.jl

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ julia> eigfact(Supper)
4141
Base.LinAlg.Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}([-2.96684,-2.72015,0.440875,7.72015,14.526],[-0.302016 -2.22045e-16 … 1.11022e-16 0.248524; -6.67755e-16 0.596931 … -0.802293 1.93069e-17; … ; 8.88178e-16 -0.802293 … -0.596931 0.0; 0.772108 8.93933e-16 … 0.0 0.630015])
4242
```
4343
44-
`eigfact` will use a method specialized for matrices known to be symmetric.
44+
[`eigfact`](@ref) will use a method specialized for matrices known to be symmetric.
4545
Note that `Supper` will not be equal to `Slower` unless `A` is itself symmetric (e.g. if `A == A.'`).
4646
"""
4747
Symmetric(A::AbstractMatrix, uplo::Symbol=:U) = (checksquare(A);Symmetric{eltype(A),typeof(A)}(A, char_uplo(uplo)))
@@ -79,7 +79,7 @@ julia> eigfact(Hupper)
7979
Base.LinAlg.Eigen{Complex{Float64},Float64,Array{Complex{Float64},2},Array{Float64,1}}([-8.32069,-2.72015,3.1496,7.72015,17.1711],Complex{Float64}[-0.231509+0.392692im -2.77556e-17+1.11022e-16im … -4.16334e-17-4.16334e-17im -0.129023-0.00628656im; 0.0+0.0im -0.523844+0.286205im … -0.521629+0.609571im 0.0+0.0im; … ; 0.0-6.93889e-18im 0.704063-0.384669im … -0.388108+0.45354im 0.0-1.38778e-17im; 0.67898+0.0im 0.0+0.0im … 0.0+0.0im -0.661651-0.0im])
8080
```
8181
82-
`eigfact` will use a method specialized for matrices known to be Hermitian.
82+
[`eigfact`](@ref) will use a method specialized for matrices known to be Hermitian.
8383
Note that `Hupper` will not be equal to `Hlower` unless `A` is itself Hermitian (e.g. if `A == A'`).
8484
"""
8585
function Hermitian(A::AbstractMatrix, uplo::Symbol=:U)
@@ -278,22 +278,114 @@ eigfact{T1<:Real,T2}(A::RealHermSymComplexHerm{T1,T2}) = (T = eltype(A); S = pro
278278

279279
eigfact!{T<:BlasReal,S<:StridedMatrix}(A::RealHermSymComplexHerm{T,S}, irange::UnitRange) = Eigen(LAPACK.syevr!('V', 'I', A.uplo, A.data, 0.0, 0.0, irange.start, irange.stop, -1.0)...)
280280
# Because of #6721 it is necessary to specify the parameters explicitly here.
281+
282+
"""
283+
eigfact(A::Union{SymTridiagonal, Hermitian, Symmetric}, irange::UnitRange) -> Eigen
284+
285+
Computes the eigenvalue decomposition of `A`, returning an `Eigen` factorization object `F`
286+
which contains the eigenvalues in `F[:values]` and the eigenvectors in the columns of the
287+
matrix `F[:vectors]`. (The `k`th eigenvector can be obtained from the slice `F[:vectors][:, k]`.)
288+
289+
The following functions are available for `Eigen` objects: [`inv`](@ref), [`det`](@ref), and [`isposdef`](@ref).
290+
291+
The `UnitRange` `irange` specifies indices of the sorted eigenvalues to search for.
292+
293+
!!! note
294+
If `irange` is not `1:n`, where `n` is the dimension of `A`, then the returned factorization
295+
will be a *truncated* factorization.
296+
"""
281297
eigfact{T1<:Real,T2}(A::RealHermSymComplexHerm{T1,T2}, irange::UnitRange) = (T = eltype(A); S = promote_type(Float32, typeof(zero(T)/norm(one(T)))); eigfact!(S != T ? convert(AbstractMatrix{S}, A) : copy(A), irange))
282298

283299
eigfact!{T<:BlasReal,S<:StridedMatrix}(A::RealHermSymComplexHerm{T,S}, vl::Real, vh::Real) = Eigen(LAPACK.syevr!('V', 'V', A.uplo, A.data, convert(T, vl), convert(T, vh), 0, 0, -1.0)...)
284300
# Because of #6721 it is necessary to specify the parameters explicitly here.
301+
"""
302+
eigfact(A::Union{SymTridiagonal, Hermitian, Symmetric}, vl::Real, vu::Real) -> Eigen
303+
304+
Computes the eigenvalue decomposition of `A`, returning an `Eigen` factorization object `F`
305+
which contains the eigenvalues in `F[:values]` and the eigenvectors in the columns of the
306+
matrix `F[:vectors]`. (The `k`th eigenvector can be obtained from the slice `F[:vectors][:, k]`.)
307+
308+
The following functions are available for `Eigen` objects: [`inv`](@ref), [`det`](@ref), and [`isposdef`](@ref).
309+
310+
`vl` is the lower bound of the window of eigenvalues to search for, and `vu` is the upper bound.
311+
312+
!!! note
313+
If [`vl`, `vu`] does not contain all eigenvalues of `A`, then the returned factorization
314+
will be a *truncated* factorization.
315+
"""
285316
eigfact{T1<:Real,T2}(A::RealHermSymComplexHerm{T1,T2}, vl::Real, vh::Real) = (T = eltype(A); S = promote_type(Float32, typeof(zero(T)/norm(one(T)))); eigfact!(S != T ? convert(AbstractMatrix{S}, A) : copy(A), vl, vh))
286317

287318
eigvals!{T<:BlasReal,S<:StridedMatrix}(A::RealHermSymComplexHerm{T,S}) = LAPACK.syevr!('N', 'A', A.uplo, A.data, 0.0, 0.0, 0, 0, -1.0)[1]
288319
# Because of #6721 it is necessary to specify the parameters explicitly here.
289320
eigvals{T1<:Real,T2}(A::RealHermSymComplexHerm{T1,T2}) = (T = eltype(A); S = promote_type(Float32, typeof(zero(T)/norm(one(T)))); eigvals!(S != T ? convert(AbstractMatrix{S}, A) : copy(A)))
290321

322+
"""
323+
eigvals!(A::Union{SymTridiagonal, Hermitian, Symmetric}, irange::UnitRange) -> values
324+
325+
Same as [`eigvals`](@ref), but saves space by overwriting the input `A`, instead of creating a copy.
326+
`irange` is a range of eigenvalue *indices* to search for - for instance, the 2nd to 8th eigenvalues.
327+
"""
291328
eigvals!{T<:BlasReal,S<:StridedMatrix}(A::RealHermSymComplexHerm{T,S}, irange::UnitRange) = LAPACK.syevr!('N', 'I', A.uplo, A.data, 0.0, 0.0, irange.start, irange.stop, -1.0)[1]
292329
# Because of #6721 it is necessary to specify the parameters explicitly here.
330+
"""
331+
eigvals(A::Union{SymTridiagonal, Hermitian, Symmetric}, irange::UnitRange) -> values
332+
333+
Returns the eigenvalues of `A`. It is possible to calculate only a subset of the
334+
eigenvalues by specifying a `UnitRange` `irange` covering indices of the sorted eigenvalues,
335+
e.g. the 2nd to 8th eigenvalues.
336+
337+
```jldoctest
338+
julia> A = SymTridiagonal([1.; 2.; 1.], [2.; 3.])
339+
3×3 SymTridiagonal{Float64}:
340+
1.0 2.0 ⋅
341+
2.0 2.0 3.0
342+
⋅ 3.0 1.0
343+
344+
julia> eigvals(A, 2:2)
345+
1-element Array{Float64,1}:
346+
1.0
347+
348+
julia> eigvals(A)
349+
3-element Array{Float64,1}:
350+
-2.14005
351+
1.0
352+
5.14005
353+
```
354+
"""
293355
eigvals{T1<:Real,T2}(A::RealHermSymComplexHerm{T1,T2}, irange::UnitRange) = (T = eltype(A); S = promote_type(Float32, typeof(zero(T)/norm(one(T)))); eigvals!(S != T ? convert(AbstractMatrix{S}, A) : copy(A), irange))
294356

357+
"""
358+
eigvals!(A::Union{SymTridiagonal, Hermitian, Symmetric}, vl::Real, vu::Real) -> values
359+
360+
Same as [`eigvals`](@ref), but saves space by overwriting the input `A`, instead of creating a copy.
361+
`vl` is the lower bound of the interval to search for eigenvalues, and `vu` is the upper bound.
362+
"""
295363
eigvals!{T<:BlasReal,S<:StridedMatrix}(A::RealHermSymComplexHerm{T,S}, vl::Real, vh::Real) = LAPACK.syevr!('N', 'V', A.uplo, A.data, convert(T, vl), convert(T, vh), 0, 0, -1.0)[1]
296364
# Because of #6721 it is necessary to specify the parameters explicitly here.
365+
"""
366+
eigvals(A::Union{SymTridiagonal, Hermitian, Symmetric}, vl::Real, vu::Real) -> values
367+
368+
Returns the eigenvalues of `A`. It is possible to calculate only a subset of the eigenvalues
369+
by specifying a pair `vl` and `vu` for the lower and upper boundaries of the eigenvalues.
370+
371+
```jldoctest
372+
julia> A = SymTridiagonal([1.; 2.; 1.], [2.; 3.])
373+
3×3 SymTridiagonal{Float64}:
374+
1.0 2.0 ⋅
375+
2.0 2.0 3.0
376+
⋅ 3.0 1.0
377+
378+
julia> eigvals(A, -1, 2)
379+
1-element Array{Float64,1}:
380+
1.0
381+
382+
julia> eigvals(A)
383+
3-element Array{Float64,1}:
384+
-2.14005
385+
1.0
386+
5.14005
387+
```
388+
"""
297389
eigvals{T1<:Real,T2}(A::RealHermSymComplexHerm{T1,T2}, vl::Real, vh::Real) = (T = eltype(A); S = promote_type(Float32, typeof(zero(T)/norm(one(T)))); eigvals!(S != T ? convert(AbstractMatrix{S}, A) : copy(A), vl, vh))
298390

299391
eigmax{T<:Real,S<:StridedMatrix}(A::RealHermSymComplexHerm{T,S}) = eigvals(A, size(A, 1):size(A, 1))[1]

base/linalg/tridiag.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,44 @@ eigmin(A::SymTridiagonal) = eigvals(A, 1:1)[1]
202202

203203
#Compute selected eigenvectors only corresponding to particular eigenvalues
204204
eigvecs(A::SymTridiagonal) = eigfact(A)[:vectors]
205+
206+
"""
207+
eigvecs(A::SymTridiagonal[, eigvals]) -> Matrix
208+
209+
Returns a matrix `M` whose columns are the eigenvectors of `A`. (The `k`th eigenvector can
210+
be obtained from the slice `M[:, k]`.)
211+
212+
If the optional vector of eigenvalues `eigvals` is specified, `eigvecs`
213+
returns the specific corresponding eigenvectors.
214+
215+
# Example
216+
217+
```jldoctest
218+
julia> A = SymTridiagonal([1.; 2.; 1.], [2.; 3.])
219+
3×3 SymTridiagonal{Float64}:
220+
1.0 2.0 ⋅
221+
2.0 2.0 3.0
222+
⋅ 3.0 1.0
223+
224+
julia> eigvals(A)
225+
3-element Array{Float64,1}:
226+
-2.14005
227+
1.0
228+
5.14005
229+
230+
julia> eigvecs(A)
231+
3×3 Array{Float64,2}:
232+
0.418304 -0.83205 0.364299
233+
-0.656749 -7.39009e-16 0.754109
234+
0.627457 0.5547 0.546448
235+
236+
julia> eigvecs(A, [1.])
237+
3×1 Array{Float64,2}:
238+
0.83205
239+
4.26351e-17
240+
-0.5547
241+
```
242+
"""
205243
eigvecs{T<:BlasFloat,Eigenvalue<:Real}(A::SymTridiagonal{T}, eigvals::Vector{Eigenvalue}) = LAPACK.stein!(A.dv, A.ev, eigvals)
206244

207245
#tril and triu

0 commit comments

Comments
 (0)