1use std::{
4 fmt::Debug,
5 ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign},
6};
7
8use ndarray::prelude::*;
9use num_complex::Complex;
10use num_traits::{One, Zero};
11
12pub type R1D = Array1<f64>;
14pub type C1D = Array1<Complex<f64>>;
15pub type VR1D<'a> = ArrayView1<'a, f64>;
16pub type VC1D<'a> = ArrayView1<'a, num_complex::Complex<f64>>;
17
18pub type R2D = Array2<f64>;
20pub type C2D = Array2<Complex<f64>>;
21pub type VR2D<'a> = ArrayView2<'a, f64>;
22pub type VC2D<'a> = ArrayView2<'a, num_complex::Complex<f64>>;
23
24pub trait IsLinearOperatable:
26 Debug
27 + Copy
28 + Clone
29 + Sized
30 + PartialEq
31 + Zero
32 + One
33 + Add<Output = Self>
34 + Sub<Output = Self>
35 + Mul<Output = Self>
36 + Div<Output = Self>
37 + Neg<Output = Self>
38 + SubAssign<Self>
39 + AddAssign<Self>
40 + MulAssign<Self>
41 + DivAssign<Self>
42{
43}
44
45impl IsLinearOperatable for f64 {}
46impl IsLinearOperatable for f32 {}
47impl IsLinearOperatable for Complex<f64> {}
48impl IsLinearOperatable for Complex<f32> {}
49impl IsLinearOperatable for i64 {}
50impl IsLinearOperatable for i32 {}
51impl IsLinearOperatable for i16 {}
52impl IsLinearOperatable for i8 {}
53
54pub trait IsAnalytic: IsLinearOperatable {
56 fn f_conj(&self) -> Self;
57 fn f_exp(&self) -> Self;
58 fn f_ln(&self) -> Self;
59 fn f_powf(&self, n: f64) -> Self;
60 fn f_powi(&self, n: i32) -> Self;
61 fn f_abs(&self) -> f64;
62 fn f_arg(&self) -> f64;
63 fn f_abs2(&self) -> f64;
64 fn f_scale(&self, factor: f64) -> Self;
65 fn to_complex(&self) -> Complex<f64>;
66 fn from_complex(value: Complex<f64>) -> Self
67 where
68 Self: Sized;
69}
70
71pub trait FromComplex64: Sized {
74 fn from_complex(value: Complex<f64>) -> Self;
75}
76
77impl IsAnalytic for f64 {
78 fn f_conj(&self) -> Self {
79 *self
80 }
81 fn f_exp(&self) -> Self {
82 self.exp()
83 }
84 fn f_ln(&self) -> Self {
85 self.ln()
86 }
87 fn f_powf(&self, n: f64) -> Self {
88 self.powf(n)
89 }
90 fn f_powi(&self, n: i32) -> Self {
91 self.powi(n)
92 }
93 fn f_abs(&self) -> f64 {
94 self.abs()
95 }
96 fn f_abs2(&self) -> f64 {
97 self * self
98 }
99 fn f_arg(&self) -> f64 {
100 if *self >= 0.0 {
101 0.0
102 } else {
103 std::f64::consts::PI
104 }
105 }
106 fn f_scale(&self, factor: f64) -> Self {
107 self * factor
108 }
109 fn to_complex(&self) -> Complex<f64> {
110 Complex::new(*self, 0.0)
111 }
112 fn from_complex(value: Complex<f64>) -> Self
113 where
114 Self: Sized,
115 {
116 value.re
118 }
119}
120
121impl FromComplex64 for f64 {
122 fn from_complex(value: Complex<f64>) -> Self {
123 value.re
124 }
125}
126
127impl IsAnalytic for Complex<f64> {
128 fn f_conj(&self) -> Self {
129 self.conj()
130 }
131 fn f_exp(&self) -> Self {
132 self.exp()
133 }
134 fn f_ln(&self) -> Self {
135 self.ln()
136 }
137 fn f_powf(&self, n: f64) -> Self {
138 self.powf(n)
139 }
140 fn f_powi(&self, n: i32) -> Self {
141 self.powi(n)
142 }
143 fn f_abs(&self) -> f64 {
144 self.norm()
145 }
146 fn f_arg(&self) -> f64 {
147 self.arg()
148 }
149 fn f_abs2(&self) -> f64 {
150 self.norm_sqr()
151 }
152 fn f_scale(&self, factor: f64) -> Self {
153 self * factor
154 }
155 fn to_complex(&self) -> Complex<f64> {
156 *self
157 }
158 fn from_complex(value: Complex<f64>) -> Self
159 where
160 Self: Sized,
161 {
162 value
163 }
164}
165
166impl FromComplex64 for Complex<f64> {
167 fn from_complex(value: Complex<f64>) -> Self {
168 value
169 }
170}