Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have an expression which is the sum of many terms of the form

$$ \frac{0.00000000274341}{(0.000363856+(0.611425+z)^2)^{3/2}} $$

My problem is that I need to include this long expression in a C++-program I made, so all the exponents $^2$ and $^{3/2}$ I need to bring on a C++-compatible form. If I use CForm, then the exponents become pow(..., 2) and pow(..., 1.5), respectively.

In order to speed up my program, I would like for all the pow(..., 2) expression to be converted to (...)*(...) and for the pow(..., 1.5) to be converted to (...)*pow(..., 0.5).

Is there an easy way to do this?

share|improve this question
1  
seems like string replacement can work for you? and why do you think x*pow(x, 0.5) is faster than pow(x, 1.5)? –  asterix314 Oct 22 '13 at 14:52
    
@asterix314 I found an optimized sqrt-function in C++, so I would like to write it all in terms of that.. I'll dig up the reference –  BillyJean Oct 22 '13 at 16:44
add comment

1 Answer

up vote 2 down vote accepted

Say you have an summation

expr = Sum[RandomReal[]/(RandomReal[] + (RandomReal[] + z)^2)^(3/2), {2}]

summation

Will the following work for you?

expr //. {x_^2 :> HoldForm[x*x], x_^(-3/2) :> 1/(x* sqrt[x]) } // CForm

converted to C form

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.