源码拾贝三则

目录

[一 一种枚举类型的新型使用方式](#一 一种枚举类型的新型使用方式)

[二 Eigen库中的LDLT分解](#二 Eigen库中的LDLT分解)

[三 Eigen中的访问者模式](#三 Eigen中的访问者模式)


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

cpp 复制代码
///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分解

cpp 复制代码
													
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中的访问者模式

cpp 复制代码
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);
}
相关推荐
无聊的小坏坏1 小时前
单调栈通关指南:从力扣 84 到力扣 42
c++·算法·leetcode
YOLO大师2 小时前
华为OD机试 2025B卷 - 小明减肥(C++&Python&JAVA&JS&C语言)
c++·python·华为od·华为od机试·华为od2025b卷·华为机试2025b卷·华为od机试2025b卷
看到我,请让我去学习3 小时前
OpenCV编程- (图像基础处理:噪声、滤波、直方图与边缘检测)
c语言·c++·人工智能·opencv·计算机视觉
xiaolang_8616_wjl11 小时前
c++文字游戏_闯关打怪2.0(开源)
开发语言·c++·开源
夜月yeyue12 小时前
设计模式分析
linux·c++·stm32·单片机·嵌入式硬件
无小道12 小时前
c++-引用(包括完美转发,移动构造,万能引用)
c语言·开发语言·汇编·c++
FirstFrost --sy14 小时前
数据结构之二叉树
c语言·数据结构·c++·算法·链表·深度优先·广度优先
Tanecious.14 小时前
C++--map和set的使用
开发语言·c++
Yingye Zhu(HPXXZYY)14 小时前
Codeforces 2021 C Those Who Are With Us
数据结构·c++·算法
liulilittle15 小时前
LinkedList 链表数据结构实现 (OPENPPP2)
开发语言·数据结构·c++·链表