]> Creatis software - CreaPhase.git/blob - octave_packages/m/polynomial/polyreduce.m
update packages
[CreaPhase.git] / octave_packages / m / polynomial / polyreduce.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} {} polyreduce (@var{c})
21 ## Reduce a polynomial coefficient vector to a minimum number of terms by
22 ## stripping off any leading zeros.
23 ## @seealso{polyout}
24 ## @end deftypefn
25
26 ## Author: Tony Richardson <arichard@stark.cc.oh.us>
27 ## Created: June 1994
28 ## Adapted-By: jwe
29
30 function p = polyreduce (c)
31
32   if (nargin != 1)
33     print_usage ();
34   endif
35
36   if (!isvector (c) || isempty (c))
37     error ("polyreduce: C must be a non-empty vector");
38   endif
39
40   if (! isempty (c))
41
42     index = find (c != 0);
43
44     if (isempty (index))
45
46       p = 0;
47
48     else
49
50       p = c(index (1):length (c));
51
52     endif
53
54   endif
55
56 endfunction
57
58 %!assert(all (all (polyreduce ([0, 0, 1, 2, 3]) == [1, 2, 3])));
59
60 %!assert(all (all (polyreduce ([1, 2, 3, 0, 0]) == [1, 2, 3, 0, 0])));
61
62 %!assert(all (all (polyreduce ([1, 0, 3]) == [1, 0, 3])));
63
64 %!error polyreduce ([1, 2; 3, 4]);
65