]> Creatis software - CreaPhase.git/blob - octave_packages/m/polynomial/poly.m
update packages
[CreaPhase.git] / octave_packages / m / polynomial / poly.m
1 ## Copyright (C) 1994-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} {} poly (@var{A})
21 ## @deftypefnx {Function File} {} poly (@var{x})
22 ## If @var{A} is a square @math{N}-by-@math{N} matrix, @code{poly (@var{A})}
23 ## is the row vector of the coefficients of @code{det (z * eye (N) - A)},
24 ## the characteristic polynomial of @var{A}.  For example,
25 ## the following code finds the eigenvalues of @var{A} which are the roots of
26 ## @code{poly (@var{A})}.
27 ##
28 ## @example
29 ## @group
30 ## roots (poly (eye (3)))
31 ##     @result{} 1.00001 + 0.00001i
32 ##        1.00001 - 0.00001i
33 ##        0.99999 + 0.00000i
34 ## @end group
35 ## @end example
36 ##
37 ## In fact, all three eigenvalues are exactly 1 which emphasizes that for
38 ## numerical performance the @code{eig} function should be used to compute
39 ## eigenvalues.
40 ##
41 ## If @var{x} is a vector, @code{poly (@var{x})} is a vector of the
42 ## coefficients of the polynomial whose roots are the elements of @var{x}. 
43 ## That is, if @var{c} is a polynomial, then the elements of @code{@var{d} =
44 ## roots (poly (@var{c}))} are contained in @var{c}.  The vectors @var{c} and
45 ## @var{d} are not identical, however, due to sorting and numerical errors.
46 ## @seealso{roots, eig}
47 ## @end deftypefn
48
49 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
50 ## Created: 24 December 1993
51 ## Adapted-By: jwe
52
53 function y = poly (x)
54
55   if (nargin != 1)
56     print_usage ();
57   endif
58
59   m = min (size (x));
60   n = max (size (x));
61   if (m == 0)
62     y = 1;
63     return;
64   elseif (m == 1)
65     v = x;
66   elseif (m == n)
67     v = eig (x);
68   else
69     print_usage ();
70   endif
71
72   y = zeros (1, n+1);
73   y(1) = 1;
74   for j = 1:n;
75     y(2:(j+1)) = y(2:(j+1)) - v(j) .* y(1:j);
76   endfor
77
78   if (all (all (imag (x) == 0)))
79     y = real (y);
80   endif
81
82 endfunction
83
84 %!assert(all (all (poly ([1, 2, 3]) == [1, -6, 11, -6])));
85
86 %!assert(all (all (abs (poly ([1, 2; 3, 4]) - [1, -5, -2]) < sqrt (eps))));
87
88 %!error poly ([1, 2, 3; 4, 5, 6]);
89
90 %!assert(poly ([]),1);
91