I am trying to speed up my code, which many times finds a lowest eigenvalue of a Hermitian (symmetric, real) matrix. Using a small 2x2 matrix as an example, this works:
Energy = Compile[{{matrix, _Real, 2}}, Min[Re[Eigenvalues[matrix]]], {{Eigenvalues[_], _Complex, 1}}];
Energy[{{1, -2}, {-2, 3}}]
-0.236068
I have to use {{Eigenvalues[_], _Complex, 1}}
here, otherwise Mathematica skips using the compiled function because of a type mismatch. Function Eigenvalues by default returns a vector of complex eigenvalues.
It should be in principle possible to compile a faster version, since I am only interested in the smallest eigenvalue, using Eigenvalues[matrix, -1]
, which only solves for one eigenvalue. However, I can't seem to make it to work:
Energy = Compile[{{matrix, _Real, 2}},Eigenvalues[matrix, -1][[1]], {{Eigenvalues[_], _Complex, 1}}];
Energy[{{1, -2}, {-2, 3}}]
During evaluation of CompiledFunction::cfex: Could not complete external evaluation at instruction 1; proceeding with uncompiled evaluation. >>
2 - Sqrt[5]
Where am I making a mistake? Also, is there a room for possible speedup if I somehow convince Mathematica, that I am only using Hermitian matrices and therefore expect only real eigenvalues?