-
-
Notifications
You must be signed in to change notification settings - Fork 34
Description
Hi maintainers,
I'd like to propose adding an in-place eigendecomposition function, eigen_inplace! (as new function or overload of eigen!), to LinearAlgebra.jl. This would allow users to compute eigenvalues (and optionally eigenvectors) without allocating memory for eigenvalues, which can be especially useful for large amount of small matrices.
Below is a minimal working example that computes the eigenvalues and overwrites a Hermitian matrix A with its eigenvectors using the MKL routine on Hermitian matrix (imitating syev! in
LinearAlgebra.jl/src/lapack.jl
Line 5543 in 28ee87e
Base.@constprop :none function _syev!(jobz::AbstractChar, uplo::AbstractChar, A::AbstractMatrix{$elty}) |
function eigen_inplace!(A::AbstractMatrix{ComplexF64}, W::AbstractArray{Float64,1})
n = size(A, 1)
work = Vector{ComplexF64}(undef, 1)
lwork = Int32(-1)
rwork = Vector{Float64}(undef, max(1, 3n - 2))
info = Ref{Int32}()
for i = 1:2 # first call returns lwork as work[1]
ccall((:zheev_, libmkl), Cvoid,
(Ref{UInt8}, Ref{UInt8}, Ref{Int32}, Ptr{ComplexF64}, Ref{Int32},
Ptr{Float64}, Ptr{ComplexF64}, Ref{Int32}, Ptr{Float64}, Ptr{Int32},
Clong, Clong),
'V', 'U', n, A, stride(A, 2), W, work, lwork, rwork, info, 1, 1)
if i == 1
lwork = Int32(real(work[1]))
resize!(work, lwork)
end
end
(W, A)
end
Ideally, a more general and robust version would:
Support both real and complex Hermitian/symmetric matrices,
Allow choosing whether to compute eigenvectors,
Be compatible with different BLAS/LAPACK providers (not just MKL, I manually config libmkl because I don't know how libblastrampoline work),
I can't do more as I'm not familiar with julia programing.
Thanks for your time!