Run Java or Scala code with CERN colt library

The CERN colt libraries provide a smashing way to complete some mathematical tasks with Java and therefore Scala too.

The following example works for java:

import cern.colt.matrix.DoubleFactory2D;
import cern.colt.matrix.DoubleMatrix2D;
import cern.colt.matrix.impl.DenseDoubleMatrix2D;
import cern.colt.matrix.linalg.Algebra;

class Matrix {
        public static void main(String[] args) {
                DenseDoubleMatrix2D matrix;
                matrix = new DenseDoubleMatrix2D(3,4);
                matrix.set(0,2, 0.001);
                DoubleMatrix2D mtrans;
                mtrans = Algebra.DEFAULT.transpose( matrix );

                System.out.println( "\n/* A normal matrix */\n" + matrix );
                System.out.println( "\n\n/* Transposed matrix */\n" + mtrans + "
\n" );
        }
}

This works as a scala script

import cern.colt.matrix.DoubleFactory2D;
import cern.colt.matrix.DoubleMatrix2D;
import cern.colt.matrix.impl.DenseDoubleMatrix2D;
import cern.colt.matrix.linalg.Algebra;

val m = new DenseDoubleMatrix2D(3,4)
m.set(0, 2, 0.1)

val mt = Algebra.DEFAULT.transpose( m )
println( "\n/* A normal matrix */\n" + m )
println( "\n\n/* Transposed matrix */\n" + mt + "\n")

The java code can then be compiled and run using the following lines:

javac -cp /opt/colt/lib/colt.jar:/opt/colt/lib/concurrent.jar Matrix.java
java Matrix

or run using scala:

scala -cp /opt/colt/lib/colt.jar:/opt/colt/lib/concurrent.jar matrix.scala

The output should look something like this:

The starting matrix:
3 x 4 matrix
0 0 0.1 0
0 0 0   0
0 0 0   0

Transposed:
4 x 3 matrix
0   0 0
0   0 0
0.1 0 0
0   0 0
Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s