macro_rules! array {
($([$([$([$($x:expr),* $(,)?]),* $(,)?]),* $(,)?]),+ $(,)?) => { ... };
($([$([$($x:expr),* $(,)?]),* $(,)?]),+ $(,)?) => { ... };
($([$($x:expr),* $(,)?]),+ $(,)?) => { ... };
($($x:expr),+ $(,)?) => { ... };
}Expand description
An array from nested literals, as MLX’s mx.array([...]): the macro’s brackets are the
outermost dimension, so array![1, 2, 3] is 1-D and array![[1.0, 2.0], [3.0, 4.0]] is
2-D (up to 4-D). Unsuffixed float literals give float32 and unsuffixed integers int32;
suffixed literals keep their type (array![1u8, 2, 3] is uint8), except f64, which
gives float32 like MLX’s Python floats (use Array::from for a float64 array).
Ragged rows do not compile.
A 0-D array is Array::from a number: a macro cannot tell array!(2.5) from
array![2.5] (the invocation’s delimiters are not part of its input), and the latter
is the 1-element vector, as with vec!.
use nuvec_core::{Array, DType, array};
let m = array![[1.0, 2.0], [3.0, 4.0]];
assert_eq!((m.shape(), m.dtype()), (&[2, 2][..], DType::F32));
assert_eq!(array![1, 2, 3].dtype(), DType::I32);
assert_eq!(array![1u8, 2, 3].dtype(), DType::U8);
assert_eq!(array![2.5].shape(), [1]);
assert_eq!(Array::from(2.5f32).ndim(), 0);