Re: [sage-support] java style output format

46 views
Skip to first unread message

Dan Drake

unread,
Nov 26, 2012, 2:19:54 PM11/26/12
to sage-s...@googlegroups.com
On Mon, 26 Nov 2012 at 10:10AM -0800, ijt wrote:
> I was wondering if there is any way to make sage produce an output
> that I can directly use in java code? I am using sage to compute
> gradients and hessians of multidim. functions which become quite large
> and replacing everything by hand is quite annoying.
>
> For example, sage returns something like:
>
> exp(x^2*log(z)-sqrt(y^5))
>
> which in java code would look like
>
> Math.exp(Math.pow(x,2)*Math.log(z)-Math.sqrt(Math.pow(y,5)))

I don't have a good answer for this (other than string manipulation and
regex matching -- if you want to be a little fancier, try the pyparsing
module), but there's a more general question here: how to rewrite Sage
expressions into other forms?

In some sense, we already do this in many places (the pty interfaces,
the LaTeX output), but I wonder if there's a nice way to overload some
of that stuff and define your own outputs, so that "exp()" becomes
"Math.exp()", just like

latex(sin(x))

is

\sin\left(x\right)


Thoughts?

Dan

--
--- Dan Drake
----- http://math.pugetsound.edu/~ddrake
-------
signature.asc

Stephen Montgomery-Smith

unread,
Nov 26, 2012, 2:30:27 PM11/26/12
to sage-s...@googlegroups.com
If you want sage to be competitive with Mathematica, you should
definitely make an equivalent of "CForm".

Jason Grout

unread,
Nov 26, 2012, 3:58:07 PM11/26/12
to sage-s...@googlegroups.com
On 11/26/12 12:10 PM, ijt wrote:
> Hello,
>
> I was wondering if there is any way to make sage produce an output that I can directly use in java code?
> I am using sage to compute gradients and hessians of multidim. functions which become quite large and replacing everything by hand is quite annoying.
>
> For example, sage returns something like:
>
> exp(x^2*log(z)-sqrt(y^5))
>
> which in java code would look like
>
> Math.exp(Math.pow(x,2)*Math.log(z)-Math.sqrt(Math.pow(y,5)))

This looks like fun!

http://interact.sagemath.org/node/76 (click evaluate)

That handles your example expression. The translate dictionary could be
expanded to handle lots of other cases as well, of course. Of note: the
sqrt function is actually a 1/2 power because that's how Sage represents
the square root.

Thanks,

Jason



Reply all
Reply to author
Forward
0 new messages