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

The array! macro's documentation should clarify that if we attempt to create arrays with more than 3 dimensions they are ignored #1252

Open
nbro opened this issue Dec 28, 2022 · 1 comment · May be fixed by #1389

Comments

@nbro
Copy link
Contributor

nbro commented Dec 28, 2022

The documentation says

Create an Array with one, two or three dimensions.

This is fine.

However, I wonder if the following code should be able to run silently (not even a warning).

use ndarray::array;

fn main() {
    let a = array![
        [
                [[[2, 2]]], 
                [[[3, 1]]], 
                [[[5, 3]]], 
                [[[2, 2]]]
        ]
        ];
    println!("An int array with shape {:?}: {:?}", a.shape(), a.ndim()); // Shape is [1, 4, 1]
}

Basically, what's happening is that some dimensions or parentheses are automatically ignored. I think this should at least be documented or maybe the macro should fail in those cases? I am happy to contribute to the documentation, if you think that's the way to go in this case. I am also opening this issue because, if we attempt to create a similar thing in numpy with e.g. the following code

import numpy as np

a = np.array(
        [
                [[[2, 2]]], 
                [[[3, 1]]], 
                [[[5, 3]]], 
                [[[2, 2]]]
        ]
        )

print(a.shape)

We get a different shape: (4, 1, 1, 2).

Btw, I suppose there's no macro that allows us to create arrays from literals with more than 3 dimensions. Is that correct? Is there any reason why the macro only supports up to 3 dimensions? In machine learning, for example, it it's not rare to have multi-dimensional arrays with more than 3 dimensions, so I think that supporting the creation of arrays from literals with more than 3 dimensions would not be a bad idea.

@akern40
Copy link
Contributor

akern40 commented May 20, 2024

I know this is an old topic, but given that it got some thumbs-ups from maintainers I figured it's still of interest. Agree with everything above, although it's worth pointing out that the difference in shapes ([1, 4, 1] vs (4, 1, 1, 2)) is correct here; the first set of brackets in the array! macro are used as the initial brackets for the array, while the same code in NumPy has parentheses where array! has brackets, and are not considered part of the dimensionality definition.

That being said, I have put in a PR that would expand the array! macro to up to (and including) 6D arrays and then create a compiler error on 7D or more. This expands its utility while preventing confusing situations similar to what's mentioned in #1253 and here about silently creating 3D ndarrays-of-rust-arrays. While technically a breaking change, it would only affect people who are explicitly relying on array! to create these 3D ndarray-of-rust-arrays, which I doubt is very many people / anybody.

The PR also adds ::from implementations for the nested Rust arrays up to 6D (necessary for the macro) and a missing implementation of NdIndex<Ix6>.

@akern40 akern40 linked a pull request May 20, 2024 that will close this issue
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 a pull request may close this issue.

2 participants