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 |
|
---|
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.
|
---|
15 |
|
---|
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.
|
---|
20 |
|
---|
21 | A copy of the GNU General Public License is available at
|
---|
22 | http://www.fsf.org/licensing/licenses
|
---|
23 |
|
---|
24 | >>> END OF LICENSE >>>
|
---|
25 | *************************************************************************/
|
---|
26 |
|
---|
27 |
|
---|
28 | namespace alglib {
|
---|
29 | public class schur {
|
---|
30 | /*************************************************************************
|
---|
31 | Subroutine performing the Schur decomposition of a general matrix by using
|
---|
32 | the QR algorithm with multiple shifts.
|
---|
33 |
|
---|
34 | The source matrix A is represented as S'*A*S = T, where S is an orthogonal
|
---|
35 | matrix (Schur vectors), T - upper quasi-triangular matrix (with blocks of
|
---|
36 | sizes 1x1 and 2x2 on the main diagonal).
|
---|
37 |
|
---|
38 | Input parameters:
|
---|
39 | A - matrix to be decomposed.
|
---|
40 | Array whose indexes range within [0..N-1, 0..N-1].
|
---|
41 | N - size of A, N>=0.
|
---|
42 |
|
---|
43 |
|
---|
44 | Output parameters:
|
---|
45 | A - contains matrix T.
|
---|
46 | Array whose indexes range within [0..N-1, 0..N-1].
|
---|
47 | S - contains Schur vectors.
|
---|
48 | Array whose indexes range within [0..N-1, 0..N-1].
|
---|
49 |
|
---|
50 | Note 1:
|
---|
51 | The block structure of matrix T can be easily recognized: since all
|
---|
52 | the elements below the blocks are zeros, the elements a[i+1,i] which
|
---|
53 | are equal to 0 show the block border.
|
---|
54 |
|
---|
55 | Note 2:
|
---|
56 | The algorithm performance depends on the value of the internal parameter
|
---|
57 | NS of the InternalSchurDecomposition subroutine which defines the number
|
---|
58 | of shifts in the QR algorithm (similarly to the block width in block-matrix
|
---|
59 | algorithms in linear algebra). If you require maximum performance on
|
---|
60 | your machine, it is recommended to adjust this parameter manually.
|
---|
61 |
|
---|
62 | Result:
|
---|
63 | True,
|
---|
64 | if the algorithm has converged and parameters A and S contain the result.
|
---|
65 | False,
|
---|
66 | if the algorithm has not converged.
|
---|
67 |
|
---|
68 | Algorithm implemented on the basis of the DHSEQR subroutine (LAPACK 3.0 library).
|
---|
69 | *************************************************************************/
|
---|
70 | public static bool rmatrixschur(ref double[,] a,
|
---|
71 | int n,
|
---|
72 | ref double[,] s) {
|
---|
73 | bool result = new bool();
|
---|
74 | double[] tau = new double[0];
|
---|
75 | double[] wi = new double[0];
|
---|
76 | double[] wr = new double[0];
|
---|
77 | double[,] a1 = new double[0, 0];
|
---|
78 | double[,] s1 = new double[0, 0];
|
---|
79 | int info = 0;
|
---|
80 | int i = 0;
|
---|
81 | int j = 0;
|
---|
82 |
|
---|
83 |
|
---|
84 | //
|
---|
85 | // Upper Hessenberg form of the 0-based matrix
|
---|
86 | //
|
---|
87 | ortfac.rmatrixhessenberg(ref a, n, ref tau);
|
---|
88 | ortfac.rmatrixhessenbergunpackq(ref a, n, ref tau, ref s);
|
---|
89 |
|
---|
90 | //
|
---|
91 | // Convert from 0-based arrays to 1-based,
|
---|
92 | // then call InternalSchurDecomposition
|
---|
93 | // Awkward, of course, but Schur decompisiton subroutine
|
---|
94 | // is too complex to fix it.
|
---|
95 | //
|
---|
96 | //
|
---|
97 | a1 = new double[n + 1, n + 1];
|
---|
98 | s1 = new double[n + 1, n + 1];
|
---|
99 | for (i = 1; i <= n; i++) {
|
---|
100 | for (j = 1; j <= n; j++) {
|
---|
101 | a1[i, j] = a[i - 1, j - 1];
|
---|
102 | s1[i, j] = s[i - 1, j - 1];
|
---|
103 | }
|
---|
104 | }
|
---|
105 | hsschur.internalschurdecomposition(ref a1, n, 1, 1, ref wr, ref wi, ref s1, ref info);
|
---|
106 | result = info == 0;
|
---|
107 |
|
---|
108 | //
|
---|
109 | // convert from 1-based arrays to -based
|
---|
110 | //
|
---|
111 | for (i = 1; i <= n; i++) {
|
---|
112 | for (j = 1; j <= n; j++) {
|
---|
113 | a[i - 1, j - 1] = a1[i, j];
|
---|
114 | s[i - 1, j - 1] = s1[i, j];
|
---|
115 | }
|
---|
116 | }
|
---|
117 | return result;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|