valarray.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 352 行 · 第 1/2 页

HPP
352
字号
////////////////////////////////////////////////////////////////////////////////// \file valarray.hpp/////  Copyright 2005 Eric Niebler. Distributed under the Boost//  Software License, Version 1.0. (See accompanying file//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)#ifndef BOOST_NUMERIC_FUNCTIONAL_VALARRAY_HPP_EAN_12_12_2005#define BOOST_NUMERIC_FUNCTIONAL_VALARRAY_HPP_EAN_12_12_2005#ifdef BOOST_NUMERIC_FUNCTIONAL_HPP_INCLUDED# error Include this file before boost/accumulators/numeric/functional.hpp#endif#include <valarray>#include <functional>#include <boost/assert.hpp>#include <boost/mpl/and.hpp>#include <boost/mpl/not.hpp>#include <boost/mpl/assert.hpp>#include <boost/utility/enable_if.hpp>#include <boost/type_traits/is_same.hpp>#include <boost/type_traits/is_scalar.hpp>#include <boost/type_traits/remove_const.hpp>#include <boost/typeof/std/valarray.hpp>#include <boost/accumulators/numeric/functional_fwd.hpp>namespace boost { namespace numeric{    namespace operators    {        ///////////////////////////////////////////////////////////////////////////////        // Handle valarray<Left> / Right where Right is a scalar and Right != Left.        template<typename Left, typename Right>        typename enable_if<            mpl::and_<is_scalar<Right>, mpl::not_<is_same<Left, Right> > >          , std::valarray<typename functional::divides<Left, Right>::result_type>        >::type        operator /(std::valarray<Left> const &left, Right const &right)        {            typedef typename functional::divides<Left, Right>::result_type value_type;            std::valarray<value_type> result(left.size());            for(std::size_t i = 0, size = result.size(); i != size; ++i)            {                result[i] = numeric::divides(left[i], right);            }            return result;        }        ///////////////////////////////////////////////////////////////////////////////        // Handle valarray<Left> * Right where Right is a scalar and Right != Left.        template<typename Left, typename Right>        typename enable_if<            mpl::and_<is_scalar<Right>, mpl::not_<is_same<Left, Right> > >          , std::valarray<typename functional::multiplies<Left, Right>::result_type>        >::type        operator *(std::valarray<Left> const &left, Right const &right)        {            typedef typename functional::multiplies<Left, Right>::result_type value_type;            std::valarray<value_type> result(left.size());            for(std::size_t i = 0, size = result.size(); i != size; ++i)            {                result[i] = numeric::multiplies(left[i], right);            }            return result;        }        ///////////////////////////////////////////////////////////////////////////////        // Handle valarray<Left> + valarray<Right> where Right != Left.        template<typename Left, typename Right>        typename disable_if<            is_same<Left, Right>          , std::valarray<typename functional::plus<Left, Right>::result_type>        >::type        operator +(std::valarray<Left> const &left, std::valarray<Right> const &right)        {            typedef typename functional::plus<Left, Right>::result_type value_type;            std::valarray<value_type> result(left.size());            for(std::size_t i = 0, size = result.size(); i != size; ++i)            {                result[i] = numeric::plus(left[i], right[i]);            }            return result;        }    }    namespace functional    {        struct std_valarray_tag;        template<typename T>        struct tag<std::valarray<T> >        {            typedef std_valarray_tag type;        };    #ifdef __GLIBCXX__        template<typename T, typename U>        struct tag<std::_Expr<T, U> >        {            typedef std_valarray_tag type;        };    #endif        /// INTERNAL ONLY        ///        // This is necessary because the GCC stdlib uses expression templates, and        // typeof(som-valarray-expression) is not an instance of std::valarray    #define BOOST_NUMERIC_FUNCTIONAL_DEFINE_VALARRAY_BIN_OP(Name, Op)                   \        template<typename Left, typename Right>                                         \        struct Name<Left, Right, std_valarray_tag, std_valarray_tag>                    \          : std::binary_function<                                                       \                Left                                                                    \              , Right                                                                   \              , std::valarray<                                                          \                    typename Name<                                                      \                        typename Left::value_type                                       \                      , typename Right::value_type                                      \                    >::result_type                                                      \                >                                                                       \            >                                                                           \        {                                                                               \            typedef typename Left::value_type left_value_type;                          \            typedef typename Right::value_type right_value_type;                        \            typedef                                                                     \                std::valarray<                                                          \                    typename Name<left_value_type, right_value_type>::result_type       \                >                                                                       \            result_type;                                                                \            result_type                                                                 \            operator ()(Left &left, Right &right) const                                 \            {                                                                           \                return numeric::promote<std::valarray<left_value_type> >(left)          \                    Op numeric::promote<std::valarray<right_value_type> >(right);       \            }                                                                           \        };                                                                              \        template<typename Left, typename Right>                                         \        struct Name<Left, Right, std_valarray_tag, void>                                \          : std::binary_function<                                                       \                Left                                                                    \              , Right                                                                   \              , std::valarray<                                                          \                    typename Name<typename Left::value_type, Right>::result_type        \                >                                                                       \            >                                                                           \        {                                                                               \            typedef typename Left::value_type left_value_type;                          \            typedef                                                                     \                std::valarray<                                                          \                    typename Name<left_value_type, Right>::result_type                  \                >                                                                       \            result_type;                                                                \            result_type                                                                 \            operator ()(Left &left, Right &right) const                                 \            {                                                                           \                return numeric::promote<std::valarray<left_value_type> >(left) Op right;\            }                                                                           \        };                                                                              \        template<typename Left, typename Right>                                         \        struct Name<Left, Right, void, std_valarray_tag>                                \          : std::binary_function<                                                       \                Left                                                                    \              , Right                                                                   \              , std::valarray<                                                          \                    typename Name<Left, typename Right::value_type>::result_type        \                >                                                                       \            >                                                                           \        {                                                                               \            typedef typename Right::value_type right_value_type;                        \            typedef                                                                     \                std::valarray<                                                          \                    typename Name<Left, right_value_type>::result_type                  \                >                                                                       \            result_type;                                                                \            result_type                                                                 \            operator ()(Left &left, Right &right) const                                 \

⌨️ 快捷键说明

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