
* mln/fun/vv2v/mean.hh, * mln/fun/vvvv2v/mean.hh, * mln/math/ceil.hh, * mln/math/floor.hh, * mln/math/max.hh, * mln/math/mean.hh, * mln/math/min.hh: New. --- milena/ChangeLog | 12 ++ milena/mln/fun/vv2v/{min.hh => mean.hh} | 26 +++--- milena/mln/fun/{vv2v/min.hh => vvvv2v/mean.hh} | 35 +++---- milena/{sandbox/green => }/mln/math/ceil.hh | 32 ++++-- milena/{sandbox/green => }/mln/math/floor.hh | 30 ++++-- milena/mln/math/max.hh | 23 ++++- milena/mln/math/mean.hh | 128 ++++++++++++++++++++++++ milena/mln/math/min.hh | 23 ++++- 8 files changed, 247 insertions(+), 62 deletions(-) copy milena/mln/fun/vv2v/{min.hh => mean.hh} (75%) copy milena/mln/fun/{vv2v/min.hh => vvvv2v/mean.hh} (66%) copy milena/{sandbox/green => }/mln/math/ceil.hh (76%) copy milena/{sandbox/green => }/mln/math/floor.hh (77%) create mode 100644 milena/mln/math/mean.hh diff --git a/milena/ChangeLog b/milena/ChangeLog index 08378ab..46d712d 100644 --- a/milena/ChangeLog +++ b/milena/ChangeLog @@ -1,3 +1,15 @@ +2012-10-04 Guillaume Lazzara <z@lrde.epita.fr> + + Add mathematical tools. + + * mln/fun/vv2v/mean.hh, + * mln/fun/vvvv2v/mean.hh, + * mln/math/ceil.hh, + * mln/math/floor.hh, + * mln/math/max.hh, + * mln/math/mean.hh, + * mln/math/min.hh: New. + 2012-10-03 Guillaume Lazzara <z@lrde.epita.fr> * tests/world/k1/Makefile.am: Fix target names. diff --git a/milena/mln/fun/vv2v/min.hh b/milena/mln/fun/vv2v/mean.hh similarity index 75% copy from milena/mln/fun/vv2v/min.hh copy to milena/mln/fun/vv2v/mean.hh index 0f885fc..510b9a6 100644 --- a/milena/mln/fun/vv2v/min.hh +++ b/milena/mln/fun/vv2v/mean.hh @@ -1,4 +1,4 @@ -// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE) +// Copyright (C) 2012 EPITA Research and Development Laboratory (LRDE) // // This file is part of Olena. // @@ -23,15 +23,15 @@ // exception does not however invalidate any other reasons why the // executable file might be covered by the GNU General Public License. -#ifndef MLN_FUN_VV2V_MIN_HH -# define MLN_FUN_VV2V_MIN_HH +#ifndef MLN_FUN_VV2V_MEAN_HH +# define MLN_FUN_VV2V_MEAN_HH /// \file /// -/// Functor that computes the minimum of two values. +/// Functor that computes the mean of two values. # include <mln/core/concept/function.hh> -# include <mln/math/min.hh> +# include <mln/math/mean.hh> namespace mln @@ -45,13 +45,13 @@ namespace mln // FIXME: Doc. - /// \brief A functor computing the minimum of two values. + /// \brief A functor computing the mean of two values. template <typename L, typename R = L> - struct min : public Function_vv2v< min<L,R> >, + struct mean : public Function_vv2v< mean<L,R> >, private mlc_converts_to(R,L)::check_t { - typedef L result; - L operator()(const L& v1, const R& v2) const; + typedef R result; + R operator()(const L& v1, const L& v2) const; }; @@ -59,10 +59,10 @@ namespace mln template <typename L, typename R> inline - L - min<L,R>::operator()(const L& v1, const R& v2) const + R + mean<L,R>::operator()(const L& v1, const L& v2) const { - return mln::math::min(v1, L(v2)); + return R(mln::math::mean(v1, v2); } # endif // ! MLN_INCLUDE_ONLY @@ -74,4 +74,4 @@ namespace mln } // end of namespace mln -#endif // ! MLN_FUN_VV2V_MIN_HH +#endif // ! MLN_FUN_VV2V_MEAN_HH diff --git a/milena/mln/fun/vv2v/min.hh b/milena/mln/fun/vvvv2v/mean.hh similarity index 66% copy from milena/mln/fun/vv2v/min.hh copy to milena/mln/fun/vvvv2v/mean.hh index 0f885fc..0dba050 100644 --- a/milena/mln/fun/vv2v/min.hh +++ b/milena/mln/fun/vvvv2v/mean.hh @@ -1,4 +1,4 @@ -// Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE) +// Copyright (C) 2012 EPITA Research and Development Laboratory (LRDE) // // This file is part of Olena. // @@ -23,15 +23,15 @@ // exception does not however invalidate any other reasons why the // executable file might be covered by the GNU General Public License. -#ifndef MLN_FUN_VV2V_MIN_HH -# define MLN_FUN_VV2V_MIN_HH +#ifndef MLN_FUN_VVVV2V_MEAN_HH +# define MLN_FUN_VVVV2V_MEAN_HH /// \file /// -/// Functor that computes the minimum of two values. +/// Functor that computes the mean of two values. # include <mln/core/concept/function.hh> -# include <mln/math/min.hh> +# include <mln/math/mean.hh> namespace mln @@ -40,38 +40,37 @@ namespace mln namespace fun { - namespace vv2v + namespace vvvv2v { // FIXME: Doc. - /// \brief A functor computing the minimum of two values. - template <typename L, typename R = L> - struct min : public Function_vv2v< min<L,R> >, - private mlc_converts_to(R,L)::check_t + /// \brief A functor computing the mean of two values. + template <typename T, typename R=T> + struct mean : public Function_vvvv2v< mean<T> > { - typedef L result; - L operator()(const L& v1, const R& v2) const; + typedef R result; + R operator()(const T& v1, const T& v2, const T& v3, const T& v4) const; }; # ifndef MLN_INCLUDE_ONLY - template <typename L, typename R> + template <typename T> inline - L - min<L,R>::operator()(const L& v1, const R& v2) const + R + mean<T,R>::operator()(const T& v1, const T& v2, const T& v3, const T& v4) const { - return mln::math::min(v1, L(v2)); + return R(mln::math::mean(v1, v2, v3, v4)); } # endif // ! MLN_INCLUDE_ONLY - } // end of namespace mln::fun::vv2v + } // end of namespace mln::fun::vvvv2v } // end of namespace mln::fun } // end of namespace mln -#endif // ! MLN_FUN_VV2V_MIN_HH +#endif // ! MLN_FUN_VVVV2V_MEAN_HH diff --git a/milena/sandbox/green/mln/math/ceil.hh b/milena/mln/math/ceil.hh similarity index 76% copy from milena/sandbox/green/mln/math/ceil.hh copy to milena/mln/math/ceil.hh index 6fc08c8..997c8b5 100644 --- a/milena/sandbox/green/mln/math/ceil.hh +++ b/milena/mln/math/ceil.hh @@ -1,5 +1,4 @@ -// Copyright (C) 2007, 2008, 2009, 2010 -// EPITA Research and Development Laboratory (LRDE) +// Copyright (C) 2012 EPITA Research and Development Laboratory (LRDE) // // This file is part of Olena. // @@ -29,36 +28,47 @@ /*! \file * - * \brief Define the ceil (ceil) routine. + * \brief Define ceil routine. */ # include <cmath> - namespace mln { namespace math { + /// Return the nearest integer not less than \p v1. template <typename T> - T ceil(const T& v); + T ceil(const T& v1); + + } // end of namespace mln::math # ifndef MLN_INCLUDE_ONLY + /// \internal Generic implementation of the ceil function. + template <typename T> + T ceil_(const T& v1) + { + return std::ceil(v1); + } + + namespace math + { + template <typename T> - inline - T ceil(const T& v) + T ceil(const T& v1) { - return std::ceil(v); + return ceil_(exact(v1)); } -# endif // ! MLN_INCLUDE_ONLY - } // end of namespace mln::math +# endif // ! MLN_INCLUDE_ONLY + } // end of namespace mln -#endif // ! MLN_MATH_FLOOR_HH +#endif // ! MLN_MATH_CEIL_HH diff --git a/milena/sandbox/green/mln/math/floor.hh b/milena/mln/math/floor.hh similarity index 77% copy from milena/sandbox/green/mln/math/floor.hh copy to milena/mln/math/floor.hh index 97e63bd..defb112 100644 --- a/milena/sandbox/green/mln/math/floor.hh +++ b/milena/mln/math/floor.hh @@ -1,5 +1,4 @@ -// Copyright (C) 2007, 2008, 2009, 2010 -// EPITA Research and Development Laboratory (LRDE) +// Copyright (C) 2012 EPITA Research and Development Laboratory (LRDE) // // This file is part of Olena. // @@ -29,35 +28,46 @@ /*! \file * - * \brief Define the floor (floor) routine. + * \brief Define floor routine. */ # include <cmath> - namespace mln { namespace math { + /// \brief Return the nearest integer not greater than \p v1. template <typename T> - T floor(const T& v); + T floor(const T& v1); + + } // end of namespace mln::math # ifndef MLN_INCLUDE_ONLY + /// \internal Generic implementation of the floor function. + template <typename T> + T floor_(const T& v1) + { + return std::floor(v1); + } + + namespace math + { + template <typename T> - inline - T floor(const T& v) + T floor(const T& v1) { - return std::floor(v); + return floor_(exact(v1)); } -# endif // ! MLN_INCLUDE_ONLY - } // end of namespace mln::math +# endif // ! MLN_INCLUDE_ONLY + } // end of namespace mln diff --git a/milena/mln/math/max.hh b/milena/mln/math/max.hh index 24f124b..0fe8ea4 100644 --- a/milena/mln/math/max.hh +++ b/milena/mln/math/max.hh @@ -1,4 +1,5 @@ -// Copyright (C) 2007, 2009 EPITA Research and Development Laboratory (LRDE) +// Copyright (C) 2007, 2009. 2012 EPITA Research and Development +// Laboratory (LRDE) // // This file is part of Olena. // @@ -38,23 +39,35 @@ namespace mln namespace math { + /// Return the maximum of two values. template <typename T> T max(const T& v1, const T& v2); + } // end of namespace mln::math + # ifndef MLN_INCLUDE_ONLY + /// \internal Generic implementation of the maximum function. + template <typename T> + T max_(const T& v1, const T& v2) + { + return v1 > v2 ? v1 : v2; + } + + namespace math + { + template <typename T> - inline T max(const T& v1, const T& v2) { - return v1 > v2 ? v1 : v2; + return max_(exact(v1), exact(v2)); } -# endif // ! MLN_INCLUDE_ONLY - } // end of namespace mln::math +# endif // ! MLN_INCLUDE_ONLY + } // end of namespace mln diff --git a/milena/mln/math/mean.hh b/milena/mln/math/mean.hh new file mode 100644 index 0000000..d9c95fc --- /dev/null +++ b/milena/mln/math/mean.hh @@ -0,0 +1,128 @@ +// Copyright (C) 2012 EPITA Research and Development Laboratory (LRDE) +// +// This file is part of Olena. +// +// Olena is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation, version 2 of the License. +// +// Olena is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Olena. If not, see <http://www.gnu.org/licenses/>. +// +// As a special exception, you may use this file as part of a free +// software project without restriction. Specifically, if other files +// instantiate templates or use macros or inline functions from this +// file, or you compile this file and link it with other files to produce +// an executable, this file does not by itself cause the resulting +// executable to be covered by the GNU General Public License. This +// exception does not however invalidate any other reasons why the +// executable file might be covered by the GNU General Public License. + +#ifndef MLN_MATH_MEAN_HH +# define MLN_MATH_MEAN_HH + +# include <mln/trait/op/div.hh> +# include <mln/metal/converts_to.hh> + +/*! \file + * + * \brief Define mean routine. + */ + + +namespace mln +{ + + namespace math + { + + /// Return the mean of two values. + template <typename T> + mln_trait_op_div(T,double) + mean(const T& v1, const T& v2); + + /// \overload \pre Type U must be convertible towards type T. + template <typename T, typename U> + mln_trait_op_div(T,double) + mean(const T& v1, const U& v2); + + /// Return the mean of four values. + template <typename T> + mln_trait_op_div(T,double) + mean(const T& v1, const T& v2, const T& v3, const T& v4); + + /// \overload \pre Type T2, T3 and T4 must be convertible towards + /// type T. + template <typename T, typename T2, typename T3, typename T4> + mln_trait_op_div(T,double) + mean(const T& v1, const T2& v2, const T3& v3, const T4& v4); + + + } // end of namespace mln::math + +# ifndef MLN_INCLUDE_ONLY + + /// \internal Generic implementation of the mean of two values. + template <typename T> + mln_trait_op_div(T,double) + mean_(const T& v1, const T& v2) + { + return (v1 + v2) / 2.; + } + + /// \internal Generic implementation of the mean of four values. + template <typename T> + mln_trait_op_div(T,double) + mean_(const T& v1, const T& v2, const T& v3, const T& v4) + { + return (v1 + v2 + v3 + v4) / 4; + } + + namespace math + { + + template <typename T> + mln_trait_op_div(T,double) + mean(const T& v1, const T& v2) + { + return mean_(exact(v1), exact(v2)); + } + + template <typename T, typename U> + mln_trait_op_div(T,double) + mean(const T& v1, const U& v2) + { + mlc_converts_to(U,T)::check(); + return mean_(v1, T(v2)); + } + + template <typename T> + mln_trait_op_div(T,double) + mean(const T& v1, const T& v2, const T& v3, const T& v4) + { + return mean_(v1, T(v2), T(v3), T(v4)); + } + + template <typename T, typename T2, typename T3, typename T4> + mln_trait_op_div(T,double) + mean(const T& v1, const T2& v2, const T3& v3, const T4& v4) + { + mlc_converts_to(T2,T)::check(); + mlc_converts_to(T3,T)::check(); + mlc_converts_to(T4,T)::check(); + return mean_(v1, T(v2), T(v3), T(v4)); + } + + } // end of namespace mln::math + +# endif // ! MLN_INCLUDE_ONLY + +} // end of namespace mln + + +#endif // ! MLN_MATH_MEAN_HH diff --git a/milena/mln/math/min.hh b/milena/mln/math/min.hh index fd81d54..d2a5503 100644 --- a/milena/mln/math/min.hh +++ b/milena/mln/math/min.hh @@ -1,4 +1,5 @@ -// Copyright (C) 2007, 2009 EPITA Research and Development Laboratory (LRDE) +// Copyright (C) 2007, 2009, 2012 EPITA Research and Development +// Laboratory (LRDE) // // This file is part of Olena. // @@ -38,23 +39,35 @@ namespace mln namespace math { + /// Return the minimum of two values. template <typename T> T min(const T& v1, const T& v2); + } // end of namespace mln::math + # ifndef MLN_INCLUDE_ONLY + /// \internal Generic implementation of the minimum function. + template <typename T> + T min_(const T& v1, const T& v2) + { + return v1 < v2 ? v1 : v2; + } + + namespace math + { + template <typename T> - inline T min(const T& v1, const T& v2) { - return v1 < v2 ? v1 : v2; + return min_(exact(v1), exact(v2)); } -# endif // ! MLN_INCLUDE_ONLY - } // end of namespace mln::math +# endif // ! MLN_INCLUDE_ONLY + } // end of namespace mln -- 1.7.2.5