📄 functional.hpp
字号:
//
// Copyright (c) 2000-2002
// Joerg Walter, Mathias Koch
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without fee,
// provided that the above copyright notice appear in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation. The authors make no representations
// about the suitability of this software for any purpose.
// It is provided "as is" without express or implied warranty.
//
// The authors gratefully acknowledge the support of
// GeNeSys mbH & Co. KG in producing this work.
//
#ifndef BOOST_UBLAS_FUNCTIONAL_H
#define BOOST_UBLAS_FUNCTIONAL_H
#include <functional>
#ifdef BOOST_UBLAS_HAVE_BINDINGS
#include <boost/numeric/bindings/traits/std_vector.hpp>
#include <boost/numeric/bindings/traits/ublas_vector.hpp>
#include <boost/numeric/bindings/traits/ublas_matrix.hpp>
#include <boost/numeric/bindings/atlas/cblas.hpp>
#endif
#include <boost/numeric/ublas/config.hpp>
#include <boost/numeric/ublas/exception.hpp>
#include <boost/numeric/ublas/traits.hpp>
#include <boost/numeric/ublas/duff.hpp>
#include <boost/numeric/ublas/raw.hpp>
namespace boost { namespace numeric { namespace ublas {
// Scalar functors
// Unary
template<class T>
struct scalar_unary_functor {
typedef T value_type;
typedef typename type_traits<T>::const_reference argument_type;
typedef typename type_traits<T>::value_type result_type;
};
template<class T>
struct scalar_identity:
public scalar_unary_functor<T> {
typedef typename scalar_unary_functor<T>::argument_type argument_type;
typedef typename scalar_unary_functor<T>::result_type result_type;
BOOST_UBLAS_INLINE
result_type operator () (argument_type t) const {
return t;
}
};
template<class T>
struct scalar_negate:
public scalar_unary_functor<T> {
typedef typename scalar_unary_functor<T>::argument_type argument_type;
typedef typename scalar_unary_functor<T>::result_type result_type;
BOOST_UBLAS_INLINE
result_type operator () (argument_type t) const {
return - t;
}
};
template<class T>
struct scalar_conj:
public scalar_unary_functor<T> {
typedef typename scalar_unary_functor<T>::value_type value_type;
typedef typename scalar_unary_functor<T>::argument_type argument_type;
typedef typename scalar_unary_functor<T>::result_type result_type;
BOOST_UBLAS_INLINE
result_type operator () (argument_type t) const {
return type_traits<value_type>::conj (t);
}
};
// Unary returning real
template<class T>
struct scalar_real_unary_functor {
typedef T value_type;
typedef typename type_traits<T>::const_reference argument_type;
typedef typename type_traits<T>::real_type result_type;
};
template<class T>
struct scalar_real:
public scalar_real_unary_functor<T> {
typedef typename scalar_real_unary_functor<T>::value_type value_type;
typedef typename scalar_real_unary_functor<T>::argument_type argument_type;
typedef typename scalar_real_unary_functor<T>::result_type result_type;
BOOST_UBLAS_INLINE
result_type operator () (argument_type t) const {
return type_traits<value_type>::real (t);
}
};
template<class T>
struct scalar_imag:
public scalar_real_unary_functor<T> {
typedef typename scalar_real_unary_functor<T>::value_type value_type;
typedef typename scalar_real_unary_functor<T>::argument_type argument_type;
typedef typename scalar_real_unary_functor<T>::result_type result_type;
BOOST_UBLAS_INLINE
result_type operator () (argument_type t) const {
return type_traits<value_type>::imag (t);
}
};
// Binary
template<class T1, class T2>
struct scalar_binary_functor {
typedef typename type_traits<T1>::const_reference argument1_type;
typedef typename type_traits<T2>::const_reference argument2_type;
typedef typename promote_traits<T1, T2>::promote_type result_type;
};
template<class T1, class T2>
struct scalar_plus:
public scalar_binary_functor<T1, T2> {
typedef typename scalar_binary_functor<T1, T2>::argument1_type argument1_type;
typedef typename scalar_binary_functor<T1, T2>::argument2_type argument2_type;
typedef typename scalar_binary_functor<T1, T2>::result_type result_type;
BOOST_UBLAS_INLINE
result_type operator () (argument1_type t1, argument2_type t2) const {
return t1 + t2;
}
};
template<class T1, class T2>
struct scalar_minus:
public scalar_binary_functor<T1, T2> {
typedef typename scalar_binary_functor<T1, T2>::argument1_type argument1_type;
typedef typename scalar_binary_functor<T1, T2>::argument2_type argument2_type;
typedef typename scalar_binary_functor<T1, T2>::result_type result_type;
BOOST_UBLAS_INLINE
result_type operator () (argument1_type t1, argument2_type t2) const {
return t1 - t2;
}
};
template<class T1, class T2>
struct scalar_multiplies:
public scalar_binary_functor<T1, T2> {
typedef typename scalar_binary_functor<T1, T2>::argument1_type argument1_type;
typedef typename scalar_binary_functor<T1, T2>::argument2_type argument2_type;
typedef typename scalar_binary_functor<T1, T2>::result_type result_type;
BOOST_UBLAS_INLINE
result_type operator () (argument1_type t1, argument2_type t2) const {
return t1 * t2;
}
};
template<class T1, class T2>
struct scalar_divides:
public scalar_binary_functor<T1, T2> {
typedef typename scalar_binary_functor<T1, T2>::argument1_type argument1_type;
typedef typename scalar_binary_functor<T1, T2>::argument2_type argument2_type;
typedef typename scalar_binary_functor<T1, T2>::result_type result_type;
BOOST_UBLAS_INLINE
result_type operator () (argument1_type t1, argument2_type t2) const {
return t1 / t2;
}
};
template<class T1, class T2>
struct scalar_binary_assign_functor {
typedef typename type_traits<typename boost::remove_reference<T1>::type>::reference argument1_type;
typedef typename type_traits<T2>::const_reference argument2_type;
};
struct assign_tag {};
struct computed_assign_tag {};
template<class T1, class T2>
struct scalar_assign:
public scalar_binary_assign_functor<T1, T2> {
typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
typedef assign_tag assign_category;
BOOST_UBLAS_INLINE
void operator () (argument1_type t1, argument2_type t2) const {
t1 = t2;
}
template<class U1, class U2>
static
BOOST_UBLAS_INLINE
scalar_assign<U1, U2> make_debug_functor () {
return scalar_assign<U1, U2> ();
}
};
template<class T1, class T2>
struct scalar_plus_assign:
public scalar_binary_assign_functor<T1, T2> {
typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
typedef computed_assign_tag assign_category;
BOOST_UBLAS_INLINE
void operator () (argument1_type t1, argument2_type t2) const {
t1 += t2;
}
template<class U1, class U2>
static
BOOST_UBLAS_INLINE
scalar_plus_assign<U1, U2> make_debug_functor () {
return scalar_plus_assign<U1, U2> ();
}
};
template<class T1, class T2>
struct scalar_minus_assign:
public scalar_binary_assign_functor<T1, T2> {
typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
typedef computed_assign_tag assign_category;
BOOST_UBLAS_INLINE
void operator () (argument1_type t1, argument2_type t2) const {
t1 -= t2;
}
template<class U1, class U2>
static
BOOST_UBLAS_INLINE
scalar_minus_assign<U1, U2> make_debug_functor () {
return scalar_minus_assign<U1, U2> ();
}
};
template<class T1, class T2>
struct scalar_multiplies_assign:
public scalar_binary_assign_functor<T1, T2> {
typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
typedef computed_assign_tag assign_category;
BOOST_UBLAS_INLINE
void operator () (argument1_type t1, argument2_type t2) const {
t1 *= t2;
}
template<class U1, class U2>
static
BOOST_UBLAS_INLINE
scalar_multiplies_assign<U1, U2> make_debug_functor () {
return scalar_multiplies_assign<U1, U2> ();
}
};
template<class T1, class T2>
struct scalar_divides_assign:
public scalar_binary_assign_functor<T1, T2> {
typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
typedef computed_assign_tag assign_category;
BOOST_UBLAS_INLINE
void operator () (argument1_type t1, argument2_type t2) const {
t1 /= t2;
}
template<class U1, class U2>
static
BOOST_UBLAS_INLINE
scalar_divides_assign<U1, U2> make_debug_functor () {
return scalar_divides_assign<U1, U2> ();
}
};
template<class T1, class T2>
struct scalar_binary_swap_functor {
typedef typename type_traits<typename boost::remove_reference<T1>::type>::reference argument1_type;
typedef typename type_traits<typename boost::remove_reference<T2>::type>::reference argument2_type;
};
template<class T1, class T2>
struct scalar_swap:
public scalar_binary_swap_functor<T1, T2> {
typedef typename scalar_binary_swap_functor<T1, T2>::argument1_type argument1_type;
typedef typename scalar_binary_swap_functor<T1, T2>::argument2_type argument2_type;
BOOST_UBLAS_INLINE
void operator () (argument1_type t1, argument2_type t2) const {
std::swap (t1, t2);
}
template<class U1, class U2>
static
BOOST_UBLAS_INLINE
scalar_swap<U1, U2> make_debug_functor () {
return scalar_swap<U1, U2> ();
}
};
// Vector functors
// Unary returning scalar
template<class T>
struct vector_scalar_unary_functor {
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T value_type;
typedef T result_type;
};
template<class T>
struct vector_sum:
public vector_scalar_unary_functor<T> {
typedef typename vector_scalar_unary_functor<T>::size_type size_type;
typedef typename vector_scalar_unary_functor<T>::difference_type difference_type;
typedef typename vector_scalar_unary_functor<T>::value_type value_type;
typedef typename vector_scalar_unary_functor<T>::result_type result_type;
template<class E>
BOOST_UBLAS_INLINE
result_type operator () (const vector_expression<E> &e) const {
result_type t = result_type ();
size_type size (e ().size ());
for (size_type i = 0; i < size; ++ i)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -