array_object/convert/
from_complex.rs

1use 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 From<&Complex<$ty>> for ArrayObject {
26                fn from(val: &Complex<$ty>) -> Self {
27                    Pair(val.re, val.im).into()
28                }
29            }
30            impl<const N: usize> From<[Complex<$ty>; N]> for ArrayObject {
31                fn from(val: [Complex<$ty>; N]) -> Self {
32                    val.to_vec().into()
33                }
34            }
35            impl From<&[Complex<$ty>]> for ArrayObject {
36                fn from(val: &[Complex<$ty>]) -> Self {
37                    val.to_vec().into()
38                }
39            }
40            impl From<Vec<Pair<$ty>>> for ArrayObject {
41                fn from(val: Vec<Pair<$ty>>) -> Self {
42                    let shape = vec![val.len() as u64];
43                    let mut data = Vec::<u8>::with_capacity(2 * val.len() * size_of::<$ty>());
44                    for v in val {
45                        let Pair(re, im) = v;
46                        data.append(&mut re.to_le_bytes().to_vec());
47                        data.append(&mut im.to_le_bytes().to_vec());
48                    }
49                    Self {
50                        data,
51                        shape,
52                        datatype: DataType::Complex,
53                    }
54                }
55            }
56            impl From<Vec<Complex<$ty>>> for ArrayObject {
57                fn from(val: Vec<Complex<$ty>>) -> Self {
58                    let shape = vec![val.len() as u64];
59                    let mut data = Vec::<u8>::with_capacity(2 * val.len() * size_of::<$ty>());
60                    for v in val {
61                        data.append(&mut v.re.to_le_bytes().to_vec());
62                        data.append(&mut v.im.to_le_bytes().to_vec());
63                    }
64                    Self {
65                        data,
66                        shape,
67                        datatype: DataType::Complex,
68                    }
69                }
70            }
71            impl From<&Vec<Complex<$ty>>> for ArrayObject {
72                fn from(val: &Vec<Complex<$ty>>) -> Self {
73                    let shape = vec![val.len() as u64];
74                    let mut data = Vec::<u8>::with_capacity(2 * val.len() * size_of::<$ty>());
75                    for v in val {
76                        data.append(&mut v.re.to_le_bytes().to_vec());
77                        data.append(&mut v.im.to_le_bytes().to_vec());
78                    }
79                    Self {
80                        data,
81                        shape,
82                        datatype: DataType::Complex,
83                    }
84                }
85            }
86            impl TryFrom<VecVec<$ty>> for ArrayObject {
87                type Error = ArrayObjectError;
88                fn try_from(VecVec(val_re, val_im): VecVec<$ty>) -> Result<Self, Self::Error> {
89                    if val_re.len() != val_im.len() {
90                        return Err(ArrayObjectError::VectorLengthMismatch(val_re.len(), val_im.len()));
91                    }
92                    let shape = vec![val_re.len() as u64];
93                    let mut data = Vec::<u8>::with_capacity(2 * val_re.len() * size_of::<$ty>());
94                    for (re, im) in val_re.into_iter().zip(val_im.into_iter()) {
95                        data.append(&mut re.to_le_bytes().to_vec());
96                        data.append(&mut im.to_le_bytes().to_vec());
97                    }
98                    Ok(Self {
99                        data,
100                        shape,
101                        datatype: DataType::Complex,
102                    })
103                }
104            }
105            impl TryFrom<VecShape<Complex<$ty>>> for ArrayObject {
106                type Error = ArrayObjectError;
107                fn try_from(VecShape(val, shape): VecShape<Complex<$ty>>) -> Result<Self, Self::Error> {
108                    if val.len() != shape.product() as usize {
109                        return Err(ArrayObjectError::NumberOfElementsMismatch(val.len(), shape.product() as usize));
110                    }
111                    if shape.len() > 15 {
112                        return Err(ArrayObjectError::TooLargeDimension(shape.len()));
113                    }
114                    let mut temp: ArrayObject = val.into();
115                    temp.shape = shape;
116                    Ok(temp)
117                }
118            }
119            impl TryFrom<VecVecShape<$ty>> for ArrayObject {
120                type Error = ArrayObjectError;
121                fn try_from(VecVecShape(val_re, val_im, shape): VecVecShape<$ty>) -> Result<Self, Self::Error> {
122                    if val_re.len() != shape.product() as usize {
123                        return Err(ArrayObjectError::NumberOfElementsMismatch(val_re.len(), shape.product() as usize));
124                    }
125                    if shape.len() > 15 {
126                        return Err(ArrayObjectError::TooLargeDimension(shape.len()));
127                    }
128                    let mut temp: ArrayObject = VecVec(val_re, val_im).try_into()?;
129                    temp.shape = shape;
130                    Ok(temp)
131                }
132            }
133        )*
134    };
135}
136
137from_complex!(f32, f64);