Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multidimensional array support #21

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open

Multidimensional array support #21

wants to merge 27 commits into from

Conversation

msakuta
Copy link
Owner

@msakuta msakuta commented Aug 4, 2024

Now we can declare and define multidimensional arrays

var a: [i32; 2, 3] = [1, 2, 3; 4, 5, 6];

the shape is checked by static type checker, so the following code will be an error, because the assigning value has incompatible shape.

var b: [i32; 3, 2] = [1, 2; 3, 4; 5, 6];
b = [1, 2, 3; 4, 5, 6];

The internal storage for the array is a flattened vec, so that it can be reshaped without modifying the payload.

Added standard functions

Multi-dimensional arrays are hard to write and manipulate in plain code, so we add few standard functions to handle them.

shape

The basic query function to get actual size of an array.

var v: [i64; 2, 3] = [1,2,3;4,5,6];
print(shape(v)); // [2, 3]

transpose

Transposes a 2-D array.

var a: [i32; 2, 3] = [1, 2, 3; 4, 5, 6];

print(a); // [1, 2, 3; 4, 5, 6]

var b: [i32; 3, 2] = transpose(a);

print(b); // [1, 4; 2, 5; 3, 6]

reshape

The most generic way to change shape of 2D matrices. It changes arrangement of dimensions without modifying payload.

var a: [i32; 2, 3] = [1, 2, 3; 4, 5, 6];

print(a); // [1, 2, 3; 4, 5, 6]

var b: [i32; 3, 2] = reshape(a, [3, 2]);

print(b); // [1, 2; 3, 4; 5, 6]

Trying to reshape an array with a different number of elements is an error, even if the target shape is smaller. In other words, it will not be a slice or a view into smaller part of the original array.

reshape([1, 2, 3; 4, 5, 6], [2, 2]);
// Error in run(): Runtime error: reshape's array ([2, 3]) and new shape ([2, 2]) does not have the same number of elements

This behavior may change since we do not want to copy the whole array if the payload doesn't change.

Unfortunately, we cannot check it on compile time, even if we have fixed shape. We may be able to typecheck once we implement generics.

vstack

A frequenty used method to concatenate 2D arrays by stacking vertically in numpy or Matlab.

var a: [i32; 2, 3] = [1, 2, 3; 4, 5, 6];
var b: [i32; 1, 3] = reshape([7, 8, 9], [1, -1]);
var c: [i32; 3, 3] = vstack(a, b);

print(c, shape(c)); // [1, 2, 3; 4, 5, 6; 7, 8, 9] [3, 3]

The literal can define up to 2 dimensions.
Higher dimensions should be constructed by standard library.

Also use Display trait to print values for consistency.
Since it is growing, it deserves its own module, rather than bloating
interpreter.rs.
It will expand dimensions to make it 2D array by adding an axis to the
right. It will effectively make a column vector.

Note that this is different behavior from numpy transpose. numpy
transpose is no-op on 1D arrays.
It is similar to numpy's shape or Matlab's size.
2D array is represented as an array of arrays, which is not really the
same as 2D array, but it is easier to understand for 3 or more
dimensions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant