Skip to content

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

Closed
@nbro

Description

@nbro

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions