3 * Copyright (C) 2001, 2007 Brian Gough
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <gsl/gsl_math.h>
23 #include <gsl/gsl_vector.h>
24 #include <gsl/gsl_matrix.h>
25 #include <gsl/gsl_linalg.h>
26 #include <gsl/gsl_eigen.h>
28 /* Compute eigenvalues/eigenvectors of real symmetric matrix using
29 reduction to tridiagonal form, followed by QR iteration with
32 See Golub & Van Loan, "Matrix Computations" (3rd ed), Section 8.3
37 gsl_eigen_symmv_workspace *
38 gsl_eigen_symmv_alloc (const size_t n)
40 gsl_eigen_symmv_workspace * w ;
44 GSL_ERROR_NULL ("matrix dimension must be positive integer", GSL_EINVAL);
47 w= ((gsl_eigen_symmv_workspace *) malloc (sizeof(gsl_eigen_symmv_workspace)));
51 GSL_ERROR_NULL ("failed to allocate space for workspace", GSL_ENOMEM);
54 w->d = (double *) malloc (n * sizeof (double));
58 GSL_ERROR_NULL ("failed to allocate space for diagonal", GSL_ENOMEM);
61 w->sd = (double *) malloc (n * sizeof (double));
65 GSL_ERROR_NULL ("failed to allocate space for subdiagonal", GSL_ENOMEM);
68 w->gc = (double *) malloc (n * sizeof (double));
72 GSL_ERROR_NULL ("failed to allocate space for cosines", GSL_ENOMEM);
75 w->gs = (double *) malloc (n * sizeof (double));
79 GSL_ERROR_NULL ("failed to allocate space for sines", GSL_ENOMEM);
88 gsl_eigen_symmv_free (gsl_eigen_symmv_workspace * w)
99 gsl_eigen_symmv (gsl_matrix * A, gsl_vector * eval, gsl_matrix * evec,
100 gsl_eigen_symmv_workspace * w)
102 if (A->size1 != A->size2)
104 GSL_ERROR ("matrix must be square to compute eigenvalues", GSL_ENOTSQR);
106 else if (eval->size != A->size1)
108 GSL_ERROR ("eigenvalue vector must match matrix size", GSL_EBADLEN);
110 else if (evec->size1 != A->size1 || evec->size2 != A->size1)
112 GSL_ERROR ("eigenvector matrix must match matrix size", GSL_EBADLEN);
116 double *const d = w->d;
117 double *const sd = w->sd;
118 const size_t N = A->size1;
121 /* handle special case */
125 double A00 = gsl_matrix_get (A, 0, 0);
126 gsl_vector_set (eval, 0, A00);
127 gsl_matrix_set (evec, 0, 0, 1.0);
131 /* use sd as the temporary workspace for the decomposition when
132 computing eigenvectors */
135 gsl_vector_view d_vec = gsl_vector_view_array (d, N);
136 gsl_vector_view sd_vec = gsl_vector_view_array (sd, N - 1);
137 gsl_vector_view tau = gsl_vector_view_array (sd, N - 1);
138 gsl_linalg_symmtd_decomp (A, &tau.vector);
139 gsl_linalg_symmtd_unpack (A, &tau.vector, evec, &d_vec.vector, &sd_vec.vector);
142 /* Make an initial pass through the tridiagonal decomposition
143 to remove off-diagonal elements which are effectively zero */
145 chop_small_elements (N, d, sd);
147 /* Progressively reduce the matrix until it is diagonal */
153 if (sd[b - 1] == 0.0 || isnan(sd[b - 1]))
159 /* Find the largest unreduced block (a,b) starting from b
160 and working backwards */
166 if (sd[a - 1] == 0.0)
175 const size_t n_block = b - a + 1;
176 double *d_block = d + a;
177 double *sd_block = sd + a;
178 double * const gc = w->gc;
179 double * const gs = w->gs;
181 /* apply QR reduction with implicit deflation to the
184 qrstep (n_block, d_block, sd_block, gc, gs);
186 /* Apply Givens rotation Gij(c,s) to matrix Q, Q <- Q G */
188 for (i = 0; i < n_block - 1; i++)
190 const double c = gc[i], s = gs[i];
193 for (k = 0; k < N; k++)
195 double qki = gsl_matrix_get (evec, k, a + i);
196 double qkj = gsl_matrix_get (evec, k, a + i + 1);
197 gsl_matrix_set (evec, k, a + i, qki * c - qkj * s);
198 gsl_matrix_set (evec, k, a + i + 1, qki * s + qkj * c);
202 /* remove any small off-diagonal elements */
204 chop_small_elements (N, d, sd);
209 gsl_vector_view d_vec = gsl_vector_view_array (d, N);
210 gsl_vector_memcpy (eval, &d_vec.vector);