3 * Copyright (C) 2007 Patrick Alken
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.
23 #include <gsl/gsl_eigen.h>
24 #include <gsl/gsl_linalg.h>
25 #include <gsl/gsl_math.h>
26 #include <gsl/gsl_blas.h>
27 #include <gsl/gsl_vector.h>
28 #include <gsl/gsl_matrix.h>
31 * This module computes the eigenvalues of a real generalized
32 * symmetric-definite eigensystem A x = \lambda B x, where A and
33 * B are symmetric, and B is positive-definite.
37 gsl_eigen_gensymm_alloc()
39 Allocate a workspace for solving the generalized symmetric-definite
40 eigenvalue problem. The size of this workspace is O(2n).
42 Inputs: n - size of matrices
44 Return: pointer to workspace
47 gsl_eigen_gensymm_workspace *
48 gsl_eigen_gensymm_alloc(const size_t n)
50 gsl_eigen_gensymm_workspace *w;
54 GSL_ERROR_NULL ("matrix dimension must be positive integer",
58 w = calloc (1, sizeof (gsl_eigen_gensymm_workspace));
62 GSL_ERROR_NULL ("failed to allocate space for workspace", GSL_ENOMEM);
67 w->symm_workspace_p = gsl_eigen_symm_alloc(n);
68 if (!w->symm_workspace_p)
70 gsl_eigen_gensymm_free(w);
71 GSL_ERROR_NULL("failed to allocate space for symm workspace", GSL_ENOMEM);
75 } /* gsl_eigen_gensymm_alloc() */
78 gsl_eigen_gensymm_free()
83 gsl_eigen_gensymm_free (gsl_eigen_gensymm_workspace * w)
85 if (w->symm_workspace_p)
86 gsl_eigen_symm_free(w->symm_workspace_p);
89 } /* gsl_eigen_gensymm_free() */
94 Solve the generalized symmetric-definite eigenvalue problem
98 for the eigenvalues \lambda.
100 Inputs: A - real symmetric matrix
101 B - real symmetric and positive definite matrix
102 eval - where to store eigenvalues
105 Return: success or error
109 gsl_eigen_gensymm (gsl_matrix * A, gsl_matrix * B, gsl_vector * eval,
110 gsl_eigen_gensymm_workspace * w)
112 const size_t N = A->size1;
114 /* check matrix and vector sizes */
118 GSL_ERROR ("matrix must be square to compute eigenvalues", GSL_ENOTSQR);
120 else if ((N != B->size1) || (N != B->size2))
122 GSL_ERROR ("B matrix dimensions must match A", GSL_EBADLEN);
124 else if (eval->size != N)
126 GSL_ERROR ("eigenvalue vector must match matrix size", GSL_EBADLEN);
128 else if (w->size != N)
130 GSL_ERROR ("matrix size does not match workspace", GSL_EBADLEN);
136 /* compute Cholesky factorization of B */
137 s = gsl_linalg_cholesky_decomp(B);
138 if (s != GSL_SUCCESS)
139 return s; /* B is not positive definite */
141 /* transform to standard symmetric eigenvalue problem */
142 gsl_eigen_gensymm_standardize(A, B);
144 s = gsl_eigen_symm(A, eval, w->symm_workspace_p);
148 } /* gsl_eigen_gensymm() */
151 gsl_eigen_gensymm_standardize()
152 Reduce the generalized symmetric-definite eigenproblem to
153 the standard symmetric eigenproblem by computing
157 where L L^t is the Cholesky decomposition of B
159 Inputs: A - (input/output) real symmetric matrix
160 B - real symmetric, positive definite matrix in Cholesky form
164 Notes: A is overwritten by L^{-1} A L^{-t}
168 gsl_eigen_gensymm_standardize(gsl_matrix *A, const gsl_matrix *B)
170 const size_t N = A->size1;
174 for (i = 0; i < N; ++i)
176 /* update lower triangle of A(i:n, i:n) */
178 a = gsl_matrix_get(A, i, i);
179 b = gsl_matrix_get(B, i, i);
181 gsl_matrix_set(A, i, i, a);
185 gsl_vector_view ai = gsl_matrix_subcolumn(A, i, i + 1, N - i - 1);
187 gsl_matrix_submatrix(A, i + 1, i + 1, N - i - 1, N - i - 1);
188 gsl_vector_const_view bi =
189 gsl_matrix_const_subcolumn(B, i, i + 1, N - i - 1);
190 gsl_matrix_const_view mb =
191 gsl_matrix_const_submatrix(B, i + 1, i + 1, N - i - 1, N - i - 1);
193 gsl_blas_dscal(1.0 / b, &ai.vector);
196 gsl_blas_daxpy(c, &bi.vector, &ai.vector);
198 gsl_blas_dsyr2(CblasLower, -1.0, &ai.vector, &bi.vector, &ma.matrix);
200 gsl_blas_daxpy(c, &bi.vector, &ai.vector);
202 gsl_blas_dtrsv(CblasLower,
211 } /* gsl_eigen_gensymm_standardize() */