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 matrix A into
24 * where U and V are orthogonal and B is upper bidiagonal.
26 * On exit, B is stored in the diagonal and first superdiagonal of A.
28 * U is stored as a packed set of Householder transformations in the
29 * lower triangular part of the input matrix below the diagonal.
31 * V is stored as a packed set of Householder transformations in the
32 * upper triangular part of the input matrix above the first
35 * The full matrix for U can be obtained as the product
41 * U_i = (I - tau_i * u_i * u_i')
43 * and where u_i is a Householder vector
45 * u_i = [0, .. , 0, 1, A(i+1,i), A(i+3,i), .. , A(M,i)]
47 * The full matrix for V can be obtained as the product
49 * V = V_1 V_2 .. V_(N-2)
53 * V_i = (I - tau_i * v_i * v_i')
55 * and where v_i is a Householder vector
57 * v_i = [0, .. , 0, 1, A(i,i+2), A(i,i+3), .. , A(i,N)]
59 * See Golub & Van Loan, "Matrix Computations" (3rd ed), Algorithm 5.4.2
61 * Note: this description uses 1-based indices. The code below uses
67 #include <gsl/gsl_math.h>
68 #include <gsl/gsl_vector.h>
69 #include <gsl/gsl_matrix.h>
70 #include <gsl/gsl_blas.h>
72 #include <gsl/gsl_linalg.h>
75 gsl_linalg_bidiag_decomp (gsl_matrix * A, gsl_vector * tau_U, gsl_vector * tau_V)
77 if (A->size1 < A->size2)
79 GSL_ERROR ("bidiagonal decomposition requires M>=N", GSL_EBADLEN);
81 else if (tau_U->size != A->size2)
83 GSL_ERROR ("size of tau_U must be N", GSL_EBADLEN);
85 else if (tau_V->size + 1 != A->size2)
87 GSL_ERROR ("size of tau_V must be (N - 1)", GSL_EBADLEN);
91 const size_t M = A->size1;
92 const size_t N = A->size2;
95 for (i = 0 ; i < N; i++)
97 /* Apply Householder transformation to current column */
100 gsl_vector_view c = gsl_matrix_column (A, i);
101 gsl_vector_view v = gsl_vector_subvector (&c.vector, i, M - i);
102 double tau_i = gsl_linalg_householder_transform (&v.vector);
104 /* Apply the transformation to the remaining columns */
109 gsl_matrix_submatrix (A, i, i + 1, M - i, N - (i + 1));
110 gsl_linalg_householder_hm (tau_i, &v.vector, &m.matrix);
113 gsl_vector_set (tau_U, i, tau_i);
117 /* Apply Householder transformation to current row */
121 gsl_vector_view r = gsl_matrix_row (A, i);
122 gsl_vector_view v = gsl_vector_subvector (&r.vector, i + 1, N - (i + 1));
123 double tau_i = gsl_linalg_householder_transform (&v.vector);
125 /* Apply the transformation to the remaining rows */
130 gsl_matrix_submatrix (A, i+1, i+1, M - (i+1), N - (i+1));
131 gsl_linalg_householder_mh (tau_i, &v.vector, &m.matrix);
134 gsl_vector_set (tau_V, i, tau_i);
142 /* Form the orthogonal matrices U, V, diagonal d and superdiagonal sd
143 from the packed bidiagonal matrix A */
146 gsl_linalg_bidiag_unpack (const gsl_matrix * A,
147 const gsl_vector * tau_U,
149 const gsl_vector * tau_V,
152 gsl_vector * superdiag)
154 const size_t M = A->size1;
155 const size_t N = A->size2;
157 const size_t K = GSL_MIN(M, N);
161 GSL_ERROR ("matrix A must have M >= N", GSL_EBADLEN);
163 else if (tau_U->size != K)
165 GSL_ERROR ("size of tau must be MIN(M,N)", GSL_EBADLEN);
167 else if (tau_V->size + 1 != K)
169 GSL_ERROR ("size of tau must be MIN(M,N) - 1", GSL_EBADLEN);
171 else if (U->size1 != M || U->size2 != N)
173 GSL_ERROR ("size of U must be M x N", GSL_EBADLEN);
175 else if (V->size1 != N || V->size2 != N)
177 GSL_ERROR ("size of V must be N x N", GSL_EBADLEN);
179 else if (diag->size != K)
181 GSL_ERROR ("size of diagonal must match size of A", GSL_EBADLEN);
183 else if (superdiag->size + 1 != K)
185 GSL_ERROR ("size of subdiagonal must be (diagonal size - 1)", GSL_EBADLEN);
191 /* Copy diagonal into diag */
193 for (i = 0; i < N; i++)
195 double Aii = gsl_matrix_get (A, i, i);
196 gsl_vector_set (diag, i, Aii);
199 /* Copy superdiagonal into superdiag */
201 for (i = 0; i < N - 1; i++)
203 double Aij = gsl_matrix_get (A, i, i+1);
204 gsl_vector_set (superdiag, i, Aij);
207 /* Initialize V to the identity */
209 gsl_matrix_set_identity (V);
211 for (i = N - 1; i > 0 && i--;)
213 /* Householder row transformation to accumulate V */
214 gsl_vector_const_view r = gsl_matrix_const_row (A, i);
215 gsl_vector_const_view h =
216 gsl_vector_const_subvector (&r.vector, i + 1, N - (i+1));
218 double ti = gsl_vector_get (tau_V, i);
221 gsl_matrix_submatrix (V, i + 1, i + 1, N-(i+1), N-(i+1));
223 gsl_linalg_householder_hm (ti, &h.vector, &m.matrix);
226 /* Initialize U to the identity */
228 gsl_matrix_set_identity (U);
230 for (j = N; j > 0 && j--;)
232 /* Householder column transformation to accumulate U */
233 gsl_vector_const_view c = gsl_matrix_const_column (A, j);
234 gsl_vector_const_view h = gsl_vector_const_subvector (&c.vector, j, M - j);
235 double tj = gsl_vector_get (tau_U, j);
238 gsl_matrix_submatrix (U, j, j, M-j, N-j);
240 gsl_linalg_householder_hm (tj, &h.vector, &m.matrix);
248 gsl_linalg_bidiag_unpack2 (gsl_matrix * A,
253 const size_t M = A->size1;
254 const size_t N = A->size2;
256 const size_t K = GSL_MIN(M, N);
260 GSL_ERROR ("matrix A must have M >= N", GSL_EBADLEN);
262 else if (tau_U->size != K)
264 GSL_ERROR ("size of tau must be MIN(M,N)", GSL_EBADLEN);
266 else if (tau_V->size + 1 != K)
268 GSL_ERROR ("size of tau must be MIN(M,N) - 1", GSL_EBADLEN);
270 else if (V->size1 != N || V->size2 != N)
272 GSL_ERROR ("size of V must be N x N", GSL_EBADLEN);
278 /* Initialize V to the identity */
280 gsl_matrix_set_identity (V);
282 for (i = N - 1; i > 0 && i--;)
284 /* Householder row transformation to accumulate V */
285 gsl_vector_const_view r = gsl_matrix_const_row (A, i);
286 gsl_vector_const_view h =
287 gsl_vector_const_subvector (&r.vector, i + 1, N - (i+1));
289 double ti = gsl_vector_get (tau_V, i);
292 gsl_matrix_submatrix (V, i + 1, i + 1, N-(i+1), N-(i+1));
294 gsl_linalg_householder_hm (ti, &h.vector, &m.matrix);
297 /* Copy superdiagonal into tau_v */
299 for (i = 0; i < N - 1; i++)
301 double Aij = gsl_matrix_get (A, i, i+1);
302 gsl_vector_set (tau_V, i, Aij);
305 /* Allow U to be unpacked into the same memory as A, copy
306 diagonal into tau_U */
308 for (j = N; j > 0 && j--;)
310 /* Householder column transformation to accumulate U */
311 double tj = gsl_vector_get (tau_U, j);
312 double Ajj = gsl_matrix_get (A, j, j);
313 gsl_matrix_view m = gsl_matrix_submatrix (A, j, j, M-j, N-j);
315 gsl_vector_set (tau_U, j, Ajj);
316 gsl_linalg_householder_hm1 (tj, &m.matrix);
325 gsl_linalg_bidiag_unpack_B (const gsl_matrix * A,
327 gsl_vector * superdiag)
329 const size_t M = A->size1;
330 const size_t N = A->size2;
332 const size_t K = GSL_MIN(M, N);
336 GSL_ERROR ("size of diagonal must match size of A", GSL_EBADLEN);
338 else if (superdiag->size + 1 != K)
340 GSL_ERROR ("size of subdiagonal must be (matrix size - 1)", GSL_EBADLEN);
346 /* Copy diagonal into diag */
348 for (i = 0; i < K; i++)
350 double Aii = gsl_matrix_get (A, i, i);
351 gsl_vector_set (diag, i, Aii);
354 /* Copy superdiagonal into superdiag */
356 for (i = 0; i < K - 1; i++)
358 double Aij = gsl_matrix_get (A, i, i+1);
359 gsl_vector_set (superdiag, i, Aij);