-
Notifications
You must be signed in to change notification settings - Fork 0
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
msakuta
wants to merge
27
commits into
master
Choose a base branch
from
muldim-array
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Previously, `[1; 2;]` would be an error, since the last element is parsed as an empty row, making the whole matrix non-rectangular. We could discard the empty row in the interpreter or the type checker, but I think it's cleaner to just ignore empty row in the parser.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Now we can declare and define multidimensional arrays
the shape is checked by static type checker, so the following code will be an error, because the assigning value has incompatible shape.
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.
transpose
Transposes a 2-D array.
reshape
The most generic way to change shape of 2D matrices. It changes arrangement of dimensions without modifying payload.
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.
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.