9 #ifndef ALPS_NUMERIC_VECTOR_FUNCTIONS_HEADER 10 #define ALPS_NUMERIC_VECTOR_FUNCTIONS_HEADER 18 #include <boost/accumulators/numeric/functional/vector.hpp> 19 #include <boost/accumulators/numeric/functional.hpp> 21 #include <boost/bind.hpp> 22 #include <boost/lambda/bind.hpp> 23 #include <boost/lambda/lambda.hpp> 24 #include <boost/throw_exception.hpp> 36 #define IMPLEMENT_ALPS_VECTOR_FUNCTION(LIB_HEADER, FUNCTION_NAME) \ 38 template<typename T> struct FUNCTION_NAME ## _VECTOR_OP_HELPER { \ 39 T operator() (T arg) { \ 40 using LIB_HEADER :: FUNCTION_NAME; \ 41 return FUNCTION_NAME (arg); \ 45 template<typename T> std::vector<T> FUNCTION_NAME(std::vector<T> vec) { \ 46 std::transform(vec.begin(), vec.end(), vec.begin(), detail:: FUNCTION_NAME ## _VECTOR_OP_HELPER<T>()); \ 70 #undef IMPLEMENT_ALPS_VECTOR_FUNCTION 76 typedef std::vector<T> value_type;
80 inf(
const value_type& vec) {
81 if (vec.empty())
return;
82 inf_.resize(vec.size(),
inf<T>(vec.front()));
86 operator value_type
const() {
return inf_; }
93 std::transform(lhs.begin(), lhs.end(), lhs.begin(), std::negate<T>());
99 template<
typename T,
typename U>
100 std::vector<T>
operator + (std::vector<T>
const & lhs, std::vector<U>
const & rhs) {
101 using boost::numeric::operators::operator+;
102 if (lhs.empty())
return rhs;
103 if (rhs.empty())
return lhs;
109 template<
typename T,
typename U>
110 std::vector<T>
operator - (std::vector<T>
const & lhs, std::vector<U>
const & rhs) {
111 using boost::numeric::operators::operator-;
112 if (rhs.empty())
return lhs;
113 if (lhs.empty())
return -rhs;
119 template<
typename T,
typename U>
120 std::vector<T>
operator * (std::vector<T>
const & lhs, std::vector<U>
const & rhs) {
121 using boost::numeric::operators::operator*;
122 if (lhs.empty())
return lhs;
123 if (rhs.empty())
return rhs;
128 template<
typename T,
typename U>
129 std::vector<T>
operator / (std::vector<T>
const & lhs, std::vector<U>
const & rhs) {
130 using boost::numeric::operators::operator/;
131 if (lhs.empty())
return lhs;
132 if (rhs.empty())
throw std::runtime_error(
"Division by default-initialized vector");
140 std::transform(rhs.begin(), rhs.end(), rhs.begin(), bind1st(std::plus<T>(), scalar));
146 std::transform(lhs.begin(), lhs.end(), lhs.begin(), bind2nd(std::plus<T>(), scalar));
154 return scalar + -rhs;
159 return lhs + -scalar;
166 using boost::numeric::operators::operator*;
172 using boost::numeric::operators::operator*;
179 using boost::numeric::operators::operator/;
185 std::transform(rhs.begin(), rhs.end(), rhs.begin(), scalar / boost::lambda::_1);
190 #define ALPS_NUMERIC_IMPLEMENT_FUNCTION(FUNCTION_NAME) \ 191 template<typename T> std::vector<T> FUNCTION_NAME (std::vector<T> arg) { \ 192 using std:: FUNCTION_NAME ; \ 193 std::transform(arg.begin(), arg.end(), arg.begin(), static_cast<T (*)(T)>(& FUNCTION_NAME )); \ 211 #undef ALPS_NUMERIC_IMPLEMENT_FUNCTION 213 template<
typename T,
typename U> std::vector<T>
pow(std::vector<T> vec, U
index) {
215 std::transform(vec.begin(), vec.end(), vec.begin(), boost::lambda::bind<T>(
static_cast<T (*)(T, T)
>(&
pow), boost::lambda::_1, index));
220 template <
typename T>
struct negate:
public std::negate<T> {};
223 template <
typename T>
225 typedef std::vector<T>
VT;
234 template <
typename T>
240 template <
typename T>
242 typedef std::vector<T>
VT;
253 template <
typename T>
struct unary_minus :
public std::unary_function<T, T> {
256 using alps::numeric::operator-;
261 template <
typename T,
typename U,
typename R>
struct plus :
public std::binary_function<T, U, R> {
264 using alps::numeric::operator+;
268 template <
typename T>
struct plus<T, T, T> :
public std::binary_function<T, T, T> {
271 using alps::numeric::operator+;
276 template <
typename T,
typename U,
typename R>
struct minus :
public std::binary_function<T, U, R> {
279 using alps::numeric::operator-;
283 template <
typename T>
struct minus<T, T, T> :
public std::binary_function<T, T, T> {
286 using alps::numeric::operator-;
291 template <
typename T,
typename U,
typename R>
struct multiplies :
public std::binary_function<T, U, R> {
294 using alps::numeric::operator*;
298 template <
typename T>
struct multiplies<T, T, T> :
public std::binary_function<T, T, T> {
301 using alps::numeric::operator*;
306 template <
typename T,
typename U,
typename R>
struct divides :
public std::binary_function<T, U, R> {
309 using alps::numeric::operator/;
313 template <
typename T>
struct divides<T, T, T> :
public std::binary_function<T, T, T> {
316 using alps::numeric::operator/;
323 #define ALPS_NUMERIC_OPERATOR_EQ(OP_NAME, OPERATOR) \ 324 template<typename T> \ 325 std::vector<T> & OP_NAME (std::vector<T> & lhs, std::vector<T> const & rhs) { \ 326 if(lhs.size() != rhs.size()) { \ 327 std::string lsz=std::to_string(lhs.size()); \ 328 std::string rsz=std::to_string(rhs.size()); \ 329 boost::throw_exception(std::runtime_error("std::vectors have different sizes:" \ 331 " right="+rsz + "\n" + \ 335 std::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs.begin(), OPERATOR <T,T,T>() ); \ 344 #undef ALPS_NUMERIC_OPERATOR_EQ 354 template <
typename T>
355 std::vector<T>&
merge(std::vector<T>& left,
const std::vector<T>& right) {
356 std::size_t lsz=left.size();
357 std::size_t rsz=right.size();
358 if (lsz<rsz) left.resize(rsz);
368 #endif // ALPS_NUMERIC_VECTOR_FUNCTIONS_HEADER boost::array< T, N > sinh(boost::array< T, N > arg)
index_mesh::index_type index
boost::array< T, N > tanh(boost::array< T, N > arg)
boost::array< T, N > cbrt(boost::array< T, N > arg)
boost::array< T, N > atan(boost::array< T, N > arg)
boost::array< T, N > log(boost::array< T, N > arg)
boost::array< T, N > sqrt(boost::array< T, N > arg)
#define ALPS_NUMERIC_IMPLEMENT_FUNCTION(FUNCTION_NAME)
boost::array< T, N > abs(boost::array< T, N > arg)
boost::array< T, N > operator*(boost::array< T, N > lhs, boost::array< U, N > const &rhs)
boost::array< T, N > cos(boost::array< T, N > arg)
#define IMPLEMENT_ALPS_VECTOR_FUNCTION(LIB_HEADER, FUNCTION_NAME)
T operator()(T const &x, T const &y) const
boost::array< T, N > operator/(boost::array< T, N > lhs, boost::array< U, N > const &rhs)
T operator()(T const &x, T const &y) const
mean_result< T > transform(no_prop, const transformer< T > &tf, const InResult &in)
R operator()(T const &x, U const &y) const
T operator()(T const &x, T const &y) const
std::vector< T > acosh(std::vector< T > vec)
Metafunction returning "mathematical scalar" type for type T.
R operator()(T const &x, U const &y) const
boost::array< T, N > cosh(boost::array< T, N > arg)
R operator()(T const &x, U const &y) const
inf(const value_type &vec)
Construct infinity object of the same size as its argument, recursively.
"Imported" negation functor class (needed to define template specializations in this namespace) ...
boost::array< T, N > exp(boost::array< T, N > arg)
#define ALPS_NUMERIC_OPERATOR_EQ(OP_NAME, OPERATOR)
boost::array< T, N > operator+(boost::array< T, N > lhs, boost::array< U, N > const &rhs)
R operator()(T const &x, U const &y) const
boost::array< T, N > asin(boost::array< T, N > arg)
boost::array< T, N > sin(boost::array< T, N > arg)
std::vector< T > asinh(std::vector< T > vec)
std::vector< T > pow(std::vector< T > vec, U index)
boost::array< T, N > acos(boost::array< T, N > arg)
boost::array< T, N > cb(boost::array< T, N > arg)
std::vector< T > atanh(std::vector< T > vec)
boost::array< T, N > tan(boost::array< T, N > arg)
T operator()(T const &x) const
std::vector< T > & merge(std::vector< T > &left, const std::vector< T > &right)
Vector merge.
boost::array< T, N > sq(boost::array< T, N > arg)
A service functor class for numerical inversion, to be used in transform()
boost::array< T, N > operator-(boost::array< T, N > lhs)
T operator()(T const &x, T const &y) const