⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 concepts.hpp

📁 矩阵运算源码最新版本
💻 HPP
📖 第 1 页 / 共 2 页
字号:
// ======================================// Algebraic concepts with two connectors// ======================================// -----------------// Based on functors// -----------------// More generic, less handy to useauto concept GenericRing<typename AddOp, typename MultOp, typename Element>  : AbelianGroup<AddOp, Element>,    SemiGroup<MultOp, Element>,    algebra::Ring<AddOp, MultOp, Element>{};auto concept GenericCommutativeRing<typename AddOp, typename MultOp, typename Element>  : GenericRing<AddOp, MultOp, Element>,    CommutativeSemiGroup<MultOp, Element>{};auto concept GenericRingWithIdentity<typename AddOp, typename MultOp, typename Element>  : GenericRing<AddOp, MultOp, Element>,    Monoid<MultOp, Element>,    algebra::RingWithIdentity<AddOp, MultOp, Element>{};auto concept GenericCommutativeRingWithIdentity<typename AddOp, typename MultOp, typename Element>  : GenericRingWithIdentity<AddOp, MultOp, Element>,    GenericCommutativeRing<AddOp, MultOp, Element>,    CommutativeMonoid<MultOp, Element>{};// autoconcept GenericDivisionRing<typename AddOp, typename MultOp, typename Element>  : GenericRingWithIdentity<AddOp, MultOp, Element>,    algebra::DivisionRing<AddOp, MultOp, Element>{    requires std::Convertible<inverse_result_type, Element>;};    auto concept GenericField<typename AddOp, typename MultOp, typename Element>  : GenericDivisionRing<AddOp, MultOp, Element>,    GenericCommutativeRingWithIdentity<AddOp, MultOp, Element>,    algebra::Field<AddOp, MultOp, Element>{};// ------------------// Based on operators // ------------------// Handier, less generic// Alternative definitions use MultiplicativeMonoid<Element> for Ring// and call such concepts Pseudo-Ringauto concept Ring<typename Element>  : AdditiveAbelianGroup<Element>,    MultiplicativeSemiGroup<Element>,    GenericRing<math::add<Element>, math::mult<Element>, Element>{};auto concept CommutativeRing<typename Element>  : Ring<Element>,    MultiplicativeCommutativeSemiGroup<Element>,    GenericCommutativeRing<math::add<Element>, math::mult<Element>, Element>    {};auto concept RingWithIdentity<typename Element>  : Ring<Element>,    MultiplicativeMonoid<Element>,    GenericRingWithIdentity<math::add<Element>, math::mult<Element>, Element>{}; auto concept CommutativeRingWithIdentity<typename Element>  : RingWithIdentity<Element>,    CommutativeRing<Element>,    MultiplicativeCommutativeMonoid<Element>,    GenericCommutativeRingWithIdentity<math::add<Element>, math::mult<Element>, Element>{};concept DivisionRing<typename Element>  : RingWithIdentity<Element>,    MultiplicativePartiallyInvertibleMonoid<Element>,     GenericDivisionRing<math::add<Element>, math::mult<Element>, Element>{    axiom NonZeroDivisibility(Element x)    {	if (x != zero(x)) 	    x / x == one(x);    }};    auto concept Field<typename Element>  : DivisionRing<Element>,    CommutativeRingWithIdentity<Element>,    GenericField<math::add<Element>, math::mult<Element>, Element>{};// ======================// Miscellaneous concepts// ======================// that shall find a better place later// EqualityComparable will have the != when defaults are supported// At this point the following won't needed anymoreauto concept FullEqualityComparable<typename T, typename U = T>{  //requires std::EqualityComparable<T, U>;    bool operator==(const T&, const U&);    bool operator!=(const T&, const U&);};// Closure of EqualityComparable under a binary operation:// That is, the result of this binary operation is also EqualityComparable// with itself and with the operand type.auto concept Closed2EqualityComparable<typename Operation, typename Element>  : BinaryIsoFunction<Operation, Element>{    requires FullEqualityComparable<Element>;    requires FullEqualityComparable< BinaryIsoFunction<Operation, Element>::result_type >;    requires FullEqualityComparable< Element, BinaryIsoFunction<Operation, Element>::result_type >;    requires FullEqualityComparable< BinaryIsoFunction<Operation, Element>::result_type, Element >;};// LessThanComparable will have the other operators when defaults are supported// At this point the following won't needed anymoreauto concept FullLessThanComparable<typename T, typename U = T>{    bool operator<(const T&, const U&);    bool operator<=(const T&, const U&);    bool operator>(const T&, const U&);    bool operator>=(const T&, const U&);};// Same for LessThanComparableauto concept Closed2LessThanComparable<typename Operation, typename Element>  : BinaryIsoFunction<Operation, Element>{    requires FullLessThanComparable<Element>;    requires FullLessThanComparable< BinaryIsoFunction<Operation, Element>::result_type >;    requires FullLessThanComparable< Element, BinaryIsoFunction<Operation, Element>::result_type >;    requires FullLessThanComparable< BinaryIsoFunction<Operation, Element>::result_type, Element >;};#if 0auto concept NumericOperatorResultConvertible<typename T>  : AddableWithAssign<T>,    SubtractableWithAssign<T>,    MultiplicableWithAssign<T>,    DivisibleWithAssign<T>{    requires std::Convertible< AddableWithAssign<T>::result_type, T>;    requires std::Convertible< SubtractableWithAssign<T>::result_type, T>;    requires std::Convertible< MultiplicableWithAssign<T>::result_type, T>;    requires std::Convertible< DivisibleWithAssign<T>::result_type, T>;}#endifauto concept AdditionResultConvertible<typename T>{    typename result_type;    result_type operator+(T t, T u);    requires std::Convertible<result_type, T>;    typename result_type;    result_type operator+=(T& t, T u);    requires std::Convertible<result_type, T>;};    auto concept SubtractionResultConvertible<typename T>{    typename result_type;    result_type operator-(T t, T u);    requires std::Convertible<result_type, T>;    typename result_type;    result_type operator-=(T& t, T u);    requires std::Convertible<result_type, T>;};    auto concept NumericOperatorResultConvertible<typename T>  : AdditionResultConvertible<T>,    SubtractionResultConvertible<T>{};// ====================// Default Concept Maps// ====================#ifndef LA_NO_CONCEPT_MAPS// ==============// Integral Types// ==============template <typename T>  requires std::SignedIntegral<T>concept_map CommutativeRingWithIdentity<T> {}template <typename T>  requires std::UnsignedIntegral<T>concept_map AdditiveCommutativeMonoid<T> {}template <typename T>  requires std::UnsignedIntegral<T>concept_map MultiplicativeCommutativeMonoid<T> {}// ====================// Floating Point Types// ====================template <typename T>  requires Float<T>concept_map Field<T> {}template <typename T>  requires Complex<T>concept_map Field<T> {}// ===========// Min and Max// ===========// Draft version: defined generously unless there will be problems with some typestemplate <typename Element>concept_map CommutativeMonoid< max<Element>, Element > {    // Why do we need this?    typedef Element identity_result_type;}template <typename Element>concept_map CommutativeMonoid< min<Element>, Element >{    // Why do we need this?    typedef Element identity_result_type;}#endif // LA_NO_CONCEPT_MAPS#endif // __GXX_CONCEPTS__// =================================================// Concept to specify return type of abs (and norms)// =================================================#ifdef __GXX_CONCEPTS__// Concept to specify to specify projection of scalar value to comparable type// For instance as return type of abs// Minimalist definition for maximal applicabilityauto concept Magnitude<typename T>{    typename type = T;};template <typename T>concept_map Magnitude<std::complex<T> >{    typedef T type;}// Concept for norms etc., which are real values in mathematical definitionsauto concept RealMagnitude<typename T>  : Magnitude<T>{    requires FullEqualityComparable<type>;    requires FullLessThanComparable<type>;    requires Field<type>;    type sqrt(type);    // typename sqrt_result;    // sqrt_result sqrt(type);    // requires std::Convertible<sqrt_result, type>;    // using std::abs;    type abs(T);}#else  // now without conceptstemplate <typename T>struct Magnitude{    typename type = T;};template <typename T>struct Magnitude<std::complex<T> >{    typedef T type;}template <typename T> struct RealMagnitude  : public Magnitude<T>{}#endif  // __GXX_CONCEPTS__// Type trait version both available with and w/o concepts (TBD: Macro finally :-( )// For the moment everything is its own magnitude type, unless stated otherwisetemplate <typename T>struct magnitude_type_trait{    typedef T type;};template <typename T>struct magnitude_type_trait< std::complex<T> >{    typedef T type;};// =========================================// Concepts for convenience (many from Rolf)// =========================================#ifdef __GXX_CONCEPTS__//The following concepts Addable, Subtractable etc. differ from std::Addable, std::Subtractable //etc. in so far that no default for result_type is provided, thus allowing automated return type deductionauto concept Addable<typename T, typename U = T>{    typename result_type;    result_type operator+(const T& t, const U& u);};   // Usually + and += are both defined// + can be efficiently derived from += but not vice versaauto concept AddableWithAssign<typename T, typename U = T>{    typename assign_result_type;      assign_result_type operator+=(T& x, U y);    // Operator + is by default defined with +=    typename result_type;      result_type operator+(T x, U y);#if 0    {	// Default requires std::CopyConstructible, without default not needed	Element tmp(x);                       	return tmp += y;                      defaults NYS    }#endif };auto concept Subtractable<typename T, typename U = T>{    typename result_type;    result_type operator-(const T& t, const U& u);};  // Usually - and -= are both defined// - can be efficiently derived from -= but not vice versaauto concept SubtractableWithAssign<typename T, typename U = T>{    typename assign_result_type;      assign_result_type operator-=(T& x, U y);    // Operator - is by default defined with -=    typename result_type;      result_type operator-(T x, U y);#if 0    {	// Default requires std::CopyConstructible, without default not needed	Element tmp(x);                       	return tmp -= y;                      defaults NYS    }#endif };auto concept Multiplicable<typename T, typename U = T>{    typename result_type;    result_type operator*(const T& t, const U& u);};// Usually * and *= are both defined// * can be efficiently derived from *= but not vice versaauto concept MultiplicableWithAssign<typename T, typename U = T>{    typename assign_result_type;      assign_result_type operator*=(T& x, U y);    // Operator * is by default defined with *=    typename result_type;      result_type operator*(T x, U y);#if 0    {	// Default requires std::CopyConstructible, without default not needed	Element tmp(x);                       	return tmp *= y;                      defaults NYS    }#endif };auto concept Divisible<typename T, typename U = T>{    typename result_type;    result_type operator / (const T&, const U&);};// Usually * and *= are both defined// * can be efficiently derived from *= but not vice versaauto concept DivisibleWithAssign<typename T, typename U = T>{    typename assign_result_type;      assign_result_type operator*=(T& x, U y);    // Operator * is by default defined with *=    typename result_type;      result_type operator*(T x, U y);#if 0    {	// Default requires std::CopyConstructible, without default not needed	Element tmp(x);                       	return tmp *= y;                      defaults NYS    }#endif };auto concept Transposable<typename T>{    typename result_type;    result_type trans(T&);};  // Unary Negation -> Any suggestions for better names?! Is there a word as "negatable"?!auto concept Negatable<typename S>{    typename result_type = S;    result_type operator-(const S&);};// Or HasAbs?using std::abs;auto concept AbsApplicable<typename S>{    // There are better ways to define abs than the way it is done in std    // Likely we replace the using one day    typename result_type;    result_type abs(const S&);};using std::conj;auto concept HasConjugate<typename S>{    typename result_type;    result_type conj(const S&);};    // We need the following; might be placed somewhere else latertemplate <Float T>concept_map HasConjugate<T> {     typedef T result_type;    result_type conj(const T& s) {return s;}}// Dot product to be defined:auto concept Dottable<typename T, typename U = T>{    typename result_type = T;    result_type dot(const T&t, const U& u);};    auto concept OneNormApplicable<typename V> {    typename result_type;    result_type one_norm(const V&);};auto concept TwoNormApplicable<typename V> {    typename result_type;    result_type two_norm(const V&);};auto concept InfinityNormApplicable<typename V> {    typename result_type;    result_type inf_norm(const V&);};#endif  // __GXX_CONCEPTS__} // namespace math#endif // LA_CONCEPTS_INCLUDE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -