3 * Copyright (C) 2006 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_vector_complex.h>
29 #include <gsl/gsl_matrix.h>
32 * This module computes the eigenvalues of a real nonsymmetric
33 * matrix, using the double shift Francis method.
35 * See the references in francis.c.
37 * This module gets the matrix ready by balancing it and
38 * reducing it to Hessenberg form before passing it to the
43 gsl_eigen_nonsymm_alloc()
45 Allocate a workspace for solving the nonsymmetric eigenvalue problem.
46 The size of this workspace is O(2n)
48 Inputs: n - size of matrix
50 Return: pointer to workspace
53 gsl_eigen_nonsymm_workspace *
54 gsl_eigen_nonsymm_alloc(const size_t n)
56 gsl_eigen_nonsymm_workspace *w;
60 GSL_ERROR_NULL ("matrix dimension must be positive integer",
64 w = (gsl_eigen_nonsymm_workspace *)
65 calloc (1, sizeof (gsl_eigen_nonsymm_workspace));
69 GSL_ERROR_NULL ("failed to allocate space for workspace", GSL_ENOMEM);
76 w->diag = gsl_vector_alloc(n);
80 gsl_eigen_nonsymm_free(w);
81 GSL_ERROR_NULL ("failed to allocate space for balancing vector", GSL_ENOMEM);
84 w->tau = gsl_vector_alloc(n);
88 gsl_eigen_nonsymm_free(w);
89 GSL_ERROR_NULL ("failed to allocate space for hessenberg coefficients", GSL_ENOMEM);
92 w->francis_workspace_p = gsl_eigen_francis_alloc();
94 if (w->francis_workspace_p == 0)
96 gsl_eigen_nonsymm_free(w);
97 GSL_ERROR_NULL ("failed to allocate space for francis workspace", GSL_ENOMEM);
101 } /* gsl_eigen_nonsymm_alloc() */
104 gsl_eigen_nonsymm_free()
109 gsl_eigen_nonsymm_free (gsl_eigen_nonsymm_workspace * w)
112 gsl_vector_free(w->tau);
115 gsl_vector_free(w->diag);
117 if (w->francis_workspace_p)
118 gsl_eigen_francis_free(w->francis_workspace_p);
121 } /* gsl_eigen_nonsymm_free() */
124 gsl_eigen_nonsymm_params()
125 Set some parameters which define how we solve the eigenvalue
128 Inputs: compute_t - 1 if we want to compute T, 0 if not
129 balance - 1 if we want to balance the matrix, 0 if not
130 w - nonsymm workspace
134 gsl_eigen_nonsymm_params (const int compute_t, const int balance,
135 gsl_eigen_nonsymm_workspace *w)
137 gsl_eigen_francis_T(compute_t, w->francis_workspace_p);
138 w->do_balance = balance;
139 } /* gsl_eigen_nonsymm_params() */
144 Solve the nonsymmetric eigenvalue problem
148 for the eigenvalues \lambda using the Francis method.
150 Here we compute the real Schur form
154 with the diagonal blocks of T giving us the eigenvalues.
155 Z is a matrix of Schur vectors which is not computed by
156 this algorithm. See gsl_eigen_nonsymm_Z().
158 Inputs: A - general real matrix
159 eval - where to store eigenvalues
162 Return: success or error
164 Notes: If T is computed, it is stored in A on output. Otherwise
165 the diagonal of A contains the 1-by-1 and 2-by-2 eigenvalue
170 gsl_eigen_nonsymm (gsl_matrix * A, gsl_vector_complex * eval,
171 gsl_eigen_nonsymm_workspace * w)
173 const size_t N = A->size1;
175 /* check matrix and vector sizes */
179 GSL_ERROR ("matrix must be square to compute eigenvalues", GSL_ENOTSQR);
181 else if (eval->size != N)
183 GSL_ERROR ("eigenvalue vector must match matrix size", GSL_EBADLEN);
191 /* balance the matrix */
192 gsl_linalg_balance_matrix(A, w->diag);
195 /* compute the Hessenberg reduction of A */
196 gsl_linalg_hessenberg_decomp(A, w->tau);
201 * initialize the matrix Z to U, which is the matrix used
202 * to construct the Hessenberg reduction.
205 /* compute U and store it in Z */
206 gsl_linalg_hessenberg_unpack(A, w->tau, w->Z);
208 /* find the eigenvalues and Schur vectors */
209 s = gsl_eigen_francis_Z(A, eval, w->Z, w->francis_workspace_p);
214 * The Schur vectors in Z are the vectors for the balanced
215 * matrix. We now must undo the balancing to get the
216 * vectors for the original matrix A.
218 gsl_linalg_balance_accum(w->Z, w->diag);
223 /* find the eigenvalues only */
224 s = gsl_eigen_francis(A, eval, w->francis_workspace_p);
227 w->n_evals = w->francis_workspace_p->n_evals;
231 } /* gsl_eigen_nonsymm() */
234 gsl_eigen_nonsymm_Z()
236 Solve the nonsymmetric eigenvalue problem
240 for the eigenvalues \lambda.
242 Here we compute the real Schur form
246 with the diagonal blocks of T giving us the eigenvalues.
247 Z is the matrix of Schur vectors.
249 Inputs: A - general real matrix
250 eval - where to store eigenvalues
251 Z - where to store Schur vectors
254 Return: success or error
256 Notes: If T is computed, it is stored in A on output. Otherwise
257 the diagonal of A contains the 1-by-1 and 2-by-2 eigenvalue
262 gsl_eigen_nonsymm_Z (gsl_matrix * A, gsl_vector_complex * eval,
263 gsl_matrix * Z, gsl_eigen_nonsymm_workspace * w)
265 /* check matrix and vector sizes */
267 if (A->size1 != A->size2)
269 GSL_ERROR ("matrix must be square to compute eigenvalues", GSL_ENOTSQR);
271 else if (eval->size != A->size1)
273 GSL_ERROR ("eigenvalue vector must match matrix size", GSL_EBADLEN);
275 else if ((Z->size1 != Z->size2) || (Z->size1 != A->size1))
277 GSL_ERROR ("Z matrix has wrong dimensions", GSL_EBADLEN);
285 s = gsl_eigen_nonsymm(A, eval, w);
291 } /* gsl_eigen_nonsymm_Z() */