DMelt:Programming/5 Groovy

From HandWiki
(Redirected from DMelt:Programming/Groovy)
Member

Using Groovy

You can program in Groovy language as well. Read Groovy (programming language) Groovy (programming language). Groovy gets its attention because is designed to work seamlessly with the JVM, unlike Jython. One important advantage of Groovy scripting is that programs (especially loops) implemented in this language are 3-4 times faster than those in Jython, and much faster than in BeanShell.

Here is the Groovy tutorial Beginners Tutorial

You can use either Groovy Console (from the Run menu) or editor. In case of the editor, do not forget to use the extension ".groovy" ".gvy" or ".gy".

First, let us show how to make a standard Java JFrame:

import javax.swing.JFrame
frame = new JFrame("Hello Swing")

Groovy and DataMelt

Let us show how to work with scientific libraries using Groovy.

Let us rewrite Jython/Python example of how to create a histogram and shown it using Groovy language.

First of all, one can use interactive Groovy shell of the IDE for (in the menu [Run]) or jut typing a code in the editor and saving it with the extension .gy or .groovy.

import jhplot.HPlot
import jhplot.H1D

c1=new HPlot()
c1.setGTitle("Global labels: F_{2},  x_{γ}  #bar{p}p F_{2}^{c#bar{c}}");
c1.visible()
c1.setAutoRange()

h1 = new H1D("Simple1",100, -2, 2.0)
rand =new java.util.Random()

print "Use of for loop in Groovy \n"
for (int i=0; i<100; i++) {
   h1.fill(rand.nextGaussian())
   }

c1.draw(h1)

Create a file "example.gy" with this code (the extension ".gy" is very important!) and run this script as usual. To run this script, use the Run button Running dmelt.png on the toolbar of DataMelt. One can also use the [F8] key for fast execution of a script. In case of an error, the DataMelt outputs error to the Output console.

Java class imports

It should be noted that many packages are imported by Groovy automatically:

  • java.lang
  • java.io
  • java.math
  • java.net
  • java.util
  • groovy.lang
  • groovy.util

Converting Jython example to Groovy examples

Since most of DataMelt example are written in Jython/Python, here are a few suggestions how to convert Jython code to Groovy scripts:

The largest differences are in the loop definitions. Instead of Jython "range", use the standard java syntax in Groovy:

for (i in 0..len-1) {...}

or

for (i in 0..len-1) {...}



Groovy and DataMelt libraries

Groovy scripts, if the run inside DataMelt, access to all numerical and scientific libraries of DataMelt. Here is one example:

import org.jscience.mathematics.number.Complex
import static org.jscience.mathematics.number.Complex.I
import static org.jscience.mathematics.number.Complex.valueOf as c
import org.jscience.mathematics.function.Polynomial
import static org.jscience.mathematics.function.Polynomial.valueOf as p
import org.jscience.mathematics.function.Variable

'' Defines two local variables (x, y).
def varX = new Variable.Local<Complex>("x")
def varY = new Variable.Local<Complex>("y")

use (JScienceCategory) {
    def ONE = Complex.ONE
    def TWO = c(2, 0)
    '' f(x) = ix?? + 2x + 1
    def x = p(ONE, varX)
    def fx = I * x ''' 2 + TWO * x + ONE
    println fx
    println fx ''' 2
    println fx.differentiate(varX)
    println fx.integrate(varY)
    println fx.compose(fx)

    '' Calculates expression.
    varX.set(c(2, 3))
    println fx.evaluate()
}

class JScienceCategory {
    static power(Polynomial p, int n) {
        p.pow(n)
    }
    static multiply(Complex c, Polynomial p) {
        p.times(c)
    }
    static multiply(Polynomial p, Complex c) {
        p.times(c)
    }
}

The output is:

[0.0 + 1.0i]x?? + [2.0 + 0.0i]x + [1.0 + 0.0i]
[-1.0 + 0.0i]x4 + [0.0 + 4.0i]x?? + [4.0 + 2.0i]x?? + [4.0 + 0.0i]x + [1.0 + 0.0i]
[0.0 + 2.0i]x + [2.0 + 0.0i]
[0.0 + 1.0i]x??y + [2.0 + 0.0i]xy + [1.0 + 0.0i]y
[0.0 - 1.0i]x4 + [-4.0 + 0.0i]x?? + [-2.0 + 6.0i]x?? + [4.0 + 4.0i]x + [3.0 + 1.0i]
-7.0 + 1.0i