X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Fsymbolic-1.1.0%2Fpoly2sym.m;fp=octave_packages%2Fsymbolic-1.1.0%2Fpoly2sym.m;h=ccf07fad8d69a44ec2bfe3314b76b45c68cf5d21;hp=0000000000000000000000000000000000000000;hb=f5f7a74bd8a4900f0b797da6783be80e11a68d86;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/symbolic-1.1.0/poly2sym.m b/octave_packages/symbolic-1.1.0/poly2sym.m new file mode 100644 index 0000000..ccf07fa --- /dev/null +++ b/octave_packages/symbolic-1.1.0/poly2sym.m @@ -0,0 +1,63 @@ +## Copyright (C) 2003 Willem J. Atsma +## +## This program is free software; you can redistribute it and/or +## modify it under the terms of the GNU General Public +## License as published by the Free Software Foundation; +## either version 2, or (at your option) any later version. +## +## This software is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied +## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +## PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public +## License along with this software; see the file COPYING. If not, +## see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{p} =} poly2sym (@var{c}, @var{x}) +## Creates a symbolic polynomial expression @var{p} with coefficients @var{c}. +## If @var{p} is not specified, the free variable is set to sym("x"). @var{c} +## may be a vector or a cell-array of symbols. @var{x} may be a symbolic +## expression or a string. +## The coefficients correspond to decreasing exponent of the free variable. +## +## Example: +## @example +## symbols +## x = sym("x"); +## y = sym("y"); +## p = poly2sym ([2,5,-3]); # p = 2*x^2+5*x-3 +## c = poly2sym (@{2*y,5,-3@},x); # p = 2*y*x^2+5*x-3 +## @end example +## +## @seealso{sym2poly,polyval,roots} +## @end deftypefn + +function p = poly2sym(c,x) + + if exist("x")!=1 + x = sym("x"); + endif + + N = length(c); + + if !iscell(c) + tmp = c; + c = cell; + for i=1:N + c(i) = tmp(i); + endfor + endif + + p = vpa(0); + for i=1:N + if isnumeric(c{i}) + p = p*x+vpa(c{i}); + else + p = p*x+c{i}; + endif + endfor + +endfunction