NumCpp  2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
count_nonzero.hpp
Go to the documentation of this file.
1 #pragma once
29 
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
35 
36 namespace nc
37 {
38  //============================================================================
39  // Method Description:
49  template<typename dtype>
51  {
53 
54  switch (inAxis)
55  {
56  case Axis::NONE:
57  {
58  NdArray<uint32> count = { inArray.size() -
59  static_cast<uint32>(stl_algorithms::count(inArray.cbegin(), inArray.cend(), dtype{ 0 })) };
60  return count;
61  }
62  case Axis::COL:
63  {
64  Shape inShape = inArray.shape();
65 
66  NdArray<uint32> returnArray(1, inShape.rows);
67  for (uint32 row = 0; row < inShape.rows; ++row)
68  {
69  returnArray(0, row) = inShape.cols -
70  static_cast<uint32>(stl_algorithms::count(inArray.cbegin(row), inArray.cend(row), dtype{ 0 }));
71  }
72 
73  return returnArray;
74  }
75  case Axis::ROW:
76  {
77  NdArray<dtype> inArrayTranspose = inArray.transpose();
78  Shape inShapeTransposed = inArrayTranspose.shape();
79  NdArray<uint32> returnArray(1, inShapeTransposed.rows);
80  for (uint32 row = 0; row < inShapeTransposed.rows; ++row)
81  {
82  returnArray(0, row) = inShapeTransposed.cols -
83  static_cast<uint32>(stl_algorithms::count(inArrayTranspose.cbegin(row), inArrayTranspose.cend(row), dtype{ 0 }));
84  }
85 
86  return returnArray;
87  }
88  default:
89  {
90  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
91  return {}; // get rid of compiler warning
92  }
93  }
94  }
95 } // namespace nc
StaticAsserts.hpp
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4356
nc::Axis::NONE
@ NONE
nc::Axis::ROW
@ ROW
STATIC_ASSERT_ARITHMETIC_OR_COMPLEX
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:50
nc::NdArray::transpose
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4652
nc::NdArray
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:71
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
nc::count_nonzero
NdArray< uint32 > count_nonzero(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: count_nonzero.hpp:50
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:40
nc::NdArray::size
size_type size() const noexcept
Definition: NdArrayCore.hpp:4370
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:45
nc::NdArray::cend
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1487
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:46
nc::stl_algorithms::count
std::iterator_traits< InputIt >::difference_type count(InputIt first, InputIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:115
Shape.hpp
nc
Definition: Coordinate.hpp:44
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:44
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
nc::NdArray::cbegin
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1143
StlAlgorithms.hpp
Types.hpp
nc::Axis::COL
@ COL