ALPSCore reference
boost_array_functions.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
7 #ifndef ALPS_NUMERIC_ARRAY_HEADER
8 #define ALPS_NUMERIC_ARRAY_HEADER
9 
11 
12 #include <alps/numeric/inf.hpp>
14 
15 #include <boost/throw_exception.hpp>
16 #include <boost/lambda/lambda.hpp>
17 #include <boost/array.hpp>
18 
19 #include <algorithm>
20 #include <functional>
21 #include <cmath>
22 #include <stdexcept>
23 
24 namespace alps {
25  namespace numeric {
26 
27  //------------------- operator equal -------------------
28  #define ALPS_NUMERIC_OPERATOR_EQ(OP_NAME, OPERATOR) \
29  template<typename T, std::size_t N> \
30  boost::array<T, N> & OP_NAME (boost::array<T, N> & lhs, boost::array<T, N> const & rhs) { \
31  if(lhs.size() != rhs.size()) \
32  boost::throw_exception(std::runtime_error("arrays must have the same size!" + ALPS_STACKTRACE)); \
33  else \
34  std::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs.begin(), std:: OPERATOR <T>() ); \
35  return lhs; \
36  }
37 
42 
43  #undef ALPS_NUMERIC_OPERATOR_EQ
44 
45  //------------------- infinity -------------------
46  template<std::size_t N> struct inf<boost::array<double, N> > {
47  operator boost::array<double, N> const() {
48  boost::array<double, N> retval;
49  for(double & arg: retval) {
50  arg = std::numeric_limits<double>::infinity();
51  }
52  return retval;
53  }
54  };
55 
56  //------------------- unary operator - -------------------
57  template<typename T, std::size_t N>
58  boost::array<T, N> operator - (boost::array<T, N> lhs) {
59  std::transform(lhs.begin(), lhs.end(), lhs.begin(), std::negate<T>());
60  return lhs;
61  }
62 
63  //------------------- operator + -------------------
64  template<typename T, typename U, std::size_t N>
65  boost::array<T, N> operator + (boost::array<T, N> lhs, boost::array<U, N> const & rhs) {
66  std::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs.begin(), std::plus<T>() );
67  return lhs;
68  }
69  //------------------- operator + with scalar -------------------
70  template<typename T, std::size_t N>
71  boost::array<T, N> operator + (boost::array<T, N> arg, T const & scalar) {
72  std::transform(arg.begin(), arg.end(), arg.begin(), boost::lambda::_1 + scalar);
73  return arg;
74  }
75  template<typename T, std::size_t N>
76  boost::array<T, N> operator + (T const & scalar, boost::array<T, N> arg) {
77  std::transform(arg.begin(), arg.end(), arg.begin(), scalar + boost::lambda::_1);
78  return arg;
79  }
80 
81  //------------------- operator - -------------------
82  template<typename T, typename U, std::size_t N>
83  boost::array<T, N> operator - (boost::array<T, N> lhs, boost::array<U, N> const & rhs) {
84  std::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs.begin(), std::minus<T>() );
85  return lhs;
86  }
87  //------------------- operator - with scalar -------------------
88  template<typename T, std::size_t N>
89  boost::array<T, N> operator - (boost::array<T, N> arg, T const & scalar) {
90  std::transform(arg.begin(), arg.end(), arg.begin(), boost::lambda::_1 - scalar);
91  return arg;
92  }
93  template<typename T, std::size_t N>
94  boost::array<T, N> operator - (T const & scalar, boost::array<T, N> arg) {
95  std::transform(arg.begin(), arg.end(), arg.begin(), scalar - boost::lambda::_1);
96  return arg;
97  }
98 
99  //------------------- operator * vector-vector-------------------
100  template<typename T, typename U, std::size_t N>
101  boost::array<T, N> operator * (boost::array<T, N> lhs, boost::array<U, N> const & rhs)
102  {
103  std::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs.begin(), std::multiplies<T>());
104  return lhs;
105  }
106  //------------------- operator / vector-vector-------------------
107  template<typename T, typename U, std::size_t N>
108  boost::array<T, N> operator / (boost::array<T, N> lhs, boost::array<U, N> const & rhs)
109  {
110  std::transform(lhs.begin(), lhs.end(), rhs.begin(), lhs.begin(), std::multiplies<T>());
111  return lhs;
112  }
113 
114  //------------------- operator * with scalar -------------------
115  template<typename T, std::size_t N>
116  boost::array<T, N> operator * (boost::array<T, N> lhs, T const & scalar) {
117  std::transform(lhs.begin(), lhs.end(), lhs.begin(), boost::lambda::_1 * scalar);
118  return lhs;
119  }
120  template<typename T, std::size_t N>
121  boost::array<T, N> operator * (T const & scalar, boost::array<T, N> rhs) {
122  std::transform(rhs.begin(), rhs.end(), rhs.begin(), scalar * boost::lambda::_1);
123  return rhs;
124  }
125 
126  //------------------- operator / with scalar -------------------
127  template<typename T, std::size_t N>
128  boost::array<T, N> operator / (boost::array<T, N> lhs, T const & scalar) {
129  std::transform(lhs.begin(), lhs.end(), lhs.begin(), boost::lambda::_1 / scalar);
130  return lhs;
131  }
132  template<typename T, std::size_t N>
133  boost::array<T, N> operator / (T const & scalar, boost::array<T, N> rhs) {
134  std::transform(rhs.begin(), rhs.end(), rhs.begin(), scalar / boost::lambda::_1);
135  return rhs;
136  }
137 
138  //------------------- numeric functions -------------------
139  #define ALPS_NUMERIC_IMPLEMENT_FUNCTION(FUNCTION_NAME) \
140  template<typename T, std::size_t N> boost::array<T, N> FUNCTION_NAME (boost::array<T, N> arg) { \
141  using std:: FUNCTION_NAME ; \
142  std::transform(arg.begin(), arg.end(), arg.begin(), static_cast<double (*)(double)>(& FUNCTION_NAME )); \
143  return arg; \
144  }
145 
159 
160  #undef ALPS_NUMERIC_IMPLEMENT_FUNCTION
161 
162  #define ALPS_NUMERIC_IMPLEMENT_FUNCTION(FUNCTION_NAME) \
163  template<typename T, std::size_t N> boost::array<T, N> FUNCTION_NAME (boost::array<T, N> arg) { \
164  using alps::numeric:: FUNCTION_NAME ; \
165  std::transform(arg.begin(), arg.end(), arg.begin(), static_cast<double (*)(double)>(& FUNCTION_NAME )); \
166  return arg; \
167  }
168 
172 
173  #undef ALPS_NUMERIC_IMPLEMENT_FUNCTION
174 
175  }
176 }
177 
178 #endif
boost::array< T, N > sinh(boost::array< T, N > arg)
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)
boost::array< T, N > operator/(boost::array< T, N > lhs, boost::array< U, N > const &rhs)
mean_result< T > transform(no_prop, const transformer< T > &tf, const InResult &in)
Definition: transform.hpp:27
Metafunction returning "mathematical scalar" type for type T.
Definition: scalar.hpp:28
boost::array< T, N > cosh(boost::array< T, N > arg)
boost::array< T, N > exp(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 > asin(boost::array< T, N > arg)
boost::array< T, N > sin(boost::array< T, N > arg)
boost::array< T, N > acos(boost::array< T, N > arg)
#define ALPS_NUMERIC_OPERATOR_EQ(OP_NAME, OPERATOR)
boost::array< T, N > cb(boost::array< T, N > arg)
boost::array< T, N > tan(boost::array< T, N > arg)
boost::array< T, N > sq(boost::array< T, N > arg)
boost::array< T, N > operator-(boost::array< T, N > lhs)