Using sympty in jhepwork

6 views
Skip to first unread message

sergei175

unread,
Jul 5, 2009, 9:33:32 AM7/5/09
to sympy

Hi,

Sympy seems is rather advanced and useful project.

I've tried to test it using jhepwork, and even included it into
http://jwork.org/jhepwork/download/
(version 2.0) for test purpose (directory python/packages)

Apparently, one can run sympy using Jython (v2.5) and Java !
A code snipped is below. Load it into the jHepWork IDE
editor and press the button [run].

However, there is on feature I want to understand. To plot a function
as in the example below using F1D class, I need to pass a string
representing this function. However, simpy cannot accept strings for
the
statements such as diff() or integral(). In the below example, what I
want
to do is to type "diff(func,x)".

Further, if I want to plot the result of "diff" or integration, I
need again a string
representing the output function and pass it to the F1D class as in
the example.
How can I do this?

thanks, Sergei

********************************************************
# jHepWork Jython example

from java.awt import Font,Color
from jhplot import *

c1 = HPlot("Canvas")
c1.setAutoRange()

c1.setGTitle("Example", Color.red)
c1.setNameX("X")
c1.setNameY("Y")
c1.setName("Simpy example")
c1.visible()
c1.setAutoRange()

# draw function
func="2*exp(-x*x/50)+sin(pi*x)/x"
f1 = F1D(func, -2.0, 5.0)
c1.draw(f1)

# use sympy
from sympy import *
x = Symbol('x')
print diff(2*exp(-x*x/50)+sin(pi*x)/x, x)

# how to get string with the result
# to build F1D with results for drawing?

# export to some image (png,eps,pdf,jpeg...)
# c1.export(Editor.DocMasterName()+".png")

sergei175

unread,
Jul 5, 2009, 9:35:19 AM7/5/09
to sympy

Aaron S. Meurer

unread,
Jul 5, 2009, 6:32:21 PM7/5/09
to sy...@googlegroups.com
Hi, I'm not completely clear on what you are doing, as I don't use
jython, but couldn't you just use str(), as in

>>> a = str(diff(2*exp(-x*x/50)+sin(pi*x)/x, x))
>>> type(a)
<type 'str'>
>>> F1D(a) # probably works now

Also, you can use sympify() or just S() to convert from a string to a
SymPy expression:

>>> func = func="2*exp(-x*x/50)+sin(pi*x)/x"
>>> print S(diff(func,x))
-sin(pi*x)/x**2 + pi*cos(pi*x)/x - 2*x*exp(-x**2/50)/25

Sergei Chekanov

unread,
Jul 5, 2009, 8:39:44 PM7/5/09
to sy...@googlegroups.com
Ok, thanks. This seems works well.
 The only thing which had to be change is to replace "**" (power)
 with the usual "^"

 The code below works well in jhepwork
 (shows the function and its differential on the same plot)

*************************************************************
from java.awt import Color

from jhplot  import *

c1 = HPlot("Canvas")
c1.setAutoRange()

c1.setGTitle("Example", Color.red)
c1.setNameX("Xaxis")
c1.setNameY("Yaxis")
c1.setName("Canvas title")
c1.visible()
c1.setAutoRange()

func="2*exp(-x*x/50)+sin(pi*x)/x"
f1 = F1D(func, 1.0, 10.0)

c1.draw(f1)

# use sympy
from sympy import *
x = Symbol('x')
a=diff(S(func), x)
ff=str(a).replace("**","^")
f2 = F1D(ff, 1.0, 10.0)
f2.setTitle("Differential")
f2.setColor(Color.green)
c1.draw(f2)

 

# export to some image (png,eps,pdf,jpeg...)
# c1.export(Editor.DocMasterName()+".png")

*********************************************************

Ondrej Certik

unread,
Jul 7, 2009, 1:49:26 AM7/7/09
to sy...@googlegroups.com
On Sun, Jul 5, 2009 at 6:39 PM, Sergei Chekanov<serg...@googlemail.com> wrote:
> Ok, thanks. This seems works well.
>  The only thing which had to be change is to replace "**" (power)
>  with the usual "^"

Nice. If something else doesn't work, let us know.

Ondrej

Reply all
Reply to author
Forward
0 new messages