]> Creatis software - CreaPhase.git/blob - octave_packages/m/polynomial/polyout.m
update packages
[CreaPhase.git] / octave_packages / m / polynomial / polyout.m
1 ## Copyright (C) 1995-2012 Auburn University.  All rights reserved.
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} {} polyout (@var{c})
21 ## @deftypefnx {Function File} {} polyout (@var{c}, @var{x})
22 ## @deftypefnx {Function File} {@var{str} =} polyout (@dots{})
23 ## Write formatted polynomial
24 ## @tex
25 ## $$ c(x) = c_1 x^n + \ldots + c_n x + c_{n+1} $$
26 ## @end tex
27 ## @ifnottex
28 ##
29 ## @example
30 ## c(x) = c(1) * x^n + @dots{} + c(n) x + c(n+1)
31 ## @end example
32 ##
33 ## @end ifnottex
34 ## and return it as a string or write it to the screen (if @var{nargout} is
35 ## zero).  @var{x} defaults to the string @code{"s"}.
36 ## @seealso{polyreduce}
37 ## @end deftypefn
38
39 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu>
40 ## Created: May 1995
41 ## Nov 1998: Correctly handles complex coefficients
42
43 function y = polyout (c, x)
44
45   if (nargin < 1) || (nargin > 2) || (nargout < 0) || (nargout > 1)
46     print_usage ();
47   endif
48
49   if (! isvector (c))
50     error ("polyout: first argument must be a vector");
51   endif
52
53   if (nargin == 1)
54     x = "s";
55   elseif (! ischar (x))
56     error ("polyout: second argument must be a string");
57   endif
58
59   n = length (c);
60   if(n > 0)
61     n1 = n+1;
62
63     tmp = coeff (c(1));
64     for ii = 2:n
65       if (real (c(ii)) < 0)
66         ns = " - ";
67         c(ii) = -c(ii);
68       else
69         ns = " + ";
70       endif
71
72       tmp = sprintf ("%s*%s^%d%s%s", tmp, x, n1-ii, ns, coeff (c(ii)));
73
74     endfor
75   else
76     tmp = " ";
77   endif
78
79   if(nargout == 0)
80     disp (tmp);
81   else
82     y = tmp;
83   endif
84
85 endfunction
86
87 function str = coeff (c)
88   if (imag (c))
89     if (real (c))
90       str = sprintf ("(%s)", num2str (c, 5));
91     else
92       str = num2str (c, 5);
93     endif
94   else
95     str = num2str (c, 5);
96   endif
97 endfunction
98
99 %!assert (polyout ([3 2 1]), '3*s^2 + 2*s^1 + 1')
100 %!assert (polyout ([3 2 1], 'x'), '3*x^2 + 2*x^1 + 1')
101 %!assert (polyout ([3 2 1], 'wxyz'), '3*wxyz^2 + 2*wxyz^1 + 1')
102 %!assert (polyout ([5 4 3 2 1], '1'),'5*1^4 + 4*1^3 + 3*1^2 + 2*1^1 + 1')
103 %!error polyout ([])