Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds mineig and maxeig methods #2725

Merged
merged 2 commits into from
Apr 2, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ export
lu,
lufact,
lufact!,
maxeig,
mineig,
norm,
normfro,
null,
Expand Down
2 changes: 2 additions & 0 deletions base/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export
lu,
lufact,
lufact!,
maxeig,
mineig,
norm,
normfro,
null,
Expand Down
10 changes: 10 additions & 0 deletions base/linalg/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,16 @@ end

eigvals(x::Number) = [one(x)]

#Computes maximum and minimum eigenvalue
function maxeig(A::Union(Number, StridedMatrix))
v = eigvals(A)
iscomplex(v) ? error("Complex eigenvalues cannot be ordered") : max(v)
end
function mineig(A::Union(Number, StridedMatrix))
v = eigvals(A)
iscomplex(v) ? error("Complex eigenvalues cannot be ordered") : min(v)
end

inv(A::EigenDense) = diagmm(A.vectors, 1.0/A.values)*A.vectors'
det(A::EigenDense) = prod(A.values)

Expand Down
5 changes: 5 additions & 0 deletions base/linalg/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ eig(m::SymTridiagonal) = LAPACK.stegr!('V', copy(m.dv), copy(m.ev))
eigvals(m::SymTridiagonal, il::Int, iu::Int) = LAPACK.stebz!('I', 'E', 0.0, 0.0, il, iu, -1.0, copy(m.dv), copy(m.ev))[1]
eigvals(m::SymTridiagonal, vl::Float64, vu::Float64) = LAPACK.stebz!('V', 'E', vl, vu, 0, 0, -1.0, copy(m.dv), copy(m.ev))[1]
eigvals(m::SymTridiagonal) = LAPACK.stebz!('A', 'E', 0.0, 0.0, 0, 0, -1.0, copy(m.dv), copy(m.ev))[1]

#Computes largest and smallest eigenvalue
maxeig(m::SymTridiagonal) = eigvals(m, size(m)[1], size(m)[1])[1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

size(m, 1) avoids the creation of an array.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think size returns a tuple, but we should still do size(m,1) to avoid the tuple creation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this should be eigvals(m, size(m,1), size(m,1))[1]? I'm assuming the last one can't be helped.

mineig(m::SymTridiagonal) = eigvals(m, 1, 1)[1]

#Compute selected eigenvectors only corresponding to particular eigenvalues
eigvecs(m::SymTridiagonal) = eig(m)[2]
eigvecs{Eigenvalue<:Real}(m::SymTridiagonal, eigvals::Vector{Eigenvalue}) = LAPACK.stein!(m.dv, m.ev, eigvals)
Expand Down
10 changes: 9 additions & 1 deletion doc/stdlib/linalg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,19 @@ Linear algebra functions in Julia are largely implemented by calling functions f

Returns the eigenvalues of ``A``.

.. function:: maxeig(A)

Returns the largest eigenvalue of ``A``.

.. function:: minvals(A)

Returns the smallest eigenvalue of ``A``.

.. function:: eigvecs(A, [eigvals])

Returns the eigenvectors of ``A``.

If the optional vector of eigenvalues ``eigvals`` is specified, returns the specific corresponding eigenvectors. (Currently this optional syntax only works for SymTridiagonal matrices.)
For SymTridiagonal matrices, if the optional vector of eigenvalues ``eigvals`` is specified, returns the specific corresponding eigenvectors.

.. function:: eigfact(A)

Expand Down