]> Creatis software - CreaPhase.git/blob - octave_packages/symbolic-1.1.0/poly2sym.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / symbolic-1.1.0 / poly2sym.m
1 ## Copyright (C) 2003 Willem J. Atsma <watsma@users.sf.net>
2 ##
3 ## This program is free software; you can redistribute it and/or
4 ## modify it under the terms of the GNU General Public
5 ## License as published by the Free Software Foundation;
6 ## either version 2, or (at your option) any later version.
7 ##
8 ## This software is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied
10 ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 ## PURPOSE.  See the GNU General Public License for more
12 ## details.
13 ##
14 ## You should have received a copy of the GNU General Public
15 ## License along with this software; see the file COPYING.  If not,
16 ## see <http://www.gnu.org/licenses/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} {@var{p} =} poly2sym (@var{c}, @var{x})
20 ## Creates a symbolic polynomial expression @var{p} with coefficients @var{c}.
21 ## If @var{p} is not specified, the free variable is set to sym("x"). @var{c}
22 ## may be a vector or a cell-array of symbols. @var{x} may be a symbolic
23 ## expression or a string.
24 ## The coefficients correspond to decreasing exponent of the free variable.
25 ##
26 ## Example:
27 ## @example
28 ## symbols
29 ## x = sym("x");
30 ## y = sym("y");
31 ## p = poly2sym ([2,5,-3]);         # p = 2*x^2+5*x-3
32 ## c = poly2sym (@{2*y,5,-3@},x);     # p = 2*y*x^2+5*x-3
33 ## @end example
34 ##
35 ## @seealso{sym2poly,polyval,roots}
36 ## @end deftypefn
37
38 function p = poly2sym(c,x)
39
40   if exist("x")!=1
41     x = sym("x");
42   endif
43
44   N = length(c);
45
46   if !iscell(c)
47     tmp = c;
48     c = cell;
49     for i=1:N
50       c(i) = tmp(i);
51     endfor
52   endif
53
54   p = vpa(0);
55   for i=1:N
56     if isnumeric(c{i})
57       p = p*x+vpa(c{i});
58     else
59       p = p*x+c{i};
60     endif
61   endfor
62
63 endfunction