Issue2758
Created on 2019-04-10.18:22:00 by waynelloydsmith, last changed 2019-05-01.20:15:43 by jeff.allen.
msg12432 (view) |
Author: Wayne Smith (waynelloydsmith) |
Date: 2019-04-10.18:22:00 |
|
need to be able to create 2D and 3D arrays like java Float[][][]
examples
a = jarray.array([[[1.0]*4]*2],'f')
TypeError: can't convert [[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]] to float
a = jarray.zeros(jarray.zeros(4096*4,"f"),"f")
TypeError: zeros(): 1st arg can't be coerced to int
a = jarray.zeros([[[1]*4]*2],"f") # 3D array of floats
TypeError: zeros(): 1st arg can't be coerced to int
|
msg12434 (view) |
Author: Adam Burke (adamburke) |
Date: 2019-04-13.11:36:45 |
|
This should already be possible in Jython. There is an overloaded version of jarray.array() which takes a java.lang.Class object instead of a string description. As arrays are first class objects in Java, once you obtain the class object, you can pass that to create a 2D array of the appropriate type. It helps to know that "[I" is the name of the class for int[] (ie an array of int). See 10.8 here:
https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
https://www.geeksforgeeks.org/array-primitive-type-object-java/
Example using jython console
Jython 2.7.2a1+ (, Apr. 11 2019, 17:41:34)
[OpenJDK 64-Bit Server VM (Oracle Corporation)] on java11.0.1
Type "help", "copyright", "credits" or "license" for more information.
>>> from java.lang import Class
>>> import jarray
>>>
>>> ci = Class.forName('[I')
>>> jarray.array([[1],[2]],ci)
array([I, [array('i', [1]), array('i', [2])])
>>>
>>> cf = Class.forName('[F')
>>> jarray.array([[1.0,1.0],[2.0,2.0]],cf)
array([F, [array('f', [1.0, 1.0]), array('f', [2.0, 2.0])])
>>>
|
msg12452 (view) |
Author: Adam Burke (adamburke) |
Date: 2019-04-26.10:17:40 |
|
Doco PR for book https://github.com/jython/book/pull/5
Website PR pending
|
msg12454 (view) |
Author: Wayne Smith (waynelloydsmith) |
Date: 2019-04-26.18:31:25 |
|
works fine ,my example of a float [][][]:
cf = Class.forName('[[F') # can use [F [[F [[[F
x = jarray.array([[[1.0]*4096*4]*2],cf)
# some tests
print("type x",type(x)) # type x <type 'array.array'>
print(x[0][0][1]) # 1 ok
# print(x[1][0][1]) # failed like it should
print(x[0][1][1]) # 1 ok
print(x[0][0][100]) # 1 ok
print(x[0][1][2000]) # 1 ok
Thanks problem solved
|
|
Date |
User |
Action |
Args |
2019-05-01 20:15:43 | jeff.allen | set | status: open -> closed components:
- Core milestone: Jython 2.7.1 -> |
2019-04-26 18:31:25 | waynelloydsmith | set | messages:
+ msg12454 |
2019-04-26 10:17:40 | adamburke | set | messages:
+ msg12452 |
2019-04-13 11:36:45 | adamburke | set | nosy:
+ adamburke messages:
+ msg12434 |
2019-04-10 18:22:00 | waynelloydsmith | create | |
|