Re: Creation and evaluation of polynomials

*To*: mathgroup at smc.vnet.net*Subject*: [mg89448] Re: Creation and evaluation of polynomials*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>*Date*: Tue, 10 Jun 2008 03:38:04 -0400 (EDT)*Organization*: The Open University, Milton Keynes, UK*References*: <g2j05j$510$1@smc.vnet.net>

jmgomezgoo at gmail.com wrote: > Image we have a function that create a Polynomial: > > polynomial[{1,0,0,0,1,0,1,}] -> p^5 + 3p^2 > > And we want to calulate roots and the derivate of this polynomial. > > I try with ToString[polynomial[{1,0,0,0,1,0,1}]] in order to use > convert the polynomial expression to string first and later use > > ToExpression[] > > and calculate roots > > D[ToExpression[ToString[polynomial[{1,0,0,0,1,0,1}]], x] > > like the next examples: > > D[ToExpression["x^2+2x"], x] > > or > > NSolve[ToExpression["x^2+2x==x"], x] > > > But ToString erase ^ symbol. > > Somebody knows how to solve it? By default, ToString converts an expression in OutputForm, i.e. in a form suitable for display by a non-graphic terminal (command line or shell interface). Note that OutputForm has not been designed to be use as input, that is to be evaluated. To get what you want, just tell ToString, by using its second argument, to convert the expression as a string that matches its InputForm. For instance, In[1]:= poly = p^5 + 3 p^2 Out[1]= 3 p^2 + p^5 In[2]:= ToString[poly] Out[2]= " 2 5 3 p + p" In[3]:= ToString[poly, InputForm] Out[3]= "3*p^2 + p^5" In[4]:= D[ToExpression[ToString[poly, InputForm]], p] Out[4]= 6 p + 5 p^4 Regards, -- Jean-Marc