* mln/fun/vv2v/median.hh,
* mln/fun/vvvv2v/median.hh,
* mln/math/median.hh,
* mln/trait/routine/median.hh,
* tests/fun/vv2v/mean.cc,
* tests/fun/vv2v/median.cc,
* tests/math/mean.cc,
* tests/math/median.cc: New.
* mln/fun/vv2v/mean.hh,
* mln/fun/vvvv2v/mean.hh,
* mln/math/mean.hh,
* mln/value/intsub.hh: Use a dedicated trait to deduce the return
type of mean() function.
* mln/trait/routine/mean.hh: Add more specializations.
* tests/fun/vv2v/Makefile.am,
* tests/math/Makefile.am: Update targets.
---
milena/ChangeLog | 24 +++++
milena/mln/fun/vv2v/mean.hh | 3 +-
milena/mln/fun/vv2v/median.hh | 77 ++++++++++++++++
milena/mln/fun/vvvv2v/mean.hh | 3 +-
milena/mln/fun/vvvv2v/median.hh | 78 ++++++++++++++++
milena/mln/math/mean.hh | 24 +++---
milena/mln/math/median.hh | 172 ++++++++++++++++++++++++++++++++++++
milena/mln/trait/routine/mean.hh | 16 +++-
milena/mln/trait/routine/median.hh | 79 ++++++++++++++++
milena/mln/value/intsub.hh | 66 +++++---------
milena/tests/fun/vv2v/Makefile.am | 7 ++-
milena/tests/fun/vv2v/mean.cc | 71 +++++++++++++++
milena/tests/fun/vv2v/median.cc | 71 +++++++++++++++
milena/tests/math/Makefile.am | 13 +++-
milena/tests/math/mean.cc | 47 ++++++++++
milena/tests/math/median.cc | 90 +++++++++++++++++++
16 files changed, 779 insertions(+), 62 deletions(-)
create mode 100644 milena/mln/fun/vv2v/median.hh
create mode 100644 milena/mln/fun/vvvv2v/median.hh
create mode 100644 milena/mln/math/median.hh
create mode 100644 milena/mln/trait/routine/median.hh
create mode 100644 milena/tests/fun/vv2v/mean.cc
create mode 100644 milena/tests/fun/vv2v/median.cc
create mode 100644 milena/tests/math/mean.cc
create mode 100644 milena/tests/math/median.cc
diff --git a/milena/ChangeLog b/milena/ChangeLog
index 704143a..a02e163 100644
--- a/milena/ChangeLog
+++ b/milena/ChangeLog
@@ -1,5 +1,29 @@
2012-10-23 Guillaume Lazzara <z(a)lrde.epita.fr>
+ Improve support for mean and median functions.
+
+ * mln/fun/vv2v/median.hh,
+ * mln/fun/vvvv2v/median.hh,
+ * mln/math/median.hh,
+ * mln/trait/routine/median.hh,
+ * tests/fun/vv2v/mean.cc,
+ * tests/fun/vv2v/median.cc,
+ * tests/math/mean.cc,
+ * tests/math/median.cc: New.
+
+ * mln/fun/vv2v/mean.hh,
+ * mln/fun/vvvv2v/mean.hh,
+ * mln/math/mean.hh,
+ * mln/value/intsub.hh: Use a dedicated trait to deduce the return
+ type of mean() function.
+
+ * mln/trait/routine/mean.hh: Add more specializations.
+
+ * tests/fun/vv2v/Makefile.am,
+ * tests/math/Makefile.am: Update targets.
+
+2012-10-23 Guillaume Lazzara <z(a)lrde.epita.fr>
+
* mln/accu/stat/median_h_interval.hh: Make it inherit from
accumulator base.
diff --git a/milena/mln/fun/vv2v/mean.hh b/milena/mln/fun/vv2v/mean.hh
index 65e9f63..809d394 100644
--- a/milena/mln/fun/vv2v/mean.hh
+++ b/milena/mln/fun/vv2v/mean.hh
@@ -31,6 +31,7 @@
/// Functor that computes the mean of two values.
# include <mln/core/concept/function.hh>
+# include <mln/trait/routine/mean.hh>
# include <mln/math/mean.hh>
@@ -44,7 +45,7 @@ namespace mln
{
/// \brief A functor computing the mean of two values.
- template <typename V, typename R = V>
+ template <typename V, typename R = mln_trait_routine_mean(2,V) >
struct mean : public Function_vv2v< mean<V,R> >,
private mlc_converts_to(R,V)::check_t
{
diff --git a/milena/mln/fun/vv2v/median.hh b/milena/mln/fun/vv2v/median.hh
new file mode 100644
index 0000000..a5a451a
--- /dev/null
+++ b/milena/mln/fun/vv2v/median.hh
@@ -0,0 +1,77 @@
+// 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_FUN_VV2V_MEDIAN_HH
+# define MLN_FUN_VV2V_MEDIAN_HH
+
+/// \file
+///
+/// Functor that computes the median of two values.
+
+# include <mln/core/concept/function.hh>
+# include <mln/trait/routine/median.hh>
+# include <mln/math/median.hh>
+
+
+namespace mln
+{
+
+ namespace fun
+ {
+
+ namespace vv2v
+ {
+
+ /// \brief A functor computing the median of two values.
+ template <typename V, typename R = mln_trait_routine_median(V) >
+ struct median : public Function_vv2v< median<V,R> >,
+ private mlc_converts_to(R,V)::check_t
+ {
+ typedef R result;
+ typedef V argument;
+ R operator()(const V& v1, const V& v2) const;
+ };
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ template <typename V, typename R>
+ inline
+ R
+ median<V,R>::operator()(const V& v1, const V& v2) const
+ {
+ return R(mln::math::mean(v1, v2));
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::fun::vv2v
+
+ } // end of namespace mln::fun
+
+} // end of namespace mln
+
+
+#endif // ! MLN_FUN_VV2V_MEDIAN_HH
diff --git a/milena/mln/fun/vvvv2v/mean.hh b/milena/mln/fun/vvvv2v/mean.hh
index d9810ab..7cc14e9 100644
--- a/milena/mln/fun/vvvv2v/mean.hh
+++ b/milena/mln/fun/vvvv2v/mean.hh
@@ -31,6 +31,7 @@
/// Functor that computes the mean of two values.
# include <mln/core/concept/function.hh>
+# include <mln/trait/routine/mean.hh>
# include <mln/math/mean.hh>
@@ -46,7 +47,7 @@ namespace mln
// FIXME: Doc.
/// \brief A functor computing the mean of two values.
- template <typename T, typename R=T>
+ template <typename T, typename R = mln_trait_routine_mean(4,T) >
struct mean : public Function_vvvv2v< mean<T> >
{
typedef R result;
diff --git a/milena/mln/fun/vvvv2v/median.hh b/milena/mln/fun/vvvv2v/median.hh
new file mode 100644
index 0000000..e08dabd
--- /dev/null
+++ b/milena/mln/fun/vvvv2v/median.hh
@@ -0,0 +1,78 @@
+// 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_FUN_VVVV2V_MEDIAN_HH
+# define MLN_FUN_VVVV2V_MEDIAN_HH
+
+/// \file
+///
+/// Functor that computes the median of two values.
+
+# include <mln/core/concept/function.hh>
+# include <mln/trait/routine/median.hh>
+# include <mln/math/median.hh>
+
+
+namespace mln
+{
+
+ namespace fun
+ {
+
+ namespace vvvv2v
+ {
+
+ // FIXME: Doc.
+
+ /// \brief A functor computing the median of two values.
+ template <typename T, typename R = mln_trait_routine_median(T) >
+ struct median : public Function_vvvv2v< median<T> >
+ {
+ typedef T argument;
+ typedef R result;
+ R operator()(const T& v1, const T& v2, const T& v3, const T& v4) const;
+ };
+
+
+# ifndef MLN_INCLUDE_ONLY
+
+ template <typename T, typename R>
+ inline
+ R
+ median<T,R>::operator()(const T& v1, const T& v2, const T& v3,
const T& v4) const
+ {
+ return R(mln::math::median(v1, v2, v3, v4));
+ }
+
+# endif // ! MLN_INCLUDE_ONLY
+
+ } // end of namespace mln::fun::vvvv2v
+
+ } // end of namespace mln::fun
+
+} // end of namespace mln
+
+
+#endif // ! MLN_FUN_VVVV2V_MEDIAN_HH
diff --git a/milena/mln/math/mean.hh b/milena/mln/math/mean.hh
index d9c95fc..86a5efc 100644
--- a/milena/mln/math/mean.hh
+++ b/milena/mln/math/mean.hh
@@ -26,7 +26,7 @@
#ifndef MLN_MATH_MEAN_HH
# define MLN_MATH_MEAN_HH
-# include <mln/trait/op/div.hh>
+# include <mln/trait/routine/mean.hh>
# include <mln/metal/converts_to.hh>
/*! \file
@@ -43,23 +43,23 @@ namespace mln
/// Return the mean of two values.
template <typename T>
- mln_trait_op_div(T,double)
+ mln_trait_routine_mean(2,T)
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)
+ mln_trait_routine_mean(2,T)
mean(const T& v1, const U& v2);
/// Return the mean of four values.
template <typename T>
- mln_trait_op_div(T,double)
+ mln_trait_routine_mean(4,T)
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)
+ mln_trait_routine_mean(4,T)
mean(const T& v1, const T2& v2, const T3& v3, const T4& v4);
@@ -69,7 +69,7 @@ namespace mln
/// \internal Generic implementation of the mean of two values.
template <typename T>
- mln_trait_op_div(T,double)
+ mln_trait_routine_mean(2,T)
mean_(const T& v1, const T& v2)
{
return (v1 + v2) / 2.;
@@ -77,24 +77,24 @@ namespace mln
/// \internal Generic implementation of the mean of four values.
template <typename T>
- mln_trait_op_div(T,double)
+ mln_trait_routine_mean(4,T)
mean_(const T& v1, const T& v2, const T& v3, const T& v4)
{
- return (v1 + v2 + v3 + v4) / 4;
+ return (v1 + v2 + v3 + v4) / 4.;
}
namespace math
{
template <typename T>
- mln_trait_op_div(T,double)
+ mln_trait_routine_mean(2,T)
mean(const T& v1, const T& v2)
{
return mean_(exact(v1), exact(v2));
}
template <typename T, typename U>
- mln_trait_op_div(T,double)
+ mln_trait_routine_mean(2,T)
mean(const T& v1, const U& v2)
{
mlc_converts_to(U,T)::check();
@@ -102,14 +102,14 @@ namespace mln
}
template <typename T>
- mln_trait_op_div(T,double)
+ mln_trait_routine_mean(4,T)
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)
+ mln_trait_routine_mean(4,T)
mean(const T& v1, const T2& v2, const T3& v3, const T4& v4)
{
mlc_converts_to(T2,T)::check();
diff --git a/milena/mln/math/median.hh b/milena/mln/math/median.hh
new file mode 100644
index 0000000..9338b90
--- /dev/null
+++ b/milena/mln/math/median.hh
@@ -0,0 +1,172 @@
+// 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_MEDIAN_HH
+# define MLN_MATH_MEDIAN_HH
+
+# include <mln/trait/routine/median.hh>
+# include <mln/math/mean.hh>
+# include <mln/metal/converts_to.hh>
+
+/*! \file
+ *
+ * \brief Define median routine.
+ */
+
+
+namespace mln
+{
+
+ namespace math
+ {
+
+ /// Return the median of two values.
+ template <typename T>
+ mln_trait_routine_median(T)
+ median(const T& v1, const T& v2);
+
+ /// \overload \pre Type U must be convertible towards type T.
+ template <typename T, typename U>
+ mln_trait_routine_median(T)
+ median(const T& v1, const U& v2);
+
+ /// Return the median of four values.
+ template <typename T>
+ mln_trait_routine_median(T)
+ median(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_routine_median(T)
+ median(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 median of four values.
+ template <typename T>
+ mln_trait_routine_median(T)
+ median_(const T& v1, const T& v2, const T& v3, const T& v4)
+ {
+ if (v1 <= v2)
+ if (v2 <= v3)
+ if (v3 <= v4)
+ return mean_(v2, v3);
+ else // v3 > v4
+ if (v4 <= v1)
+ return mean_(v1,v2);
+ else // v4 > v1
+ return mean_(v4,v2);
+ else // v2 > v3
+ if (v1 <= v3)
+ if (v2 <= v4)
+ return mean_(v3, v2);
+ else // v2 > v4
+ if (v4 <= v1)
+ return mean_(v1, v3);
+ else // v4 > v1
+ return mean_(v4, v3);
+ else // v1 > v3
+ if (v2 <= v4)
+ return mean_(v1,v2);
+ else // v2 > v4
+ if (v4 <= v3)
+ return mean_(v3,v1);
+ else // v4 > v3
+ return mean_(v4,v1);
+ else // v1 > v2
+ if (v1 <= v3)
+ if (v4 <= v3)
+ if (v4 <= v2)
+ return mean_(v2, v1);
+ else // v4 > v2
+ return mean_(v1, v4);
+ else // v4 > v2
+ return mean_(v1, v3);
+ else // v1 > v3
+ if (v2 <= v3)
+ if (v4 <= v1)
+ if (v4 <= v2)
+ return mean_(v2, v3);
+ else // v4 > v2
+ return mean_(v4, v3);
+ else // v4 > v1
+ return mean_(v3, v1);
+ else // v2 > v3
+ if (v4 <= v1)
+ if (v3 <= v4)
+ return mean_(v4, v2);
+ else // v3 > v4
+ return mean_(v3, v2);
+ else // v4 > v1
+ return mean_(v2, v1);
+ }
+
+ namespace math
+ {
+
+ template <typename T>
+ mln_trait_routine_median(T)
+ median(const T& v1, const T& v2)
+ {
+ return mean_(exact(v1), exact(v2));
+ }
+
+ template <typename T, typename U>
+ mln_trait_routine_median(T)
+ median(const T& v1, const U& v2)
+ {
+ mlc_converts_to(U,T)::check();
+ return mean_(v1, T(v2));
+ }
+
+ template <typename T>
+ mln_trait_routine_median(T)
+ median(const T& v1, const T& v2, const T& v3, const T& v4)
+ {
+ return median_(v1, T(v2), T(v3), T(v4));
+ }
+
+ template <typename T, typename T2, typename T3, typename T4>
+ mln_trait_routine_median(T)
+ median(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 median_(v1, T(v2), T(v3), T(v4));
+ }
+
+ } // end of namespace mln::math
+
+# endif // ! MLN_INCLUDE_ONLY
+
+} // end of namespace mln
+
+
+#endif // ! MLN_MATH_MEDIAN_HH
diff --git a/milena/mln/trait/routine/mean.hh b/milena/mln/trait/routine/mean.hh
index 6d96344..8cdbcc2 100644
--- a/milena/mln/trait/routine/mean.hh
+++ b/milena/mln/trait/routine/mean.hh
@@ -26,8 +26,8 @@
#ifndef MLN_TRAIT_ROUTINE_MEAN_HH
# define MLN_TRAIT_ROUTINE_MEAN_HH
-# define mln_trait_routine_mean(n,T) typename mln::trait::routine::mean<n,T>::ret
-# define mln_trait_routine_mean_(n,T) mln::trait::routine::mean<n,T>::ret
+# define mln_trait_routine_mean(n,T) typename mln::trait::routine::mean< n,T
>::ret
+# define mln_trait_routine_mean_(n,T) mln::trait::routine::mean< n,T >::ret
namespace mln
{
@@ -58,6 +58,18 @@ namespace mln
typedef mln::value::intsub<nvalues> ret;
};
+ template <unsigned nvalues>
+ struct mean< nvalues, float >
+ {
+ typedef float ret;
+ };
+
+ template <unsigned nvalues>
+ struct mean< nvalues, double >
+ {
+ typedef double ret;
+ };
+
} // end of namespace mln::trait::routine
} // end of namespace mln::routine
diff --git a/milena/mln/trait/routine/median.hh b/milena/mln/trait/routine/median.hh
new file mode 100644
index 0000000..40ea41e
--- /dev/null
+++ b/milena/mln/trait/routine/median.hh
@@ -0,0 +1,79 @@
+// 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_TRAIT_ROUTINE_MEDIAN_HH
+# define MLN_TRAIT_ROUTINE_MEDIAN_HH
+
+# define mln_trait_routine_median(T) typename mln::trait::routine::median< T
>::ret
+# define mln_trait_routine_median_(T) mln::trait::routine::median< T >::ret
+
+namespace mln
+{
+
+ // Forward Declaration
+ namespace value { template <unsigned n> class intsub; }
+
+ namespace trait
+ {
+
+ namespace routine
+ {
+
+ // median
+
+ template <typename T>
+ struct median
+ {
+ // undef
+ };
+
+
+ // specializations
+
+ template <>
+ struct median< int >
+ {
+ typedef mln::value::intsub<2> ret;
+ };
+
+ template <>
+ struct median< float >
+ {
+ typedef float ret;
+ };
+
+ template <>
+ struct median< double >
+ {
+ typedef double ret;
+ };
+
+ } // end of namespace mln::trait::routine
+
+ } // end of namespace mln::routine
+
+} // end of namespace mln
+
+#endif // ! MLN_TRAIT_ROUTINE_MEDIAN_HH
diff --git a/milena/mln/value/intsub.hh b/milena/mln/value/intsub.hh
index eaff527..5cf6ca5 100644
--- a/milena/mln/value/intsub.hh
+++ b/milena/mln/value/intsub.hh
@@ -37,6 +37,7 @@
# include <algorithm>
# include <mln/core/routine/ops.hh>
# include <mln/trait/routine/mean.hh>
+# include <mln/trait/routine/median.hh>
# include <mln/value/ops.hh>
# include <mln/value/builtin/ops.hh>
# include <mln/value/internal/value_like.hh>
@@ -109,6 +110,12 @@ namespace mln
typedef mln::value::intsub<nvalues * n> ret;
};
+ template <unsigned n>
+ struct median< mln::value::intsub<n> >
+ {
+ typedef mln::value::intsub<2*n> ret;
+ };
+
} // end of namespace mln::trait::routine
} // end of namespace mln::trait
@@ -205,21 +212,19 @@ namespace mln
/// Specific implementation of the mean function.
template <unsigned n>
- intsub<2*n> mean(const intsub<n>& v1, const intsub<n>&
v2);
+ mln_trait_routine_mean(2,intsub<n>)
+ mean_(const intsub<n>& v1, const intsub<n>& v2);
/// Specific implementation of the mean function.
template <unsigned n>
- intsub<4*n> mean(const intsub<n>& v1, const intsub<n>& v2,
- const intsub<n>& v3, const intsub<n>& v4);
+ mln_trait_routine_mean(4,intsub<n>)
+ mean_(const intsub<n>& v1, const intsub<n>& v2,
+ const intsub<n>& v3, const intsub<n>& v4);
/// Specific implementation of the median function.
template <unsigned n>
- intsub<2*n> median(const intsub<n>& v1, const intsub<n>&
v2);
-
- /// Specific implementation of the median function.
- template <unsigned n>
- intsub<4*n> median(const intsub<n>& v1, const intsub<n>&
v2,
- const intsub<n>& v3, const intsub<n>& v4);
+ mln_trait_routine_median(intsub<n>)
+ median_(const intsub<n>& v1, const intsub<n>& v2);
// Iota
@@ -484,55 +489,28 @@ namespace mln
return intsub<n>::make_from_enc_(v1.to_enc() > v2.to_enc() ? v1.to_enc() :
v2.to_enc());
}
- // FIXME: Make use of mean_() overloads with math::mean. Require
- // to fix an issue with the return type which differs according to
- // the overload : with 2 (intsub<2*n>) or 4 (intsub<4*n>)
- // arguments.
template <unsigned n>
- intsub<2*n> mean(const intsub<n>& v1, const intsub<n>& v2)
+ mln_trait_routine_mean(2,intsub<n>)
+ mean_(const intsub<n>& v1, const intsub<n>& v2)
{
return intsub<2*n>::make_from_enc_((v1.to_enc() + v2.to_enc()));
}
- // FIXME: Make use of mean_() overloads with math::mean. Require
- // to fix an issue with the return type which differs according to
- // the overload : with 2 (intsub<2*n>) or 4 (intsub<4*n>)
- // arguments.
template <unsigned n>
- intsub<4*n> mean(const intsub<n>& v1, const intsub<n>& v2,
- const intsub<n>& v3, const intsub<n>& v4)
+ mln_trait_routine_mean(4,intsub<n>)
+ mean_(const intsub<n>& v1, const intsub<n>& v2,
+ const intsub<n>& v3, const intsub<n>& v4)
{
return mean_(mean_(v1, v2), mean_(v3, v4));
}
- // FIXME: Make use of median_() overloads with
- // math::median. Require to fix an issue with the return type
- // which differs according to the overload : with 2 (intsub<2*n>)
- // or 4 (intsub<4*n>) arguments.
template <unsigned n>
- intsub<2*n> median(const intsub<n>& v1, const intsub<n>&
v2)
+ mln_trait_routine_median(intsub<n>)
+ median_(const intsub<n>& v1, const intsub<n>& v2)
{
- return mean(v1, v2);
- }
-
- // FIXME: Make use of median_() overloads with
- // math::median. Require to fix an issue with the return type
- // which differs according to the overload : with 2 (intsub<2*n>)
- // or 4 (intsub<4*n>) arguments.
- template <unsigned n>
- intsub<4*n> median(const intsub<n>& v1, const intsub<n>&
v2,
- const intsub<n>& v3, const intsub<n>& v4)
- {
- std::vector<intsub<n> > vec(4);
- vec.push_back(v1);
- vec.push_back(v2);
- vec.push_back(v3);
- vec.push_back(v4);
- std::sort(vec.begin(), vec.end());
- return mean(vec[1], vec[2]);
+ return mean_(v1, v2);
}
-
# endif // ! MLN_INCLUDE_ONLY
} // end of namespace mln::value
diff --git a/milena/tests/fun/vv2v/Makefile.am b/milena/tests/fun/vv2v/Makefile.am
index d180e3d..47ae244 100644
--- a/milena/tests/fun/vv2v/Makefile.am
+++ b/milena/tests/fun/vv2v/Makefile.am
@@ -1,4 +1,5 @@
-# Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE).
+# Copyright (C) 2008, 2009, 2012 EPITA Research and Development
+# Laboratory (LRDE).
#
# This file is part of Olena.
#
@@ -18,8 +19,12 @@ include $(top_srcdir)/milena/tests/tests.mk
check_PROGRAMS = \
max \
+ mean \
+ median \
min
+mean_SOURCES = mean.cc
+median_SOURCES = median.cc
max_SOURCES = max.cc
min_SOURCES = min.cc
diff --git a/milena/tests/fun/vv2v/mean.cc b/milena/tests/fun/vv2v/mean.cc
new file mode 100644
index 0000000..fc11025
--- /dev/null
+++ b/milena/tests/fun/vv2v/mean.cc
@@ -0,0 +1,71 @@
+// 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.
+
+#include <mln/fun/vv2v/mean.hh>
+#include <mln/value/intsub.hh>
+
+using namespace mln;
+
+void do_it(float v1, float v2, float res)
+{
+
+ {
+ fun::vv2v::mean<value::intsub<2> > f;
+ mln_assertion(f(v1, v2) == res);
+ }
+
+ {
+ fun::vv2v::mean<int> f;
+ mln_assertion(f(v1, v2) == res);
+ }
+
+ {
+ fun::vv2v::mean<float> f;
+ mln_assertion(f(v1, v2) == res);
+ }
+}
+
+
+void do_it2(float v1, float v2, float res)
+{
+ {
+ fun::vv2v::mean<value::intsub<2> > f;
+ mln_assertion(f(v1, v2) == res);
+ }
+
+ {
+ fun::vv2v::mean<float> f;
+ mln_assertion(f(v1, v2) == res);
+ }
+}
+
+
+int main()
+{
+ do_it(3, 4, 3.5);
+ do_it(0, 4, 2);
+
+ do_it2(0, 4.5, 2.25);
+}
diff --git a/milena/tests/fun/vv2v/median.cc b/milena/tests/fun/vv2v/median.cc
new file mode 100644
index 0000000..1bf83d6
--- /dev/null
+++ b/milena/tests/fun/vv2v/median.cc
@@ -0,0 +1,71 @@
+// 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.
+
+#include <mln/fun/vv2v/median.hh>
+#include <mln/value/intsub.hh>
+
+using namespace mln;
+
+void do_it(float v1, float v2, float res)
+{
+
+ {
+ fun::vv2v::median<value::intsub<2> > f;
+ mln_assertion(f(v1, v2) == res);
+ }
+
+ {
+ fun::vv2v::median<int> f;
+ mln_assertion(f(v1, v2) == res);
+ }
+
+ {
+ fun::vv2v::median<float> f;
+ mln_assertion(f(v1, v2) == res);
+ }
+}
+
+
+void do_it2(float v1, float v2, float res)
+{
+ {
+ fun::vv2v::median<value::intsub<2> > f;
+ mln_assertion(f(v1, v2) == res);
+ }
+
+ {
+ fun::vv2v::median<float> f;
+ mln_assertion(f(v1, v2) == res);
+ }
+}
+
+
+int main()
+{
+ do_it(3, 4, 3.5);
+ do_it(0, 4, 2);
+
+ do_it2(0, 4.5, 2.25);
+}
diff --git a/milena/tests/math/Makefile.am b/milena/tests/math/Makefile.am
index 4c3ccb4..864e21c 100644
--- a/milena/tests/math/Makefile.am
+++ b/milena/tests/math/Makefile.am
@@ -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.
#
@@ -15,3 +16,13 @@
# along with Olena. If not, see <http://www.gnu.org/licenses/>.
include $(top_srcdir)/milena/tests/tests.mk
+
+check_PROGRAMS = \
+ mean \
+ median
+
+mean_SOURCES = mean.cc
+median_SOURCES = median.cc
+
+
+TESTS = $(check_PROGRAMS)
diff --git a/milena/tests/math/mean.cc b/milena/tests/math/mean.cc
new file mode 100644
index 0000000..a62cab6
--- /dev/null
+++ b/milena/tests/math/mean.cc
@@ -0,0 +1,47 @@
+// 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.
+
+#include <mln/math/mean.hh>
+#include <mln/value/intsub.hh>
+
+int main()
+{
+ using namespace mln;
+
+ mln_assertion(math::mean(2,3) == 2.5);
+
+ {
+ value::intsub<2> v1(1.5);
+ value::intsub<2> v2(2);
+ mln_assertion(math::mean(v1, v2) == 1.75);
+ }
+
+ mln_assertion(math::mean(1,2,3,4) == 2.5);
+
+ {
+ value::intsub<2> v1(1), v2(2), v3(3), v4(5);
+ mln_assertion(math::mean(v1, v2, v3, v4) == 2.75);
+ }
+}
diff --git a/milena/tests/math/median.cc b/milena/tests/math/median.cc
new file mode 100644
index 0000000..5c0a58d
--- /dev/null
+++ b/milena/tests/math/median.cc
@@ -0,0 +1,90 @@
+// 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.
+
+#include <mln/math/median.hh>
+#include <mln/value/intsub.hh>
+
+template <typename T>
+void do_it(std::vector<T>& vec, float res)
+{
+ for (int i = 0; i < 3; ++i)
+ {
+ for (int j = 0; j < 3; ++j)
+ {
+ for (int k = 0; k < 3; ++k)
+ {
+ mln_assertion(mln::math::median(vec[0], vec[1], vec[2], vec[3]) == res);
+ std::swap(vec[k], vec[k + 1]);
+ }
+ }
+ }
+}
+
+int main()
+{
+ using namespace mln;
+
+ {
+ mln_assertion(math::median(2,3) == 2.5);
+ }
+
+ {
+ value::intsub<2> v1(1.5);
+ value::intsub<2> v2(2);
+ mln_assertion(math::median(v1, v2) == 1.75);
+ }
+
+ {
+ std::vector<int> vec(4);
+ vec[0] = 1;
+ vec[1] = 2;
+ vec[2] = 3;
+ vec[3] = 4;
+
+ do_it(vec, 2.5);
+ }
+
+ {
+ std::vector<int> vec(4);
+ vec[0] = 0;
+ vec[1] = 0;
+ vec[2] = 6;
+ vec[3] = 24;
+
+ do_it(vec, 3);
+ }
+
+
+ {
+ std::vector<value::intsub<2> > vec(4);
+ vec[0] = 2;
+ vec[1] = 1.5;
+ vec[2] = 3;
+ vec[3] = 5;
+
+ do_it(vec, 2.5);
+ }
+
+}
--
1.7.2.5