Mojo-Numerics-and-Algorithms-group / NuMojo

NuMojo is a library for numerical computing in Mojo 🔥 similar to numpy in Python.
Apache License 2.0
86 stars 15 forks source link

[lib] `__getitem__` uniformed behavior + 0d-array + get item with indices #69

Closed forFudan closed 2 months ago

forFudan commented 2 months ago

Uniform the getitem method and allows 0d array

Related to Discussion #70

Here are example when A is a vector, a matrix, and a 3d array. The behavior met our expectation: The number of dimension reduced equals to the number of integers as input.

A is a matrix
[[      -128    -95     65      -11     ]
 [      8       -72     -116    45      ]
 [      45      111     -30     4       ]
 [      84      -120    -115    7       ]]
2-D array  Shape: [4, 4]  DType: int8

A[0]
[       -128    -95     65      -11     ]
1-D array  Shape: [4]  DType: int8

A[0, 1]
-95
0-D array  Shape: [0]  DType: int8

A[Slice(1,3)]
[[      8       -72     -116    45      ]
 [      45      111     -30     4       ]]
2-D array  Shape: [2, 4]  DType: int8

A[1, Slice(2,4)]
[       -116    45      ]
1-D array  Shape: [2]  DType: int8

A[Slice(1,3), Slice(1,3)]
[[      -72     -116    ]
 [      111     -30     ]]
2-D array  Shape: [2, 2]  DType: int8

A.at(0,1) as Scalar
-95

==============================
A is a vector
[       43      -127    -30     -111    ]
1-D array  Shape: [4]  DType: int8

A[0]
43
0-D array  Shape: [0]  DType: int8

A[Slice(1,3)]
[       -127    -30     ]
1-D array  Shape: [2]  DType: int8

A.at(0) as Scalar
43

==============================
A is a 3darray
[[[     -22     47      22      110     ]
  [     88      6       -105    39      ]
  [     -22     51      105     67      ]
  [     -61     -116    60      -44     ]]
 [[     33      65      125     -35     ]
  [     -65     123     57      64      ]
  [     38      -110    33      98      ]
  [     -59     -17     68      -6      ]]
 [[     -68     -58     -37     -86     ]
  [     -4      101     104     -113    ]
  [     103     1       4       -47     ]
  [     124     -2      -60     -105    ]]
 [[     114     -110    0       -30     ]
  [     -58     105     7       -10     ]
  [     112     -116    66      69      ]
  [     83      -96     -124    48      ]]]
3-D array  Shape: [4, 4, 4]  DType: int8

A[0]
[[      -22     47      22      110     ]
 [      88      6       -105    39      ]
 [      -22     51      105     67      ]
 [      -61     -116    60      -44     ]]
2-D array  Shape: [4, 4]  DType: int8

A[0, 1]
[       88      6       -105    39      ]
1-D array  Shape: [4]  DType: int8

A[0, 1, 2]
-105
0-D array  Shape: [0]  DType: int8

A[Slice(1,3)]
[[[     33      65      125     -35     ]
  [     -65     123     57      64      ]
  [     38      -110    33      98      ]
  [     -59     -17     68      -6      ]]
 [[     -68     -58     -37     -86     ]
  [     -4      101     104     -113    ]
  [     103     1       4       -47     ]
  [     124     -2      -60     -105    ]]]
3-D array  Shape: [2, 4, 4]  DType: int8

A[1, Slice(2,4)]
[[      38      -110    33      98      ]
 [      -59     -17     68      -6      ]]
2-D array  Shape: [2, 4]  DType: int8

A[Slice(1,3), Slice(1,3), 2]
[[      57      33      ]
 [      104     4       ]]
2-D array  Shape: [2, 2]  DType: int8

A.at(0,1,2) as Scalar
-105

Get items of array from indices

To-do: Currently it supports 1-d array. In future, expand it to high dimensional arrays.

Example:

var A = numojo.core.NDArray[numojo.i16](6, random=True)
var idx = A.argsort()
print(A)
print(idx)
print(A[idx])
[       -32768  -24148  16752   -2709   2148    -18418  ]
Shape: [6]  DType: int16

[       0       1       5       3       4       2       ]
Shape: [6]  DType: index

[       -32768  -24148  -18418  -2709   2148    16752   ]
Shape: [6]  DType: int16