# When java.lang.Math isn’t enough: math on the JVM

Why would anyone want to perform serious calculations on the JVM? It’s not
exactly known for being *the* platform for number crunching. Despite getting
gradually better it’s surely slower than compiled languages, and besides, the
scope of
`java.lang.Math`

is
very limited.

But then, there are not many computer programs that wouldn’t calculate at least something (images and sounds in a computer consist of numbers and calculations). Even so called “scientific math” is often just about modest curve-fitting, filtering, statistics, plots — not exactly supercomputing but something that you would do with GNU Octave, for instance. And if you’re already working on the JVM, then… why not?

One possible (coarse) categorization for math related tools could be like this:

- Math libraries — someone else has already done most of the hard stuff for you.
- Plot libraries — plain numbers are dull, we want to see something!
- Storage — how to store all these vectors, matrices, functions, plots…?
- Packages that combine some of the above to form some kind of mix of an application and a programming environment, like GNU Octave.

On this post we’ll have a look at some math libraries and my favorite 2D plotting library. We’ll also have a quick look at two Clojure-based computing environments. Three-dimensional plots and storage alternatives are left for some other time.

## What we have

The “scientific Java community” has obviously had difficulties working together, and as a result we have a plethora of various libraries that do more or less the same thing — and still, none of them is usually sufficient alone. Just to name a few of the hotchpotch:

- Colt
- A Java library developed at CERN. Includes various linear algebra and statistical tools. It also has a parallel version available, which is being used in the core of the Incanter project, a Clojure-based computing environment.
- JAMA
- A basic linear algebra package.
- JSci
- Includes linear algebra, statistics, mechanics, wavelets and graph/chart features.
- JScience
- An ambitious attempt to provide the most comprehensive Java library for the scientific community and to create a common API for all sciences to core JDK. Unfortunately the JSR 275 was rejected and the project seems to have frozen in 2007, but regardless there’s quite good and interesting stuff, like units and conversions between them.
- Mantissa
- A collection of various mathematical tools aimed towards for simulation. Contains linear algebra, optimization, integrator, vector, and polynomial algorithms.
- Michael Thomas Flanagan’s Java Scientific Library
- Quite original library, includes diverse tools from plotting and file I/O to error propagation, interpolation and simulation classes.

A lot more libraries are listed for instance here: http://math.nist.gov/javanumerics/

From a potential user’s viewpoint this diversity is unfortunate. To build anything serious you would definitely prefer to pick something that is likely to be maintained in the future, instead of a bunch of partially overlapping one-man projects that don’t even play together, no matter how excellent they may be alone. In that case, I would pick Apache Commons Math and complement it with JFreeChart.

## Apache Commons Math

The recently released Apache Commons Math 2.0 is a delightful step forward in the world of unified Java mathematics. It contains lots of features that were missing from the previous versions, and it’s definitely the most complete (free) single package for the JVM available today. It includes:

- Statistics (distributions, regressions, covariance & correlation, tests…)
- Data Generation (random numbers and sequences, probability distributions…)
- Linear Algebra (matrices, vectors, operations & decompositions, systems…)
- Numerical Analysis (root-finding, integration, interpolation, polynomials…)
- Special Functions (error, gamma, beta…)
- Utilities (resizable arrays, hash maps, continued fractions, binomial coefficients…)
- Complex Numbers (utilities, transcendental functions…)
- Transform Methods (Fourier, cosine, sine, Hadamard…)
- 3D Geometry (vectors, rotations…)
- Optimization (univariate & multivariate, scalar & vector, linear & nonlinear…)
- Distributions, Fractions, Ordinary Differential Equations, Genetic Algorithms…

Unlike most of the similar libraries out there, this is a live and active project — quite likely becoming the de facto standard of its kind.

## JFreeChart

JFreeChart is quite possibly the best 2D charting / plotting library out there (on any platform). It can render credible plots for scientific publications as well as financial pie charts to delight your CEO. Its only “problem” is that it’s almost too flexible: you might find yourself writing simple wrappers for the functionality you use most, to avoid typing the same boilerplate code repeatedly.

A remarkable thing is that most of JFreeChart is a work by a single individual, David Gilbert. I strongly encourage everyone to support him by purchasing a manual and sample codes. The manual is very good (750+ pages pdf) and the sample codes are well worth the money.

## Incanter

Incanter is a R-like statistical computing and graphics environment for the JVM. It links Clojure to the Parallel Colt numerics library, the JFreeChart charting library, the Processing visualization library, as well as several other Java and Clojure libraries.

Looks like a pretty impressive package for any statistical needs!

## Clojure + Mathematica = Clojuratica

Clojuratica is an interface between Clojure and Wolfram Mathematica. It features:

- Translation of native data structures between Clojure and Mathematica.
- A way to evaluate Mathematica expressions from Clojure.
- A syntax for accessing Clojure from Mathematica.
- A way to call, pass, and store Mathematica functions just as though they were first-class functions in Clojure.

The canonical pronunciation of *Clojuratica* starts with *Clojure* and rhymes
with *erotica*. Pretty crazy, huh? The only problem is that you need Mathematica
to do anything with it.

## Afterwords

For some good discussion on Clojure’s scientific applications, see this thread: http://groups.google.com/group/clojure/browse_thread/thread/1957b44c95c99778

Last modified: 2009-11-01 21:05 +0200blog comments powered by Disqus