[2154] | 1 | /*************************************************************************
|
---|
| 2 | Copyright (c) 1992-2007 The University of Tennessee. All rights reserved.
|
---|
| 3 |
|
---|
| 4 | Contributors:
|
---|
| 5 | * Sergey Bochkanov (ALGLIB project). Translation from FORTRAN to
|
---|
| 6 | pseudocode.
|
---|
| 7 |
|
---|
| 8 | See subroutines comments for additional copyrights.
|
---|
| 9 |
|
---|
[2430] | 10 | >>> SOURCE LICENSE >>>
|
---|
| 11 | This program is free software; you can redistribute it and/or modify
|
---|
| 12 | it under the terms of the GNU General Public License as published by
|
---|
| 13 | the Free Software Foundation (www.fsf.org); either version 2 of the
|
---|
| 14 | License, or (at your option) any later version.
|
---|
[2154] | 15 |
|
---|
[2430] | 16 | This program is distributed in the hope that it will be useful,
|
---|
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | GNU General Public License for more details.
|
---|
[2154] | 20 |
|
---|
[2430] | 21 | A copy of the GNU General Public License is available at
|
---|
| 22 | http://www.fsf.org/licensing/licenses
|
---|
[2154] | 23 |
|
---|
[2430] | 24 | >>> END OF LICENSE >>>
|
---|
[2154] | 25 | *************************************************************************/
|
---|
| 26 |
|
---|
| 27 | using System;
|
---|
| 28 |
|
---|
[2430] | 29 | namespace alglib
|
---|
[2154] | 30 | {
|
---|
[2430] | 31 | public class qr
|
---|
| 32 | {
|
---|
| 33 | /*************************************************************************
|
---|
| 34 | QR decomposition of a rectangular matrix of size MxN
|
---|
[2154] | 35 |
|
---|
[2430] | 36 | Input parameters:
|
---|
| 37 | A - matrix A whose indexes range within [0..M-1, 0..N-1].
|
---|
| 38 | M - number of rows in matrix A.
|
---|
| 39 | N - number of columns in matrix A.
|
---|
[2154] | 40 |
|
---|
[2430] | 41 | Output parameters:
|
---|
| 42 | A - matrices Q and R in compact form (see below).
|
---|
| 43 | Tau - array of scalar factors which are used to form
|
---|
| 44 | matrix Q. Array whose index ranges within [0.. Min(M-1,N-1)].
|
---|
[2154] | 45 |
|
---|
[2430] | 46 | Matrix A is represented as A = QR, where Q is an orthogonal matrix of size
|
---|
| 47 | MxM, R - upper triangular (or upper trapezoid) matrix of size M x N.
|
---|
[2154] | 48 |
|
---|
[2430] | 49 | The elements of matrix R are located on and above the main diagonal of
|
---|
| 50 | matrix A. The elements which are located in Tau array and below the main
|
---|
| 51 | diagonal of matrix A are used to form matrix Q as follows:
|
---|
[2154] | 52 |
|
---|
[2430] | 53 | Matrix Q is represented as a product of elementary reflections
|
---|
[2154] | 54 |
|
---|
[2430] | 55 | Q = H(0)*H(2)*...*H(k-1),
|
---|
[2154] | 56 |
|
---|
[2430] | 57 | where k = min(m,n), and each H(i) is in the form
|
---|
[2154] | 58 |
|
---|
[2430] | 59 | H(i) = 1 - tau * v * (v^T)
|
---|
[2154] | 60 |
|
---|
[2430] | 61 | where tau is a scalar stored in Tau[I]; v - real vector,
|
---|
| 62 | so that v(0:i-1) = 0, v(i) = 1, v(i+1:m-1) stored in A(i+1:m-1,i).
|
---|
[2154] | 63 |
|
---|
[2430] | 64 | -- LAPACK routine (version 3.0) --
|
---|
| 65 | Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
|
---|
| 66 | Courant Institute, Argonne National Lab, and Rice University
|
---|
| 67 | February 29, 1992.
|
---|
| 68 | Translation from FORTRAN to pseudocode (AlgoPascal)
|
---|
| 69 | by Sergey Bochkanov, ALGLIB project, 2005-2007.
|
---|
| 70 | *************************************************************************/
|
---|
| 71 | public static void rmatrixqr(ref double[,] a,
|
---|
| 72 | int m,
|
---|
| 73 | int n,
|
---|
| 74 | ref double[] tau)
|
---|
| 75 | {
|
---|
| 76 | double[] work = new double[0];
|
---|
| 77 | double[] t = new double[0];
|
---|
| 78 | int i = 0;
|
---|
| 79 | int k = 0;
|
---|
| 80 | int minmn = 0;
|
---|
| 81 | double tmp = 0;
|
---|
| 82 | int i_ = 0;
|
---|
| 83 | int i1_ = 0;
|
---|
[2154] | 84 |
|
---|
[2430] | 85 | if( m<=0 | n<=0 )
|
---|
| 86 | {
|
---|
| 87 | return;
|
---|
| 88 | }
|
---|
| 89 | minmn = Math.Min(m, n);
|
---|
| 90 | work = new double[n-1+1];
|
---|
| 91 | t = new double[m+1];
|
---|
| 92 | tau = new double[minmn-1+1];
|
---|
[2154] | 93 |
|
---|
| 94 | //
|
---|
[2430] | 95 | // Test the input arguments
|
---|
[2154] | 96 | //
|
---|
[2430] | 97 | k = minmn;
|
---|
| 98 | for(i=0; i<=k-1; i++)
|
---|
[2154] | 99 | {
|
---|
| 100 |
|
---|
| 101 | //
|
---|
[2430] | 102 | // Generate elementary reflector H(i) to annihilate A(i+1:m,i)
|
---|
[2154] | 103 | //
|
---|
[2430] | 104 | i1_ = (i) - (1);
|
---|
| 105 | for(i_=1; i_<=m-i;i_++)
|
---|
| 106 | {
|
---|
| 107 | t[i_] = a[i_+i1_,i];
|
---|
| 108 | }
|
---|
| 109 | reflections.generatereflection(ref t, m-i, ref tmp);
|
---|
| 110 | tau[i] = tmp;
|
---|
| 111 | i1_ = (1) - (i);
|
---|
| 112 | for(i_=i; i_<=m-1;i_++)
|
---|
| 113 | {
|
---|
| 114 | a[i_,i] = t[i_+i1_];
|
---|
| 115 | }
|
---|
| 116 | t[1] = 1;
|
---|
| 117 | if( i<n )
|
---|
| 118 | {
|
---|
| 119 |
|
---|
| 120 | //
|
---|
| 121 | // Apply H(i) to A(i:m-1,i+1:n-1) from the left
|
---|
| 122 | //
|
---|
| 123 | reflections.applyreflectionfromtheleft(ref a, tau[i], ref t, i, m-1, i+1, n-1, ref work);
|
---|
| 124 | }
|
---|
[2154] | 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 |
|
---|
[2430] | 129 | /*************************************************************************
|
---|
| 130 | Partial unpacking of matrix Q from the QR decomposition of a matrix A
|
---|
[2154] | 131 |
|
---|
[2430] | 132 | Input parameters:
|
---|
| 133 | A - matrices Q and R in compact form.
|
---|
| 134 | Output of RMatrixQR subroutine.
|
---|
| 135 | M - number of rows in given matrix A. M>=0.
|
---|
| 136 | N - number of columns in given matrix A. N>=0.
|
---|
| 137 | Tau - scalar factors which are used to form Q.
|
---|
| 138 | Output of the RMatrixQR subroutine.
|
---|
| 139 | QColumns - required number of columns of matrix Q. M>=QColumns>=0.
|
---|
[2154] | 140 |
|
---|
[2430] | 141 | Output parameters:
|
---|
| 142 | Q - first QColumns columns of matrix Q.
|
---|
| 143 | Array whose indexes range within [0..M-1, 0..QColumns-1].
|
---|
| 144 | If QColumns=0, the array remains unchanged.
|
---|
[2154] | 145 |
|
---|
[2430] | 146 | -- ALGLIB --
|
---|
| 147 | Copyright 2005 by Bochkanov Sergey
|
---|
| 148 | *************************************************************************/
|
---|
| 149 | public static void rmatrixqrunpackq(ref double[,] a,
|
---|
| 150 | int m,
|
---|
| 151 | int n,
|
---|
| 152 | ref double[] tau,
|
---|
| 153 | int qcolumns,
|
---|
| 154 | ref double[,] q)
|
---|
| 155 | {
|
---|
| 156 | int i = 0;
|
---|
| 157 | int j = 0;
|
---|
| 158 | int k = 0;
|
---|
| 159 | int minmn = 0;
|
---|
| 160 | double[] v = new double[0];
|
---|
| 161 | double[] work = new double[0];
|
---|
| 162 | int i_ = 0;
|
---|
| 163 | int i1_ = 0;
|
---|
[2154] | 164 |
|
---|
[2430] | 165 | System.Diagnostics.Debug.Assert(qcolumns<=m, "UnpackQFromQR: QColumns>M!");
|
---|
| 166 | if( m<=0 | n<=0 | qcolumns<=0 )
|
---|
[2154] | 167 | {
|
---|
[2430] | 168 | return;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | //
|
---|
| 172 | // init
|
---|
| 173 | //
|
---|
| 174 | minmn = Math.Min(m, n);
|
---|
| 175 | k = Math.Min(minmn, qcolumns);
|
---|
| 176 | q = new double[m-1+1, qcolumns-1+1];
|
---|
| 177 | v = new double[m+1];
|
---|
| 178 | work = new double[qcolumns-1+1];
|
---|
| 179 | for(i=0; i<=m-1; i++)
|
---|
| 180 | {
|
---|
| 181 | for(j=0; j<=qcolumns-1; j++)
|
---|
[2154] | 182 | {
|
---|
[2430] | 183 | if( i==j )
|
---|
| 184 | {
|
---|
| 185 | q[i,j] = 1;
|
---|
| 186 | }
|
---|
| 187 | else
|
---|
| 188 | {
|
---|
| 189 | q[i,j] = 0;
|
---|
| 190 | }
|
---|
[2154] | 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | //
|
---|
[2430] | 195 | // unpack Q
|
---|
[2154] | 196 | //
|
---|
[2430] | 197 | for(i=k-1; i>=0; i--)
|
---|
[2154] | 198 | {
|
---|
[2430] | 199 |
|
---|
| 200 | //
|
---|
| 201 | // Apply H(i)
|
---|
| 202 | //
|
---|
| 203 | i1_ = (i) - (1);
|
---|
| 204 | for(i_=1; i_<=m-i;i_++)
|
---|
| 205 | {
|
---|
| 206 | v[i_] = a[i_+i1_,i];
|
---|
| 207 | }
|
---|
| 208 | v[1] = 1;
|
---|
| 209 | reflections.applyreflectionfromtheleft(ref q, tau[i], ref v, i, m-1, 0, qcolumns-1, ref work);
|
---|
[2154] | 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 |
|
---|
[2430] | 214 | /*************************************************************************
|
---|
| 215 | Unpacking of matrix R from the QR decomposition of a matrix A
|
---|
[2154] | 216 |
|
---|
[2430] | 217 | Input parameters:
|
---|
| 218 | A - matrices Q and R in compact form.
|
---|
| 219 | Output of RMatrixQR subroutine.
|
---|
| 220 | M - number of rows in given matrix A. M>=0.
|
---|
| 221 | N - number of columns in given matrix A. N>=0.
|
---|
[2154] | 222 |
|
---|
[2430] | 223 | Output parameters:
|
---|
| 224 | R - matrix R, array[0..M-1, 0..N-1].
|
---|
[2154] | 225 |
|
---|
[2430] | 226 | -- ALGLIB --
|
---|
| 227 | Copyright 2005 by Bochkanov Sergey
|
---|
| 228 | *************************************************************************/
|
---|
| 229 | public static void rmatrixqrunpackr(ref double[,] a,
|
---|
| 230 | int m,
|
---|
| 231 | int n,
|
---|
| 232 | ref double[,] r)
|
---|
| 233 | {
|
---|
| 234 | int i = 0;
|
---|
| 235 | int k = 0;
|
---|
| 236 | int i_ = 0;
|
---|
[2154] | 237 |
|
---|
[2430] | 238 | if( m<=0 | n<=0 )
|
---|
[2154] | 239 | {
|
---|
[2430] | 240 | return;
|
---|
[2154] | 241 | }
|
---|
[2430] | 242 | k = Math.Min(m, n);
|
---|
| 243 | r = new double[m-1+1, n-1+1];
|
---|
| 244 | for(i=0; i<=n-1; i++)
|
---|
[2154] | 245 | {
|
---|
[2430] | 246 | r[0,i] = 0;
|
---|
[2154] | 247 | }
|
---|
[2430] | 248 | for(i=1; i<=m-1; i++)
|
---|
| 249 | {
|
---|
| 250 | for(i_=0; i_<=n-1;i_++)
|
---|
| 251 | {
|
---|
| 252 | r[i,i_] = r[0,i_];
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | for(i=0; i<=k-1; i++)
|
---|
| 256 | {
|
---|
| 257 | for(i_=i; i_<=n-1;i_++)
|
---|
| 258 | {
|
---|
| 259 | r[i,i_] = a[i,i_];
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
[2154] | 262 | }
|
---|
| 263 |
|
---|
| 264 |
|
---|
[2430] | 265 | /*************************************************************************
|
---|
| 266 | Obsolete 1-based subroutine. See RMatrixQR for 0-based replacement.
|
---|
| 267 | *************************************************************************/
|
---|
| 268 | public static void qrdecomposition(ref double[,] a,
|
---|
| 269 | int m,
|
---|
| 270 | int n,
|
---|
| 271 | ref double[] tau)
|
---|
| 272 | {
|
---|
| 273 | double[] work = new double[0];
|
---|
| 274 | double[] t = new double[0];
|
---|
| 275 | int i = 0;
|
---|
| 276 | int k = 0;
|
---|
| 277 | int mmip1 = 0;
|
---|
| 278 | int minmn = 0;
|
---|
| 279 | double tmp = 0;
|
---|
| 280 | int i_ = 0;
|
---|
| 281 | int i1_ = 0;
|
---|
[2154] | 282 |
|
---|
[2430] | 283 | minmn = Math.Min(m, n);
|
---|
| 284 | work = new double[n+1];
|
---|
| 285 | t = new double[m+1];
|
---|
| 286 | tau = new double[minmn+1];
|
---|
[2154] | 287 |
|
---|
| 288 | //
|
---|
[2430] | 289 | // Test the input arguments
|
---|
[2154] | 290 | //
|
---|
[2430] | 291 | k = Math.Min(m, n);
|
---|
| 292 | for(i=1; i<=k; i++)
|
---|
[2154] | 293 | {
|
---|
| 294 |
|
---|
| 295 | //
|
---|
[2430] | 296 | // Generate elementary reflector H(i) to annihilate A(i+1:m,i)
|
---|
[2154] | 297 | //
|
---|
[2430] | 298 | mmip1 = m-i+1;
|
---|
| 299 | i1_ = (i) - (1);
|
---|
| 300 | for(i_=1; i_<=mmip1;i_++)
|
---|
| 301 | {
|
---|
| 302 | t[i_] = a[i_+i1_,i];
|
---|
| 303 | }
|
---|
| 304 | reflections.generatereflection(ref t, mmip1, ref tmp);
|
---|
| 305 | tau[i] = tmp;
|
---|
| 306 | i1_ = (1) - (i);
|
---|
| 307 | for(i_=i; i_<=m;i_++)
|
---|
| 308 | {
|
---|
| 309 | a[i_,i] = t[i_+i1_];
|
---|
| 310 | }
|
---|
| 311 | t[1] = 1;
|
---|
| 312 | if( i<n )
|
---|
| 313 | {
|
---|
| 314 |
|
---|
| 315 | //
|
---|
| 316 | // Apply H(i) to A(i:m,i+1:n) from the left
|
---|
| 317 | //
|
---|
| 318 | reflections.applyreflectionfromtheleft(ref a, tau[i], ref t, i, m, i+1, n, ref work);
|
---|
| 319 | }
|
---|
[2154] | 320 | }
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 |
|
---|
[2430] | 324 | /*************************************************************************
|
---|
| 325 | Obsolete 1-based subroutine. See RMatrixQRUnpackQ for 0-based replacement.
|
---|
| 326 | *************************************************************************/
|
---|
| 327 | public static void unpackqfromqr(ref double[,] a,
|
---|
| 328 | int m,
|
---|
| 329 | int n,
|
---|
| 330 | ref double[] tau,
|
---|
| 331 | int qcolumns,
|
---|
| 332 | ref double[,] q)
|
---|
| 333 | {
|
---|
| 334 | int i = 0;
|
---|
| 335 | int j = 0;
|
---|
| 336 | int k = 0;
|
---|
| 337 | int minmn = 0;
|
---|
| 338 | double[] v = new double[0];
|
---|
| 339 | double[] work = new double[0];
|
---|
| 340 | int vm = 0;
|
---|
| 341 | int i_ = 0;
|
---|
| 342 | int i1_ = 0;
|
---|
[2154] | 343 |
|
---|
[2430] | 344 | System.Diagnostics.Debug.Assert(qcolumns<=m, "UnpackQFromQR: QColumns>M!");
|
---|
| 345 | if( m==0 | n==0 | qcolumns==0 )
|
---|
[2154] | 346 | {
|
---|
[2430] | 347 | return;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | //
|
---|
| 351 | // init
|
---|
| 352 | //
|
---|
| 353 | minmn = Math.Min(m, n);
|
---|
| 354 | k = Math.Min(minmn, qcolumns);
|
---|
| 355 | q = new double[m+1, qcolumns+1];
|
---|
| 356 | v = new double[m+1];
|
---|
| 357 | work = new double[qcolumns+1];
|
---|
| 358 | for(i=1; i<=m; i++)
|
---|
| 359 | {
|
---|
| 360 | for(j=1; j<=qcolumns; j++)
|
---|
[2154] | 361 | {
|
---|
[2430] | 362 | if( i==j )
|
---|
| 363 | {
|
---|
| 364 | q[i,j] = 1;
|
---|
| 365 | }
|
---|
| 366 | else
|
---|
| 367 | {
|
---|
| 368 | q[i,j] = 0;
|
---|
| 369 | }
|
---|
[2154] | 370 | }
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | //
|
---|
[2430] | 374 | // unpack Q
|
---|
[2154] | 375 | //
|
---|
[2430] | 376 | for(i=k; i>=1; i--)
|
---|
[2154] | 377 | {
|
---|
[2430] | 378 |
|
---|
| 379 | //
|
---|
| 380 | // Apply H(i)
|
---|
| 381 | //
|
---|
| 382 | vm = m-i+1;
|
---|
| 383 | i1_ = (i) - (1);
|
---|
| 384 | for(i_=1; i_<=vm;i_++)
|
---|
| 385 | {
|
---|
| 386 | v[i_] = a[i_+i1_,i];
|
---|
| 387 | }
|
---|
| 388 | v[1] = 1;
|
---|
| 389 | reflections.applyreflectionfromtheleft(ref q, tau[i], ref v, i, m, 1, qcolumns, ref work);
|
---|
[2154] | 390 | }
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 |
|
---|
[2430] | 394 | /*************************************************************************
|
---|
| 395 | Obsolete 1-based subroutine. See RMatrixQR for 0-based replacement.
|
---|
| 396 | *************************************************************************/
|
---|
| 397 | public static void qrdecompositionunpacked(double[,] a,
|
---|
| 398 | int m,
|
---|
| 399 | int n,
|
---|
| 400 | ref double[,] q,
|
---|
| 401 | ref double[,] r)
|
---|
| 402 | {
|
---|
| 403 | int i = 0;
|
---|
| 404 | int k = 0;
|
---|
| 405 | double[] tau = new double[0];
|
---|
| 406 | double[] work = new double[0];
|
---|
| 407 | double[] v = new double[0];
|
---|
| 408 | int i_ = 0;
|
---|
[2154] | 409 |
|
---|
[2430] | 410 | a = (double[,])a.Clone();
|
---|
[2154] | 411 |
|
---|
[2430] | 412 | k = Math.Min(m, n);
|
---|
| 413 | if( n<=0 )
|
---|
[2154] | 414 | {
|
---|
[2430] | 415 | return;
|
---|
[2154] | 416 | }
|
---|
[2430] | 417 | work = new double[m+1];
|
---|
| 418 | v = new double[m+1];
|
---|
| 419 | q = new double[m+1, m+1];
|
---|
| 420 | r = new double[m+1, n+1];
|
---|
| 421 |
|
---|
| 422 | //
|
---|
| 423 | // QRDecomposition
|
---|
| 424 | //
|
---|
| 425 | qrdecomposition(ref a, m, n, ref tau);
|
---|
| 426 |
|
---|
| 427 | //
|
---|
| 428 | // R
|
---|
| 429 | //
|
---|
| 430 | for(i=1; i<=n; i++)
|
---|
[2154] | 431 | {
|
---|
[2430] | 432 | r[1,i] = 0;
|
---|
[2154] | 433 | }
|
---|
[2430] | 434 | for(i=2; i<=m; i++)
|
---|
| 435 | {
|
---|
| 436 | for(i_=1; i_<=n;i_++)
|
---|
| 437 | {
|
---|
| 438 | r[i,i_] = r[1,i_];
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
| 441 | for(i=1; i<=k; i++)
|
---|
| 442 | {
|
---|
| 443 | for(i_=i; i_<=n;i_++)
|
---|
| 444 | {
|
---|
| 445 | r[i,i_] = a[i,i_];
|
---|
| 446 | }
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | //
|
---|
| 450 | // Q
|
---|
| 451 | //
|
---|
| 452 | unpackqfromqr(ref a, m, n, ref tau, m, ref q);
|
---|
[2154] | 453 | }
|
---|
| 454 | }
|
---|
| 455 | }
|
---|