Message Boards Message Boards

0
|
3502 Views
|
8 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to convert numerical polynomial to string as InputForm?

Posted 2 years ago

Hello everybody,

I have this polynom:

pp = -0.017405914799331364819 u^3 v + 0.051335637554670000974 u^2 v^2 - 
 0.039163308298495570843 u v^3 + 0.074308214044552913018 u^3 w + 
 0.60368896954782411026 u^2 v w - 0.51875755425567358214 u v^2 w - 
 0.16593638775362567794 v^3 w + 1.0000000000000000000 u^2 w^2 - 
 0.81786201225836559051 u v w^2 - 0.18098149081617408008 v^2 w^2 + 
 0.34883578259804006389 u w^3 - 0.34621295716497209349 v w^3

How to convert it to String as InputForm ( multiplication signs are required, no sci notation), i.e. how to get;

output = "-0.017405914799331364819*u^3*v+0.074308214044552913018*u^3*w+0.051335637554670000974*u^2*v^2+0.60368896954782411026*u^2*v*w+1.0000000000000000000*u^2*w^2-0.039163308298495570843*u*v^3-0.51875755425567358214*u*v^2*w-0.81786201225836559051*u*v*w^2+0.34883578259804006389*u*w^3-0.16593638775362567794*v^3*w-0.18098149081617408008*v^2*w^2-0.34621295716497209349*v*w^3"

output must be plain STRING.

Thanks in advance, César Lozada

POSTED BY: Cesar Lozada
8 Replies

César,

You are correct. It seems to be missing in the documentation. I will report the oversight and link this thread. It should be added to the documentation for ToString[], CForm[], and FortranForm[].

I'm glad it helped. I use this feature regularly when I need to parse expressions or export them for use in C or other languages.

Regards,

Neil

POSTED BY: Neil Singer
Posted 2 years ago

Thanks, Daniel.

Now I know how to skip these annoying extra digits at the end of converted numbers.

As expressed in my answer to Neil, documentation does not mention this 3rd parameter in ToString[].

That's why this forum is absolutely necessesary.

Thanks again and regards, César Lozada

POSTED BY: Cesar Lozada
Posted 2 years ago

Hi Neil,

It works. Thanks.

I have checked again the documentation for ToString[] and there is not reference to FortranForm or CForm. ToString[] documentation and examples mention only OutpputForm, StandardFrom, InputForm and TraditionalForm. There is no way I can link ToString[] wirh these other forms to make a conversion. Maybe documentation needs to be improved.

Regards, César Lozada

POSTED BY: Cesar Lozada

Did you want the power function? CForm will give you power() instead of ** or do Daniel's approach.

POSTED BY: Neil Singer
In[6]:= ToString[pp, InputForm, NumberMarks -> False]

Out[6]= "-0.017405914799331364819*u^3*v + \
0.051335637554670000974*u^2*v^2 - 0.039163308298495570843*u*v^3 + \
0.074308214044552913018*u^3*w + 0.60368896954782411026*u^2*v*w - \
0.51875755425567358214*u*v^2*w - 0.16593638775362567794*v^3*w + \
1.*u^2*w^2 - 0.81786201225836559051*u*v*w^2 - \
0.18098149081617408008*v^2*w^2 + 0.34883578259804006389*u*w^3 - \
0.34621295716497209349*v*w^3"
POSTED BY: Daniel Lichtblau

...and If you need fewer decimal places:

ToString[N[pp, 5], FortranForm]
POSTED BY: Neil Singer

César,

How about

ToString[pp, FortranForm]

Regards,

Neil

POSTED BY: Neil Singer
Posted 2 years ago

I found a way. Just like to know if Mathematica can do it directly:

lis1 = CoefficientRules[pp, {u, v, w}]
lis2 = Map[{If[#[[2]] > 0, "+", ""] <> ToString[#[[2]]], #[[1]]} &,  lis1]
lis3 = Map[#[[1]] <> If[#[[2]] === {0,0,0}, "", "*" <> ToString[Times @@ ({u, v, w}^#[[2]]), InputForm]] &, lis2]   
res = StringJoin @@ lis3

Note: Databases sqlite or mysql do not allow "^" as operator. In this case;

pp1 = pp /. X_^Y_ -> POWER[X, Y] 
res = StringReplace[StringReplace[ ToString[N[pp1], InputForm], {"[" -> "(", "]" -> ")", " " .. -> "" }], ".)" -> ")"]

But I don't know how to control the number of decimals here.

César Lozada

POSTED BY: Cesar Lozada
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract