Tomographer  v2.0
Tomographer C++ Framework Documentation
pos_semidef_util.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) 2015 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
27 
28 #ifndef _TOMOGRAPHER_MATHTOOLS_POS_SEMIDEF_UTIL
29 #define _TOMOGRAPHER_MATHTOOLS_POS_SEMIDEF_UTIL
30 
37 #include <Eigen/Core>
38 #include <Eigen/Eigenvalues>
39 
40 #include <tomographer2/tools/cxxutil.h> // tomographer_assert()
41 
42 
43 namespace Tomographer {
44 namespace MathTools {
45 
46 
55 template<typename VectorType>
57  const typename Eigen::NumTraits<typename VectorType::Scalar>::Real tolerance = 1e-8)
58 {
59  typedef typename Eigen::NumTraits<typename VectorType::Scalar>::Real RealScalar;
60  typedef typename VectorType::Index Index;
61 
62  RealScalar trace = 0;
63  RealScalar new_excess_trace = 0;
64  Index k;
65  Index num_pos = 0;
66  for (k = 0; k < vec.size(); ++k) {
67  trace += vec(k);
68  if (vec(k) < tolerance) {
69  new_excess_trace += tolerance - vec(k);
70  } else {
71  ++num_pos;
72  }
73  }
74 
75  tomographer_assert(trace >= vec.size()*tolerance);
76 
77  RealScalar remove_from_each = new_excess_trace / num_pos;
78  for (k = 0; k < vec.size(); ++k) {
79  if (vec(k) < tolerance) {
80  vec(k) = tolerance;
81  } else {
82  vec(k) -= remove_from_each;
83  }
84  }
85 }
86 
94 template<typename MatrixType, typename Der>
95 inline MatrixType forcePosSemiDef(const Eigen::MatrixBase<Der> & rho,
96  const typename Eigen::NumTraits<typename MatrixType::Scalar>::Real tolerance = 1e-8)
97 {
98  typedef typename Eigen::SelfAdjointEigenSolver<MatrixType>::RealVectorType RealVectorType;
99 
101  rho.template cast<typename MatrixType::Scalar>()
102  );
103  auto U = eig_rho_ref.eigenvectors();
104  RealVectorType d = eig_rho_ref.eigenvalues();
105 
106  forcePosVecKeepSum<RealVectorType>(d, tolerance);
107 
108  return U * d.asDiagonal() * U.adjoint();
109 }
110 
111 
119 template<typename MatrixType>
120 inline MatrixType safeOperatorSqrt(const Eigen::Ref<const MatrixType> & A,
122  tolerance = 1e-8)
123 {
125  auto U = eig_A_ref.eigenvectors();
126  auto d = eig_A_ref.eigenvalues();
127 
128  typedef typename Eigen::SelfAdjointEigenSolver<MatrixType>::RealVectorType RealVectorType;
129 
130  forcePosVecKeepSum<RealVectorType>(d, tolerance);
131 
132  return U * d.cwiseSqrt().asDiagonal() * U.adjoint();
133 }
134 
143 template<typename MatrixType>
146  tolerance = 1e-8)
147 {
149  auto U = eig_A_ref.eigenvectors();
150  auto d = eig_A_ref.eigenvalues();
151 
152  typedef typename Eigen::SelfAdjointEigenSolver<MatrixType>::RealVectorType RealVectorType;
153 
154  forcePosVecKeepSum<RealVectorType>(d, tolerance);
155 
156  for (typename MatrixType::Index k = 0; k < d.size(); ++k) {
157  if (d(k) > tolerance) {
158  d(k) = 1 / std::sqrt(d(k));
159  }
160  }
161 
162  return U * d.asDiagonal() * U.adjoint();
163 }
164 
165 
166 
167 
168 } // MathTools
169 } // Tomographer
170 
171 
172 
173 
174 
175 #endif
Base namespace for the Tomographer project.
Definition: densellh.h:44
const RealVectorType & eigenvalues() const
MatrixType safeOperatorSqrt(const Eigen::Ref< const MatrixType > &A, const typename Eigen::NumTraits< typename MatrixType::Scalar >::Real tolerance=1e-8)
Safe version of operator square root for positive semidefinite matrices.
Some C++ utilities, with a tad of C++11 tricks.
internal::plain_col_type< MatrixType, RealScalar >::type RealVectorType
void forcePosVecKeepSum(Eigen::Ref< VectorType > vec, const typename Eigen::NumTraits< typename VectorType::Scalar >::Real tolerance=1e-8)
Make sure that the given vector is numerically positive.
const MatrixType & eigenvectors() const
MatrixType safeOperatorInvSqrt(const Eigen::Ref< const MatrixType > &A, const typename Eigen::NumTraits< typename MatrixType::Scalar >::Real tolerance=1e-8)
Safe version of operator inverse square root for positive semidefinite matrices.
MatrixType forcePosSemiDef(const Eigen::MatrixBase< Der > &rho, const typename Eigen::NumTraits< typename MatrixType::Scalar >::Real tolerance=1e-8)
Make sure that rho is numerically positive semidefinite.
T sqrt(T...args)