TBTK
Need a break? Support the development by playing Polarity Puzzles
Complex.h
Go to the documentation of this file.
1 /* Copyright 2019 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_COMPLEX
24 #define COM_DAFER45_TBTK_COMPLEX
25 
26 #include "TBTK/Boolean.h"
28 #include "TBTK/Real.h"
29 
30 #include <complex>
31 #include <sstream>
32 
33 namespace TBTK{
34 
35 #if TBTK_WRAP_PRIMITIVE_TYPES
36 
39 public:
41  Complex(){};
42 
46  constexpr Complex(std::complex<double> value) : value(value) {}
47 
51  constexpr Complex(Real real, Real imag) : value(real, imag) {}
52 
56  constexpr Complex(double value) : value(value) {}
57 
64  Complex(const std::string &serialization, Serializable::Mode mode);
65 
67  constexpr operator std::complex<double>() const{
68  return value;
69  };
70 
76  Complex& operator=(const std::complex<double> &rhs){
77  value = rhs;
78 
79  return *this;
80  }
81 
87  Complex& operator+=(const Complex &rhs){
88  value += rhs.value;
89 
90  return *this;
91  }
92 
99  friend std::complex<double>& operator+=(
100  std::complex<double> &lhs,
101  const Complex &rhs
102  ){
103  lhs += rhs.value;
104 
105  return lhs;
106  }
107 
114  friend Complex operator+(const Complex &lhs, const double &rhs){
115  Complex result(lhs);
116  result.value += rhs;
117 
118  return result;
119  }
120 
127  friend Complex operator+(const double &lhs, const Complex &rhs){
128  Complex result(rhs);
129  result.value += lhs;
130 
131  return result;
132  }
133 
139  Complex& operator-=(const Complex &rhs){
140  value -= rhs.value;
141 
142  return *this;
143  }
144 
150  Complex operator-(const Complex &rhs) const{
151  Complex result(*this);
152  result -= rhs;
153 
154  return result;
155  }
156 
163  friend Complex operator-(const Complex &lhs, const double &rhs){
164  Complex result(lhs);
165  result.value -= rhs;
166 
167  return result;
168  }
169 
176  friend Complex operator-(const double &lhs, const Complex &rhs){
177  Complex result(lhs - rhs.value.real(), -rhs.value.imag());
178 
179  return result;
180  }
181 
186  return Complex(-value);
187  }
188 
194  Complex& operator*=(const Complex &rhs){
195  value *= rhs.value;
196 
197  return *this;
198  }
199 
206  friend Complex operator*(const Complex &lhs, const double &rhs){
207  Complex result(lhs);
208  result.value *= rhs;
209 
210  return result;
211  }
212 
219  friend Complex operator*(const double &lhs, const Complex &rhs){
220  Complex result(rhs);
221  result.value *= lhs;
222 
223  return result;
224  }
225 
231  Complex& operator/=(const Complex &rhs){
232  value /= rhs.value;
233 
234  return *this;
235  }
236 
242  friend Complex operator/(const Complex &lhs, const double &rhs){
243  Complex result(lhs);
244  result.value /= rhs;
245 
246  return result;
247  }
248 
254  Boolean operator==(const Complex &rhs) const{
255  return value == rhs.value;
256  }
257 
263  Boolean operator!=(const Complex &rhs) const{
264  return value != rhs.value;
265  }
266 
273  friend std::ostream& operator<<(std::ostream &os, const Complex &complex){
274  os << complex.value;
275 
276  return os;
277  }
278 
285  friend std::istream& operator>>(std::istream &is, Complex &complex){
286  is >> complex.value;
287 
288  return is;
289  }
290 
298  std::string serialize(Serializable::Mode mode) const;
299 
305  friend constexpr Real real(const Complex &complex){
306  return real(complex.value);
307  }
308 
314  friend constexpr Real imag(const Complex &complex){
315  return imag(complex.value);
316  }
317 
323  friend Complex conj(const Complex &complex){
324  return conj(complex.value);
325  }
326 
332  friend Real abs(const Complex &complex){
333  return abs(complex.value);
334  }
335 private:
337  std::complex<double> value;
338 };
339 
341  const std::string &serialization,
342  Serializable::Mode mode
343 ){
344  switch(mode){
345  case Serializable::Mode::JSON:
346  {
347  std::stringstream ss(serialization);
348  ss >> value;
349 
350  break;
351  }
352  default:
353  TBTKExit(
354  "Complex::Complex()",
355  "Only Serializable::Mode::JSON is supported yet.",
356  ""
357  );
358  }
359 }
360 
361 inline std::string Complex::serialize(Serializable::Mode mode) const{
362  switch(mode){
363  case Serializable::Mode::JSON:
364  {
365  std::stringstream ss;
366  ss << value;
367 
368  return ss.str();
369  }
370  default:
371  TBTKExit(
372  "Complex::serialize()",
373  "Only Serializable::Mode::JSON is supported yet.",
374  ""
375  );
376  }
377 }
378 
379 #else
380  typedef std::complex<double> Complex;
381 #endif
382 
383 }; //End of namespace TBTK
384 
385 #endif
TBTK::Complex::operator+
friend Complex operator+(const Complex &lhs, const double &rhs)
Definition: Complex.h:114
TBTK::Complex::operator<<
friend std::ostream & operator<<(std::ostream &os, const Complex &complex)
Definition: Complex.h:273
TBTK::Complex::operator/=
Complex & operator/=(const Complex &rhs)
Definition: Complex.h:231
TBTK::Boolean
Boolean number.
Definition: Boolean.h:37
TBTK::Complex::serialize
std::string serialize(Serializable::Mode mode) const
Definition: Complex.h:361
TBTK::Complex::operator+=
friend std::complex< double > & operator+=(std::complex< double > &lhs, const Complex &rhs)
Definition: Complex.h:99
TBTK::Complex::operator*=
Complex & operator*=(const Complex &rhs)
Definition: Complex.h:194
TBTK::Complex::operator>>
friend std::istream & operator>>(std::istream &is, Complex &complex)
Definition: Complex.h:285
TBTK::Complex::operator-
Complex operator-(const Complex &rhs) const
Definition: Complex.h:150
TBTK::Complex::operator!=
Boolean operator!=(const Complex &rhs) const
Definition: Complex.h:263
Boolean.h
Boolean number.
TBTK::Complex::operator-
friend Complex operator-(const double &lhs, const Complex &rhs)
Definition: Complex.h:176
TBTK::Real
Real number.
Definition: Real.h:33
TBTK::Complex::operator==
Boolean operator==(const Complex &rhs) const
Definition: Complex.h:254
TBTK::Complex::Complex
constexpr Complex(double value)
Definition: Complex.h:56
TBTK::Complex::real
constexpr friend Real real(const Complex &complex)
Definition: Complex.h:305
TBTK::Complex::operator*
friend Complex operator*(const Complex &lhs, const double &rhs)
Definition: Complex.h:206
TBTK::Complex::operator+
friend Complex operator+(const double &lhs, const Complex &rhs)
Definition: Complex.h:127
TBTK::Complex::operator-
Complex operator-() const
Definition: Complex.h:185
TBTK::Complex::operator=
Complex & operator=(const std::complex< double > &rhs)
Definition: Complex.h:76
TBTK::Complex
Complex number.
Definition: Complex.h:38
TBTK::Complex::operator+=
Complex & operator+=(const Complex &rhs)
Definition: Complex.h:87
TBTK::Complex::imag
constexpr friend Real imag(const Complex &complex)
Definition: Complex.h:314
TBTK::Complex::Complex
constexpr Complex(Real real, Real imag)
Definition: Complex.h:51
TBTK::Complex::operator*
friend Complex operator*(const double &lhs, const Complex &rhs)
Definition: Complex.h:219
TBTK::Complex::conj
friend Complex conj(const Complex &complex)
Definition: Complex.h:323
TBTK::Complex::operator/
friend Complex operator/(const Complex &lhs, const double &rhs)
Definition: Complex.h:242
TBTK::Complex::abs
friend Real abs(const Complex &complex)
Definition: Complex.h:332
TBTK::Complex::Complex
Complex()
Definition: Complex.h:41
PseudoSerializable.h
Base class for psudo-serializable objects.
TBTK::Complex::operator-=
Complex & operator-=(const Complex &rhs)
Definition: Complex.h:139
TBTK::Serializable::Mode
Mode
Definition: Serializable.h:47
TBTK::Complex::operator-
friend Complex operator-(const Complex &lhs, const double &rhs)
Definition: Complex.h:163
TBTK::PseudoSerializable
Definition: PseudoSerializable.h:31
Real.h
Real number.
TBTK::Complex::Complex
constexpr Complex(std::complex< double > value)
Definition: Complex.h:46