// Copyright Gunter Winkler 2006, 2007 // // 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) namespace functor { template struct log { public: typedef T value_type; typedef T result_type; static result_type apply(const value_type& x) { return std::log(x); } }; template struct maximum { public: typedef T value_type; typedef T result_type; static result_type apply(const value_type& x, const value_type& y) { return std::max(x, y); } static result_type initial_value() { return (-std::numeric_limits::max()); } static void update(result_type& t, const value_type& x) { if (x>t) t=x; } }; template struct minimum { public: typedef T value_type; typedef T result_type; static result_type apply(const value_type& x, const value_type& y) { return std::min(x, y); } static result_type initial_value() { return (std::numeric_limits::max()); } static void update(result_type& t, const value_type& x) { if (x struct log_minimum { public: typedef T value_type; typedef T result_type; static result_type initial_value() { return std::log(std::numeric_limits::max()); } static void update(result_type& t, const value_type& x) { if ( (x>0) && (std::log(x) struct general_vector_scalar_unary: public vector_scalar_unary_functor { typedef typename vector_scalar_unary_functor::value_type value_type; typedef typename vector_scalar_unary_functor::result_type result_type; // general case, access by index template static BOOST_UBLAS_INLINE result_type apply (const vector_expression &e) { result_type t = OP::initial_value(); typename E::size_type size (e ().size ()); for (typename E::size_type i = 0; i < size; ++ i) OP::update(t, e () (i)); return t; } // Dense case template static BOOST_UBLAS_INLINE result_type apply (D size, I it) { result_type t = OP::initial_value(); while (-- size >= 0) OP::update(t, *it), ++ it; return t; } // Sparse case template static BOOST_UBLAS_INLINE result_type apply (I it, const I &it_end) { result_type t = OP::initial_value(); while (it != it_end) OP::update(t, *it), ++ it; return t; } }; // max v = max (v [i], i=1..N) template BOOST_UBLAS_INLINE typename vector_scalar_unary_traits > >::result_type max (const vector_expression &e) { typedef typename vector_scalar_unary_traits > >::expression_type expression_type; return expression_type (e ()); } // min v = min (v [i], i=1..N) template BOOST_UBLAS_INLINE typename vector_scalar_unary_traits > >::result_type min (const vector_expression &e) { typedef typename vector_scalar_unary_traits > >::expression_type expression_type; return expression_type (e ()); } // log_min v = min ( log(v [i]), i=1..N if v[i]>0 ) template BOOST_UBLAS_INLINE typename vector_scalar_unary_traits > >::result_type log_min (const vector_expression &e) { typedef typename vector_scalar_unary_traits > >::expression_type expression_type; return expression_type (e ()); } // //////////// general vector unary expression ///////////// // (op v) [i] = op( v [i] ) template BOOST_UBLAS_INLINE typename vector_unary_traits::result_type apply_to_all (const vector_expression &e, const OP& op = OP()) { typedef typename vector_unary_traits::expression_type expression_type; return expression_type (e ()); } }}}