1use 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#[derive(Debug, Clone)]
20#[non_exhaustive]
21pub enum DynArray {
22 Bool(Array<bool, IxDyn>),
24 U8(Array<u8, IxDyn>),
26 U16(Array<u16, IxDyn>),
28 U32(Array<u32, IxDyn>),
30 U64(Array<u64, IxDyn>),
32 U128(Array<u128, IxDyn>),
34 I8(Array<i8, IxDyn>),
36 I16(Array<i16, IxDyn>),
38 I32(Array<i32, IxDyn>),
40 I64(Array<i64, IxDyn>),
42 I128(Array<i128, IxDyn>),
44 I256(Array<I256, IxDyn>),
48 F32(Array<f32, IxDyn>),
50 F64(Array<f64, IxDyn>),
52 Complex32(Array<Complex<f32>, IxDyn>),
54 Complex64(Array<Complex<f64>, IxDyn>),
56 #[cfg(feature = "f16")]
58 F16(Array<half::f16, IxDyn>),
59 #[cfg(feature = "bf16")]
61 BF16(Array<half::bf16, IxDyn>),
62 DateTime64(Array<DateTime64, IxDyn>, TimeUnit),
67 Timedelta64(Array<Timedelta64, IxDyn>, TimeUnit),
69}
70
71macro_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 Self::DateTime64($binding, _) => $expr,
101 Self::Timedelta64($binding, _) => $expr,
102 }
103 };
104}
105
106impl DynArray {
107 #[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 #[must_use]
138 pub fn shape(&self) -> &[usize] {
139 dispatch!(self, a => a.shape())
140 }
141
142 #[must_use]
144 pub fn ndim(&self) -> usize {
145 self.shape().len()
146 }
147
148 #[must_use]
150 pub fn size(&self) -> usize {
151 self.shape().iter().product()
152 }
153
154 #[must_use]
156 pub fn is_empty(&self) -> bool {
157 self.size() == 0
158 }
159
160 #[must_use]
162 pub const fn itemsize(&self) -> usize {
163 self.dtype().size_of()
164 }
165
166 #[must_use]
168 pub fn nbytes(&self) -> usize {
169 self.size() * self.itemsize()
170 }
171
172 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 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 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 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 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 pub fn astype(&self, target: DType, casting: CastKind) -> FerrayResult<Self> {
245 #[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 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 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 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 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 DType::DateTime64(unit) => Self::DateTime64(Array::zeros(dim)?, unit),
368 DType::Timedelta64(unit) => Self::Timedelta64(Array::zeros(dim)?, unit),
369 })
370 }
371
372 #[must_use]
374 pub fn from_datetime64(arr: Array<DateTime64, IxDyn>, unit: TimeUnit) -> Self {
375 Self::DateTime64(arr, unit)
376 }
377
378 #[must_use]
380 pub fn from_timedelta64(arr: Array<Timedelta64, IxDyn>, unit: TimeUnit) -> Self {
381 Self::Timedelta64(arr, unit)
382 }
383
384 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 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
421macro_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 #[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 #[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}