summaryrefslogtreecommitdiffstats
path: root/contrib/python/pythran/pythran/pythonic/numpy/nanargmin.hpp
blob: 3ea1be21299704fa6eacb116e63424b00742ca7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef PYTHONIC_NUMPY_NANARGMIN_HPP
#define PYTHONIC_NUMPY_NANARGMIN_HPP

#include "pythonic/include/numpy/nanargmin.hpp"

#include "pythonic/utils/functor.hpp"
#include "pythonic/types/ndarray.hpp"
#include "pythonic/builtins/ValueError.hpp"
#include "pythonic/numpy/isnan.hpp"

PYTHONIC_NS_BEGIN

namespace numpy
{

  namespace
  {
    template <class E, class F>
    void _nanargmin(E begin, E end, F &min, long &index, long &where,
                    utils::int_<1>)
    {
      for (; begin != end; ++begin, ++index) {
        auto curr = *begin;
        if (!functor::isnan()(curr) && curr < min) {
          min = curr;
          where = index;
        }
      }
    }

    template <class E, class F, size_t N>
    void _nanargmin(E begin, E end, F &min, long &index, long &where,
                    utils::int_<N>)
    {
      for (; begin != end; ++begin)
        _nanargmin((*begin).begin(), (*begin).end(), min, index, where,
                   utils::int_<N - 1>());
    }
  }

  template <class E>
  long nanargmin(E const &expr)
  {
    typename E::dtype min = std::numeric_limits<typename E::dtype>::infinity();
    long where = -1;
    long index = 0;
    _nanargmin(expr.begin(), expr.end(), min, index, where,
               utils::int_<E::value>());
    if (where >= 0)
      return where;
    else
      throw types::ValueError("empty sequence");
  }
}
PYTHONIC_NS_END

#endif