Skip to main content

ferray_core/
dynarray.rs

1// ferray-core: DynArray — runtime-typed array enum (REQ-30)
2
3use num_complex::Complex;
4
5use crate::array::owned::Array;
6use crate::dimension::IxDyn;
7use crate::dtype::casting::CastKind;
8use crate::dtype::{DType, DateTime64, I256, TimeUnit, Timedelta64};
9use crate::error::{FerrayError, FerrayResult};
10
11/// A runtime-typed array whose element type is determined at runtime.
12///
13/// This is analogous to a Python `numpy.ndarray` where the dtype is a
14/// runtime property. Each variant wraps an `Array<T, IxDyn>` for the
15/// corresponding element type.
16///
17/// Use this when the element type is not known at compile time (e.g.,
18/// loading from a file, receiving from Python/FFI).
19#[derive(Debug, Clone)]
20#[non_exhaustive]
21pub enum DynArray {
22    /// `bool` elements
23    Bool(Array<bool, IxDyn>),
24    /// `u8` elements
25    U8(Array<u8, IxDyn>),
26    /// `u16` elements
27    U16(Array<u16, IxDyn>),
28    /// `u32` elements
29    U32(Array<u32, IxDyn>),
30    /// `u64` elements
31    U64(Array<u64, IxDyn>),
32    /// `u128` elements
33    U128(Array<u128, IxDyn>),
34    /// `i8` elements
35    I8(Array<i8, IxDyn>),
36    /// `i16` elements
37    I16(Array<i16, IxDyn>),
38    /// `i32` elements
39    I32(Array<i32, IxDyn>),
40    /// `i64` elements
41    I64(Array<i64, IxDyn>),
42    /// `i128` elements
43    I128(Array<i128, IxDyn>),
44    /// `I256` elements — 256-bit two's-complement signed integer,
45    /// used as the promoted type for mixed `u128` + signed-int
46    /// arithmetic (#375, #562).
47    I256(Array<I256, IxDyn>),
48    /// `f32` elements
49    F32(Array<f32, IxDyn>),
50    /// `f64` elements
51    F64(Array<f64, IxDyn>),
52    /// `Complex<f32>` elements
53    Complex32(Array<Complex<f32>, IxDyn>),
54    /// `Complex<f64>` elements
55    Complex64(Array<Complex<f64>, IxDyn>),
56    /// `f16` elements (feature-gated)
57    #[cfg(feature = "f16")]
58    F16(Array<half::f16, IxDyn>),
59    /// `bf16` (bfloat16) elements (feature-gated)
60    #[cfg(feature = "bf16")]
61    BF16(Array<half::bf16, IxDyn>),
62    /// `datetime64[unit]` elements — the `TimeUnit` rides alongside the
63    /// data because [`DType::DateTime64`] is parameterized but the
64    /// element-level [`Element::dtype`](crate::dtype::Element::dtype)
65    /// can only return one fixed unit.
66    DateTime64(Array<DateTime64, IxDyn>, TimeUnit),
67    /// `timedelta64[unit]` elements.
68    Timedelta64(Array<Timedelta64, IxDyn>, TimeUnit),
69}
70
71/// Dispatch a single expression across every `DynArray` variant, binding
72/// the inner `Array<T, IxDyn>` to `$binding`. This turns repeated 17-way
73/// match arms into one-line methods (see issue #125); the f16/bf16
74/// variants are conditionally compiled in the same way as the enum.
75macro_rules! dispatch {
76    ($value:expr, $binding:ident => $expr:expr) => {
77        match $value {
78            Self::Bool($binding) => $expr,
79            Self::U8($binding) => $expr,
80            Self::U16($binding) => $expr,
81            Self::U32($binding) => $expr,
82            Self::U64($binding) => $expr,
83            Self::U128($binding) => $expr,
84            Self::I8($binding) => $expr,
85            Self::I16($binding) => $expr,
86            Self::I32($binding) => $expr,
87            Self::I64($binding) => $expr,
88            Self::I128($binding) => $expr,
89            Self::I256($binding) => $expr,
90            Self::F32($binding) => $expr,
91            Self::F64($binding) => $expr,
92            Self::Complex32($binding) => $expr,
93            Self::Complex64($binding) => $expr,
94            #[cfg(feature = "f16")]
95            Self::F16($binding) => $expr,
96            #[cfg(feature = "bf16")]
97            Self::BF16($binding) => $expr,
98            // datetime64 / timedelta64 carry an extra TimeUnit slot;
99            // the dispatch only needs to bind the inner Array.
100            Self::DateTime64($binding, _) => $expr,
101            Self::Timedelta64($binding, _) => $expr,
102        }
103    };
104}
105
106impl DynArray {
107    /// The runtime dtype of the elements in this array.
108    #[must_use]
109    pub const fn dtype(&self) -> DType {
110        match self {
111            Self::Bool(_) => DType::Bool,
112            Self::U8(_) => DType::U8,
113            Self::U16(_) => DType::U16,
114            Self::U32(_) => DType::U32,
115            Self::U64(_) => DType::U64,
116            Self::U128(_) => DType::U128,
117            Self::I8(_) => DType::I8,
118            Self::I16(_) => DType::I16,
119            Self::I32(_) => DType::I32,
120            Self::I64(_) => DType::I64,
121            Self::I128(_) => DType::I128,
122            Self::I256(_) => DType::I256,
123            Self::F32(_) => DType::F32,
124            Self::F64(_) => DType::F64,
125            Self::Complex32(_) => DType::Complex32,
126            Self::Complex64(_) => DType::Complex64,
127            #[cfg(feature = "f16")]
128            Self::F16(_) => DType::F16,
129            #[cfg(feature = "bf16")]
130            Self::BF16(_) => DType::BF16,
131            Self::DateTime64(_, u) => DType::DateTime64(*u),
132            Self::Timedelta64(_, u) => DType::Timedelta64(*u),
133        }
134    }
135
136    /// Shape as a slice.
137    #[must_use]
138    pub fn shape(&self) -> &[usize] {
139        dispatch!(self, a => a.shape())
140    }
141
142    /// Number of dimensions.
143    #[must_use]
144    pub fn ndim(&self) -> usize {
145        self.shape().len()
146    }
147
148    /// Total number of elements.
149    #[must_use]
150    pub fn size(&self) -> usize {
151        self.shape().iter().product()
152    }
153
154    /// Whether the array has zero elements.
155    #[must_use]
156    pub fn is_empty(&self) -> bool {
157        self.size() == 0
158    }
159
160    /// Size in bytes of one element.
161    #[must_use]
162    pub const fn itemsize(&self) -> usize {
163        self.dtype().size_of()
164    }
165
166    /// Total size in bytes.
167    #[must_use]
168    pub fn nbytes(&self) -> usize {
169        self.size() * self.itemsize()
170    }
171
172    /// Try to extract the inner `Array<f64, IxDyn>`.
173    ///
174    /// # Errors
175    /// Returns `FerrayError::InvalidDtype` if the dtype is not `f64`.
176    pub fn try_into_f64(self) -> FerrayResult<Array<f64, IxDyn>> {
177        match self {
178            Self::F64(a) => Ok(a),
179            other => Err(FerrayError::invalid_dtype(format!(
180                "expected float64, got {}",
181                other.dtype()
182            ))),
183        }
184    }
185
186    /// Try to extract the inner `Array<f32, IxDyn>`.
187    pub fn try_into_f32(self) -> FerrayResult<Array<f32, IxDyn>> {
188        match self {
189            Self::F32(a) => Ok(a),
190            other => Err(FerrayError::invalid_dtype(format!(
191                "expected float32, got {}",
192                other.dtype()
193            ))),
194        }
195    }
196
197    /// Try to extract the inner `Array<i64, IxDyn>`.
198    pub fn try_into_i64(self) -> FerrayResult<Array<i64, IxDyn>> {
199        match self {
200            Self::I64(a) => Ok(a),
201            other => Err(FerrayError::invalid_dtype(format!(
202                "expected int64, got {}",
203                other.dtype()
204            ))),
205        }
206    }
207
208    /// Try to extract the inner `Array<i32, IxDyn>`.
209    pub fn try_into_i32(self) -> FerrayResult<Array<i32, IxDyn>> {
210        match self {
211            Self::I32(a) => Ok(a),
212            other => Err(FerrayError::invalid_dtype(format!(
213                "expected int32, got {}",
214                other.dtype()
215            ))),
216        }
217    }
218
219    /// Try to extract the inner `Array<bool, IxDyn>`.
220    pub fn try_into_bool(self) -> FerrayResult<Array<bool, IxDyn>> {
221        match self {
222            Self::Bool(a) => Ok(a),
223            other => Err(FerrayError::invalid_dtype(format!(
224                "expected bool, got {}",
225                other.dtype()
226            ))),
227        }
228    }
229
230    /// Cast this array to a different element dtype at the requested safety level.
231    ///
232    /// Mirrors `NumPy`'s `arr.astype(dtype, casting=...)`. The conversion routes
233    /// through [`crate::dtype::unsafe_cast::CastTo`] for the underlying typed
234    /// arrays, so it supports the same set of element pairs (every primitive
235    /// numeric, bool, and Complex<f32>/Complex<f64> in any combination).
236    ///
237    /// `f16` / `bf16` are not yet supported in this dispatch and will return
238    /// `FerrayError::InvalidDtype` — track via the umbrella casting issue.
239    ///
240    /// # Errors
241    /// Returns `FerrayError::InvalidDtype` if:
242    /// - The cast is not permitted at the chosen `casting` level, or
243    /// - either source or target dtype is `f16`/`bf16` (not yet wired).
244    pub fn astype(&self, target: DType, casting: CastKind) -> FerrayResult<Self> {
245        // Reject f16/bf16 — see method docs.
246        #[cfg(feature = "f16")]
247        if matches!(self, Self::F16(_)) || target == DType::F16 {
248            return Err(FerrayError::invalid_dtype(
249                "DynArray::astype does not yet support f16",
250            ));
251        }
252        #[cfg(feature = "bf16")]
253        if matches!(self, Self::BF16(_)) || target == DType::BF16 {
254            return Err(FerrayError::invalid_dtype(
255                "DynArray::astype does not yet support bf16",
256            ));
257        }
258        // I256 is accepted as a storage type (from promotion) but
259        // generic cast-through-`CastTo` is not yet wired for it.
260        // Reject both source and target explicitly — users who want
261        // I256 arrays should construct them directly rather than via
262        // astype (#562).
263        if matches!(self, Self::I256(_)) || target == DType::I256 {
264            return Err(FerrayError::invalid_dtype(
265                "DynArray::astype does not yet support I256 — construct I256 arrays directly",
266            ));
267        }
268        // datetime64 / timedelta64 don't go through the generic CastTo
269        // machinery (they're not in numeric promotion's hierarchy). Casts
270        // between time and numeric dtypes are intentionally not supported
271        // by NumPy either — datetime arithmetic uses dedicated kernels.
272        if matches!(self, Self::DateTime64(_, _) | Self::Timedelta64(_, _))
273            || matches!(target, DType::DateTime64(_) | DType::Timedelta64(_))
274        {
275            return Err(FerrayError::invalid_dtype(format!(
276                "DynArray::astype: cast involving {target} not supported \
277                 — datetime/timedelta dtypes use dedicated arithmetic, not generic casts"
278            )));
279        }
280
281        // Inner macro: dispatch on the *source* variant. The target type `$U`
282        // is fixed by the outer match below.
283        macro_rules! cast_into {
284            ($U:ty) => {
285                match self {
286                    Self::Bool(a) => a.cast::<$U>(casting),
287                    Self::U8(a) => a.cast::<$U>(casting),
288                    Self::U16(a) => a.cast::<$U>(casting),
289                    Self::U32(a) => a.cast::<$U>(casting),
290                    Self::U64(a) => a.cast::<$U>(casting),
291                    Self::U128(a) => a.cast::<$U>(casting),
292                    Self::I8(a) => a.cast::<$U>(casting),
293                    Self::I16(a) => a.cast::<$U>(casting),
294                    Self::I32(a) => a.cast::<$U>(casting),
295                    Self::I64(a) => a.cast::<$U>(casting),
296                    Self::I128(a) => a.cast::<$U>(casting),
297                    Self::F32(a) => a.cast::<$U>(casting),
298                    Self::F64(a) => a.cast::<$U>(casting),
299                    Self::Complex32(a) => a.cast::<$U>(casting),
300                    Self::Complex64(a) => a.cast::<$U>(casting),
301                    Self::I256(_) => unreachable!("I256 source rejected above"),
302                    #[cfg(feature = "f16")]
303                    Self::F16(_) => unreachable!("f16 source rejected above"),
304                    #[cfg(feature = "bf16")]
305                    Self::BF16(_) => unreachable!("bf16 source rejected above"),
306                    Self::DateTime64(_, _) | Self::Timedelta64(_, _) => {
307                        unreachable!("time-dtype source rejected above")
308                    }
309                }
310            };
311        }
312
313        Ok(match target {
314            DType::Bool => Self::Bool(cast_into!(bool)?),
315            DType::U8 => Self::U8(cast_into!(u8)?),
316            DType::U16 => Self::U16(cast_into!(u16)?),
317            DType::U32 => Self::U32(cast_into!(u32)?),
318            DType::U64 => Self::U64(cast_into!(u64)?),
319            DType::U128 => Self::U128(cast_into!(u128)?),
320            DType::I8 => Self::I8(cast_into!(i8)?),
321            DType::I16 => Self::I16(cast_into!(i16)?),
322            DType::I32 => Self::I32(cast_into!(i32)?),
323            DType::I64 => Self::I64(cast_into!(i64)?),
324            DType::I128 => Self::I128(cast_into!(i128)?),
325            DType::F32 => Self::F32(cast_into!(f32)?),
326            DType::F64 => Self::F64(cast_into!(f64)?),
327            DType::Complex32 => Self::Complex32(cast_into!(Complex<f32>)?),
328            DType::Complex64 => Self::Complex64(cast_into!(Complex<f64>)?),
329            DType::I256 => unreachable!("I256 target rejected above"),
330            #[cfg(feature = "f16")]
331            DType::F16 => unreachable!("f16 target rejected above"),
332            #[cfg(feature = "bf16")]
333            DType::BF16 => unreachable!("bf16 target rejected above"),
334            DType::DateTime64(_) | DType::Timedelta64(_) => {
335                unreachable!("time-dtype target rejected above")
336            }
337        })
338    }
339
340    /// Create a `DynArray` of zeros with the given dtype and shape.
341    pub fn zeros(dtype: DType, shape: &[usize]) -> FerrayResult<Self> {
342        let dim = IxDyn::new(shape);
343        Ok(match dtype {
344            DType::Bool => Self::Bool(Array::zeros(dim)?),
345            DType::U8 => Self::U8(Array::zeros(dim)?),
346            DType::U16 => Self::U16(Array::zeros(dim)?),
347            DType::U32 => Self::U32(Array::zeros(dim)?),
348            DType::U64 => Self::U64(Array::zeros(dim)?),
349            DType::U128 => Self::U128(Array::zeros(dim)?),
350            DType::I8 => Self::I8(Array::zeros(dim)?),
351            DType::I16 => Self::I16(Array::zeros(dim)?),
352            DType::I32 => Self::I32(Array::zeros(dim)?),
353            DType::I64 => Self::I64(Array::zeros(dim)?),
354            DType::I128 => Self::I128(Array::zeros(dim)?),
355            DType::I256 => Self::I256(Array::zeros(dim)?),
356            DType::F32 => Self::F32(Array::zeros(dim)?),
357            DType::F64 => Self::F64(Array::zeros(dim)?),
358            DType::Complex32 => Self::Complex32(Array::zeros(dim)?),
359            DType::Complex64 => Self::Complex64(Array::zeros(dim)?),
360            #[cfg(feature = "f16")]
361            DType::F16 => Self::F16(Array::zeros(dim)?),
362            #[cfg(feature = "bf16")]
363            DType::BF16 => Self::BF16(Array::zeros(dim)?),
364            // datetime64 / timedelta64 carry a TimeUnit alongside the
365            // typed Array. The element value is the i64 zero (the Unix
366            // epoch for datetime, a no-op duration for timedelta).
367            DType::DateTime64(unit) => Self::DateTime64(Array::zeros(dim)?, unit),
368            DType::Timedelta64(unit) => Self::Timedelta64(Array::zeros(dim)?, unit),
369        })
370    }
371
372    /// Construct a [`DynArray::DateTime64`] from a typed array plus its unit.
373    #[must_use]
374    pub fn from_datetime64(arr: Array<DateTime64, IxDyn>, unit: TimeUnit) -> Self {
375        Self::DateTime64(arr, unit)
376    }
377
378    /// Construct a [`DynArray::Timedelta64`] from a typed array plus its unit.
379    #[must_use]
380    pub fn from_timedelta64(arr: Array<Timedelta64, IxDyn>, unit: TimeUnit) -> Self {
381        Self::Timedelta64(arr, unit)
382    }
383
384    /// Try to extract the inner `Array<DateTime64, IxDyn>` along with the
385    /// stored [`TimeUnit`].
386    ///
387    /// # Errors
388    /// Returns `FerrayError::InvalidDtype` if the dtype is not `datetime64`.
389    pub fn try_into_datetime64(self) -> FerrayResult<(Array<DateTime64, IxDyn>, TimeUnit)> {
390        match self {
391            Self::DateTime64(a, u) => Ok((a, u)),
392            other => Err(FerrayError::invalid_dtype(format!(
393                "expected datetime64, got {}",
394                other.dtype()
395            ))),
396        }
397    }
398
399    /// Try to extract the inner `Array<Timedelta64, IxDyn>` along with the
400    /// stored [`TimeUnit`].
401    ///
402    /// # Errors
403    /// Returns `FerrayError::InvalidDtype` if the dtype is not `timedelta64`.
404    pub fn try_into_timedelta64(self) -> FerrayResult<(Array<Timedelta64, IxDyn>, TimeUnit)> {
405        match self {
406            Self::Timedelta64(a, u) => Ok((a, u)),
407            other => Err(FerrayError::invalid_dtype(format!(
408                "expected timedelta64, got {}",
409                other.dtype()
410            ))),
411        }
412    }
413}
414
415impl std::fmt::Display for DynArray {
416    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
417        dispatch!(self, a => write!(f, "{a}"))
418    }
419}
420
421// Conversion from typed arrays to DynArray
422macro_rules! impl_from_array_dyn {
423    ($ty:ty, $variant:ident) => {
424        impl From<Array<$ty, IxDyn>> for DynArray {
425            fn from(a: Array<$ty, IxDyn>) -> Self {
426                Self::$variant(a)
427            }
428        }
429    };
430}
431
432impl_from_array_dyn!(bool, Bool);
433impl_from_array_dyn!(u8, U8);
434impl_from_array_dyn!(u16, U16);
435impl_from_array_dyn!(u32, U32);
436impl_from_array_dyn!(u64, U64);
437impl_from_array_dyn!(u128, U128);
438impl_from_array_dyn!(i8, I8);
439impl_from_array_dyn!(i16, I16);
440impl_from_array_dyn!(i32, I32);
441impl_from_array_dyn!(i64, I64);
442impl_from_array_dyn!(i128, I128);
443impl_from_array_dyn!(I256, I256);
444impl_from_array_dyn!(f32, F32);
445impl_from_array_dyn!(f64, F64);
446impl_from_array_dyn!(Complex<f32>, Complex32);
447impl_from_array_dyn!(Complex<f64>, Complex64);
448#[cfg(feature = "f16")]
449impl_from_array_dyn!(half::f16, F16);
450#[cfg(feature = "bf16")]
451impl_from_array_dyn!(half::bf16, BF16);
452
453#[cfg(test)]
454mod tests {
455    use super::*;
456
457    #[test]
458    fn dynarray_zeros_f64() {
459        let da = DynArray::zeros(DType::F64, &[2, 3]).unwrap();
460        assert_eq!(da.dtype(), DType::F64);
461        assert_eq!(da.shape(), &[2, 3]);
462        assert_eq!(da.ndim(), 2);
463        assert_eq!(da.size(), 6);
464        assert_eq!(da.itemsize(), 8);
465        assert_eq!(da.nbytes(), 48);
466    }
467
468    #[test]
469    fn dynarray_zeros_i32() {
470        let da = DynArray::zeros(DType::I32, &[4]).unwrap();
471        assert_eq!(da.dtype(), DType::I32);
472        assert_eq!(da.shape(), &[4]);
473    }
474
475    #[test]
476    fn dynarray_try_into_f64() {
477        let da = DynArray::zeros(DType::F64, &[3]).unwrap();
478        let arr = da.try_into_f64().unwrap();
479        assert_eq!(arr.shape(), &[3]);
480    }
481
482    #[test]
483    fn dynarray_try_into_wrong_type() {
484        let da = DynArray::zeros(DType::I32, &[3]).unwrap();
485        assert!(da.try_into_f64().is_err());
486    }
487
488    // ----- DynArray::astype tests (issue #361) -----
489
490    #[test]
491    fn dynarray_astype_f64_to_i32_unsafe() {
492        let arr = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[3]), vec![1.5, 2.7, -3.9]).unwrap();
493        let dy = DynArray::F64(arr);
494        let casted = dy.astype(DType::I32, CastKind::Unsafe).unwrap();
495        assert_eq!(casted.dtype(), DType::I32);
496        match casted {
497            DynArray::I32(a) => assert_eq!(a.as_slice().unwrap(), &[1, 2, -3]),
498            _ => panic!("expected I32"),
499        }
500    }
501
502    #[test]
503    fn dynarray_astype_safe_widening() {
504        let arr = Array::<i32, IxDyn>::from_vec(IxDyn::new(&[3]), vec![10, 20, 30]).unwrap();
505        let dy = DynArray::I32(arr);
506        let casted = dy.astype(DType::I64, CastKind::Safe).unwrap();
507        assert_eq!(casted.dtype(), DType::I64);
508        match casted {
509            DynArray::I64(a) => assert_eq!(a.as_slice().unwrap(), &[10i64, 20, 30]),
510            _ => panic!("expected I64"),
511        }
512    }
513
514    #[test]
515    fn dynarray_astype_safe_narrowing_errors() {
516        let arr = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2]), vec![1.0, 2.0]).unwrap();
517        let dy = DynArray::F64(arr);
518        assert!(dy.astype(DType::F32, CastKind::Safe).is_err());
519    }
520
521    #[test]
522    fn dynarray_astype_complex_to_real_unsafe() {
523        let arr = Array::<Complex<f64>, IxDyn>::from_vec(
524            IxDyn::new(&[2]),
525            vec![Complex::new(1.5, 9.0), Complex::new(2.5, -1.0)],
526        )
527        .unwrap();
528        let dy = DynArray::Complex64(arr);
529        let casted = dy.astype(DType::F64, CastKind::Unsafe).unwrap();
530        match casted {
531            DynArray::F64(a) => assert_eq!(a.as_slice().unwrap(), &[1.5, 2.5]),
532            _ => panic!("expected F64"),
533        }
534    }
535
536    #[test]
537    fn dynarray_astype_bool_to_u8_safe() {
538        let arr =
539            Array::<bool, IxDyn>::from_vec(IxDyn::new(&[3]), vec![true, false, true]).unwrap();
540        let dy = DynArray::Bool(arr);
541        let casted = dy.astype(DType::U8, CastKind::Safe).unwrap();
542        match casted {
543            DynArray::U8(a) => assert_eq!(a.as_slice().unwrap(), &[1u8, 0, 1]),
544            _ => panic!("expected U8"),
545        }
546    }
547
548    #[test]
549    fn dynarray_astype_no_kind_requires_identity() {
550        let arr = Array::<f64, IxDyn>::from_vec(IxDyn::new(&[2]), vec![1.0, 2.0]).unwrap();
551        let dy = DynArray::F64(arr);
552        assert!(dy.astype(DType::F64, CastKind::No).is_ok());
553        assert!(dy.astype(DType::F32, CastKind::No).is_err());
554    }
555
556    #[test]
557    fn dynarray_from_typed() {
558        let arr = Array::<f64, IxDyn>::zeros(IxDyn::new(&[2, 2])).unwrap();
559        let da: DynArray = arr.into();
560        assert_eq!(da.dtype(), DType::F64);
561    }
562
563    #[test]
564    fn dynarray_display() {
565        let da = DynArray::zeros(DType::I32, &[3]).unwrap();
566        let s = format!("{da}");
567        assert!(s.contains("[0, 0, 0]"));
568    }
569
570    #[test]
571    fn dynarray_is_empty() {
572        let da = DynArray::zeros(DType::F32, &[0]).unwrap();
573        assert!(da.is_empty());
574    }
575
576    // ----- f16 / bf16 DynArray coverage (#139) -----
577
578    #[cfg(feature = "f16")]
579    #[test]
580    fn dynarray_f16_zeros_shape_and_dtype() {
581        let da = DynArray::zeros(DType::F16, &[2, 3]).unwrap();
582        assert_eq!(da.dtype(), DType::F16);
583        assert_eq!(da.shape(), &[2, 3]);
584        assert_eq!(da.size(), 6);
585        assert_eq!(da.itemsize(), 2);
586        assert_eq!(da.nbytes(), 12);
587    }
588
589    #[cfg(feature = "f16")]
590    #[test]
591    fn dynarray_f16_from_typed_roundtrips() {
592        use half::f16;
593        let raw = [f16::from_f32(1.0), f16::from_f32(2.5), f16::from_f32(-3.0)];
594        let arr = Array::<f16, IxDyn>::from_vec(IxDyn::new(&[3]), raw.to_vec()).unwrap();
595        let da: DynArray = arr.into();
596        assert_eq!(da.dtype(), DType::F16);
597        assert_eq!(da.shape(), &[3]);
598    }
599
600    #[cfg(feature = "bf16")]
601    #[test]
602    fn dynarray_bf16_zeros_shape_and_dtype() {
603        let da = DynArray::zeros(DType::BF16, &[4]).unwrap();
604        assert_eq!(da.dtype(), DType::BF16);
605        assert_eq!(da.shape(), &[4]);
606        assert_eq!(da.itemsize(), 2);
607    }
608
609    #[cfg(feature = "bf16")]
610    #[test]
611    fn dynarray_bf16_from_typed_roundtrips() {
612        use half::bf16;
613        let raw = [bf16::from_f32(1.0), bf16::from_f32(2.0)];
614        let arr = Array::<bf16, IxDyn>::from_vec(IxDyn::new(&[2]), raw.to_vec()).unwrap();
615        let da: DynArray = arr.into();
616        assert_eq!(da.dtype(), DType::BF16);
617    }
618}