]> Creatis software - CreaPhase.git/blob - octave_packages/m/polynomial/mpoles.m
update packages
[CreaPhase.git] / octave_packages / m / polynomial / mpoles.m
1 ## Copyright (C) 2007-2012 Ben Abbott
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{multp}, @var{idxp}] =} mpoles (@var{p})
21 ## @deftypefnx {Function File} {[@var{multp}, @var{idxp}] =} mpoles (@var{p}, @var{tol})
22 ## @deftypefnx {Function File} {[@var{multp}, @var{idxp}] =} mpoles (@var{p}, @var{tol}, @var{reorder})
23 ## Identify unique poles in @var{p} and their associated multiplicity.  The
24 ## output is ordered from largest pole to smallest pole.
25 ##
26 ## If the relative difference of two poles is less than @var{tol} then
27 ## they are considered to be multiples.  The default value for @var{tol}
28 ## is 0.001.
29 ##
30 ## If the optional parameter @var{reorder} is zero, poles are not sorted.
31 ##
32 ## The output @var{multp} is a vector specifying the multiplicity of the
33 ## poles.  @code{@var{multp}(n)} refers to the multiplicity of the Nth pole
34 ## @code{@var{p}(@var{idxp}(n))}.
35 ##
36 ## For example:
37 ##
38 ## @example
39 ## @group
40 ## p = [2 3 1 1 2];
41 ## [m, n] = mpoles (p)
42 ##    @result{} m = [1; 1; 2; 1; 2]
43 ##    @result{} n = [2; 5; 1; 4; 3]
44 ##    @result{} p(n) = [3, 2, 2, 1, 1]
45 ## @end group
46 ## @end example
47 ##
48 ## @seealso{residue, poly, roots, conv, deconv}
49 ## @end deftypefn
50
51 ## Author: Ben Abbott <bpabbott@mac.com>
52 ## Created: Sept 30, 2007
53
54 function [multp, indx] = mpoles (p, tol, reorder)
55
56   if (nargin < 1 || nargin > 3)
57     print_usage ();
58   endif
59
60    if (nargin < 2 || isempty (tol))
61      tol = 0.001;
62    endif
63
64    if (nargin < 3 || isempty (reorder))
65      reorder = true;
66    endif
67
68   Np = numel (p);
69
70   ## Force the poles to be a column vector.
71
72   p = p(:);
73
74   ## Sort the poles according to their magnitidues, largest first.
75
76   if (reorder)
77     ## Sort with smallest magnitude first.
78     [p, ordr] = sort (p);
79     ## Reverse order, largest maginitude first.
80     n = Np:-1:1;
81     p = p(n);
82     ordr = ordr(n);
83   else
84     ordr = 1:Np;
85   endif
86
87   ## Find pole multiplicty by comparing the relative differnce in the
88   ## poles.
89
90   multp = zeros (Np, 1);
91   indx = [];
92   n = find (multp == 0, 1);
93   while (n)
94     dp = abs (p-p(n));
95     if (p(n) == 0.0)
96       if (any (abs (p) > 0 & isfinite (p)))
97         p0 = mean (abs (p(abs (p) > 0 & isfinite (p))));
98       else
99         p0 = 1;
100       endif
101     else
102       p0 = abs (p(n));
103     endif
104     k = find (dp < tol * p0);
105     ## Poles can only be members of one multiplicity group.
106     if (numel (indx))
107       k = k(! ismember (k, indx));
108     endif
109     m = 1:numel (k);
110     multp(k) = m;
111     indx = [indx; k];
112     n = find (multp == 0, 1);
113   endwhile
114   multp = multp(indx);
115   indx = ordr(indx);
116
117 endfunction
118
119 %!test
120 %! [mp, n] = mpoles ([0 0], 0.01);
121 %! assert (mp, [1; 2])
122