MathGroup Archive 2001

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

Search the Archive

Re: Trouble with Splice for Java (i.e. there is no JForm)


"Matthew D. Langston" wrote:
> 
> Hello all,
> 
> I can't figure out how to output Java language constructs using Splice, like
> I can for Fortran and C/C++ files.
> 
> For example, here is how x^2 is represented in different languages:
> 
> C/C++ :   Power(x,2)
> Fortran:   x**2
> TeX     :   x^2
> Java     :  Math.pow(x,2)
> 
> Since there is no JForm command in Mathematica, I have been trying to use
> CForm.   However, I can't figure out how to output the Java syntax.  If I
> try something like this:
> 
> x^2/.Power->Math.pow
> 
> then the CForm comes out like this:
> 
> Dot(Math,pow)(x,2)
> 
> If I try something like this:
> 
> x^2/.Power->"Math.pow"
> 
> then the CForm comes out like this:
> 
> "Math.pow"(x,2)
> 
> Can someone help me understand how to get x^2 to output as Math.pow(x,2)
> please?  Thank you.

StringReplace[CForm[ yourexpression ],"Power"->"Math.pow"]

should do what you want. If your expression is huge and the above
command is slow you can use any other system that can do efficient
string replacements (e.g. perl, sed,...) for the task...

An alternative would be to define your own Output Format for Power, but
that would probably get more complicated.

Hope that helps.

Albert


  • Prev by Date: Re: coloring everything outside a circle
  • Next by Date: Re: Backtrack
  • Previous by thread: Trouble with Splice for Java (i.e. there is no JForm)
  • Next by thread: Re: Trouble with Splice for Java (i.e. there is no JForm)