Groovy
SuanShu Scripting in Groovy
We can make Groovy into one all-powerful math computing environment for numerical analysis.
Groovy is an object-oriented programming language for the Java platform. It is dynamically compiled to Java Virtual Machine bytecode and therefore inter-operates with SuanShu and other Java libraries. Most Java code is also syntactically valid Groovy. Groovy uses a Java-like but simplified syntax. For example, Groovy syntax removes type (dynamic type), semicolons (optional), return keyword (optional). If you already know Java, the learning effort will be minimal. Therefore, Groovy is a popular scripting language for the Java platform.
Setup
This section describes how to set up in Groovy to use SuanShu to do numerical computing.
- You need a copy of suanshu.jar and a valid SuanShu license. You may download and purchase our products here.
- Download the latest stable version of Groovy from the Groovy website.
- Run the installer. When choosing the destination folder, please use one that contains no space and special characters. We recommend for example “C:\Groovy\Groovy-1.7.5”.
4. Configure the environment variables as in the picture below.
5. Set up the environment variable JAVA_HOME to point to where your JDK is installed. For example, “C:\Program Files (x86)\Java\jdk1.6.0_20”.
- Create the Groovy ${user.home} folder. The user.home system property is set by your operating system. For example, it can be C:\Users\Haksun Li on Vista, or C:\Document and Settings\Haksun Li on XP. Create the folder “.groovy”. Please note that when you rename on Windows, you need to type “.groovy.” (a “.” at the end) otherwise Windows will complain (a bug?). Then create the “lib” folder in “.groovy”.
- Setup SuanShu. Copy and paste the SuanShu jar files, e.g., “suanshu.jar” in .groovy/lib. Copy the SuanShu license file “suanshu.lic” into .groovy.
8. Modify the Groovy shortcuts to start in the .groovy folder. I created two shortcuts on desktop: one for the Groovy shell (groovysh.exe) and another for the Groovy Console (GroovyConsole). The start-in folder is the current/working directory for Groovy. The SuanShu license management will look for the license file in the current directory.
9. Congratulations! You are now set up with Groovy and ready to go. You can start up the Groovy shell by double clicking on groovysh.exe (or the shortcut you created). Type this to say ‘hi’.
println hi
- You may modify the properties on the shell screen by clicking on the Groovy icon at the upper left hand corner. I changed mine to white background and black font.
Tutorials
The best way to learn a language is by imitation and practice. We have prepared some examples to get you started on scripting SuanShu in Groovy. You may want to consult the references below to learn more about Groovy.
import
For those converting from the Matlab/Octave/R/Scilab, the hardest thing to get used to is “import”. To use any Java class in Groovy, we can either type the fully qualified name such as
com.numericalmethod.suanshu.matrix.doubles.dense.DenseMatrix
We need to do this every time we want to create a DenseMatrix. This is very cumbersome.
Alternatively, we can declare the namespace of DenseMatrix by “importing” them once for all.
import com.numericalmethod.suanshu.matrix.doubles.dense.DenseMatrix
Or, we can do
import com.numericalmethod.suanshu.matrix.doubles.dense.*
to import all classes in the package import com.numericalmethod.suanshu.matrix.doubles.dense.
Although the keyword “import” saves some typing, it can get cumbersome if you need to import many classes. The Groovy shell partially solves this problem by letting users to put the most often used imports in the shell startup script, “groovysh.rc”. This file is found in the .groovy folder (or you can create one in the folder).
We recommend the SuanShu users to include these imports. You can simply copy and paste to append them in your groovysh.rc.