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

📄 identity.hpp

📁 矩阵运算源码最新版本
💻 HPP
字号:
// Copyright 2006. Peter Gottschling, Matthias Troyer, Rolf Bonderer// Software License for MTL// // Copyright (c) 2007 The Trustees of Indiana University. All rights reserved.// Authors: Peter Gottschling and Andrew Lumsdaine// // This file is part of the Matrix Template Library// // See also license.mtl.txt in the distribution.#ifndef MATH_IDENTITY_INCLUDE#define MATH_IDENTITY_INCLUDE#include <boost/numeric/linear_algebra/operators.hpp>#include <limits>#include <functional>namespace math {template <typename Operation, typename Element>struct identity_t {};// TBD: Do we the case that the return type is different? Using std::unary_function?// Additive identity of Element type is by default a converted 0// However, for vectors one needs to know the dimension// (and in parallel eventually also the distribution).// Therefore, an element is passed as reference.// It is strongly recommended to specialize this functor// for better efficiency.template <typename Element>struct identity_t< add<Element>, Element >   : public std::binary_function<add<Element>, Element, Element>{     Element operator() (const add<Element>&, const Element& ref) const    {	Element tmp(ref);	tmp= 0;	return tmp;    }};// Multiplicative identity of Element type is by default a converted 1// Same comments as above.// In contrast to additive identity, this default more likely to be wrong (e.g. matrices with all 1s)template <typename Element>struct identity_t< mult<Element>, Element >   : public std::binary_function<mult<Element>, Element, Element>{     Element operator() (const mult<Element>&, const Element& ref) const    {	Element tmp(ref);	tmp= 1;	return tmp;    }};// Identity of max is minimal representable value, for standard types defined in numeric_limitstemplate <typename Element>struct identity_t< max<Element>, Element >   : public std::binary_function<max<Element>, Element, Element>{     Element operator() (const max<Element>&, const Element& ref) const    {	using std::numeric_limits;	return numeric_limits<Element>::min();    }};// Identity of min is maximal representable value, for standard types defined in numeric_limitstemplate <typename Element>struct identity_t< min<Element>, Element >   : public std::binary_function<min<Element>, Element, Element>{     Element operator() (const min<Element>&, const Element& ref) const    {	using std::numeric_limits;	return numeric_limits<Element>::max();    }};// Function is shorter than typetrait-like functortemplate <typename Operation, typename Element>inline Element identity(const Operation& op, const Element& v){    return identity_t<Operation, Element>() (op, v);}// Short-cut for additive identitytemplate <typename Element>inline Element zero(const Element& v){    return identity_t<math::add<Element>, Element>() (math::add<Element>(), v);}// Short-cut for multiplicative identitytemplate <typename Element>inline Element one(const Element& v){    return identity_t<math::mult<Element>, Element>() (math::mult<Element>(), v);}} // namespace math#endif // MATH_IDENTITY_INCLUDE

⌨️ 快捷键说明

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