NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
nan_to_num.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include "NumCpp/NdArray.hpp"
36 
37 #include <utility>
38 
39 namespace nc
40 {
41  //============================================================================
42  // Method Description:
54  template<typename dtype>
56  dtype nan = static_cast<dtype>(0.0),
57  dtype posInf = DtypeInfo<dtype>::max(),
58  dtype negInf = DtypeInfo<dtype>::min())
59  {
60  STATIC_ASSERT_FLOAT(dtype);
61 
62  stl_algorithms::for_each(inArray.begin(), inArray.end(),
63  [nan, posInf, negInf](dtype& value)
64  {
65  if (isnan(value))
66  {
67  value = nan;
68  }
69  else if (isinf(value))
70  {
71  if (value > static_cast<dtype>(0.0))
72  {
73  value = posInf;
74  }
75  else
76  {
77  value = negInf;
78  }
79  }
80  }
81  );
82 
83  return inArray;
84  }
85 } // namespace nc
#define STATIC_ASSERT_FLOAT(dtype)
Definition: StaticAsserts.hpp:43
Holds info about the dtype.
Definition: DtypeInfo.hpp:41
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
iterator end() noexcept
Definition: NdArrayCore.hpp:1474
iterator begin() noexcept
Definition: NdArrayCore.hpp:1166
const double nan
NaN.
Definition: Constants.hpp:44
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
Definition: Coordinate.hpp:45
NdArray< dtype > nan_to_num(NdArray< dtype > inArray, dtype nan=static_cast< dtype >(0.0), dtype posInf=DtypeInfo< dtype >::max(), dtype negInf=DtypeInfo< dtype >::min())
Definition: nan_to_num.hpp:55
bool isinf(dtype inValue) noexcept
Definition: isinf.hpp:49