One of the things I like from Jama is their useful methods and its Java code easy to follow and understand. The Matrix printing method is something you will find using very often to give a nice output to your matrix operations.

By default, most of the Java matrix packages will give you something like this:

RealMatrixImpl{{1.0,2.0,3.0},{2.0,5.0,3.0}}

while the Jama print method will send out:
1.00 2.00 3.00
2.00 5.00 3.00

Neat, isn't it?

Below, the example. Enjoy!

**Note**: Download the .jar libraries from these links and add them to your .../jre/lib/ext folder or your classpath.

Apache Commons Math

Jama
# Demo of the simultaneous use of Apache-Commons-Math and Jama
# in arrays and matrix operations
from org.apache.commons.math.linear import *
import Jama
# Create a real matrix with two rows and three columns
arrayA = ( [1, 2, 3], [2, 5, 3]); # a Python arrays
matrixA = RealMatrixImpl(arrayA); # convert array to matrix
print matrixA
print matrixA.getColumnDimension() # gets # of columns -> 3
# One more array with three rows, two columns
arrayB = ( [1, 2], [2, 5], [1, 7]); # Python array
matrixB = RealMatrixImpl(arrayB); # converted to matrix
print matrixB
# Note: The constructor copies the input double[][] array.
# Now multiply A x B
p = matrixA.multiply(matrixB);
print "p = ", p
print "rows p =", p.getRowDimension(); # 2
print "columns p = ", p.getColumnDimension(); # 2
# Invert p
pInverse = p.inverse();
print "Inverse(p) = ", pInverse
print "Doesn't look nice, does it?"
print "Let's use Jama for matrix printing ...\n"
# we use Jama to get a nice printing of the matrix
JmatrixA = Jama.Matrix(arrayA)
print "matrixA",
JmatrixA.print(5, 2) # 4 places, 2 decimals
JmatrixB = Jama.Matrix(arrayB)
print "matrixB",
JmatrixB.print(5, 2) # 4 places, 2 decimals
JpInverse = Jama.Matrix(pInverse.getData())
print "Inverse(p)",
JpInverse.print(6, 3) # 6 places, 3 decimals

View this message in context: Apache-Commons-Math and Jama working together. Matrix & array example in Jython

Sent from the jython-users mailing list archive at Nabble.com.