| 1 | /* linalg/qr.c |
| 2 | * |
| 3 | * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough |
| 4 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 18 | */ |
| 19 | |
| 20 | /* Author: G. Jungman */ |
| 21 | |
| 22 | #include "gsl__config.h"cxx . . $ `<file>/external/gsl/gsl__config.h`/ |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include "gsl_math.h"cxx . . $ `<file>/external/gsl/gsl_math.h`/ |
| 26 | #include "gsl_vector.h"cxx . . $ `<file>/external/gsl/gsl_vector.h`/ |
| 27 | #include "gsl_matrix.h"cxx . . $ `<file>/external/gsl/gsl_matrix.h`/ |
| 28 | #include "gsl_blas.h"cxx . . $ `<file>/external/gsl/gsl_blas.h`/ |
| 29 | |
| 30 | #include "gsl_linalg.h"cxx . . $ `<file>/external/gsl/gsl_linalg.h`/ |
| 31 | |
| 32 | #define REALcxx . . $ `external/gsl/gsl_linalg__qr.c:32:9`! double |
| 33 | |
| 34 | #include "gsl_linalg__givens.c"cxx . . $ `<file>/external/gsl/gsl_linalg__givens.c`/ |
| 35 | #include "gsl_linalg__apply_givens.c"cxx . . $ `<file>/external/gsl/gsl_linalg__apply_givens.c`/ |
| 36 | |
| 37 | /* Factorise a general M x N matrix A into |
| 38 | * |
| 39 | * A = Q R |
| 40 | * |
| 41 | * where Q is orthogonal (M x M) and R is upper triangular (M x N). |
| 42 | * |
| 43 | * Q is stored as a packed set of Householder transformations in the |
| 44 | * strict lower triangular part of the input matrix. |
| 45 | * |
| 46 | * R is stored in the diagonal and upper triangle of the input matrix. |
| 47 | * |
| 48 | * The full matrix for Q can be obtained as the product |
| 49 | * |
| 50 | * Q = Q_k .. Q_2 Q_1 |
| 51 | * |
| 52 | * where k = MIN(M,N) and |
| 53 | * |
| 54 | * Q_i = (I - tau_i * v_i * v_i') |
| 55 | * |
| 56 | * and where v_i is a Householder vector |
| 57 | * |
| 58 | * v_i = [1, m(i+1,i), m(i+2,i), ... , m(M,i)] |
| 59 | * |
| 60 | * This storage scheme is the same as in LAPACK. */ |
| 61 | |
| 62 | int |
| 63 | gsl_linalg_QR_decompcxx . . $ gsl_linalg_QR_decomp(2b074ddc1e11e0af). (gsl_matrixcxx . . $ gsl_matrix# * A, gsl_vectorcxx . . $ gsl_vector# * tau) |
| 64 | { |
| 65 | const size_t M = A->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; |
| 66 | const size_t N = A->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.; |
| 67 | |
| 68 | if (tau->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != GSL_MINcxx . . $ `external/gsl/gsl_math.h:123:9`! (M, N)) |
| 69 | { |
| 70 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("size of tau must be MIN(M,N)", GSL_EBADLEN); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | size_t i; |
| 75 | |
| 76 | for (i = 0; i < GSL_MINcxx . . $ `external/gsl/gsl_math.h:123:9`! (M, N); i++) |
| 77 | { |
| 78 | /* Compute the Householder transformation to reduce the j-th |
| 79 | column of the matrix to a multiple of the j-th unit vector */ |
| 80 | |
| 81 | gsl_vector_viewcxx . . $ gsl_vector_view# c_full = gsl_matrix_columncxx . . $ gsl_matrix_column(127874ddf25d9e6a). (A, i); |
| 82 | gsl_vector_viewcxx . . $ gsl_vector_view# c = gsl_vector_subvectorcxx . . $ gsl_vector_subvector(d6b353ad5d4a1905). (&(c_full.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_1#vector.), i, M-i); |
| 83 | |
| 84 | double tau_i = gsl_linalg_householder_transformcxx . . $ gsl_linalg_householder_transform(ebba72bb30db9efe). (&(c.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_1#vector.)); |
| 85 | |
| 86 | gsl_vector_setcxx . . $ gsl_vector_set(c35f863a529e8fae). (tau, i, tau_i); |
| 87 | |
| 88 | /* Apply the transformation to the remaining columns and |
| 89 | update the norms */ |
| 90 | |
| 91 | if (i + 1 < N) |
| 92 | { |
| 93 | gsl_matrix_viewcxx . . $ gsl_matrix_view# m = gsl_matrix_submatrixcxx . . $ gsl_matrix_submatrix(c7aeed2a734480d1). (A, i, i + 1, M - i, N - (i + 1)); |
| 94 | gsl_linalg_householder_hmcxx . . $ gsl_linalg_householder_hm(69ab0d44557b6447). (tau_i, &(c.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_1#vector.), &(m.matrixcxx . . $ $anonymous_type_2f547ace9ee68af9_1#matrix.)); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* Solves the system A x = b using the QR factorisation, |
| 103 | |
| 104 | * R x = Q^T b |
| 105 | * |
| 106 | * to obtain x. Based on SLATEC code. |
| 107 | */ |
| 108 | |
| 109 | int |
| 110 | gsl_linalg_QR_solvecxx . . $ gsl_linalg_QR_solve(e63c807f44c57be8). (const gsl_matrixcxx . . $ gsl_matrix# * QR, const gsl_vectorcxx . . $ gsl_vector# * tau, const gsl_vectorcxx . . $ gsl_vector# * b, gsl_vectorcxx . . $ gsl_vector# * x) |
| 111 | { |
| 112 | if (QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != QR->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.) |
| 113 | { |
| 114 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("QR matrix must be square", GSL_ENOTSQR); |
| 115 | } |
| 116 | else if (QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != b->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 117 | { |
| 118 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match b size", GSL_EBADLEN); |
| 119 | } |
| 120 | else if (QR->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2. != x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 121 | { |
| 122 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match solution size", GSL_EBADLEN); |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | /* Copy x <- b */ |
| 127 | |
| 128 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (x, b); |
| 129 | |
| 130 | /* Solve for x */ |
| 131 | |
| 132 | gsl_linalg_QR_svxcxx . . $ gsl_linalg_QR_svx(c419fed7545aef1d). (QR, tau, x); |
| 133 | |
| 134 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* Solves the system A x = b in place using the QR factorisation, |
| 139 | |
| 140 | * R x = Q^T b |
| 141 | * |
| 142 | * to obtain x. Based on SLATEC code. |
| 143 | */ |
| 144 | |
| 145 | int |
| 146 | gsl_linalg_QR_svxcxx . . $ gsl_linalg_QR_svx(c419fed7545aef1d). (const gsl_matrixcxx . . $ gsl_matrix# * QR, const gsl_vectorcxx . . $ gsl_vector# * tau, gsl_vectorcxx . . $ gsl_vector# * x) |
| 147 | { |
| 148 | |
| 149 | if (QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != QR->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.) |
| 150 | { |
| 151 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("QR matrix must be square", GSL_ENOTSQR); |
| 152 | } |
| 153 | else if (QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 154 | { |
| 155 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match x/rhs size", GSL_EBADLEN); |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | /* compute rhs = Q^T b */ |
| 160 | |
| 161 | gsl_linalg_QR_QTveccxx . . $ gsl_linalg_QR_QTvec(c419fed7545aef1d). (QR, tau, x); |
| 162 | |
| 163 | /* Solve R x = rhs, storing x in-place */ |
| 164 | |
| 165 | gsl_blas_dtrsvcxx . . $ gsl_blas_dtrsv(e00bac16b1e3cfe5). (CblasUppercxx . . $ CblasUpper., CblasNoTranscxx . . $ CblasNoTrans., CblasNonUnitcxx . . $ CblasNonUnit., QR, x); |
| 166 | |
| 167 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /* Find the least squares solution to the overdetermined system |
| 173 | * |
| 174 | * A x = b |
| 175 | * |
| 176 | * for M >= N using the QR factorization A = Q R. |
| 177 | */ |
| 178 | |
| 179 | int |
| 180 | gsl_linalg_QR_lssolvecxx . . $ gsl_linalg_QR_lssolve(eb0bc0557edf5b1). (const gsl_matrixcxx . . $ gsl_matrix# * QR, const gsl_vectorcxx . . $ gsl_vector# * tau, const gsl_vectorcxx . . $ gsl_vector# * b, gsl_vectorcxx . . $ gsl_vector# * x, gsl_vectorcxx . . $ gsl_vector# * residual) |
| 181 | { |
| 182 | const size_t M = QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; |
| 183 | const size_t N = QR->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.; |
| 184 | |
| 185 | if (M < N) |
| 186 | { |
| 187 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("QR matrix must have M>=N", GSL_EBADLEN); |
| 188 | } |
| 189 | else if (M != b->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 190 | { |
| 191 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match b size", GSL_EBADLEN); |
| 192 | } |
| 193 | else if (N != x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 194 | { |
| 195 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match solution size", GSL_EBADLEN); |
| 196 | } |
| 197 | else if (M != residual->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 198 | { |
| 199 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match residual size", GSL_EBADLEN); |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | gsl_matrix_const_viewcxx . . $ gsl_matrix_const_view# R = gsl_matrix_const_submatrixcxx . . $ gsl_matrix_const_submatrix(72addf6b6ae1c41e). (QR, 0, 0, N, N); |
| 204 | gsl_vector_viewcxx . . $ gsl_vector_view# c = gsl_vector_subvectorcxx . . $ gsl_vector_subvector(d6b353ad5d4a1905).(residual, 0, N); |
| 205 | |
| 206 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555).(residual, b); |
| 207 | |
| 208 | /* compute rhs = Q^T b */ |
| 209 | |
| 210 | gsl_linalg_QR_QTveccxx . . $ gsl_linalg_QR_QTvec(c419fed7545aef1d). (QR, tau, residual); |
| 211 | |
| 212 | /* Solve R x = rhs */ |
| 213 | |
| 214 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555).(x, &(c.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_1#vector.)); |
| 215 | |
| 216 | gsl_blas_dtrsvcxx . . $ gsl_blas_dtrsv(e00bac16b1e3cfe5). (CblasUppercxx . . $ CblasUpper., CblasNoTranscxx . . $ CblasNoTrans., CblasNonUnitcxx . . $ CblasNonUnit., &(R.matrixcxx . . $ $anonymous_type_2f547ace9ee68af9_2#matrix.), x); |
| 217 | |
| 218 | /* Compute residual = b - A x = Q (Q^T b - R x) */ |
| 219 | |
| 220 | gsl_vector_set_zerocxx . . $ gsl_vector_set_zero(5d65852ca0326259).(&(c.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_1#vector.)); |
| 221 | |
| 222 | gsl_linalg_QR_Qveccxx . . $ gsl_linalg_QR_Qvec(c419fed7545aef1d).(QR, tau, residual); |
| 223 | |
| 224 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
| 229 | int |
| 230 | gsl_linalg_QR_Rsolvecxx . . $ gsl_linalg_QR_Rsolve(c419fed7545aef1d). (const gsl_matrixcxx . . $ gsl_matrix# * QR, const gsl_vectorcxx . . $ gsl_vector# * b, gsl_vectorcxx . . $ gsl_vector# * x) |
| 231 | { |
| 232 | if (QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != QR->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.) |
| 233 | { |
| 234 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("QR matrix must be square", GSL_ENOTSQR); |
| 235 | } |
| 236 | else if (QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != b->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 237 | { |
| 238 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match b size", GSL_EBADLEN); |
| 239 | } |
| 240 | else if (QR->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2. != x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 241 | { |
| 242 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match x size", GSL_EBADLEN); |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | /* Copy x <- b */ |
| 247 | |
| 248 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (x, b); |
| 249 | |
| 250 | /* Solve R x = b, storing x in-place */ |
| 251 | |
| 252 | gsl_blas_dtrsvcxx . . $ gsl_blas_dtrsv(e00bac16b1e3cfe5). (CblasUppercxx . . $ CblasUpper., CblasNoTranscxx . . $ CblasNoTrans., CblasNonUnitcxx . . $ CblasNonUnit., QR, x); |
| 253 | |
| 254 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | int |
| 260 | gsl_linalg_QR_Rsvxcxx . . $ gsl_linalg_QR_Rsvx(8fc934df1d6f8665). (const gsl_matrixcxx . . $ gsl_matrix# * QR, gsl_vectorcxx . . $ gsl_vector# * x) |
| 261 | { |
| 262 | if (QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != QR->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.) |
| 263 | { |
| 264 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("QR matrix must be square", GSL_ENOTSQR); |
| 265 | } |
| 266 | else if (QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 267 | { |
| 268 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match rhs size", GSL_EBADLEN); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | /* Solve R x = b, storing x in-place */ |
| 273 | |
| 274 | gsl_blas_dtrsvcxx . . $ gsl_blas_dtrsv(e00bac16b1e3cfe5). (CblasUppercxx . . $ CblasUpper., CblasNoTranscxx . . $ CblasNoTrans., CblasNonUnitcxx . . $ CblasNonUnit., QR, x); |
| 275 | |
| 276 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | int |
| 281 | gsl_linalg_R_solvecxx . . $ gsl_linalg_R_solve(c419fed7545aef1d). (const gsl_matrixcxx . . $ gsl_matrix# * R, const gsl_vectorcxx . . $ gsl_vector# * b, gsl_vectorcxx . . $ gsl_vector# * x) |
| 282 | { |
| 283 | if (R->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != R->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.) |
| 284 | { |
| 285 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("R matrix must be square", GSL_ENOTSQR); |
| 286 | } |
| 287 | else if (R->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != b->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 288 | { |
| 289 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match b size", GSL_EBADLEN); |
| 290 | } |
| 291 | else if (R->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2. != x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 292 | { |
| 293 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match solution size", GSL_EBADLEN); |
| 294 | } |
| 295 | else |
| 296 | { |
| 297 | /* Copy x <- b */ |
| 298 | |
| 299 | gsl_vector_memcpycxx . . $ gsl_vector_memcpy(950c164c4e50c555). (x, b); |
| 300 | |
| 301 | /* Solve R x = b, storing x inplace in b */ |
| 302 | |
| 303 | gsl_blas_dtrsvcxx . . $ gsl_blas_dtrsv(e00bac16b1e3cfe5). (CblasUppercxx . . $ CblasUpper., CblasNoTranscxx . . $ CblasNoTrans., CblasNonUnitcxx . . $ CblasNonUnit., R, x); |
| 304 | |
| 305 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | int |
| 310 | gsl_linalg_R_svxcxx . . $ gsl_linalg_R_svx(8fc934df1d6f8665). (const gsl_matrixcxx . . $ gsl_matrix# * R, gsl_vectorcxx . . $ gsl_vector# * x) |
| 311 | { |
| 312 | if (R->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != R->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.) |
| 313 | { |
| 314 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("R matrix must be square", GSL_ENOTSQR); |
| 315 | } |
| 316 | else if (R->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2. != x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size.) |
| 317 | { |
| 318 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix size must match solution size", GSL_EBADLEN); |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | /* Solve R x = b, storing x inplace in b */ |
| 323 | |
| 324 | gsl_blas_dtrsvcxx . . $ gsl_blas_dtrsv(e00bac16b1e3cfe5). (CblasUppercxx . . $ CblasUpper., CblasNoTranscxx . . $ CblasNoTrans., CblasNonUnitcxx . . $ CblasNonUnit., R, x); |
| 325 | |
| 326 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | |
| 331 | |
| 332 | /* Form the product Q^T v from a QR factorized matrix |
| 333 | */ |
| 334 | |
| 335 | int |
| 336 | gsl_linalg_QR_QTveccxx . . $ gsl_linalg_QR_QTvec(c419fed7545aef1d). (const gsl_matrixcxx . . $ gsl_matrix# * QR, const gsl_vectorcxx . . $ gsl_vector# * tau, gsl_vectorcxx . . $ gsl_vector# * v) |
| 337 | { |
| 338 | const size_t M = QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; |
| 339 | const size_t N = QR->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.; |
| 340 | |
| 341 | if (tau->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != GSL_MINcxx . . $ `external/gsl/gsl_math.h:123:9`! (M, N)) |
| 342 | { |
| 343 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("size of tau must be MIN(M,N)", GSL_EBADLEN); |
| 344 | } |
| 345 | else if (v->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != M) |
| 346 | { |
| 347 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("vector size must be N", GSL_EBADLEN); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | size_t i; |
| 352 | |
| 353 | /* compute Q^T v */ |
| 354 | |
| 355 | for (i = 0; i < GSL_MINcxx . . $ `external/gsl/gsl_math.h:123:9`! (M, N); i++) |
| 356 | { |
| 357 | gsl_vector_const_viewcxx . . $ gsl_vector_const_view# c = gsl_matrix_const_columncxx . . $ gsl_matrix_const_column(ddcc4d81452c888e). (QR, i); |
| 358 | gsl_vector_const_viewcxx . . $ gsl_vector_const_view# h = gsl_vector_const_subvectorcxx . . $ gsl_vector_const_subvector(ef2cbc2337690d31). (&(c.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_2#vector.), i, M - i); |
| 359 | gsl_vector_viewcxx . . $ gsl_vector_view# w = gsl_vector_subvectorcxx . . $ gsl_vector_subvector(d6b353ad5d4a1905). (v, i, M - i); |
| 360 | double ti = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (tau, i); |
| 361 | gsl_linalg_householder_hvcxx . . $ gsl_linalg_householder_hv(b9b02baa1ffb1e94). (ti, &(h.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_2#vector.), &(w.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_1#vector.)); |
| 362 | } |
| 363 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | |
| 368 | int |
| 369 | gsl_linalg_QR_Qveccxx . . $ gsl_linalg_QR_Qvec(c419fed7545aef1d). (const gsl_matrixcxx . . $ gsl_matrix# * QR, const gsl_vectorcxx . . $ gsl_vector# * tau, gsl_vectorcxx . . $ gsl_vector# * v) |
| 370 | { |
| 371 | const size_t M = QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; |
| 372 | const size_t N = QR->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.; |
| 373 | |
| 374 | if (tau->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != GSL_MINcxx . . $ `external/gsl/gsl_math.h:123:9`! (M, N)) |
| 375 | { |
| 376 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("size of tau must be MIN(M,N)", GSL_EBADLEN); |
| 377 | } |
| 378 | else if (v->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != M) |
| 379 | { |
| 380 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("vector size must be N", GSL_EBADLEN); |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | size_t i; |
| 385 | |
| 386 | /* compute Q^T v */ |
| 387 | |
| 388 | for (i = GSL_MINcxx . . $ `external/gsl/gsl_math.h:123:9`! (M, N); i > 0 && i--;) |
| 389 | { |
| 390 | gsl_vector_const_viewcxx . . $ gsl_vector_const_view# c = gsl_matrix_const_columncxx . . $ gsl_matrix_const_column(ddcc4d81452c888e). (QR, i); |
| 391 | gsl_vector_const_viewcxx . . $ gsl_vector_const_view# h = gsl_vector_const_subvectorcxx . . $ gsl_vector_const_subvector(ef2cbc2337690d31). (&(c.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_2#vector.), |
| 392 | i, M - i); |
| 393 | gsl_vector_viewcxx . . $ gsl_vector_view# w = gsl_vector_subvectorcxx . . $ gsl_vector_subvector(d6b353ad5d4a1905). (v, i, M - i); |
| 394 | double ti = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (tau, i); |
| 395 | gsl_linalg_householder_hvcxx . . $ gsl_linalg_householder_hv(b9b02baa1ffb1e94). (ti, &h.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_2#vector., &w.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_1#vector.); |
| 396 | } |
| 397 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /* Form the product Q^T A from a QR factorized matrix */ |
| 402 | |
| 403 | int |
| 404 | gsl_linalg_QR_QTmatcxx . . $ gsl_linalg_QR_QTmat(defdb6603595db34). (const gsl_matrixcxx . . $ gsl_matrix# * QR, const gsl_vectorcxx . . $ gsl_vector# * tau, gsl_matrixcxx . . $ gsl_matrix# * A) |
| 405 | { |
| 406 | const size_t M = QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; |
| 407 | const size_t N = QR->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.; |
| 408 | |
| 409 | if (tau->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != GSL_MINcxx . . $ `external/gsl/gsl_math.h:123:9`! (M, N)) |
| 410 | { |
| 411 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("size of tau must be MIN(M,N)", GSL_EBADLEN); |
| 412 | } |
| 413 | else if (A->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != M) |
| 414 | { |
| 415 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("matrix must have M rows", GSL_EBADLEN); |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | size_t i; |
| 420 | |
| 421 | /* compute Q^T A */ |
| 422 | |
| 423 | for (i = 0; i < GSL_MINcxx . . $ `external/gsl/gsl_math.h:123:9`! (M, N); i++) |
| 424 | { |
| 425 | gsl_vector_const_viewcxx . . $ gsl_vector_const_view# c = gsl_matrix_const_columncxx . . $ gsl_matrix_const_column(ddcc4d81452c888e). (QR, i); |
| 426 | gsl_vector_const_viewcxx . . $ gsl_vector_const_view# h = gsl_vector_const_subvectorcxx . . $ gsl_vector_const_subvector(ef2cbc2337690d31). (&(c.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_2#vector.), i, M - i); |
| 427 | gsl_matrix_viewcxx . . $ gsl_matrix_view# m = gsl_matrix_submatrixcxx . . $ gsl_matrix_submatrix(c7aeed2a734480d1).(A, i, 0, M - i, A->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.); |
| 428 | double ti = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (tau, i); |
| 429 | gsl_linalg_householder_hmcxx . . $ gsl_linalg_householder_hm(69ab0d44557b6447). (ti, &(h.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_2#vector.), &(m.matrixcxx . . $ $anonymous_type_2f547ace9ee68af9_1#matrix.)); |
| 430 | } |
| 431 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | |
| 436 | /* Form the orthogonal matrix Q from the packed QR matrix */ |
| 437 | |
| 438 | int |
| 439 | gsl_linalg_QR_unpackcxx . . $ gsl_linalg_QR_unpack(b59274c26d5d73d6). (const gsl_matrixcxx . . $ gsl_matrix# * QR, const gsl_vectorcxx . . $ gsl_vector# * tau, gsl_matrixcxx . . $ gsl_matrix# * Q, gsl_matrixcxx . . $ gsl_matrix# * R) |
| 440 | { |
| 441 | const size_t M = QR->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; |
| 442 | const size_t N = QR->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.; |
| 443 | |
| 444 | if (Q->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != M || Q->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2. != M) |
| 445 | { |
| 446 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("Q matrix must be M x M", GSL_ENOTSQR); |
| 447 | } |
| 448 | else if (R->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != M || R->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2. != N) |
| 449 | { |
| 450 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("R matrix must be M x N", GSL_ENOTSQR); |
| 451 | } |
| 452 | else if (tau->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != GSL_MINcxx . . $ `external/gsl/gsl_math.h:123:9`! (M, N)) |
| 453 | { |
| 454 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("size of tau must be MIN(M,N)", GSL_EBADLEN); |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | size_t i, j; |
| 459 | |
| 460 | /* Initialize Q to the identity */ |
| 461 | |
| 462 | gsl_matrix_set_identitycxx . . $ gsl_matrix_set_identity(ab29a617cccd3636). (Q); |
| 463 | |
| 464 | for (i = GSL_MINcxx . . $ `external/gsl/gsl_math.h:123:9`! (M, N); i > 0 && i--;) |
| 465 | { |
| 466 | gsl_vector_const_viewcxx . . $ gsl_vector_const_view# c = gsl_matrix_const_columncxx . . $ gsl_matrix_const_column(ddcc4d81452c888e). (QR, i); |
| 467 | gsl_vector_const_viewcxx . . $ gsl_vector_const_view# h = gsl_vector_const_subvectorcxx . . $ gsl_vector_const_subvector(ef2cbc2337690d31). (&c.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_2#vector., |
| 468 | i, M - i); |
| 469 | gsl_matrix_viewcxx . . $ gsl_matrix_view# m = gsl_matrix_submatrixcxx . . $ gsl_matrix_submatrix(c7aeed2a734480d1). (Q, i, i, M - i, M - i); |
| 470 | double ti = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (tau, i); |
| 471 | gsl_linalg_householder_hmcxx . . $ gsl_linalg_householder_hm(69ab0d44557b6447). (ti, &h.vectorcxx . . $ $anonymous_type_a8c2e3e00d6867c2_2#vector., &m.matrixcxx . . $ $anonymous_type_2f547ace9ee68af9_1#matrix.); |
| 472 | } |
| 473 | |
| 474 | /* Form the right triangular matrix R from a packed QR matrix */ |
| 475 | |
| 476 | for (i = 0; i < M; i++) |
| 477 | { |
| 478 | for (j = 0; j < i && j < N; j++) |
| 479 | gsl_matrix_setcxx . . $ gsl_matrix_set(b56d954874bd7b13). (R, i, j, 0.0); |
| 480 | |
| 481 | for (j = i; j < N; j++) |
| 482 | gsl_matrix_setcxx . . $ gsl_matrix_set(b56d954874bd7b13). (R, i, j, gsl_matrix_getcxx . . $ gsl_matrix_get(dfa4c0641bd665a5). (QR, i, j)); |
| 483 | } |
| 484 | |
| 485 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | |
| 490 | /* Update a QR factorisation for A= Q R , A' = A + u v^T, |
| 491 | |
| 492 | * Q' R' = QR + u v^T |
| 493 | * = Q (R + Q^T u v^T) |
| 494 | * = Q (R + w v^T) |
| 495 | * |
| 496 | * where w = Q^T u. |
| 497 | * |
| 498 | * Algorithm from Golub and Van Loan, "Matrix Computations", Section |
| 499 | * 12.5 (Updating Matrix Factorizations, Rank-One Changes) |
| 500 | */ |
| 501 | |
| 502 | int |
| 503 | gsl_linalg_QR_updatecxx . . $ gsl_linalg_QR_update(9319f07bb2feed5b). (gsl_matrixcxx . . $ gsl_matrix# * Q, gsl_matrixcxx . . $ gsl_matrix# * R, |
| 504 | gsl_vectorcxx . . $ gsl_vector# * w, const gsl_vectorcxx . . $ gsl_vector# * v) |
| 505 | { |
| 506 | const size_t M = R->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; |
| 507 | const size_t N = R->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.; |
| 508 | |
| 509 | if (Q->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != M || Q->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2. != M) |
| 510 | { |
| 511 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("Q matrix must be M x M if R is M x N", GSL_ENOTSQR); |
| 512 | } |
| 513 | else if (w->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != M) |
| 514 | { |
| 515 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("w must be length M if R is M x N", GSL_EBADLEN); |
| 516 | } |
| 517 | else if (v->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != N) |
| 518 | { |
| 519 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("v must be length N if R is M x N", GSL_EBADLEN); |
| 520 | } |
| 521 | else |
| 522 | { |
| 523 | size_t j, k; |
| 524 | double w0; |
| 525 | |
| 526 | /* Apply Given's rotations to reduce w to (|w|, 0, 0, ... , 0) |
| 527 | |
| 528 | J_1^T .... J_(n-1)^T w = +/- |w| e_1 |
| 529 | |
| 530 | simultaneously applied to R, H = J_1^T ... J^T_(n-1) R |
| 531 | so that H is upper Hessenberg. (12.5.2) */ |
| 532 | |
| 533 | for (k = M - 1; k > 0; k--) |
| 534 | { |
| 535 | double c, s; |
| 536 | double wk = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (w, k); |
| 537 | double wkm1 = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (w, k - 1); |
| 538 | |
| 539 | create_givenscxx . . $ create_givens(1c2b5cb2f7ad0402). (wkm1, wk, &c, &s); |
| 540 | apply_givens_veccxx . . $ apply_givens_vec(6b773c7e67d76c66). (w, k - 1, k, c, s); |
| 541 | apply_givens_qrcxx . . $ apply_givens_qr(6e7c64dd992df8ea). (M, N, Q, R, k - 1, k, c, s); |
| 542 | } |
| 543 | |
| 544 | w0 = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (w, 0); |
| 545 | |
| 546 | /* Add in w v^T (Equation 12.5.3) */ |
| 547 | |
| 548 | for (j = 0; j < N; j++) |
| 549 | { |
| 550 | double r0j = gsl_matrix_getcxx . . $ gsl_matrix_get(dfa4c0641bd665a5). (R, 0, j); |
| 551 | double vj = gsl_vector_getcxx . . $ gsl_vector_get(bd1f20df78adc7a). (v, j); |
| 552 | gsl_matrix_setcxx . . $ gsl_matrix_set(b56d954874bd7b13). (R, 0, j, r0j + w0 * vj); |
| 553 | } |
| 554 | |
| 555 | /* Apply Givens transformations R' = G_(n-1)^T ... G_1^T H |
| 556 | Equation 12.5.4 */ |
| 557 | |
| 558 | for (k = 1; k < GSL_MINcxx . . $ `external/gsl/gsl_math.h:123:9`!(M,N+1); k++) |
| 559 | { |
| 560 | double c, s; |
| 561 | double diag = gsl_matrix_getcxx . . $ gsl_matrix_get(dfa4c0641bd665a5). (R, k - 1, k - 1); |
| 562 | double offdiag = gsl_matrix_getcxx . . $ gsl_matrix_get(dfa4c0641bd665a5). (R, k, k - 1); |
| 563 | |
| 564 | create_givenscxx . . $ create_givens(1c2b5cb2f7ad0402). (diag, offdiag, &c, &s); |
| 565 | apply_givens_qrcxx . . $ apply_givens_qr(6e7c64dd992df8ea). (M, N, Q, R, k - 1, k, c, s); |
| 566 | |
| 567 | gsl_matrix_setcxx . . $ gsl_matrix_set(b56d954874bd7b13). (R, k, k - 1, 0.0); /* exact zero of G^T */ |
| 568 | } |
| 569 | |
| 570 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | int |
| 575 | gsl_linalg_QR_QRsolvecxx . . $ gsl_linalg_QR_QRsolve(843c84aa949ea745). (gsl_matrixcxx . . $ gsl_matrix# * Q, gsl_matrixcxx . . $ gsl_matrix# * R, const gsl_vectorcxx . . $ gsl_vector# * b, gsl_vectorcxx . . $ gsl_vector# * x) |
| 576 | { |
| 577 | const size_t M = R->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1.; |
| 578 | const size_t N = R->size2cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size2.; |
| 579 | |
| 580 | if (M != N) |
| 581 | { |
| 582 | return GSL_ENOTSQRcxx . . $ GSL_ENOTSQR.; |
| 583 | } |
| 584 | else if (Q->size1cxx . . $ $anonymous_type_2f547ace9ee68af9_0#size1. != M || b->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != M || x->sizecxx . . $ $anonymous_type_a8c2e3e00d6867c2_0#size. != M) |
| 585 | { |
| 586 | return GSL_EBADLENcxx . . $ GSL_EBADLEN.; |
| 587 | } |
| 588 | else |
| 589 | { |
| 590 | /* compute sol = Q^T b */ |
| 591 | |
| 592 | gsl_blas_dgemvcxx . . $ gsl_blas_dgemv(ae04c3c4cab3cc37). (CblasTranscxx . . $ CblasTrans., 1.0, Q, b, 0.0, x); |
| 593 | |
| 594 | /* Solve R x = sol, storing x in-place */ |
| 595 | |
| 596 | gsl_blas_dtrsvcxx . . $ gsl_blas_dtrsv(e00bac16b1e3cfe5). (CblasUppercxx . . $ CblasUpper., CblasNoTranscxx . . $ CblasNoTrans., CblasNonUnitcxx . . $ CblasNonUnit., R, x); |
| 597 | |
| 598 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 599 | } |
| 600 | } |