I was feeling the need to exercise my rusty array learnings from the university when we used to do this with Pascal and Fortran and decided to evaluate the Java array libraries available. One of them is JAMA, a very powerful and simple to use matrix manipulation package.

I chose Jython instead of Java (read also Python) because Jython is fun to use and don't need to declare "everything" to perform simple operations - it is a roll-your-sleeves kind of language. Don't misunderstand me, you have to know Java if want the get the best juice out of all.

Though I didn't ported the Jama (http://math.nist.gov/javanumerics/jama/) example for speed racing purposes (Jython vs Java), for those interested below are my results. Try it at home.

Jython 0.407 s
Java 0.203 s

Unless for me, Jython is not about speed, it's about fast development and prototyping.
The Jython code here: """ MagicSquareExample.py Written by NIST for Jama (Java Matrix) test. http://math.nist.gov/javanumerics/jama/ Ported to Jython by: Alfonso Reyes, September 2007 """ from java.util import Date; from java.lang import Math from Jama import *; # Jama linear matrix library from java.lang.reflect import Array # will allow craeting java arrays import java class magic(object): def __new__(cls, n=5): """ the __new__ static method is called before __init__ and can return a value while __init__ can't. cls: it's the class as 1st argument n: is the size of the square matrix Returns: a new instance of the class or object """ # Not, let's create a Java array instance, two-dimensions # it could be more if you want. M = Array.newInstance(java.lang.Double.TYPE, [n,n] ) # Odd order if ((n % 2) == 1): a = (n+1)/2; b = (n+1); for j in range(0,n,1): for i in range(0,n,1): M[i][j] = n*((i+j+a) % n) + ((i+2*j+b) % n) + 1; # Doubly Even Order elif ((n % 4) == 0): for j in range(0,n,1): for i in range(0,n,1): if (((i+1)/2)%2 == ((j+1)/2)%2): M[i][j] = n*n-n*i-j; else: M[i][j] = n*i+j+1; # Singly Even Order else: p = n/2; k = (n-2)/4; A = magic(p); for j in range(0,p,1): for i in range(0,p,1): aij = A.get(i,j); M[i][j] = aij; M[i][j+p] = aij + 2*p*p; M[i+p][j] = aij + 3*p*p; M[i+p][j+p] = aij + p*p; for i in range(0,p,1): for j in range(0,k,1): t = M[i][j]; M[i][j] = M[i+p][j]; M[i+p][j] = t; for j in range(n-k+1,n,1): t = M[i][j]; M[i][j] = M[i+p][j]; M[i+p][j] = t; t = M[k][0]; M[k][0] = M[k+p][0]; M[k+p][0] = t; t = M[k][k]; M[k][k] = M[k+p][k]; M[k+p][k] = t; return Matrix(M) # return a Jama java matrix def fixedWidthDoubletoString(x, w, d): fmt = java.text.DecimalFormat(); fmt.setMaximumFractionDigits(d); fmt.setMinimumFractionDigits(d); fmt.setGroupingUsed(False); s = fmt.format(x); while (len(s) < w): s = " " + s; return s; def fixedWidthIntegertoString(n, w): s = str(n) #Integer.toString(n); while (len(s) < w): s = " " + s; return s; if __name__ == '__main__': print "Testing first the nice print method of Matrix" M = magic(5); # test first this nice Jama print method M.print(6,3) print "\n Test of Matrix Class, using magic squares.\n", print " See MagicSquareExample.main() for an explanation.\n", print "\n n trace max_eig rank cond lu_res qr_res\n\n", start_time = Date(); eps = Math.pow(2.0,-52.0); for n in range(3,32+1,1): print fixedWidthIntegertoString(n,7), M = magic(n) t = int(M.trace()) print fixedWidthIntegertoString(t,10), E = EigenvalueDecomposition(M.plus(M.transpose()).times(0.5)); d = E.getRealEigenvalues(); print fixedWidthDoubletoString(d[n-1],14,3), r = M.rank(); print fixedWidthIntegertoString(r,7), c = M.cond(); if (c < 1/eps ): print fixedWidthDoubletoString(c,12,3), else: print " Inf", LU = LUDecomposition(M); L = LU.getL(); U = LU.getU(); p = LU.getPivot(); R = L.times(U).minus(M.getMatrix(p,0,n-1)); res = R.norm1()/(n*eps); print fixedWidthDoubletoString(res,12,3), QR = QRDecomposition(M); Q = QR.getQ(); R = QR.getR(); R = Q.times(R).minus(M); res = R.norm1()/(n*eps); print fixedWidthDoubletoString(res,12,3), print "\n", stop_time = Date(); etime = (stop_time.getTime() - start_time.getTime())/1000.; print "\nElapsed Time = " , fixedWidthDoubletoString(etime,12,3), " seconds\n" print "Adios\n" This is the Java version.
MagicSquareExample.java: Cheers! Alfonso Reyes Houston, Texas

View this message in context: Using JAMA arrays with Jython. Jython examples
Sent from the jython-users mailing list archive at Nabble.com.