{{announcement.body}}
{{announcement.title}}

# Symbolic Math Scripting With JShell

DZone 's Guide to

# Symbolic Math Scripting With JShell

### Want to learn how to use symbolic math scripting? Check out this tutorial on using math scripting from the Symja computer algebra in JShell.

· Java Zone ·
Free Resource

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

JShell is a Read-Evaluate-Print-Loop (REPL). In other words, it is a command line tool that allows you to enter Java statements (simple statements, compound statements, or even full methods and classes), evaluate them, and print the result. We'll go through an example that will show you how to use JShell to explore the Symja computer algebra library.

## Installation

Download the latest Symja release and unzip the files in  a sub-directory named /symja.

In this directory, you can run the symja-jshell.bat batch file under windows. Please adjust the JAVA_HOME path for your environment to a JDK installation greater equal than Java 9.

  SET "JAVA_HOME=C:\Program Files\Java\jdk-10.0.1"
"%JAVA_HOME%\bin\jshell" --class-path "lib/*" --startup start-symja.jsh

By running the BAT file, a start-symja.jsh default scripting file, with some additional imports, will be executed:

...
import org.matheclipse.core.expression.*;
import org.matheclipse.core.eval.*;
import org.matheclipse.core.interfaces.*;
import static org.matheclipse.core.expression.F.*;

With the static imports of the F.class, it's possible to use the symbols a,b,c,...x,y,z symbolically and to call functions like Integrate() or FactorInteger() and to omit the F. prefix in the scripting. With the predefined eval method, it's possible to evaluate a math string expressions, for example eval("D(Sin(x),x)").

## Example Shell Interactions

After starting JShell, you should see something like the following:

C:\temp\symja>symja-jshell
|  Welcome to JShell -- Version 10.0.1
|  For an introduction type: /help intro
jshell>

Now, you can input specific JShell commands or Java Symja API code (referred to as snippets). So, let's start with a simple expression:

jshell> eval("1+1")
$1 ==> 2 With this snippet, you can evaluate a partial derivative of Sin(x) with respect to x: jshell> D.of(Sin(x),x)$2 ==> Cos(x)

Integration of Sin(x)*Cos():

jshell> Integrate.of(Times(Sin(x),Cos(x)),x)
$3 ==> -Cos(x)^2/2 Determine the factors of the integer 324 (use ZZ to define integers, QQ to define rational numbers, and CC to define exact complex numbers): jshell> FactorInteger.of(ZZ(324))$4 ==> {{2,2},{3,4}}

Print the HornerForm   of the expression (x+y)^3:

jshell> HornerForm.of(Expand(Power(Plus(x,y),C3)))
$5 ==> x*(x*(x+3*y)+3*y^2)+y^3 Print the TeXForm of the expression Sum( f(n), {n, 1, m}): jshell> TeXForm.of(Sum(unary(f,n),List(n,C1,m)))$6 ==> \sum_{n = 1}^{m} {f(n)}

Print the internal rules used for the ArcTan function

jshell> Definition.of(ArcTan)
$7 ==> Attributes(ArcTan)={Listable,NumericFunction} ArcTan(Sqrt(5-2*Sqrt(5)))=Pi/5 ... ArcTan(0)=0 ArcTan(1,1)=Pi/4 ArcTan(Infinity)=Pi/2 ... Evaluate ArcTan(1,1) : jshell> ArcTan.of(C1,C1)$8 ==> Pi/4

Define a symbolic variable xx and assign x+y

jshell> ISymbol xx=Dummy("xx")
xx ==> xx

jshell> Set.of(xx, Plus(x,y))  // assign x+y
$10 ==> x+y jshell> Definition.of(xx)$11 ==> Attributes(xx)={}
xx=x+y

jshell> eval(xx)
$12 ==> x+y Print the documentation for the Im function: jshell> usage(Im)$13 ==>
## Im

Im(z)
> returns the imaginary component of the complex number z.

### Examples
>> Im(3+4I)
4

>> Im(0.5 + 2.3*I)
2.3

Evaluate this expression from the Java string input:

jshell> eval("D(Sin(x),x)")
\$14 ==> Cos(x)

If you're finished, exit JShell with the /exit JShell command:

jshell> /exit
|  Goodbye 
Topics:
algebra ,java ,java 10 ,java 9 ,jshell ,math ,tutorial

Comment (0)

Save
{{ articles[0].views | formatCount}} Views

Opinions expressed by DZone contributors are their own.