Mercurial > octave
annotate scripts/polynomial/polyeig.m @ 27919:1891570abac8
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2020.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Jan 2020 22:29:51 -0500 |
parents | b442ec6dda5c |
children | bd51beb6205e |
rev | line source |
---|---|
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
1 ## Copyright (C) 2012-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26963
diff
changeset
|
2 ## |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26963
diff
changeset
|
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26963
diff
changeset
|
4 ## or <https://octave.org/COPYRIGHT.html/>. |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26963
diff
changeset
|
5 ## |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
6 ## |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
7 ## This file is part of Octave. |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
8 ## |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
9 ## Octave is free software: you can redistribute it and/or modify it |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
10 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
11 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## (at your option) any later version. |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
13 ## |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
14 ## 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
|
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## GNU General Public License for more details. |
15189
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 ## 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
|
20 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
21 ## <https://www.gnu.org/licenses/>. |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
22 |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
23 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
24 ## @deftypefn {} {@var{z} =} polyeig (@var{C0}, @var{C1}, @dots{}, @var{Cl}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20165
diff
changeset
|
25 ## @deftypefnx {} {[@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
|
26 ## |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
27 ## 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
|
28 ## |
26156
a4a7e887ddb0
polyeig.m: Improve documentation (bug #53139).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
29 ## Given an @var{n}x@var{n} matrix polynomial |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
30 ## |
26156
a4a7e887ddb0
polyeig.m: Improve documentation (bug #53139).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
31 ## @code{@var{C}(@var{s}) = @var{C0} + @var{C1} @var{s} + @dots{} + @var{Cl} |
a4a7e887ddb0
polyeig.m: Improve documentation (bug #53139).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
32 ## @var{s}^@var{l}} |
20165
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
33 ## |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
34 ## @code{polyeig} solves the eigenvalue problem |
f1d0f506ee78
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
35 ## |
26963
589b00536a39
doc: Small fixes to mat2cell and polyeig docstrings (bug #55985).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
36 ## @code{(@var{C0} + @var{C1} @var{z} + @dots{} + @var{Cl} @var{z}^@var{l}) |
589b00536a39
doc: Small fixes to mat2cell and polyeig docstrings (bug #55985).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
37 ## @var{v} = 0}. |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
38 ## |
15222
e3dc9ff8e0f2
polyeig.m: Use @code, not @var, macro to silence warnings from texinfo.
Rik <rik@octave.org>
parents:
15197
diff
changeset
|
39 ## Note that the eigenvalues @var{z} are the zeros of the matrix polynomial. |
26156
a4a7e887ddb0
polyeig.m: Improve documentation (bug #53139).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
40 ## @var{z} is a row vector with @code{@var{n}*@var{l}} elements. @var{v} is a |
a4a7e887ddb0
polyeig.m: Improve documentation (bug #53139).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
41 ## matrix (@var{n} x @var{n}*@var{l}) with columns that correspond to the |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
42 ## eigenvectors. |
15190
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
43 ## |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
44 ## @seealso{eig, eigs, compan} |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
45 ## @end deftypefn |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
46 |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
47 function [z, v] = polyeig (varargin) |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
48 |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
49 if (nargin < 1 || nargout > 2) |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
50 print_usage (); |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
51 endif |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
52 |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
53 n = rows (varargin{1}); |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
54 |
26158
e5a40e1395fb
polyeig.m: Simplify code by using nargin.
Rik <rik@octave.org>
parents:
26156
diff
changeset
|
55 for i = 1 : nargin |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
56 if (! issquare (varargin{i})) |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
57 error ("polyeig: coefficients must be square matrices"); |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
58 endif |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
59 if (rows (varargin{i}) != n) |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
60 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
|
61 endif |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
62 endfor |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
63 |
15190
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
64 ## matrix polynomial degree |
26158
e5a40e1395fb
polyeig.m: Simplify code by using nargin.
Rik <rik@octave.org>
parents:
26156
diff
changeset
|
65 l = nargin - 1; |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
66 |
15190
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
67 ## form needed matrices |
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
68 C = [ zeros(n * (l - 1), n), eye(n * (l - 1)); |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
69 -cell2mat(varargin(1:end-1)) ]; |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
70 |
15190
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
71 D = [ eye(n * (l - 1)), zeros(n * (l - 1), n); |
26158
e5a40e1395fb
polyeig.m: Simplify code by using nargin.
Rik <rik@octave.org>
parents:
26156
diff
changeset
|
72 zeros(n, n * (l - 1)), varargin{end} ]; |
15190
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
73 |
ee9b1270c25a
polyeig.m: Style fixes
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15189
diff
changeset
|
74 ## solve generalized eigenvalue problem |
26158
e5a40e1395fb
polyeig.m: Simplify code by using nargin.
Rik <rik@octave.org>
parents:
26156
diff
changeset
|
75 if (nargout < 2) |
e5a40e1395fb
polyeig.m: Simplify code by using nargin.
Rik <rik@octave.org>
parents:
26156
diff
changeset
|
76 z = eig (C, D); |
e5a40e1395fb
polyeig.m: Simplify code by using nargin.
Rik <rik@octave.org>
parents:
26156
diff
changeset
|
77 else |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
78 [z, v] = eig (C, D); |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
79 v = diag (v); |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
80 ## 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
|
81 z = z(1:n,:); |
26158
e5a40e1395fb
polyeig.m: Simplify code by using nargin.
Rik <rik@octave.org>
parents:
26156
diff
changeset
|
82 t = max (z); # max() takes the abs if complex. |
e5a40e1395fb
polyeig.m: Simplify code by using nargin.
Rik <rik@octave.org>
parents:
26156
diff
changeset
|
83 z ./= t; |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
84 endif |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
85 |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
86 endfunction |
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
87 |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
88 |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
89 %!shared C0, C1 |
26158
e5a40e1395fb
polyeig.m: Simplify code by using nargin.
Rik <rik@octave.org>
parents:
26156
diff
changeset
|
90 %! C0 = [8, 0; 0, 4]; |
e5a40e1395fb
polyeig.m: Simplify code by using nargin.
Rik <rik@octave.org>
parents:
26156
diff
changeset
|
91 %! C1 = [1, 0; 0, 1]; |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
92 |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
93 %!test |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
94 %! z = polyeig (C0, C1); |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
95 %! assert (z, [-8; -4]); |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
96 |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
97 %!test |
15189
2ad5e6212cd7
polyeig: remove DOS newlines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
15187
diff
changeset
|
98 %! [v,z] = polyeig (C0, C1); |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
99 %! assert (z, [-8; -4]); |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
100 %! z = diag (z); |
15197
0c0f4eef30f4
* polyeig.m: Missing semicolon in test.
John W. Eaton <jwe@octave.org>
parents:
15190
diff
changeset
|
101 %! d = C0*v + C1*v*z; |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
102 %! assert (norm (d), 0.0); |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
16933
diff
changeset
|
103 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
104 ## Test input validation |
18562
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
105 %!error polyeig () |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
106 %!error [a,b,c] = polyeig (1) |
f57148641869
polyeig.m: Overhaul function for Matlab compatibility (bug #41865).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
107 %!error <coefficients must be square matrices> polyeig (ones (3,2)) |
26158
e5a40e1395fb
polyeig.m: Simplify code by using nargin.
Rik <rik@octave.org>
parents:
26156
diff
changeset
|
108 %!error <coefficients must have the same dimensions> |
e5a40e1395fb
polyeig.m: Simplify code by using nargin.
Rik <rik@octave.org>
parents:
26156
diff
changeset
|
109 %! polyeig (ones (3,3), ones (2,2)) |