Revision for “Groovy” created on April 14, 2015 @ 07:36:14

TitleContentExcerptRevision Note
<h1 id="SuanShuScriptinginGroovy"><a href="" target="_blank">SuanShu</a> Scripting in Groovy</h1>
We can make Groovy into one all-powerful math computing environment for numerical analysis.

<a class="ext-link" href=""><span class="icon">​</span>Groovy</a> is an object-oriented programming language for the Java platform. It is dynamically compiled to Java Virtual Machine bytecode and therefore inter-operates with <a href="" target="_blank">SuanShu</a> 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.

<h2 id="Setup">Setup</h2>
This section describes how to set up in Groovy to use <a href="" target="_blank">SuanShu</a> to do numerical computing.
<li>You need a copy of suanshu.jar and a valid <a href="" target="_blank">SuanShu</a> license. You may download and purchase our products <a class="ext-link" href=""><span class="icon">​</span>here</a>.</li>
<ol start="2">
<li>Download the latest stable version of Groovy from the <a class="ext-link" href=""><span class="icon">​</span>Groovy website</a>, <a class="ext-link" href=""><span class="icon">​</span>here</a>.</li>
<ol start="3">
<li>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".</li>
<a href=""><img class="alignnone size-full wp-image-3802" src="" alt="groovy_destination_folder" width="513" height="400" /></a>

4.  Configure the environment variables as in the picture below.


<a href=""><img class="alignnone size-full wp-image-3804" src="" alt="groovy_environment_variables" width="513" height="400" /></a>

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".

<a href=""><img class="alignnone size-full wp-image-3805" src="" alt="groovy_java_home" width="1236" height="825" /></a>
<ol start="6">
<li>Create the Groovy ${user.home} folder. The user.home system property is set by your operating system. For example, it can be <tt>C:\Users\Haksun Li</tt> on Vista, or <tt>C:\Document and Settings\Haksun Li</tt> 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".</li>
<ol start="7">
<li>Setup <a href="" target="_blank">SuanShu</a>. Copy and paste the <a href="" target="_blank">SuanShu</a> jar files, e.g., "suanshu.jar" in .groovy/lib. Copy the <a href="" target="_blank">SuanShu</a> license file "suanshu.lic" into .groovy.</li>
<a href=""><img class="alignnone size-full wp-image-3808" src="" alt="groovy_folder" width="947" height="751" /></a>


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 <a href="" target="_blank">SuanShu</a> license management will look for the license file in the current directory.

<a href=""><img class="alignnone size-full wp-image-3809" src="" alt="groovy_startin_folder" width="386" height="532" /></a>

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

<a href=""><img class="alignnone size-full wp-image-3810" src="" alt="groovy_shell" width="757" height="532" /></a>

<ol start="10">
<li>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.</li>
<a href=""><img class="alignnone size-full wp-image-3811" src="" alt="groovy_shell_properties" width="767" height="520" /></a>

<h2 id="Tutorials">Tutorials</h2>
The best way to learn a language is by imitation and practice. We have prepared some <a href="" target="_blank">examples</a> to get you started on scripting <a href="" target="_blank">SuanShu</a> in Groovy. You may want to consult the references below to learn more about Groovy.
<h3 id="import">import</h3>
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


We need to do this every time we want to create a <tt>DenseMatrix</tt>. This is very cumbersome.

Alternatively, we can declare the namespace of <tt>DenseMatrix</tt> 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 <tt>import com.numericalmethod.suanshu.matrix.doubles.dense</tt>.

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 <a href="" target="_blank">SuanShu</a> users to include these <a href="" target="_blank">imports</a>. You can simply copy and paste to append them in your groovysh.rc.


<h2 id="References">References</h2>
<li><a class="ext-link" href=""><span class="icon">​</span>Groovy</a></li>
<li><a class="ext-link" href=""><span class="icon">​</span>Groovy setup guide</a></li>
<li><a class="ext-link" href=""><span class="icon">​</span>Groovy documentation</a></li>
<li><a class="ext-link" href=""><span class="icon">​</span>Groovy in Action</a></li>

Old New Date Created Author Actions
April 14, 2015 @ 07:36:14 sanjay
April 14, 2015 @ 07:35:30 [Autosave] sanjay
April 13, 2015 @ 12:28:40 sanjay
April 13, 2015 @ 12:19:44 sanjay
April 13, 2015 @ 12:18:41 sanjay
April 13, 2015 @ 11:57:30 sanjay