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