How to hold a form and convert it to a string
- To: mathgroup at smc.vnet.net
- Subject: [mg93915] How to hold a form and convert it to a string
- From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
- Date: Fri, 28 Nov 2008 05:09:58 -0500 (EST)
Hi,
I'm sure it's a simple question but I don't get it. I want to transform
the expression
a*q + b*q^2 + c*q^3
into a C-Code form
"a*q + b*q*q + c*q*q*q"
With the rule
exp //. (Power[q_, n_] :> Times@@Array[q &, n])
for the transformation of the Power of q,
the expression is instantly reevaluated to the first form (as I expected
it). I could transform the complete first expression to a C-Form-String
and manipulate this. This would look like
f[exp_] := StringReplace[ToString@CForm[exp],
"Power(" ~~ q_ ~~ "," ~~ n_ ~~ ")" :>
StringJoin[Riffle[Array[ToString@q &, ToExpression[n]], "*"]]]
Isn't there a more stylish way?
Cheers
Patrick