Groovy

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

TitleContentExcerptRevision Note
Groovy
<h1 id="SuanShuScriptinginGroovy"><a href="http://numericalmethod.com/up/suanshu/" 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="http://en.wikipedia.org/wiki/Groovy_%28programming_language%29"><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="http://numericalmethod.com/up/suanshu/" 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.

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

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

&nbsp;

<a href="http://numericalmethod.com/wp-content/uploads/2015/04/groovy_environment_variables.png"><img class="alignnone size-full wp-image-3804" src="http://numericalmethod.com/wp-content/uploads/2015/04/groovy_environment_variables.png" 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="http://numericalmethod.com/wp-content/uploads/2015/04/groovy_java_home.png"><img class="alignnone size-full wp-image-3805" src="http://numericalmethod.com/wp-content/uploads/2015/04/groovy_java_home.png" 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>
<ol start="7">
<li>Setup <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a>. Copy and paste the <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> jar files, e.g., "suanshu.jar" in .groovy/lib. Copy the <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> license file "suanshu.lic" into .groovy.</li>
</ol>
<a href="http://numericalmethod.com/wp-content/uploads/2015/04/groovy_folder.png"><img class="alignnone size-full wp-image-3808" src="http://numericalmethod.com/wp-content/uploads/2015/04/groovy_folder.png" alt="groovy_folder" width="947" height="751" /></a>

&nbsp;

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="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> license management will look for the license file in the current directory.

<a href="http://numericalmethod.com/wp-content/uploads/2015/04/groovy_startin_folder.png"><img class="alignnone size-full wp-image-3809" src="http://numericalmethod.com/wp-content/uploads/2015/04/groovy_startin_folder.png" 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’.

&nbsp;

println hi

<a href="http://numericalmethod.com/wp-content/uploads/2015/04/groovy_shell.png"><img class="alignnone size-full wp-image-3810" src="http://numericalmethod.com/wp-content/uploads/2015/04/groovy_shell.png" alt="groovy_shell" width="757" height="532" /></a>

&nbsp;
<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>
</ol>
<a href="http://numericalmethod.com/wp-content/uploads/2015/04/groovy_shell_properties.png"><img class="alignnone size-full wp-image-3811" src="http://numericalmethod.com/wp-content/uploads/2015/04/groovy_shell_properties.png" alt="groovy_shell_properties" width="767" height="520" /></a>

&nbsp;
<h2 id="Tutorials">Tutorials</h2>
The best way to learn a language is by imitation and practice. We have prepared some <a href="http://numericalmethod.com/up/scripting/groovy/examples/" target="_blank">examples</a> to get you started on scripting <a href="http://numericalmethod.com/up/suanshu/" 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

com.numericalmethod.suanshu.matrix.doubles.dense.DenseMatrix

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="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> users to include these <a href="http://redmine.numericalmethod.com/projects/public/repository/entry/Groovy/groovysh.rc" target="_blank">imports</a>. You can simply copy and paste to append them in your groovysh.rc.

&nbsp;

&nbsp;
<h2 id="References">References</h2>
<ul>
<li><a class="ext-link" href="http://groovy.codehaus.org/"><span class="icon">​</span>Groovy</a></li>
<li><a class="ext-link" href="http://groovy.codehaus.org/Tutorial+1+-+Getting+started"><span class="icon">​</span>Groovy setup guide</a></li>
<li><a class="ext-link" href="http://groovy.codehaus.org/Documentation"><span class="icon">​</span>Groovy documentation</a></li>
<li><a class="ext-link" href="http://www.manning.com/koenig/"><span class="icon">​</span>Groovy in Action</a></li>
</ul>



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