MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to handle powers in c-code generation?


Hi,

a) if C code with pow() is nice or not is a matter of taste
b) you may have a power c-function that handle integer powers
   without calling log()
c)
Unprotect[Power]
Format[Power[x_, n_Integer], CForm] := 
  HoldForm[Evaluate[Table[x, {n}]]] /. List -> Times

Protect[Power]

does what you want, but it is not nice for x^10-x^9 ..
you should use the Horner rule if you have polynoms.

Regards
  Jens

Christian Ott wrote:
> 
> Hi there,
> 
> I have a problem with generating "nice" c-code in mathematica.
> When I use the packages "Format" and "Optimize" to generate the
> c-code, the (integer) powers are coded as function calls instead of
> explicit
> multiplications (e.g. pow(x,2.) instead of x*x). These function calls
> are obviously quite ineffective in many cases.
> 
> Here is a simple example of my problem:
> ----------------
> Mathematica Input:
>   << Format'
>   << Optimize'
>   CAssign[x^2, AssignOptimize -> False, OptimizePower -> Binary]
> 
> Mathematica Output: pow(x,2.)
> Desired Output: x*x
> -----------------
> 
> I thought that the option "OptimizePower -> Binary" would be
> sufficient to get an output of the form "x*x".
> Is there another option that I should use in the CAssign call?
> Or is there a better way of generating c-code from mathematica?
> 
> thanks in advance,
> Christian


  • Prev by Date: Re: Increase in efficiency with Module
  • Next by Date: Re: FindRoot problem
  • Previous by thread: How to handle powers in c-code generation?
  • Next by thread: FindRoot problem