gsl_linalg__ptlq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* linalg/ptlq.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
  4. * Copyright (C) 2004 Joerg Wensch, modifications for LQ.
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. #include "gsl__config.h"
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "gsl_blas.h"
  24. #include "gsl_math.h"
  25. #include "gsl_vector.h"
  26. #include "gsl_matrix.h"
  27. #include "gsl_permute_vector.h"
  28. #include "gsl_linalg.h"
  29. #include "gsl_linalg__givens.c"
  30. #include "gsl_linalg__apply_givens.c"
  31. /* The purpose of this package is to speed up QR-decomposition for
  32. large matrices. Because QR-decomposition is column oriented, but
  33. GSL uses a row-oriented matrix format, there can considerable
  34. speedup obtained by computing the LQ-decomposition of the
  35. transposed matrix instead. This package provides LQ-decomposition
  36. and related algorithms. */
  37. /* Factorise a general N x M matrix A into
  38. *
  39. * P A = L Q
  40. *
  41. * where Q is orthogonal (M x M) and L is lower triangular (N x M).
  42. * When A is rank deficient, r = rank(A) < n, then the permutation is
  43. * used to ensure that the lower n - r columns of L are zero and the first
  44. * l rows of Q form an orthonormal basis for the rows of A.
  45. *
  46. * Q is stored as a packed set of Householder transformations in the
  47. * strict upper triangular part of the input matrix.
  48. *
  49. * L is stored in the diagonal and lower triangle of the input matrix.
  50. *
  51. * P: column j of P is column k of the identity matrix, where k =
  52. * permutation->data[j]
  53. *
  54. * The full matrix for Q can be obtained as the product
  55. *
  56. * Q = Q_k .. Q_2 Q_1
  57. *
  58. * where k = MIN(M,N) and
  59. *
  60. * Q_i = (I - tau_i * v_i * v_i')
  61. *
  62. * and where v_i is a Householder vector
  63. *
  64. * v_i = [1, m(i,i+1), m(i,i+2), ... , m(i,M)]
  65. *
  66. * This storage scheme is the same as in LAPACK. See LAPACK's
  67. * dgeqpf.f for details.
  68. *
  69. */
  70. int
  71. gsl_linalg_PTLQ_decomp (gsl_matrix * A, gsl_vector * tau, gsl_permutation * p, int *signum, gsl_vector * norm)
  72. {
  73. const size_t N = A->size1;
  74. const size_t M = A->size2;
  75. if (tau->size != GSL_MIN (M, N))
  76. {
  77. GSL_ERROR ("size of tau must be MIN(M,N)", GSL_EBADLEN);
  78. }
  79. else if (p->size != N)
  80. {
  81. GSL_ERROR ("permutation size must be N", GSL_EBADLEN);
  82. }
  83. else if (norm->size != N)
  84. {
  85. GSL_ERROR ("norm size must be N", GSL_EBADLEN);
  86. }
  87. else
  88. {
  89. size_t i;
  90. *signum = 1;
  91. gsl_permutation_init (p); /* set to identity */
  92. /* Compute column norms and store in workspace */
  93. for (i = 0; i < N; i++)
  94. {
  95. gsl_vector_view c = gsl_matrix_row (A, i);
  96. double x = gsl_blas_dnrm2 (&c.vector);
  97. gsl_vector_set (norm, i, x);
  98. }
  99. for (i = 0; i < GSL_MIN (M, N); i++)
  100. {
  101. /* Bring the column of largest norm into the pivot position */
  102. double max_norm = gsl_vector_get(norm, i);
  103. size_t j, kmax = i;
  104. for (j = i + 1; j < N; j++)
  105. {
  106. double x = gsl_vector_get (norm, j);
  107. if (x > max_norm)
  108. {
  109. max_norm = x;
  110. kmax = j;
  111. }
  112. }
  113. if (kmax != i)
  114. {
  115. gsl_matrix_swap_rows (A, i, kmax);
  116. gsl_permutation_swap (p, i, kmax);
  117. gsl_vector_swap_elements(norm,i,kmax);
  118. (*signum) = -(*signum);
  119. }
  120. /* Compute the Householder transformation to reduce the j-th
  121. column of the matrix to a multiple of the j-th unit vector */
  122. {
  123. gsl_vector_view c_full = gsl_matrix_row (A, i);
  124. gsl_vector_view c = gsl_vector_subvector (&c_full.vector,
  125. i, M - i);
  126. double tau_i = gsl_linalg_householder_transform (&c.vector);
  127. gsl_vector_set (tau, i, tau_i);
  128. /* Apply the transformation to the remaining columns */
  129. if (i + 1 < N)
  130. {
  131. gsl_matrix_view m = gsl_matrix_submatrix (A, i +1, i, N - (i+1), M - i);
  132. gsl_linalg_householder_mh (tau_i, &c.vector, &m.matrix);
  133. }
  134. }
  135. /* Update the norms of the remaining columns too */
  136. if (i + 1 < M)
  137. {
  138. for (j = i + 1; j < N; j++)
  139. {
  140. double x = gsl_vector_get (norm, j);
  141. if (x > 0.0)
  142. {
  143. double y = 0;
  144. double temp= gsl_matrix_get (A, j, i) / x;
  145. if (fabs (temp) >= 1)
  146. y = 0.0;
  147. else
  148. y = x * sqrt (1 - temp * temp);
  149. /* recompute norm to prevent loss of accuracy */
  150. if (fabs (y / x) < sqrt (20.0) * GSL_SQRT_DBL_EPSILON)
  151. {
  152. gsl_vector_view c_full = gsl_matrix_row (A, j);
  153. gsl_vector_view c =
  154. gsl_vector_subvector(&c_full.vector,
  155. i+1, M - (i+1));
  156. y = gsl_blas_dnrm2 (&c.vector);
  157. }
  158. gsl_vector_set (norm, j, y);
  159. }
  160. }
  161. }
  162. }
  163. return GSL_SUCCESS;
  164. }
  165. }
  166. int
  167. 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)
  168. {
  169. const size_t N = A->size1;
  170. const size_t M = A->size2;
  171. if (q->size1 != M || q->size2 !=M)
  172. {
  173. GSL_ERROR ("q must be M x M", GSL_EBADLEN);
  174. }
  175. else if (r->size1 != N || r->size2 !=M)
  176. {
  177. GSL_ERROR ("r must be N x M", GSL_EBADLEN);
  178. }
  179. else if (tau->size != GSL_MIN (M, N))
  180. {
  181. GSL_ERROR ("size of tau must be MIN(M,N)", GSL_EBADLEN);
  182. }
  183. else if (p->size != N)
  184. {
  185. GSL_ERROR ("permutation size must be N", GSL_EBADLEN);
  186. }
  187. else if (norm->size != N)
  188. {
  189. GSL_ERROR ("norm size must be N", GSL_EBADLEN);
  190. }
  191. gsl_matrix_memcpy (r, A);
  192. gsl_linalg_PTLQ_decomp (r, tau, p, signum, norm);
  193. /* FIXME: aliased arguments depends on behavior of unpack routine! */
  194. gsl_linalg_LQ_unpack (r, tau, q, r);
  195. return GSL_SUCCESS;
  196. }
  197. /* Solves the system x^T A = b^T using the P^T L Q factorisation,
  198. z^T L = b^T Q^T
  199. x = P z;
  200. to obtain x. Based on SLATEC code. */
  201. int
  202. gsl_linalg_PTLQ_solve_T (const gsl_matrix * QR,
  203. const gsl_vector * tau,
  204. const gsl_permutation * p,
  205. const gsl_vector * b,
  206. gsl_vector * x)
  207. {
  208. if (QR->size1 != QR->size2)
  209. {
  210. GSL_ERROR ("QR matrix must be square", GSL_ENOTSQR);
  211. }
  212. else if (QR->size2 != p->size)
  213. {
  214. GSL_ERROR ("matrix size must match permutation size", GSL_EBADLEN);
  215. }
  216. else if (QR->size2 != b->size)
  217. {
  218. GSL_ERROR ("matrix size must match b size", GSL_EBADLEN);
  219. }
  220. else if (QR->size1 != x->size)
  221. {
  222. GSL_ERROR ("matrix size must match solution size", GSL_EBADLEN);
  223. }
  224. else
  225. {
  226. gsl_vector_memcpy (x, b);
  227. gsl_linalg_PTLQ_svx_T (QR, tau, p, x);
  228. return GSL_SUCCESS;
  229. }
  230. }
  231. int
  232. gsl_linalg_PTLQ_svx_T (const gsl_matrix * LQ,
  233. const gsl_vector * tau,
  234. const gsl_permutation * p,
  235. gsl_vector * x)
  236. {
  237. if (LQ->size1 != LQ->size2)
  238. {
  239. GSL_ERROR ("LQ matrix must be square", GSL_ENOTSQR);
  240. }
  241. else if (LQ->size2 != p->size)
  242. {
  243. GSL_ERROR ("matrix size must match permutation size", GSL_EBADLEN);
  244. }
  245. else if (LQ->size1 != x->size)
  246. {
  247. GSL_ERROR ("matrix size must match solution size", GSL_EBADLEN);
  248. }
  249. else
  250. {
  251. /* compute sol = b^T Q^T */
  252. gsl_linalg_LQ_vecQT (LQ, tau, x);
  253. /* Solve L^T x = sol, storing x inplace in sol */
  254. gsl_blas_dtrsv (CblasLower, CblasTrans, CblasNonUnit, LQ, x);
  255. gsl_permute_vector_inverse (p, x);
  256. return GSL_SUCCESS;
  257. }
  258. }
  259. int
  260. gsl_linalg_PTLQ_LQsolve_T (const gsl_matrix * Q, const gsl_matrix * L,
  261. const gsl_permutation * p,
  262. const gsl_vector * b,
  263. gsl_vector * x)
  264. {
  265. if (Q->size1 != Q->size2 || L->size1 != L->size2)
  266. {
  267. return GSL_ENOTSQR;
  268. }
  269. else if (Q->size1 != p->size || Q->size1 != L->size1
  270. || Q->size1 != b->size)
  271. {
  272. return GSL_EBADLEN;
  273. }
  274. else
  275. {
  276. /* compute b' = Q b */
  277. gsl_blas_dgemv (CblasNoTrans, 1.0, Q, b, 0.0, x);
  278. /* Solve L^T x = b', storing x inplace */
  279. gsl_blas_dtrsv (CblasLower, CblasTrans, CblasNonUnit, L, x);
  280. /* Apply permutation to solution in place */
  281. gsl_permute_vector_inverse (p, x);
  282. return GSL_SUCCESS;
  283. }
  284. }
  285. int
  286. gsl_linalg_PTLQ_Lsolve_T (const gsl_matrix * LQ,
  287. const gsl_permutation * p,
  288. const gsl_vector * b,
  289. gsl_vector * x)
  290. {
  291. if (LQ->size1 != LQ->size2)
  292. {
  293. GSL_ERROR ("LQ matrix must be square", GSL_ENOTSQR);
  294. }
  295. else if (LQ->size1 != b->size)
  296. {
  297. GSL_ERROR ("matrix size must match b size", GSL_EBADLEN);
  298. }
  299. else if (LQ->size2 != x->size)
  300. {
  301. GSL_ERROR ("matrix size must match x size", GSL_EBADLEN);
  302. }
  303. else if (p->size != x->size)
  304. {
  305. GSL_ERROR ("permutation size must match x size", GSL_EBADLEN);
  306. }
  307. else
  308. {
  309. /* Copy x <- b */
  310. gsl_vector_memcpy (x, b);
  311. /* Solve L^T x = b, storing x inplace */
  312. gsl_blas_dtrsv (CblasLower, CblasTrans, CblasNonUnit, LQ, x);
  313. gsl_permute_vector_inverse (p, x);
  314. return GSL_SUCCESS;
  315. }
  316. }
  317. int
  318. gsl_linalg_PTLQ_Lsvx_T (const gsl_matrix * LQ,
  319. const gsl_permutation * p,
  320. gsl_vector * x)
  321. {
  322. if (LQ->size1 != LQ->size2)
  323. {
  324. GSL_ERROR ("LQ matrix must be square", GSL_ENOTSQR);
  325. }
  326. else if (LQ->size2 != x->size)
  327. {
  328. GSL_ERROR ("matrix size must match x size", GSL_EBADLEN);
  329. }
  330. else if (p->size != x->size)
  331. {
  332. GSL_ERROR ("permutation size must match x size", GSL_EBADLEN);
  333. }
  334. else
  335. {
  336. /* Solve L^T x = b, storing x inplace */
  337. gsl_blas_dtrsv (CblasLower, CblasTrans, CblasNonUnit, LQ, x);
  338. gsl_permute_vector_inverse (p, x);
  339. return GSL_SUCCESS;
  340. }
  341. }
  342. /* Update a P^T L Q factorisation for P A= L Q , A' = A + v u^T,
  343. PA' = PA + Pv u^T
  344. * P^T L' Q' = P^T LQ + v u^T
  345. * = P^T (L + (P v) u^T Q^T) Q
  346. * = P^T (L + (P v) w^T) Q
  347. *
  348. * where w = Q^T u.
  349. *
  350. * Algorithm from Golub and Van Loan, "Matrix Computations", Section
  351. * 12.5 (Updating Matrix Factorizations, Rank-One Changes)
  352. */
  353. int
  354. gsl_linalg_PTLQ_update (gsl_matrix * Q, gsl_matrix * L,
  355. const gsl_permutation * p,
  356. const gsl_vector * v, gsl_vector * w)
  357. {
  358. if (Q->size1 != Q->size2 || L->size1 != L->size2)
  359. {
  360. return GSL_ENOTSQR;
  361. }
  362. else if (L->size1 != Q->size2 || v->size != Q->size2 || w->size != Q->size2)
  363. {
  364. return GSL_EBADLEN;
  365. }
  366. else
  367. {
  368. size_t j, k;
  369. const size_t N = Q->size1;
  370. const size_t M = Q->size2;
  371. double w0;
  372. /* Apply Given's rotations to reduce w to (|w|, 0, 0, ... , 0)
  373. J_1^T .... J_(n-1)^T w = +/- |w| e_1
  374. simultaneously applied to L, H = J_1^T ... J^T_(n-1) L
  375. so that H is upper Hessenberg. (12.5.2) */
  376. for (k = M - 1; k > 0; k--)
  377. {
  378. double c, s;
  379. double wk = gsl_vector_get (w, k);
  380. double wkm1 = gsl_vector_get (w, k - 1);
  381. create_givens (wkm1, wk, &c, &s);
  382. apply_givens_vec (w, k - 1, k, c, s);
  383. apply_givens_lq (M, N, Q, L, k - 1, k, c, s);
  384. }
  385. w0 = gsl_vector_get (w, 0);
  386. /* Add in v w^T (Equation 12.5.3) */
  387. for (j = 0; j < N; j++)
  388. {
  389. double lj0 = gsl_matrix_get (L, j, 0);
  390. size_t p_j = gsl_permutation_get (p, j);
  391. double vj = gsl_vector_get (v, p_j);
  392. gsl_matrix_set (L, j, 0, lj0 + w0 * vj);
  393. }
  394. /* Apply Givens transformations L' = G_(n-1)^T ... G_1^T H
  395. Equation 12.5.4 */
  396. for (k = 1; k < N; k++)
  397. {
  398. double c, s;
  399. double diag = gsl_matrix_get (L, k - 1, k - 1);
  400. double offdiag = gsl_matrix_get (L, k - 1, k );
  401. create_givens (diag, offdiag, &c, &s);
  402. apply_givens_lq (M, N, Q, L, k - 1, k, c, s);
  403. }
  404. return GSL_SUCCESS;
  405. }
  406. }