Replies: 1 comment 4 replies
-
The approach 2 (allow 0-d array) has been considered. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This is related to PR #69. Before I can continue working on iterators and indices, I found it particularly important to define the expected behavior of
__getitem__
, particularly when the dimension of arrays decrease. So, I would like to have this discussion. It shows:__getitem__
of Numpy and Numojo.Please let me know how you think @MadAlex1997 @shivasankarka .
On decrease of dimensions
A decrease of dimensions may or may not happen when
__getitem__
is called on an ndarray. An ndarray of X-D array can become Y-D array after__getitem__
whereY<=X
.Whether the dimension decerases or not depends on:
__getitem__
.__getitem__
.On 0-D array
Scalar is a special case of ndarray with a dimension of 0.
When do dimensions decrease?
In Numpy, the number of dimensions to be decreased is determined by the number of
Int
passed in__getitem__
.For example,
A
is a 10x10x10 ndarray (3-D). Then,A[1, 2, 3]
leads to a 0-D array (scalar), since there are 3 integers.A[1, 2]
leads to a 1-D array (vector), since there are 2 integers, so the dimension decreases by 2.A[1]
leads to a 2-D array (matrix), since there is 1 integer, so the dimension decreases by 1.When dimensions do not decrease?
In Numpy, the number of dimensions will not decrease when Slice is passed in
__getitem__
or no argument is passed in for a certain dimension (it is an implicit slide and a slide of all items will be used).Take the same example
A
with 10x10x10 in shape. Then,A[1:4, 2:5, 3:6]
, leads to a 3-D array (no decrease in dimension), since there are 3 slices.A[2:8]
, leads to a 3-D array (no decrease in dimension), since there are 1 explicit slice and 2 implicit slices.Mixture of int and slices
When there is a mixture of int and slices passed into
__getitem__
, the number of integers will be the number of dimensions to be decreased. Example,A[1:4, 2, 2]
, leads to a 1-D array (vector), since there are 2 integers, so the dimension decreases by 2.Note that, even though a slice contains one row, it does not reduce the dimensions. Example,
A[1:2, 2:3, 3:4]
, leads to a 3-D array (no decrease in dimension), since there are 3 slices.Gap between current Numpy and Numojo
Currently, Numojo behaves as follows:
A
is a vecotr.A[0]
returns the first scalar.B
is a matrix.B[0]
returns the first scalar.C
is a 3-D array.C[0]
returns the first scalar.Numpy, however, behaves like this:
A
is a vecotr.A[0]
returns the first scalar.B
is a matrix.B[0]
returns the first row (1-D darray).C
is a 3-D array.C[0]
returns the first 2-D array.PR #69 tries to align the behavior of Numojo on
A
andC
(matrix and higher-level ndarray). After this PR, Numojo works as:A
is a vecotr.A[0]
returns the first scalar but wrapped as a 1-D array with size 1.B
is a matrix.B[0]
returns the first row (1-D darray).C
is a 3-D array.C[0]
returns the first 2-D array.Potential difficulties in complete alignment
The difficulty of a complete alignment between Numpy and Numjo is due to the existence of 0-D array (scalar). When passing an ineger into an ndarray, Numpy will return either a scalar (0-D arary) or a ndarray, depending on the
ndim
of the input array.However, in Numojo, we cannot achieve this goal easily, as the dimension of the ndarray is not known at compile time. For example, if
A
is a 10x10 ndarray, andB
is vector of 10 items. We cannot achive the following behavior as numpy does:Solutions
There are several solutions:
Deviate from the behavior of Numpy for 0-D array (scalar)
We deviate from the behavior of Numpy, where 0-D array is not a scalar but a 1-D array with only one item. Use the previous example, if
A
is a 10x10 ndarray, andB
is vector of 10 items:A[0]
returns the first row (1-D array), with shape[10]
.B[0]
returns the first scalar as a 1-D array, with shape[1]
.This is what has been included in PR #69.
Pros of this approach is that we achieve the behavior of Numpy except for 0-D array (scalar).
Cons is that the scalar has to be expressed as a 1-D array with size 1.
Allow
ndim = 0
We treat scalar as a 0-D array. It will be printed without brackets. (This is like that Float64 is a special SIMD vector).
Pros of this approach is to keep the generalization of the ndarray and the rules of decreased dimensions, as described above, will work perfectly.
Cons is that the 0-D array would be confused with a scalar.
Make
ndim
a parameter known at compile timeThis is related to Issue #58.
Pros of this approach is that we can achieve the rules of decreased dimensions, as described above, perfectly. When
ndim
is 1, thenA[0]
results in a scalar.Cons of this approach is that the reshape would be difficult, as mentioned by @MadAlex1997 .
But I think that reshape is possible: We construct a new ndarray according to the new shape, based on the data buffer of the old ndarray. This can be achieved by reusing the data buffer on memory via Unsafe Pointer.
Beta Was this translation helpful? Give feedback.
All reactions