源码拾贝三则

发布于:2024-05-09 ⋅ 阅读:(25) ⋅ 点赞:(0)

目录

一  一种枚举类型的新型使用方式

二 Eigen库中的LDLT分解

三 Eigen中的访问者模式


一  一种枚举类型的新型使用方式

///D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\xiosbase							
							
    enum _Iostate { // constants for stream states							
        _Statmask = 0x17							
    };							
							
    static constexpr _Iostate goodbit = static_cast<_Iostate>(0x0);							
    static constexpr _Iostate eofbit  = static_cast<_Iostate>(0x1);							
    static constexpr _Iostate failbit = static_cast<_Iostate>(0x2);							
    static constexpr _Iostate badbit  = static_cast<_Iostate>(0x4);							
							
							
    _NODISCARD bool __CLR_OR_THIS_CALL good() const {							
        return rdstate() == ios_base::goodbit;							
    }							
							
    _NODISCARD bool __CLR_OR_THIS_CALL eof() const {							
        return rdstate() & ios_base::eofbit;							
    }							
							
    _NODISCARD bool __CLR_OR_THIS_CALL fail() const {							
        return rdstate() & (ios_base::badbit | ios_base::failbit);							
    }							
							
    _NODISCARD bool __CLR_OR_THIS_CALL bad() const {							
        return rdstate() & ios_base::badbit;							
    }							
							
    _NODISCARD iostate __CLR_OR_THIS_CALL exceptions() const {							
        return _Except;							
    }							
							
    void __CLR_OR_THIS_CALL exceptions(iostate _Newexcept) { // set exception mask to argument							
        _Except = _Newexcept & _Statmask;							
        clear(rdstate());							
    }							
							
							

二 Eigen库中的LDLT分解

													
Eigen-3.4.0\Eigen\src\Cholesky\LDLT.h													
template<typename _MatrixType,int _UpLo>													
template<bool Conjugate, typename RhsType, typename DstType>													
void LDLT<_MatrixType,_UpLo>::_solve_impl_transposed(const RhsType &rhs, DstType &dst) const													
{													
  // dst = P b													
  dst = m_transpositions * rhs;													
													
  // dst = L^-1 (P b)													
  // dst = L^-*T (P b)													
  matrixL().template conjugateIf<!Conjugate>().solveInPlace(dst);													
													
  // dst = D^-* (L^-1 P b)													
  // dst = D^-1 (L^-*T P b)													
  // more precisely, use pseudo-inverse of D (see bug 241)													
  using std::abs;													
  const typename Diagonal<const MatrixType>::RealReturnType vecD(vectorD());													
  // In some previous versions, tolerance was set to the max of 1/highest (or rather numeric_limits::min())													
  // and the maximal diagonal entry * epsilon as motivated by LAPACK's xGELSS:													
  // RealScalar tolerance = numext::maxi(vecD.array().abs().maxCoeff() * NumTraits<RealScalar>::epsilon(),RealScalar(1) / NumTraits<RealScalar>::highest());													
  // However, LDLT is not rank revealing, and so adjusting the tolerance wrt to the highest													
  // diagonal element is not well justified and leads to numerical issues in some cases.													
  // Moreover, Lapack's xSYTRS routines use 0 for the tolerance.													
  // Using numeric_limits::min() gives us more robustness to denormals.													
  RealScalar tolerance = (std::numeric_limits<RealScalar>::min)();													
  for (Index i = 0; i < vecD.size(); ++i)													
  {													
    if(abs(vecD(i)) > tolerance)													
      dst.row(i) /= vecD(i);													
    else													
      dst.row(i).setZero();													
  }													
													
  // dst = L^-* (D^-* L^-1 P b)													
  // dst = L^-T (D^-1 L^-*T P b)													
  matrixL().transpose().template conjugateIf<Conjugate>().solveInPlace(dst);													
													
  // dst = P^T (L^-* D^-* L^-1 P b) = A^-1 b													
  // dst = P^-T (L^-T D^-1 L^-*T P b) = A^-1 b													
  dst = m_transpositions.transpose() * dst;													
}													
#endif													
													
													
													
													

三 Eigen中的访问者模式

Eigen-3.4.0\Eigen\src\Core\Visitor.h
template<typename Derived>
template<typename Visitor>
EIGEN_DEVICE_FUNC
void DenseBase<Derived>::visit(Visitor& visitor) const
{
  if(size()==0)
    return;

  typedef typename internal::visitor_evaluator<Derived> ThisEvaluator;
  ThisEvaluator thisEval(derived());

  enum {
    unroll =  SizeAtCompileTime != Dynamic
           && SizeAtCompileTime * int(ThisEvaluator::CoeffReadCost) + (SizeAtCompileTime-1) * int(internal::functor_traits<Visitor>::Cost) <= EIGEN_UNROLLING_LIMIT
  };
  return internal::visitor_impl<Visitor, ThisEvaluator, unroll ? int(SizeAtCompileTime) : Dynamic>::run(thisEval, visitor);
}


网站公告

今日签到

点亮在社区的每一天
去签到