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.
20 /* Factorise a symmetric matrix A into
24 * where Q is orthogonal and T is symmetric tridiagonal. Only the
25 * diagonal and lower triangular part of A is referenced and modified.
27 * On exit, T is stored in the diagonal and first subdiagonal of
28 * A. Since T is symmetric the upper diagonal is not stored.
30 * Q is stored as a packed set of Householder transformations in the
31 * lower triangular part of the input matrix below the first subdiagonal.
33 * The full matrix for Q can be obtained as the product
35 * Q = Q_1 Q_2 ... Q_(N-2)
39 * Q_i = (I - tau_i * v_i * v_i')
41 * and where v_i is a Householder vector
43 * v_i = [0, ... , 0, 1, A(i+1,i), A(i+2,i), ... , A(N,i)]
45 * This storage scheme is the same as in LAPACK. See LAPACK's
46 * ssytd2.f for details.
48 * See Golub & Van Loan, "Matrix Computations" (3rd ed), Section 8.3
50 * Note: this description uses 1-based indices. The code below uses
56 #include <gsl/gsl_math.h>
57 #include <gsl/gsl_vector.h>
58 #include <gsl/gsl_matrix.h>
59 #include <gsl/gsl_blas.h>
61 #include <gsl/gsl_linalg.h>
64 gsl_linalg_symmtd_decomp (gsl_matrix * A, gsl_vector * tau)
66 if (A->size1 != A->size2)
68 GSL_ERROR ("symmetric tridiagonal decomposition requires square matrix",
71 else if (tau->size + 1 != A->size1)
73 GSL_ERROR ("size of tau must be (matrix size - 1)", GSL_EBADLEN);
77 const size_t N = A->size1;
80 for (i = 0 ; i < N - 2; i++)
82 gsl_vector_view c = gsl_matrix_column (A, i);
83 gsl_vector_view v = gsl_vector_subvector (&c.vector, i + 1, N - (i + 1));
84 double tau_i = gsl_linalg_householder_transform (&v.vector);
86 /* Apply the transformation H^T A H to the remaining columns */
90 gsl_matrix_view m = gsl_matrix_submatrix (A, i + 1, i + 1,
91 N - (i+1), N - (i+1));
92 double ei = gsl_vector_get(&v.vector, 0);
93 gsl_vector_view x = gsl_vector_subvector (tau, i, N-(i+1));
94 gsl_vector_set (&v.vector, 0, 1.0);
97 gsl_blas_dsymv (CblasLower, tau_i, &m.matrix, &v.vector, 0.0, &x.vector);
99 /* w = x - (1/2) tau * (x' * v) * v */
102 gsl_blas_ddot(&x.vector, &v.vector, &xv);
103 alpha = - (tau_i / 2.0) * xv;
104 gsl_blas_daxpy(alpha, &v.vector, &x.vector);
107 /* apply the transformation A = A - v w' - w v' */
108 gsl_blas_dsyr2(CblasLower, -1.0, &v.vector, &x.vector, &m.matrix);
110 gsl_vector_set (&v.vector, 0, ei);
113 gsl_vector_set (tau, i, tau_i);
121 /* Form the orthogonal matrix Q from the packed QR matrix */
124 gsl_linalg_symmtd_unpack (const gsl_matrix * A,
125 const gsl_vector * tau,
130 if (A->size1 != A->size2)
132 GSL_ERROR ("matrix A must be square", GSL_ENOTSQR);
134 else if (tau->size + 1 != A->size1)
136 GSL_ERROR ("size of tau must be (matrix size - 1)", GSL_EBADLEN);
138 else if (Q->size1 != A->size1 || Q->size2 != A->size1)
140 GSL_ERROR ("size of Q must match size of A", GSL_EBADLEN);
142 else if (diag->size != A->size1)
144 GSL_ERROR ("size of diagonal must match size of A", GSL_EBADLEN);
146 else if (sdiag->size + 1 != A->size1)
148 GSL_ERROR ("size of subdiagonal must be (matrix size - 1)", GSL_EBADLEN);
152 const size_t N = A->size1;
156 /* Initialize Q to the identity */
158 gsl_matrix_set_identity (Q);
160 for (i = N - 2; i > 0 && i--;)
162 gsl_vector_const_view c = gsl_matrix_const_column (A, i);
163 gsl_vector_const_view h = gsl_vector_const_subvector (&c.vector, i + 1, N - (i+1));
164 double ti = gsl_vector_get (tau, i);
166 gsl_matrix_view m = gsl_matrix_submatrix (Q, i + 1, i + 1, N-(i+1), N-(i+1));
168 gsl_linalg_householder_hm (ti, &h.vector, &m.matrix);
171 /* Copy diagonal into diag */
173 for (i = 0; i < N; i++)
175 double Aii = gsl_matrix_get (A, i, i);
176 gsl_vector_set (diag, i, Aii);
179 /* Copy subdiagonal into sd */
181 for (i = 0; i < N - 1; i++)
183 double Aji = gsl_matrix_get (A, i+1, i);
184 gsl_vector_set (sdiag, i, Aji);
192 gsl_linalg_symmtd_unpack_T (const gsl_matrix * A,
196 if (A->size1 != A->size2)
198 GSL_ERROR ("matrix A must be square", GSL_ENOTSQR);
200 else if (diag->size != A->size1)
202 GSL_ERROR ("size of diagonal must match size of A", GSL_EBADLEN);
204 else if (sdiag->size + 1 != A->size1)
206 GSL_ERROR ("size of subdiagonal must be (matrix size - 1)", GSL_EBADLEN);
210 const size_t N = A->size1;
214 /* Copy diagonal into diag */
216 for (i = 0; i < N; i++)
218 double Aii = gsl_matrix_get (A, i, i);
219 gsl_vector_set (diag, i, Aii);
222 /* Copy subdiagonal into sdiag */
224 for (i = 0; i < N - 1; i++)
226 double Aij = gsl_matrix_get (A, i+1, i);
227 gsl_vector_set (sdiag, i, Aij);