array_object/convert/
from_complex.rs1use crate::adaptor::*;
2use crate::error::ArrayObjectError;
3use crate::misc::Product;
4use crate::storage::*;
5use num_complex::Complex;
6
7macro_rules! from_complex {
8 ($($ty:ty),*) => {
9 $(
10 impl From<Pair<$ty>> for ArrayObject {
11 fn from(Pair(val_re, val_im): Pair<$ty>) -> Self {
12 let data = [val_re.to_le_bytes().to_vec(), val_im.to_le_bytes().to_vec()].concat();
13 Self {
14 data,
15 shape: vec![],
16 datatype: DataType::Complex,
17 }
18 }
19 }
20 impl From<Complex<$ty>> for ArrayObject {
21 fn from(val: Complex<$ty>) -> Self {
22 Pair(val.re, val.im).into()
23 }
24 }
25 impl<const N: usize> From<[Complex<$ty>; N]> for ArrayObject {
26 fn from(val: [Complex<$ty>; N]) -> Self {
27 val.to_vec().into()
28 }
29 }
30 impl From<&[Complex<$ty>]> for ArrayObject {
31 fn from(val: &[Complex<$ty>]) -> Self {
32 val.to_vec().into()
33 }
34 }
35 impl From<Vec<Pair<$ty>>> for ArrayObject {
36 fn from(val: Vec<Pair<$ty>>) -> Self {
37 let shape = vec![val.len() as u64];
38 let mut data = Vec::<u8>::with_capacity(2 * val.len() * size_of::<$ty>());
39 for v in val {
40 let Pair(re, im) = v;
41 data.append(&mut re.to_le_bytes().to_vec());
42 data.append(&mut im.to_le_bytes().to_vec());
43 }
44 Self {
45 data,
46 shape,
47 datatype: DataType::Complex,
48 }
49 }
50 }
51 impl From<Vec<Complex<$ty>>> for ArrayObject {
52 fn from(val: Vec<Complex<$ty>>) -> Self {
53 let shape = vec![val.len() as u64];
54 let mut data = Vec::<u8>::with_capacity(2 * val.len() * size_of::<$ty>());
55 for v in val {
56 data.append(&mut v.re.to_le_bytes().to_vec());
57 data.append(&mut v.im.to_le_bytes().to_vec());
58 }
59 Self {
60 data,
61 shape,
62 datatype: DataType::Complex,
63 }
64 }
65 }
66 impl TryFrom<VecVec<$ty>> for ArrayObject {
67 type Error = ArrayObjectError;
68 fn try_from(VecVec(val_re, val_im): VecVec<$ty>) -> Result<Self, Self::Error> {
69 if val_re.len() != val_im.len() {
70 return Err(ArrayObjectError::VectorLengthMismatch(val_re.len(), val_im.len()));
71 }
72 let shape = vec![val_re.len() as u64];
73 let mut data = Vec::<u8>::with_capacity(2 * val_re.len() * size_of::<$ty>());
74 for (re, im) in val_re.into_iter().zip(val_im.into_iter()) {
75 data.append(&mut re.to_le_bytes().to_vec());
76 data.append(&mut im.to_le_bytes().to_vec());
77 }
78 Ok(Self {
79 data,
80 shape,
81 datatype: DataType::Complex,
82 })
83 }
84 }
85 impl TryFrom<VecShape<Complex<$ty>>> for ArrayObject {
86 type Error = ArrayObjectError;
87 fn try_from(VecShape(val, shape): VecShape<Complex<$ty>>) -> Result<Self, Self::Error> {
88 if val.len() != shape.product() as usize {
89 return Err(ArrayObjectError::NumberOfElementsMismatch(val.len(), shape.product() as usize));
90 }
91 if shape.len() > 15 {
92 return Err(ArrayObjectError::TooLargeDimension(shape.len()));
93 }
94 let mut temp: ArrayObject = val.into();
95 temp.shape = shape;
96 Ok(temp)
97 }
98 }
99 impl TryFrom<VecVecShape<$ty>> for ArrayObject {
100 type Error = ArrayObjectError;
101 fn try_from(VecVecShape(val_re, val_im, shape): VecVecShape<$ty>) -> Result<Self, Self::Error> {
102 if val_re.len() != shape.product() as usize {
103 return Err(ArrayObjectError::NumberOfElementsMismatch(val_re.len(), shape.product() as usize));
104 }
105 if shape.len() > 15 {
106 return Err(ArrayObjectError::TooLargeDimension(shape.len()));
107 }
108 let mut temp: ArrayObject = VecVec(val_re, val_im).try_into()?;
109 temp.shape = shape;
110 Ok(temp)
111 }
112 }
113 )*
114 };
115}
116
117from_complex!(f32, f64);