]> Creatis software - CreaPhase.git/blob - octave_packages/m/linear-algebra/qzhess.m
update packages
[CreaPhase.git] / octave_packages / m / linear-algebra / qzhess.m
1 ## Copyright (C) 1993-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## 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 ## Octave 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 Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {[@var{aa}, @var{bb}, @var{q}, @var{z}] =} qzhess (@var{A}, @var{B})
21 ## Compute the Hessenberg-triangular decomposition of the matrix pencil
22 ## @code{(@var{A}, @var{B})}, returning
23 ## @code{@var{aa} = @var{q} * @var{A} * @var{z}},
24 ## @code{@var{bb} = @var{q} * @var{B} * @var{z}}, with @var{q} and @var{z}
25 ## orthogonal.  For example:
26 ##
27 ## @example
28 ## @group
29 ## [aa, bb, q, z] = qzhess ([1, 2; 3, 4], [5, 6; 7, 8])
30 ##      @result{} aa = [ -3.02244, -4.41741;  0.92998,  0.69749 ]
31 ##      @result{} bb = [ -8.60233, -9.99730;  0.00000, -0.23250 ]
32 ##      @result{}  q = [ -0.58124, -0.81373; -0.81373,  0.58124 ]
33 ##      @result{}  z = [ 1, 0; 0, 1 ]
34 ## @end group
35 ## @end example
36 ##
37 ## The Hessenberg-triangular decomposition is the first step in
38 ## Moler and Stewart's QZ@tie{}decomposition algorithm.
39 ##
40 ## Algorithm taken from Golub and Van Loan,
41 ## @cite{Matrix Computations, 2nd edition}.
42 ## @end deftypefn
43
44 ## Author: A. S. Hodel <scotte@eng.auburn.edu>
45 ## Created: August 1993
46 ## Adapted-By: jwe
47
48 function [aa, bb, q, z] = qzhess (A, B)
49
50   if (nargin != 2)
51     print_usage ();
52   endif
53
54   [na, ma] = size (A);
55   [nb, mb] = size (B);
56   if (na != ma || na != nb || nb != mb)
57     error ("qzhess: incompatible dimensions");
58   endif
59
60   ## Reduce to hessenberg-triangular form.
61
62   [q, bb] = qr (B);
63   aa = q' * A;
64   q = q';
65   z = eye (na);
66   for j = 1:(na-2)
67     for i = na:-1:(j+2)
68
69       ## disp (["zero out aa(", num2str(i), ",", num2str(j), ")"])
70
71       rot = givens (aa (i-1, j), aa (i, j));
72       aa ((i-1):i, :) = rot *aa ((i-1):i, :);
73       bb ((i-1):i, :) = rot *bb ((i-1):i, :);
74       q  ((i-1):i, :) = rot *q  ((i-1):i, :);
75
76       ## disp (["now zero out bb(", num2str(i), ",", num2str(i-1), ")"])
77
78       rot = givens (bb (i, i), bb (i, i-1))';
79       bb (:, (i-1):i) = bb (:, (i-1):i) * rot';
80       aa (:, (i-1):i) = aa (:, (i-1):i) * rot';
81       z  (:, (i-1):i) = z  (:, (i-1):i) * rot';
82
83     endfor
84   endfor
85
86   bb (2, 1) = 0.0;
87   for i = 3:na
88     bb (i, 1:(i-1)) = zeros (1, i-1);
89     aa (i, 1:(i-2)) = zeros (1, i-2);
90   endfor
91
92 endfunction
93
94 %!test
95 %! a = [1 2 1 3;
96 %!      2 5 3 2;
97 %!      5 5 1 0;
98 %!      4 0 3 2];
99 %! b = [0 4 2 1;
100 %!      2 3 1 1;
101 %!      1 0 2 1;
102 %!      2 5 3 2];
103 %! mask = [0 0 0 0;
104 %!         0 0 0 0;
105 %!         1 0 0 0;
106 %!         1 1 0 0];
107 %! [aa, bb, q, z] = qzhess(a, b);
108 %! assert(inv(q) - q', zeros(4), 2e-8);
109 %! assert(inv(z) - z', zeros(4), 2e-8);
110 %! assert(q * a * z, aa, 2e-8);
111 %! assert(aa .* mask, zeros(4), 2e-8);
112 %! assert(q * b * z, bb, 2e-8);
113 %! assert(bb .* mask, zeros(4), 2e-8);
114
115 %!test
116 %! a = [1 2 3 4 5;
117 %!      3 2 3 1 0;
118 %!      4 3 2 1 1;
119 %!      0 1 0 1 0;
120 %!      3 2 1 0 5];
121 %! b = [5 0 4 0 1;
122 %!      1 1 1 2 5;
123 %!      0 3 2 1 0;
124 %!      4 3 0 3 5;
125 %!      2 1 2 1 3];
126 %! mask = [0 0 0 0 0;
127 %!         0 0 0 0 0;
128 %!         1 0 0 0 0;
129 %!         1 1 0 0 0;
130 %!         1 1 1 0 0];
131 %! [aa, bb, q, z] = qzhess(a, b);
132 %! assert(inv(q) - q', zeros(5), 2e-8);
133 %! assert(inv(z) - z', zeros(5), 2e-8);
134 %! assert(q * a * z, aa, 2e-8);
135 %! assert(aa .* mask, zeros(5), 2e-8);
136 %! assert(q * b * z, bb, 2e-8);
137 %! assert(bb .* mask, zeros(5), 2e-8);
138
139 %!error qzhess([0]);
140 %!error qzhess();
141