You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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>.
The documentation says
This is fine.
However, I wonder if the following code should be able to run silently (not even a warning).
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
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.
The text was updated successfully, but these errors were encountered: