ceras
yet another deep learning engine
complex_operator.hpp
Go to the documentation of this file.
1 #ifndef SJTXPMDBEYHNPQSGKFIEKTKOYWOFMOEGAHNOHMJVHJIAWTBHCCFUKCHLJMJAFPRHRXEOTYEDC
2 #define SJTXPMDBEYHNPQSGKFIEKTKOYWOFMOEGAHNOHMJVHJIAWTBHCCFUKCHLJMJAFPRHRXEOTYEDC
3 
4 #include "./operation.hpp"
5 
6 namespace ceras
7 {
8 
9  template< Expression Real_Ex, Expression Imag_Ex >
10  struct complex
11  {
12  Real_Ex real_;
13  Imag_Ex imag_;
14  };//struct complex
15 
16 
17  template< typename T >
18  struct is_complex : std::false_type {};
19 
20  template< Expression Real_Ex, Expression Imag_Ex >
21  struct is_complex<complex<Real_Ex, Imag_Ex>> : std::true_type {};
22 
23  template< typename T >
25 
30  template< typename T >
31  concept Complex = is_complex_v<T>;
32 
33 
38  template< Expression Real_Ex, Expression Imag_Ex >
39  Real_Ex real( complex<Real_Ex, Imag_Ex> const& c ) noexcept
40  {
41  return c.real_;
42  }
43 
48  template< Expression Real_Ex, Expression Imag_Ex >
49  Imag_Ex imag( complex<Real_Ex, Imag_Ex> const& c ) noexcept
50  {
51  return c.imag_;
52  }
53 
54 
66  template< Complex C >
67  auto abs( C const& c ) noexcept
68  {
69  return hypot( real(c), imag(c) );
70  }
71 
72 
84  template< Complex C >
85  auto norm( C const& c ) noexcept
86  {
87  auto const& r = real( c );
88  auto const& i = imag( c );
89  return hadamard_product( r, r ) + hadamard_product( i, i );
90  }
91 
92 
104  template< Complex C >
105  auto conj( C const& c ) noexcept
106  {
107  return complex{ real(c), -imag(c) };
108  }
109 
110 
122  template< Expression Em, Expression Ep >
123  auto polar( Em const& em, Ep const& ep ) noexcept
124  {
125  return complex{ hadamard_product( em, cos(ep) ), hadamard_product( em, sin(ep) ) };
126  }
127 
128 
129 
143  template< Complex C >
144  auto arg( C const& c ) noexcept
145  {
146  return atan2( imag(c), real(c) );
147  }
148 
149 
153  template< Complex C >
154  auto operator + ( C const& c ) noexcept
155  {
156  return c;
157  }
158 
162  template< Complex C >
163  auto operator - ( C const& c ) noexcept
164  {
165  return complex{ negative(real(c)), negative(imag(c)) };
166  }
167 
168 
172  template< Complex Cl, Complex Cr >
173  auto operator + ( Cl const& cl, Cr const& cr ) noexcept
174  {
175  return complex{ real(cl)+real(cr), imag(cl)+imag(cr) };
176  }
177 
178 
182  template< Complex Cl, Complex Cr >
183  auto operator - ( Cl const& cl, Cr const& cr ) noexcept
184  {
185  return complex{ real(cl)-real(cr), imag(cl)-imag(cr) };
186  }
187 
188 
199  template< Complex Cl, Complex Cr >
200  auto operator * ( Cl const& cl, Cr const& cr ) noexcept
201  {
202  auto const& a = real(cl);
203  auto const& b = imag(cl);
204  auto const& c = real(cr);
205  auto const& d = imag(cr);
206  auto const& ac = a * c; // 1st multiplication
207  auto const& bd = b * d; // 2nd multiplication
208  auto const& a_b = a + b;
209  auto const& c_d = c + d;
210  auto const& abcd = a_b * c_d; // 3rd multiplication
211 
212  return complex{ ac-bd, abcd-ac-bd };
213  }
214 
215 
216 
220  template< Complex C, Expression E >
221  auto operator + ( C const& c, E const& e ) noexcept
222  {
223  return complex{ real(c)+e, imag(c) };
224  }
225 
229  template< Complex C, Expression E >
230  auto operator + ( E const& e, C const& c ) noexcept
231  {
232  return c + e;
233  }
234 
235 
239  template< Complex C, Expression E >
240  auto operator - ( C const& c, E const& e ) noexcept
241  {
242  return complex{ real(c)-e, imag(c) };
243  }
244 
248  template< Complex C, Expression E >
249  auto operator - ( E const& e, C const& c ) noexcept
250  {
251  return c + e;
252  }
253 
257  template< Complex C, Expression E >
258  auto operator * ( C const& c, E const& e ) noexcept
259  {
260  return complex{ real(c)*e, imag(c)*e };
261  }
262 
266  template< Complex C, Expression E >
267  auto operator * ( E const& e, C const& c ) noexcept
268  {
269  return c * e;
270  }
271 
272 
273 }//namespace ceras
274 
275 #endif//SJTXPMDBEYHNPQSGKFIEKTKOYWOFMOEGAHNOHMJVHJIAWTBHCCFUKCHLJMJAFPRHRXEOTYEDC
276 
Definition: activation.hpp:12
Imag_Ex imag(complex< Real_Ex, Imag_Ex > const &c) noexcept
Definition: complex_operator.hpp:49
constexpr bool is_complex_v
Definition: complex_operator.hpp:24
auto norm(C const &c) noexcept
Returns the squared magnitude of the complex expression.
Definition: complex_operator.hpp:85
concept Complex
A type that represents a complex expression.
Definition: complex_operator.hpp:31
Real_Ex real(complex< Real_Ex, Imag_Ex > const &c) noexcept
Definition: complex_operator.hpp:39
auto operator+(C const &c) noexcept
Returns the complex expression.
Definition: complex_operator.hpp:154
auto abs(C const &c) noexcept
Returns the magnitude of the complex expression.
Definition: complex_operator.hpp:67
constexpr auto negative(Ex const &ex) noexcept
Definition: operation.hpp:389
auto operator-(C const &c) noexcept
Negatives the complex expression.
Definition: complex_operator.hpp:163
auto conj(C const &c) noexcept
Returns the conjugate of the complex expression.
Definition: complex_operator.hpp:105
auto polar(Em const &em, Ep const &ep) noexcept
Returns with given magnitude and phase angle.
Definition: complex_operator.hpp:123
constexpr auto hadamard_product(Lhs_Expression const &lhs_ex, Rhs_Expression const &rhs_ex) noexcept
Definition: operation.hpp:444
auto arg(C const &c) noexcept
Calculates the phase angle (in radians) of the complex expression.
Definition: complex_operator.hpp:144
auto operator*(Cl const &cl, Cr const &cr) noexcept
Multiplies two complex expressions. Optimization here: (a+ib)*(c+id) = (ac-bd) + i(ad+bc) = (ac-bd) +...
Definition: complex_operator.hpp:200
constexpr auto sin(Ex const &ex) noexcept
Computes Sin of the given expression.
Definition: operation.hpp:3583
*endcode **constexpr auto hypot(Ex const &ex, Ey const &ey) noexcept
Definition: operation.hpp:632
constexpr auto atan2(Lhs_Expression const &lhs_ex, Rhs_Expression const &rhs_ex) noexcept
Computes the arc tangent of y/x using the signs of arguments to determine the correct quadrant.
Definition: operation.hpp:1672
constexpr auto cos(Ex const &ex) noexcept
Computes Cos of the given expression.
Definition: operation.hpp:2777
Definition: complex_operator.hpp:11
Imag_Ex imag_
Definition: complex_operator.hpp:13
Real_Ex real_
Definition: complex_operator.hpp:12
Definition: complex_operator.hpp:18