|
From: Alfonso R. <ms...@re...> - 2007-10-14 12:34:44
|
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/ 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. http://www.nabble.com/file/p13198679/MagicSquareExample.java MagicSquareExample.java : Cheers! Alfonso Reyes Houston, Texas -- View this message in context: http://www.nabble.com/Using-JAMA-arrays-with-Jython.-Jython-examples-tf4621603.html#a13198679 Sent from the jython-users mailing list archive at Nabble.com. |
|
From: Steve L. <lis...@ar...> - 2007-10-15 15:59:59
|
Hey Alfonso, On Oct 14, 2007, at 8:34 AM, Alfonso Reyes wrote: > 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. That's pretty neat. I often wonder if it would be worth it to port something like NumPy over to Jython w/ one of these java ndarray backends. I've been using the colt library for some time (http:// dsd.lbl.gov/~hoschek/colt/) in Java land and find it to be quite nice. Has anybody else found "the need" to undertake a project like this? (Some googling actually turned up this issue at the Astronomy BOF @ the SciPy conference this year (http://www.google.com/notebook/public/ 00116375172106219610/BDQGkIwoQzuK738Yi )) I'm not sure if there's a big payoff in doing this, though. The IPython+numpy/scipy+matplotlib combo is great, but I'm not sure what you'd get from porting to jython, since its pretty well self contained in and of itself. Thoughts? -steve |
|
From: Alfonso R. <ms...@re...> - 2007-10-16 02:21:18
|
Steve, maybe there is no need to port NumPy or SciPy to Jython. I have found that Java can surprise us with a vast ocean of mathematical and scientific libraries. The only thing we have to do is add them to our classpath and done. Well ..., there is some work to do which is the "how-to-use" the libraries in the Pythonic manner. That's why I am taking the work of "porting" the calls to the Java libraries. I am not actually creating a new library - it is more a how-to use that library. Regarding the combo IPython+numpy/scipy+matplotlib, yes, it really does its work on the engineering calculations and plotting. I worked with and researched it for some months. But before starting development of a scientific application I saw that the users were more comfortable running applets and java applications (clicking on the .jar file or the .jnlp. That meant that I had to write the application in Java which I didn't want to do after being at the Python universe. So, how to get together the best of both worlds: Jython. To finish, my reasons for parallel and concurrent use of Python and Java (=Jython): Python - organized, structured and clean syntaxis (the use of indentation was a very clever invention). - intuitive and natural language: can use it to teach my kids programming - can't imagine any other language to teach object oriented programming - it's fun to program with it: non-declarative, easier to detect bugs Java - the swing GUI libraries are a perfect match for Python: they look (and work) so Pythonic - many, many libraries out there to use - lots of engineering and scientific applications we can learn of; lots of code - front-end familiar and friendly to the use: applets, .jnlp, .jar executables - liked by IT guys (you know, seem to panic to anything that carries .exe) Alfonso Reyes Houston, TX ***************************** Steve Lianoglou-2 wrote: > > Hey Alfonso, > > On Oct 14, 2007, at 8:34 AM, Alfonso Reyes wrote: > >> 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. > > That's pretty neat. I often wonder if it would be worth it to port > something like NumPy over to Jython w/ one of these java ndarray > backends. I've been using the colt library for some time (http:// > dsd.lbl.gov/~hoschek/colt/) in Java land and find it to be quite nice. > > Has anybody else found "the need" to undertake a project like this? > (Some googling actually turned up this issue at the Astronomy BOF @ > the SciPy conference this year (http://www.google.com/notebook/public/ > 00116375172106219610/BDQGkIwoQzuK738Yi > )) > > I'm not sure if there's a big payoff in doing this, though. The > IPython+numpy/scipy+matplotlib combo is great, but I'm not sure what > you'd get from porting to jython, since its pretty well self > contained in and of itself. > > Thoughts? > > -steve > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > _______________________________________________ > Jython-users mailing list > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users > > -- View this message in context: http://www.nabble.com/Using-JAMA-arrays-with-Jython.-Jython-examples-tf4621603.html#a13225691 Sent from the jython-users mailing list archive at Nabble.com. |
|
From: Simon G. <sga...@cs...> - 2007-10-16 02:37:59
|
I've done a little work on this with the colt distribution and jython.. Not too tricky.. the hardest part is if you want to index by 1 (e.g. matlab) or 0 (java).. My end goal is to sort of create a matlab-esque clone for my specific area... but I agree the applet way is a great way to go.. Pieces of this could be open sourced BSD style if we want to collaborate.. _simon Alfonso Reyes wrote: > Steve, > maybe there is no need to port NumPy or SciPy to Jython. > I have found that Java can surprise us with a vast ocean of mathematical and > scientific libraries. The only thing we have to do is add them to our > classpath and done. Well ..., there is some work to do which is the > "how-to-use" the libraries in the Pythonic manner. That's why I am taking > the work of "porting" the calls to the Java libraries. I am not actually > creating a new library - it is more a how-to use that library. > > Regarding the combo IPython+numpy/scipy+matplotlib, yes, it really does its > work on the engineering calculations and plotting. I worked with and > researched it for some months. But before starting development of a > scientific application I saw that the users were more comfortable running > applets and java applications (clicking on the .jar file or the .jnlp. That > meant that I had to write the application in Java which I didn't want to do > after being at the Python universe. So, how to get together the best of both > worlds: Jython. > > To finish, my reasons for parallel and concurrent use of Python and Java > (=Jython): > Python > - organized, structured and clean syntaxis (the use of indentation was a > very clever invention). > - intuitive and natural language: can use it to teach my kids programming > - can't imagine any other language to teach object oriented programming > - it's fun to program with it: non-declarative, easier to detect bugs > > Java > - the swing GUI libraries are a perfect match for Python: they look (and > work) so Pythonic > - many, many libraries out there to use > - lots of engineering and scientific applications we can learn of; lots of > code > - front-end familiar and friendly to the use: applets, .jnlp, .jar > executables > - liked by IT guys (you know, seem to panic to anything that carries .exe) > > > Alfonso Reyes > Houston, TX > ***************************** > > > > Steve Lianoglou-2 wrote: > >> Hey Alfonso, >> >> On Oct 14, 2007, at 8:34 AM, Alfonso Reyes wrote: >> >> >>> 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. >>> >> That's pretty neat. I often wonder if it would be worth it to port >> something like NumPy over to Jython w/ one of these java ndarray >> backends. I've been using the colt library for some time (http:// >> dsd.lbl.gov/~hoschek/colt/) in Java land and find it to be quite nice. >> >> Has anybody else found "the need" to undertake a project like this? >> (Some googling actually turned up this issue at the Astronomy BOF @ >> the SciPy conference this year (http://www.google.com/notebook/public/ >> 00116375172106219610/BDQGkIwoQzuK738Yi >> )) >> >> I'm not sure if there's a big payoff in doing this, though. The >> IPython+numpy/scipy+matplotlib combo is great, but I'm not sure what >> you'd get from porting to jython, since its pretty well self >> contained in and of itself. >> >> Thoughts? >> >> -steve >> >> ------------------------------------------------------------------------- >> This SF.net email is sponsored by: Splunk Inc. >> Still grepping through log files to find problems? Stop. >> Now Search log events and configuration files using AJAX and a browser. >> Download your FREE copy of Splunk now >> http://get.splunk.com/ >> _______________________________________________ >> Jython-users mailing list >> Jyt...@li... >> https://lists.sourceforge.net/lists/listinfo/jython-users >> >> >> > > |