View Full Version : Groovy and JMSL

06-03-2008, 08:58 AM
This topic came up again briefly this morning, so I figured I'll post something quickly on it. I have played around with Groovy a little (others in the company more so), but find it a very interesting environment. Being so closely integrated with Java, it's quite easy to utilize the JMSL Library from the Groovy framework.

While I haven't written any real applications, using the GroovyConsole I have successfully called JMSL functions. On the installation side, it's easiest to copy the jmsl.jar archive into the groovy/lib directory so it is automatically included in the classpath. A system-wide CLASSPATH variable would also work. Then you can start up the GroovyConsole application and try some things out.

For example, my favorite basic starting point of calling a static function is the Error Function in the special functions class of the math package. In Groovy, this is as simple as:

import com.imsl.math.Sfun

which gives the output as:

groovy> import com.imsl.math.Sfun
groovy> println(Sfun.erf(0.5))


Couldn't be simpler! Stepping up to actually creating an object is just as easy. I'm sure some of this could be written more Groovy-like instead of Java-like, but the point is that it all works:

double[][] a = [[1, 3, 3],[1, 3, 4],[1, 4, 3]]
double[] b = [12, 13, 14]
import com.imsl.math.*
lu = new LU(a)
x = lu.solve(b)
//new PrintMatrix("x").print(x)
println("x = " + x)
ainv = lu.inverse()
new PrintMatrix("a inv").print(ainv)

And this generates the following output (ignoring the echo of the statements in the script). Using a 'code' tag here to retain the formatting of the output matrix.

x = [3.000000000000001, 2.0, 1.0]
a inv
0 1 2
0 7 -3 -3
1 -1 0 1
2 -1 1 0

And so there you have it. Get the jmsl.jar file on the classpath and import packages as usual.

06-04-2008, 07:58 AM
Charting can be used easily as well. Here we use the Groovy SwingBuilder along with a JMSL JPanelChart to create a pie chart with colors and labels. The code is still pretty Java-ish, but I'm slowly learning Groovy. The i++ inside the each() block doesn't feel needed, but I don't see how else you'd index into the colors and titles lists.

And I should mention this is adapted from the JMSL PieChart example (http://www.vni.com/products/imsl/jmsl/v50/manual/api/com/imsl/chart/PieEx1.html) and from an example on the Groovy Cookbook Examples (http://groovy.codehaus.org/Cookbook+Examples) page.

import com.imsl.chart.*
import groovy.swing.SwingBuilder
import java.awt.*
import javax.swing.WindowConstants as WC

// build the data
double[] y = [10, 20, 30, 40]
def colors = [Color.red, Color.blue, Color.green, Color.yellow]
def titles = ["Fish", "Pork", "Poultry", "Beef"]

// create the chart objects
def chart = new Chart()
def pie = new Pie(chart, y)
slice = pie.getPieSlice()

// set the slice properties
i = 0
slice.each() {

// build the gui and show it
def swing = new SwingBuilder()
def jPanelChart = new JPanelChart(chart)
jPanelChart.setPreferredSize(new Dimension(500,500))
def frame = swing.frame(title:'Groovy PieChart',
defaultCloseOperation:WC.DISPOSE_ON_CLOSE) {
panel(id:'canvas') { widget(jPanelChart) }