3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
4 * Copyright (C) 2004 Joerg Wensch, modifications for LQ.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <gsl/gsl_blas.h>
25 #include <gsl/gsl_math.h>
26 #include <gsl/gsl_vector.h>
27 #include <gsl/gsl_matrix.h>
28 #include <gsl/gsl_permute_vector.h>
29 #include <gsl/gsl_linalg.h>
32 #include "apply_givens.c"
34 /* The purpose of this package is to speed up QR-decomposition for
35 large matrices. Because QR-decomposition is column oriented, but
36 GSL uses a row-oriented matrix format, there can considerable
37 speedup obtained by computing the LQ-decomposition of the
38 transposed matrix instead. This package provides LQ-decomposition
39 and related algorithms. */
41 /* Factorise a general N x M matrix A into
45 * where Q is orthogonal (M x M) and L is lower triangular (N x M).
46 * When A is rank deficient, r = rank(A) < n, then the permutation is
47 * used to ensure that the lower n - r columns of L are zero and the first
48 * l rows of Q form an orthonormal basis for the rows of A.
50 * Q is stored as a packed set of Householder transformations in the
51 * strict upper triangular part of the input matrix.
53 * L is stored in the diagonal and lower triangle of the input matrix.
55 * P: column j of P is column k of the identity matrix, where k =
56 * permutation->data[j]
58 * The full matrix for Q can be obtained as the product
62 * where k = MIN(M,N) and
64 * Q_i = (I - tau_i * v_i * v_i')
66 * and where v_i is a Householder vector
68 * v_i = [1, m(i,i+1), m(i,i+2), ... , m(i,M)]
70 * This storage scheme is the same as in LAPACK. See LAPACK's
71 * dgeqpf.f for details.
76 gsl_linalg_PTLQ_decomp (gsl_matrix * A, gsl_vector * tau, gsl_permutation * p, int *signum, gsl_vector * norm)
78 const size_t N = A->size1;
79 const size_t M = A->size2;
81 if (tau->size != GSL_MIN (M, N))
83 GSL_ERROR ("size of tau must be MIN(M,N)", GSL_EBADLEN);
85 else if (p->size != N)
87 GSL_ERROR ("permutation size must be N", GSL_EBADLEN);
89 else if (norm->size != N)
91 GSL_ERROR ("norm size must be N", GSL_EBADLEN);
99 gsl_permutation_init (p); /* set to identity */
101 /* Compute column norms and store in workspace */
103 for (i = 0; i < N; i++)
105 gsl_vector_view c = gsl_matrix_row (A, i);
106 double x = gsl_blas_dnrm2 (&c.vector);
107 gsl_vector_set (norm, i, x);
110 for (i = 0; i < GSL_MIN (M, N); i++)
112 /* Bring the column of largest norm into the pivot position */
114 double max_norm = gsl_vector_get(norm, i);
117 for (j = i + 1; j < N; j++)
119 double x = gsl_vector_get (norm, j);
130 gsl_matrix_swap_rows (A, i, kmax);
131 gsl_permutation_swap (p, i, kmax);
132 gsl_vector_swap_elements(norm,i,kmax);
134 (*signum) = -(*signum);
137 /* Compute the Householder transformation to reduce the j-th
138 column of the matrix to a multiple of the j-th unit vector */
141 gsl_vector_view c_full = gsl_matrix_row (A, i);
142 gsl_vector_view c = gsl_vector_subvector (&c_full.vector,
144 double tau_i = gsl_linalg_householder_transform (&c.vector);
146 gsl_vector_set (tau, i, tau_i);
148 /* Apply the transformation to the remaining columns */
152 gsl_matrix_view m = gsl_matrix_submatrix (A, i +1, i, N - (i+1), M - i);
154 gsl_linalg_householder_mh (tau_i, &c.vector, &m.matrix);
158 /* Update the norms of the remaining columns too */
162 for (j = i + 1; j < N; j++)
164 double x = gsl_vector_get (norm, j);
169 double temp= gsl_matrix_get (A, j, i) / x;
171 if (fabs (temp) >= 1)
174 y = x * sqrt (1 - temp * temp);
176 /* recompute norm to prevent loss of accuracy */
178 if (fabs (y / x) < sqrt (20.0) * GSL_SQRT_DBL_EPSILON)
180 gsl_vector_view c_full = gsl_matrix_row (A, j);
182 gsl_vector_subvector(&c_full.vector,
184 y = gsl_blas_dnrm2 (&c.vector);
187 gsl_vector_set (norm, j, y);
198 gsl_linalg_PTLQ_decomp2 (const gsl_matrix * A, gsl_matrix * q, gsl_matrix * r, gsl_vector * tau, gsl_permutation * p, int *signum, gsl_vector * norm)
200 const size_t N = A->size1;
201 const size_t M = A->size2;
203 if (q->size1 != M || q->size2 !=M)
205 GSL_ERROR ("q must be M x M", GSL_EBADLEN);
207 else if (r->size1 != N || r->size2 !=M)
209 GSL_ERROR ("r must be N x M", GSL_EBADLEN);
211 else if (tau->size != GSL_MIN (M, N))
213 GSL_ERROR ("size of tau must be MIN(M,N)", GSL_EBADLEN);
215 else if (p->size != N)
217 GSL_ERROR ("permutation size must be N", GSL_EBADLEN);
219 else if (norm->size != N)
221 GSL_ERROR ("norm size must be N", GSL_EBADLEN);
224 gsl_matrix_memcpy (r, A);
226 gsl_linalg_PTLQ_decomp (r, tau, p, signum, norm);
228 /* FIXME: aliased arguments depends on behavior of unpack routine! */
230 gsl_linalg_LQ_unpack (r, tau, q, r);
236 /* Solves the system x^T A = b^T using the P^T L Q factorisation,
242 to obtain x. Based on SLATEC code. */
245 gsl_linalg_PTLQ_solve_T (const gsl_matrix * QR,
246 const gsl_vector * tau,
247 const gsl_permutation * p,
248 const gsl_vector * b,
251 if (QR->size1 != QR->size2)
253 GSL_ERROR ("QR matrix must be square", GSL_ENOTSQR);
255 else if (QR->size2 != p->size)
257 GSL_ERROR ("matrix size must match permutation size", GSL_EBADLEN);
259 else if (QR->size2 != b->size)
261 GSL_ERROR ("matrix size must match b size", GSL_EBADLEN);
263 else if (QR->size1 != x->size)
265 GSL_ERROR ("matrix size must match solution size", GSL_EBADLEN);
269 gsl_vector_memcpy (x, b);
271 gsl_linalg_PTLQ_svx_T (QR, tau, p, x);
278 gsl_linalg_PTLQ_svx_T (const gsl_matrix * LQ,
279 const gsl_vector * tau,
280 const gsl_permutation * p,
283 if (LQ->size1 != LQ->size2)
285 GSL_ERROR ("LQ matrix must be square", GSL_ENOTSQR);
287 else if (LQ->size2 != p->size)
289 GSL_ERROR ("matrix size must match permutation size", GSL_EBADLEN);
291 else if (LQ->size1 != x->size)
293 GSL_ERROR ("matrix size must match solution size", GSL_EBADLEN);
297 /* compute sol = b^T Q^T */
299 gsl_linalg_LQ_vecQT (LQ, tau, x);
301 /* Solve L^T x = sol, storing x inplace in sol */
303 gsl_blas_dtrsv (CblasLower, CblasTrans, CblasNonUnit, LQ, x);
305 gsl_permute_vector_inverse (p, x);
313 gsl_linalg_PTLQ_LQsolve_T (const gsl_matrix * Q, const gsl_matrix * L,
314 const gsl_permutation * p,
315 const gsl_vector * b,
318 if (Q->size1 != Q->size2 || L->size1 != L->size2)
322 else if (Q->size1 != p->size || Q->size1 != L->size1
323 || Q->size1 != b->size)
329 /* compute b' = Q b */
331 gsl_blas_dgemv (CblasNoTrans, 1.0, Q, b, 0.0, x);
333 /* Solve L^T x = b', storing x inplace */
335 gsl_blas_dtrsv (CblasLower, CblasTrans, CblasNonUnit, L, x);
337 /* Apply permutation to solution in place */
339 gsl_permute_vector_inverse (p, x);
346 gsl_linalg_PTLQ_Lsolve_T (const gsl_matrix * LQ,
347 const gsl_permutation * p,
348 const gsl_vector * b,
351 if (LQ->size1 != LQ->size2)
353 GSL_ERROR ("LQ matrix must be square", GSL_ENOTSQR);
355 else if (LQ->size1 != b->size)
357 GSL_ERROR ("matrix size must match b size", GSL_EBADLEN);
359 else if (LQ->size2 != x->size)
361 GSL_ERROR ("matrix size must match x size", GSL_EBADLEN);
363 else if (p->size != x->size)
365 GSL_ERROR ("permutation size must match x size", GSL_EBADLEN);
371 gsl_vector_memcpy (x, b);
373 /* Solve L^T x = b, storing x inplace */
375 gsl_blas_dtrsv (CblasLower, CblasTrans, CblasNonUnit, LQ, x);
377 gsl_permute_vector_inverse (p, x);
385 gsl_linalg_PTLQ_Lsvx_T (const gsl_matrix * LQ,
386 const gsl_permutation * p,
389 if (LQ->size1 != LQ->size2)
391 GSL_ERROR ("LQ matrix must be square", GSL_ENOTSQR);
393 else if (LQ->size2 != x->size)
395 GSL_ERROR ("matrix size must match x size", GSL_EBADLEN);
397 else if (p->size != x->size)
399 GSL_ERROR ("permutation size must match x size", GSL_EBADLEN);
403 /* Solve L^T x = b, storing x inplace */
405 gsl_blas_dtrsv (CblasLower, CblasTrans, CblasNonUnit, LQ, x);
407 gsl_permute_vector_inverse (p, x);
415 /* Update a P^T L Q factorisation for P A= L Q , A' = A + v u^T,
418 * P^T L' Q' = P^T LQ + v u^T
419 * = P^T (L + (P v) u^T Q^T) Q
420 * = P^T (L + (P v) w^T) Q
424 * Algorithm from Golub and Van Loan, "Matrix Computations", Section
425 * 12.5 (Updating Matrix Factorizations, Rank-One Changes)
429 gsl_linalg_PTLQ_update (gsl_matrix * Q, gsl_matrix * L,
430 const gsl_permutation * p,
431 const gsl_vector * v, gsl_vector * w)
433 if (Q->size1 != Q->size2 || L->size1 != L->size2)
437 else if (L->size1 != Q->size2 || v->size != Q->size2 || w->size != Q->size2)
444 const size_t N = Q->size1;
445 const size_t M = Q->size2;
448 /* Apply Given's rotations to reduce w to (|w|, 0, 0, ... , 0)
450 J_1^T .... J_(n-1)^T w = +/- |w| e_1
452 simultaneously applied to L, H = J_1^T ... J^T_(n-1) L
453 so that H is upper Hessenberg. (12.5.2) */
455 for (k = M - 1; k > 0; k--)
458 double wk = gsl_vector_get (w, k);
459 double wkm1 = gsl_vector_get (w, k - 1);
461 create_givens (wkm1, wk, &c, &s);
462 apply_givens_vec (w, k - 1, k, c, s);
463 apply_givens_lq (M, N, Q, L, k - 1, k, c, s);
466 w0 = gsl_vector_get (w, 0);
468 /* Add in v w^T (Equation 12.5.3) */
470 for (j = 0; j < N; j++)
472 double lj0 = gsl_matrix_get (L, j, 0);
473 size_t p_j = gsl_permutation_get (p, j);
474 double vj = gsl_vector_get (v, p_j);
475 gsl_matrix_set (L, j, 0, lj0 + w0 * vj);
478 /* Apply Givens transformations L' = G_(n-1)^T ... G_1^T H
481 for (k = 1; k < N; k++)
484 double diag = gsl_matrix_get (L, k - 1, k - 1);
485 double offdiag = gsl_matrix_get (L, k - 1, k );
487 create_givens (diag, offdiag, &c, &s);
488 apply_givens_lq (M, N, Q, L, k - 1, k, c, s);