Size: 342
Comment:
|
Size: 1780
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 20: | Line 20: |
=== JArray === I was curious on how to work with jarray and move the arrays between classes after being filled with values and operated. I think this example explains some of the basics. Of course, there are more pythonic ways to do this. Cheers! Alfonso Reyes {{{#!python from java.lang import Math from jarray import array, zeros import java.util.Random; class ArrayClass: """ This class will fill an array with random numbers and then return the array for further operations """ def __init__(self, elems): self.N = elems systemEnergy = 0.0025 self.v = zeros(self.N, "d") # array of zeros, double type v0 = Math.sqrt(2.0 * systemEnergy / self.N) for i in range(0, self.N, 1): r = java.util.Random() self.v[i] = v0 * r.nextInt(self.N) # same velocity for all particles def out(self): for i in range(0, self.N, 1): print i, self.v[i] def get(self): return self.v n = 100 uarr = zeros(n, "d") # array of double to store some operations ac = ArrayClass(n) ac.out() # print the array arr = ac.get() # get the array to start doing some work on it # get a first third of the array members and times 10 print "Get a first third of the array members" for i in range(0, n/3, 1): uarr[i] = arr[i] * 10 print i, arr[i], uarr[i] }}} |
Core Jython / Python Examples
Examples related to core Jython / Python will be here. Intended for those new to Python / Jython
Print Hello world {{{ print 'Hello World' print "Hello World" }}}
JArray
I was curious on how to work with jarray and move the arrays between classes after being filled with values and operated. I think this example explains some of the basics. Of course, there are more pythonic ways to do this. Cheers! Alfonso Reyes
1 from java.lang import Math
2 from jarray import array, zeros
3 import java.util.Random;
4
5 class ArrayClass:
6 """ This class will fill an array with random numbers
7 and then return the array for further operations
8 """
9 def __init__(self, elems):
10 self.N = elems
11 systemEnergy = 0.0025
12 self.v = zeros(self.N, "d") # array of zeros, double type
13 v0 = Math.sqrt(2.0 * systemEnergy / self.N)
14 for i in range(0, self.N, 1):
15 r = java.util.Random()
16 self.v[i] = v0 * r.nextInt(self.N) # same velocity for all particles
17
18 def out(self):
19 for i in range(0, self.N, 1):
20 print i, self.v[i]
21
22 def get(self):
23 return self.v
24
25 n = 100
26 uarr = zeros(n, "d") # array of double to store some operations
27 ac = ArrayClass(n)
28 ac.out() # print the array
29 arr = ac.get() # get the array to start doing some work on it
30
31 # get a first third of the array members and times 10
32 print "Get a first third of the array members"
33 for i in range(0, n/3, 1):
34 uarr[i] = arr[i] * 10
35 print i, arr[i], uarr[i]