35 eigs_sym(
const CTensor &A, RArpack::EigType t,
size_t neig, CTensor *eigenvectors,
36 const CTensor::elt_t *initial_guess)
38 size_t n = A.columns();
40 if ((A.rank() != 2) || (A.rows() != n)) {
41 std::cerr <<
"In eigs(): Can only compute eigenvalues of square matrices.";
45 if (neig > n || neig == 0) {
46 std::cerr <<
"In eigs(): Can only compute up to " << n <<
" eigenvalues\n"
47 <<
"in a matrix that has " << n <<
" times " << n <<
" elements.";
52 RTensor values =
eig_sym(A, eigenvectors);
53 UIVector ndx = RArpack::sort_values(values, t)(Range(0, neig-1));
55 *eigenvectors = (*eigenvectors)(Range(), Range(ndx));
57 return re_part(values(Range(ndx)));
60 RArpack data(2*n, t, neig);
63 data.set_start_vector((
double *)initial_guess);
65 while (data.update() < RArpack::Finished) {
66 cdouble *x = (CTensor::elt_t*)data.get_x_vector();
67 cdouble *y = (CTensor::elt_t*)data.get_y_vector();
68 gemv(
'N', n, n, CTensor::elt_one(), A.pointer(), n, x, 1,
69 CTensor::elt_zero(), y, 1);
71 if (data.get_status() == RArpack::Finished) {
73 *eigenvectors = CTensor(n, neig);
75 return data.get_data(eigenvectors? (
double*)eigenvectors->pointer() : NULL);
77 std::cerr << data.error_message() <<
'\n';
RTensor eig_sym(const RTensor &A, RTensor *pR=0)
Eigenvalue decomposition of a real matrix.
RTensor eigs_sym(const RTensor &A, int eig_type, size_t neig, RTensor *vectors=NULL, const double *initial_guess=NULL)
Find out a few eigenvalues and eigenvectors of a symmetric real matrix.