YAP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ComplexBasis.h
Go to the documentation of this file.
1 /* YAP - Yet another PWA toolkit
2  Copyright 2015, Technische Universitaet Muenchen,
3  Authors: Daniel Greenwald, Johannes Rauch
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
20 
21 #ifndef yap_ComplexBasis_h
22 #define yap_ComplexBasis_h
23 
24 #include "fwd/ComplexBasis.h"
25 
26 #include "Matrix.h"
27 #include "Vector.h"
28 
29 #include <complex>
30 #include <math.h>
31 
32 namespace yap {
33 
34 namespace complex_basis {
35 
37 template <typename T>
38 using covariance_matrix = SquareMatrix<T, 2>;
39 
40 
41 namespace jacobian {
42 
44 template <typename T>
45 SquareMatrix<T, 2> polar_to_cartesian(const polar<T>& pol)
46 {
47  std::complex<T> v(pol);
48  return SquareMatrix<T, 2>({cos(std::arg(v)), -std::abs(v)*sin(std::arg(v)),
49  sin(std::arg(v)), std::abs(v)*cos(std::arg(v)) });
50 }
51 
53 template <typename T>
54 SquareMatrix<T, 2> cartesian_to_polar(const cartesian<T>& cart)
55 {
56  std::complex<T> v(cart);
57  return SquareMatrix<T, 2>({ std::real(v)/std::abs(v), std::imag(v)/std::abs(v),
58  -std::imag(v)/std::norm(v), std::real(v)/std::norm(v) });
59 }
60 
61 }
62 
65 template <typename T>
66 class basis {
67 protected:
69  basis(const Vector<T, 2>& val, const covariance_matrix<T>& cov) :
70  Value_(val), Covariance_(cov)
71  {}
72 
73 public:
75  virtual operator std::complex<T>() const = 0;
76 
78  const Vector<T, 2>& value() const
79  { return Value_; }
80 
82  const covariance_matrix<T>& covariance() const
83  { return Covariance_; }
84 
85 private:
86  const Vector<T, 2> Value_;
87  const covariance_matrix<T> Covariance_;
88 };
89 
92 template <typename T>
93 class cartesian : public basis<T> {
94 public:
99  explicit cartesian(T re, T im, const covariance_matrix<T>& cov = covariance_matrix<T>()) :
100  basis<T>(Vector<T, 2>({re, im}), cov)
101  {}
102 
106  explicit cartesian(const Vector<T, 2>& val, const covariance_matrix<T>& cov = covariance_matrix<T>()) :
107  basis<T>(val, cov)
108  {}
109 
113  explicit cartesian(const std::complex<T>& val, const covariance_matrix<T>& cov = covariance_matrix<T>()) :
114  cartesian<T>(std::real(val), std::imag(val), cov)
115  {}
116 
120  explicit cartesian(const std::complex<T>& val, std::initializer_list<T> cov_elements) :
121  cartesian<T>(val, symmetricMatrix<T, 2>(std::forward<std::initializer_list<T>>(cov_elements)))
122  {}
123 
126  cartesian(const polar<T>& pol) :
127  cartesian<T>(std::complex<T>(pol),
128  jacobian::polar_to_cartesian(pol) * pol.covariance() * transpose(jacobian::polar_to_cartesian(pol)))
129  {}
130 
132  virtual operator std::complex<T>() const override
133  { return std::complex<T>(basis<T>::value()[0], basis<T>::value()[1]); }
134 };
135 
138 template <typename T>
139 class polar : public basis<T> {
140 public:
145  explicit polar(T r, T phi, const covariance_matrix<T>& cov = covariance_matrix<T>()) :
146  basis<T>(Vector<T, 2>({r, phi}), cov)
147  {}
148 
152  explicit polar(const Vector<T, 2>& val, const covariance_matrix<T>& cov = covariance_matrix<T>()) :
153  basis<T>(val, cov)
154  {}
155 
160  explicit polar(T r, T phi, std::initializer_list<T> cov_elements) :
161  polar<T>(r, phi,
162  symmetricMatrix<T, 2>(std::forward<std::initializer_list<T>>(cov_elements)))
163  {}
164 
167  polar(const cartesian<T>& cart) :
168  polar<T>(std::abs(std::complex<T>(cart)), std::arg(std::complex<T>(cart)),
169  jacobian::cartesian_to_polar(cart) * cart.covariance() * transpose(jacobian::cartesian_to_polar(cart)))
170  {}
171 
173  virtual operator std::complex<T>() const override
174  { return std::polar<T>(basis<T>::value()[0], basis<T>::value()[1]); }
175 };
176 
177 
179 template <typename T>
180 inline std::string to_string(const cartesian<T>& z)
181 {
182  std::string s = to_string(z.value()[0]) + " + i * " + to_string(z.value()[1]);
183  s += "covariance = " + to_string(z.covariance());
184  return s;
185 }
186 
188 template <typename T>
189 inline std::string to_string(const polar<T>& z)
190 {
191  std::string s = to_string(z.value()[0]) + " * exp(i * " + to_string(z.value()[1]) + ")";
192  s += "covariance = " + to_string(z.covariance());
193  return s;
194 }
195 
196 }
197 
198 }
199 
200 #endif
RealIntegralElement real(const ComplexIntegralElement &Z)
Definition: IntegralElement.h:144
const Vector< T, 2 > & value() const
Definition: ComplexBasis.h:78
polar(T r, T phi, const covariance_matrix< T > &cov=covariance_matrix< T >())
Definition: ComplexBasis.h:145
Definition: ComplexBasis.h:139
const Matrix< T, C, R > transpose(const Matrix< T, R, C > &M)
transpose a matrix
Definition: Matrix.h:148
constexpr T abs(const Vector< T, N > &A)
Definition: Vector.h:371
basis(const Vector< T, 2 > &val, const covariance_matrix< T > &cov)
constructor
Definition: ComplexBasis.h:69
const SquareMatrix< T, N > symmetricMatrix(std::initializer_list< T > elements)
Definition: Matrix.h:125
cartesian(T re, T im, const covariance_matrix< T > &cov=covariance_matrix< T >())
Definition: ComplexBasis.h:99
Definition: ComplexBasis.h:66
const covariance_matrix< T > & covariance() const
Definition: ComplexBasis.h:82
std::string to_string(const CachedValue::Status &S)
streaming operator for CachedValue::Status
Definition: CachedValue.cxx:27
Definition: ComplexBasis.h:93
constexpr T phi(const ThreeVector< T > &V, const CoordinateSystem< T, 3 > &C)
Definition: CoordinateSystem.h:94
RealIntegralElement imag(const ComplexIntegralElement &Z)
Definition: IntegralElement.h:148