Mercurial > hg > octave-nkf
annotate scripts/polynomial/polyeig.m @ 20279:db30302bedc3
Added tag rc-4-0-0-3 for changeset 065f933ef083
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 10 Apr 2015 14:41:21 -0400 |
parents | 9fc020886ae9 |
children | f1d0f506ee78 |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19792
diff
changeset
|
1 ## Copyright (C) 2012-2015 Fotios Kasolis |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
2 ## |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
3 ## This file is part of Octave. |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
4 ## |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
5 ## Octave is free software; you can redistribute it and/or modify it |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
6 ## under the terms of the GNU General Public License as published by |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
8 ## your option) any later version. |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
9 ## |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
10 ## Octave is distributed in the hope that it will be useful, but |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
13 ## General Public License for more details. |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
14 ## |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
15 ## You should have received a copy of the GNU General Public License |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
16 ## along with Octave; see the file COPYING. If not, see |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
18 |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
19 ## -*- texinfo -*- |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
20 ## @deftypefn {Function File} {@var{z} =} polyeig (@var{C0}, @var{C1}, @dots{}, @var{Cl}) |
15222
e3dc9ff8e0f2
polyeig.m: Use @code, not @var, macro to silence warnings from texinfo.
Rik <rik@octave.org>
parents:
15197
diff
changeset
|
21 ## @deftypefnx {Function File} {[@var{v}, @var{z}] =} polyeig (@var{C0}, @var{C1}, @dots{}, @var{Cl}) |
15190
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
22 ## |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
23 ## Solve the polynomial eigenvalue problem of degree @var{l}. |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
24 ## |
15222
e3dc9ff8e0f2
polyeig.m: Use @code, not @var, macro to silence warnings from texinfo.
Rik <rik@octave.org>
parents:
15197
diff
changeset
|
25 ## Given an @var{n*n} matrix polynomial |
e3dc9ff8e0f2
polyeig.m: Use @code, not @var, macro to silence warnings from texinfo.
Rik <rik@octave.org>
parents:
15197
diff
changeset
|
26 ## @code{@var{C}(s) = @var{C0} + @var{C1} s + @dots{} + @var{Cl} s^l} |
e3dc9ff8e0f2
polyeig.m: Use @code, not @var, macro to silence warnings from texinfo.
Rik <rik@octave.org>
parents:
15197
diff
changeset
|
27 ## polyeig solves the eigenvalue problem |
e3dc9ff8e0f2
polyeig.m: Use @code, not @var, macro to silence warnings from texinfo.
Rik <rik@octave.org>
parents:
15197
diff
changeset
|
28 ## @code{(@var{C0} + @var{C1} + @dots{} + @var{Cl})v = 0}. |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
29 ## |
15222
e3dc9ff8e0f2
polyeig.m: Use @code, not @var, macro to silence warnings from texinfo.
Rik <rik@octave.org>
parents:
15197
diff
changeset
|
30 ## Note that the eigenvalues @var{z} are the zeros of the matrix polynomial. |
19792
be7ac98fab43
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
19791
diff
changeset
|
31 ## @var{z} is a row vector with @var{n*l} elements. @var{v} is a matrix |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
32 ## (@var{n} x @var{n}*@var{l}) with columns that correspond to the |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
33 ## eigenvectors. |
15190
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
34 ## |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
35 ## @seealso{eig, eigs, compan} |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
36 ## @end deftypefn |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
37 |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
38 ## Author: Fotios Kasolis |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
39 |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
40 function [z, v] = polyeig (varargin) |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
41 |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 if (nargin < 1 || nargout > 2) |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
43 print_usage (); |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
44 endif |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
45 |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
46 nin = numel (varargin); |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
47 n = rows (varargin{1}); |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
48 |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
49 for i = 1 : nin |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
50 if (! issquare (varargin{i})) |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
51 error ("polyeig: coefficients must be square matrices"); |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
52 endif |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
53 if (rows (varargin{i}) != n) |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
54 error ("polyeig: coefficients must have the same dimensions"); |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
55 endif |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
56 endfor |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
57 |
15190
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
58 ## matrix polynomial degree |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
59 l = nin - 1; |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
60 |
15190
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
61 ## form needed matrices |
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
62 C = [ zeros(n * (l - 1), n), eye(n * (l - 1)); |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
63 -cell2mat(varargin(1:end-1)) ]; |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
64 |
15190
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
65 D = [ eye(n * (l - 1)), zeros(n * (l - 1), n); |
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
66 zeros(n, n * (l - 1)), varargin{end} ]; |
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
67 |
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
68 ## solve generalized eigenvalue problem |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
69 if (nargout == 2) |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
70 [z, v] = eig (C, D); |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
71 v = diag (v); |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
72 ## return n-element eigenvectors normalized so that the infinity-norm = 1 |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
73 z = z(1:n,:); |
15190
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
74 ## max() takes the abs if complex: |
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
75 t = max (z); |
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
76 z /= diag (t); |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
77 else |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
78 z = eig (C, D); |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
79 endif |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
80 |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
81 endfunction |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
82 |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
83 |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
84 %!shared C0, C1 |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
85 %! C0 = [8, 0; 0, 4]; C1 = [1, 0; 0, 1]; |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
86 |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
87 %!test |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
88 %! z = polyeig (C0, C1); |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
89 %! assert (z, [-8; -4]); |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
90 |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
91 %!test |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
92 %! [v,z] = polyeig (C0, C1); |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
93 %! assert (z, [-8; -4]); |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
94 %! z = diag (z); |
15197
0c0f4eef30f4
* polyeig.m: Missing semicolon in test.
John W. Eaton <jwe@octave.org>
parents:
15190
diff
changeset
|
95 %! d = C0*v + C1*v*z; |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
96 %! assert (norm (d), 0.0); |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
97 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
98 ## Test input validation |
18657
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
99 %!error polyeig () |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
100 %!error [a,b,c] = polyeig (1) |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
101 %!error <coefficients must be square matrices> polyeig (ones (3,2)) |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
102 %!error <coefficients must have the same dimensions> polyeig (ones (3,3), ones (2,2)) |