### Table of Contents

# Symbolic calculations

Including symbolic calculations in Jython/Java code, mixing numerical and symbolic calculations is very easy in SCaVis

There are several ways to use SCaVis for symbolic calculations.

- using Java libraries for symbolic calculations which allows a seamless integration of symbolic calculations with SCaVis data structures, Java containers and a powerful I/O. All scripts in Jython, BeanShell and Java code can call symbolic calculations directly.
- using JMathLab - a program which uses Octave/Matlab language for numeric and symbolic calculations. You can learn about it from jLearn portal. This program is based on the Jasymca engine developed by Helmut Dersch, but is significantly extended using SCaVis libraries. This program uses Octave/Matlab high-level language intended for numerical or analytic computations. It is mostly compatible with Matlab.

Below we will discuss the native SCaVis approach based on Java/Jython, than an approach based on Octave/Matlab language, and finally the native Python libraries included to the SCaVis.

# SCaVis as a math calculator

If you are using an interactive JythonShell for math calculations, remember that you should import math packages. You can import either
Python math package or `java.lang.Math`

. Or import both!

In JythonShell, type:

>>> from math import * >>> from java.lang.Math import *

After this, check what methods are available:

>>> sin(10) # simple example -0.5440211108893698 >>> dir() # print all available methods ['ClassPath', 'DicDir', 'DocDir', 'DocMasterName', 'DocMasterNameShort', 'DocName', 'DocStyle', 'E', 'IEEEremainder', 'ObjectBrowser', 'PI', 'ProjDir', 'SetEnv', 'SystemDir', 'SystemMacrosDir', 'UserMacrosDir', '__doc__', '__name__', 'abs', 'acos', 'asin', 'atan', 'atan2', 'cbrt', 'ceil', 'classDictInit', 'copySign', 'cos', 'cosh', 'degrees', 'e', 'equals', 'exp', 'expm1', 'fSep', 'fabs', 'floor', 'fmod', 'frexp', 'getClass', 'getExponent', 'hashCode', 'hypot', 'ldexp', 'log', 'log10', 'log1p', 'max', 'min', 'modf', 'module_dir1', 'module_dir2', 'module_dir3', 'module_dir4', 'nextAfter', 'nextUp', 'notify', 'notifyAll', 'os', 'pi', 'pow', 'radians', 'random', 'rint', 'round', 'scalb', 'signum', 'sin', 'sinh', 'sqrt', 'sys', 'tan', 'tanh', 'textArea', 'toDegrees', 'toRadians', 'toString', 'ulp', 'view', 'wait', 'wget']

Now you know what math functions can be used. Finally, add extra package:

>>> from jhplot.math import *

This adds symbolic calculations to your scripting. You should be able symbolically differentiate, integrate, simplify, evaluate math equations and even plot them. In the example below we simplify a long expression. To initialize symbolic calculations (in Java core or in Jython), use the class “Symbolic”.

>>> from jhplot.math import * >>> j=Symbolic() # this sets the default "jasymca" engine (by H. Dersch), or >>> j=Symbolic("jasymca") # as above, or >>> j=Symbolic("jscl") # this sets the engine to "jscl" (R.Jolly etc.)

Once you have selected the engine, you can evaluate expressions.

>>> print j.simplify("20+89+x*x+2*x^2+10*cos(x)-cos(x)") 109+3*x^2+9*cos(x)

Below we will discuss analytic calculations in more details.

# Symbolic calculation

Symbolic calculations in ScaVis are done using the interface function `jhplot.math.Symbolic`

that tahe an argument which specifies the engine for calculations. You can combine such calculations with the powerful
ScaVis methods for graphic output as discussed in graphics

`jhplot.math.Symbolic`

constructor takes the following arguments:

**jscl**- JSCL engine for symbolic calculations Jscl**jaymca**- Jasymca engine Jasymca**symja**- SymJa engine SymJa

The engines support the following calculations:

- polynomial system solving
- vectors and matrices
- factorization
- derivatives
- integrals (rational functions)
- boolean algebra
- simplification
- geometric algebra
- java code generation
- graphical rendering of math expressions

Below we show a simple example using Jython code (Java can also be used):

1: from jhplot.math import * 2: from jhplot import * 3: from java.awt import Color 4: 5: j=Symbolic("jscl") # using jscl engine 6: 7: print "Integral ##############" 8: s1="integral(cos(x)+x^2,x)" 9: ans1=j.eval(s1) # this is the same as simplify(expand(s1)) 10: print s1, " is ", ans1 11: 12: print "Differentiate ##############" 13: s2="d(1/(1+x^2),x)" 14: ans2=j.eval(s2) 15: print s2, " is ", ans2 16: 17: ######## now plotting #################### 18: from java.awt import Color 19: from jhplot import * 20: 21: c1 = HPlot("Canvas") 22: c1.setNameX("X") 23: c1.setNameY("Y") 24: c1.visible() 25: c1.setAutoRange() 26: 27: f1 = F1D( ans1, 0, 1.0) 28: f1.setTitle("Diff") 29: c1.draw(f1) 30: 31: f1 = F1D( ans2, 0, 1.0) 32: f1.setTitle("Integral") 33: f1.setColor(Color.blue) 34: c1.draw(f1)

As you can see, in this example we perform symbolic integration and differentiation. The output are strings representing the results which can be used as arguments to build a F1D function for plotting (see Sect. functions.

The output of this code is shown below:

Integral ############## integral(cos(x)+x^2,x) is (x^3+3*sin(x))/3 Differentiate ############## d(1/(1+x^2),x) is -(2*x)/(1+2*x^2+x^4)

and the generated figure is:

# jMathLab symbolic calculations

Below is a detailed description of symbolic calculations integrated into the Java/Jython code of SCaVis. Symbolic calculations somewhat depend on the engine.

Below we will discuss “jscl” engine and its syntax. Then we will discuss the “jasymca” engine used by the JMathLab. Note that latter engine is fully integrated with jMathLab which uses the Matlab/Octave syntax.

# Using Octave/Matlab

The “jasymca” engine mentioned below has its own life in the form of Matlab/Octave language which can use the jMathLab Shell integrated with SCaVis. So, instead of programing in Jython/Java, using the full-featured Matlab/Octave language. It should be noted that JMathLab can also be used without the full SCaVis framework.

Below we will describe how to perform analytic calculations using Java using the jMathLab (look at the tab below the main editor panel). This description is heavily based on the original work of H.Dersch. Unlike the previous approach, there is no integration with Java classes of SCaVis and you are bound to use Octave/Matlab scripts.

## Table of content

You can learn about jMathLab from jLearn portal. It is a part of SCaVis, but you can also use it as a stand-alone version from jLearn portal.

# Symbolic calculations using SymPy

SCaVis allows to use the SimPy package using Java VM, without installing Python. The main power is in the graphical canvases: one can use SymPy as usual for numerical calculations, but plot the output using advanced Java plotting canvases.

**Table of content**