Tomographer  v5.2
Tomographer C++ Framework Documentation
param_herm_x.h
Go to the documentation of this file.
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  * Copyright (c) 2017 Caltech, Institute for Quantum Information and Matter, Philippe Faist
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27 
28 #ifndef TOMOGRAPHER_DENSEDM_PARAM_HERM_X_H
29 #define TOMOGRAPHER_DENSEDM_PARAM_HERM_X_H
30 
31 
32 
39 #include <cmath>
40 #include <complex>
41 
42 #include <boost/math/constants/constants.hpp>
43 
44 #include <Eigen/Core>
45 
46 #include <tomographer/tools/cxxutil.h> // static_or_dynamic, tomographer_assert()
47 
48 
49 
50 namespace Tomographer {
51 namespace DenseDM {
52 
56 template<typename DMTypes_>
57 class TOMOGRAPHER_EXPORT ParamX
58 {
59 public:
60  typedef DMTypes_ DMTypes;
61  typedef typename DMTypes::MatrixType MatrixType;
65  typedef typename DMTypes::RealScalar RealScalar;
66  typedef typename DMTypes::ComplexScalar ComplexScalar;
67  typedef typename MatrixType::Index IndexType;
68 
72  ParamX(DMTypes dmt) : _dmt(dmt) { }
73 
81  inline VectorParamType HermToX(MatrixTypeConstRef Herm) const
82  {
83  // hope RVO kicks in
84  VectorParamType x(_dmt.initVectorParamType());
85  const IndexType dimtri = (_dmt.dim2() - _dmt.dim())/2;
86 
87  tomographer_assert((IndexType)_dmt.dim() == Herm.cols()); // assert Herm is (dim x dim)
88 
89  x.block(0,0,(IndexType)_dmt.dim(),1) = Herm.real().diagonal();
90 
91  IndexType k = (IndexType)_dmt.dim();
92  IndexType n, m;
93  for (n = 1; n < (IndexType)_dmt.dim(); ++n) {
94  for (m = 0; m < n; ++m) {
95  x(k) = Herm(n,m).real() * boost::math::constants::root_two<RealScalar>();
96  x(dimtri + k) = Herm(n,m).imag() * boost::math::constants::root_two<RealScalar>();
97  ++k;
98  }
99  }
100 
101  return x;
102  }
103 
109  template<bool OnlyLowerTri = false>
110  inline MatrixType XToHerm(VectorParamTypeConstRef x) const
111  {
112  // should be optimized by compiler via RVO
113  MatrixType Herm(_dmt.initMatrixType());
114 
115  const IndexType dimtri = (IndexType)(_dmt.dim2()-_dmt.dim())/2;
116  tomographer_assert(x.rows() == (IndexType)_dmt.dim2() && x.cols() == 1); // assert x is (dim*dim x 1)
117 
118  Herm.diagonal().real() = x.block(0,0,(IndexType)_dmt.dim(),1);
119  Herm.diagonal().imag().setZero();
120 
121  IndexType k = (IndexType)_dmt.dim();
122  IndexType n, m;
123  for (n = 1; n < (IndexType)_dmt.dim(); ++n) {
124  for (m = 0; m < n; ++m) {
125  Herm(n,m) = boost::math::constants::half_root_two<RealScalar>() * ComplexScalar(x(k), x(dimtri + k));
126  if (!OnlyLowerTri) {
127  // complex conj. on opposite triangular part
128  Herm(m,n) = boost::math::constants::half_root_two<RealScalar>() * ComplexScalar(x(k), -x(dimtri + k));
129  }
130  ++k;
131  }
132  }
133  return Herm;
134  }
135 
136 protected:
137  const DMTypes _dmt;
138 };
139 
140 
141 
142 
143 } // namespace DenseDM
144 } // namespace Tomographer
145 
146 
147 #endif
Base namespace for the Tomographer project.
Definition: densellh.h:45
internal::traits< Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::Index Index
Convert hermitian matrices to vectors via their X Parameterization.
Definition: param_herm_x.h:57
STL class.
MatrixType XToHerm(VectorParamTypeConstRef x) const
Get the Hermitian matrix parameterized by the "X-parameter" vector x.
Definition: param_herm_x.h:110
VectorParamType HermToX(MatrixTypeConstRef Herm) const
Get the X-parameterization corresponding to a given hermitian matrix.
Definition: param_herm_x.h:81
Some C++ utilities, with a tad of C++11 tricks.
ParamX(DMTypes dmt)
Constructor. Just give it the DMTypes instance.
Definition: param_herm_x.h:72
#define tomographer_assert(...)
Assertion test macro.
Definition: cxxdefs.h:84
RealScalar_ RealScalar
Real scalar type, given in template parameter. Usually double is fine.
Definition: dmtypes.h:121