DMelt:Symbolic/5 SimPy

From jWork.ORG
Jump to: navigation, search
Limitted access. Reguest membership or login to this link first if you are already a member
Contents

Using SimPy engine

SimPy is a pure Python library and this means you might find it a bit slower than using the pure-Java calculations based on Jasymca. Let us show a simple SymPy example. Start DataMelt IDE and type:

from sympy import *
 var('x y')
 Rational(3,2)*pi + exp(I*x) / (x**2 + y)

The output of this code is

(3/2)*pi + 1/(y + x**2)*exp(I*x)

Look at other SymPy example in this wiki.

Check out a few other examples:

Calculate an integral

from sympy import *
 var('x')
 integrate(x**2 * cos(x), x)

The output is:

-2*sin(x) + x**2*sin(x) + 2*x*cos(x)

Solve an equation

from sympy import *
 var('x y')
 solve([Eq(x + 5*y, 2), Eq(-3*x + 6*y, 15)], [x, y])

The output is:

{x: -3, y: 1}

Integration

DataMelt extends SimPy and integrates it into Java. Below we show an advanced example of how to plot a function and its differential on the same plot.

from java.awt import Color
from jhplot  import *

c1 = HPlot("Canvas")
c1.setAutoRange()
c1.setGTitle("Differential", 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)

from sympy import *
x = Symbol('x')
a=diff(S(func), x)
f2 = F1D(str(a), 1.0, 10.0)
f2.setTitle("Differential")
f2.setColor(Color.green)
c1.draw(f2)
c1.export("figure.pdf")

The output figure is shown below:

DMelt example: Differentiation using SymPy

More information on this topic is in DMelt books