NumCpp  1.0
A C++ implementation of the Python Numpy library
count_nonzero.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/NdArray.hpp"
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Types.hpp"
36 
37 namespace nc
38 {
39  //============================================================================
40  // Method Description:
50  template<typename dtype>
52  {
54 
55  switch (inAxis)
56  {
57  case Axis::NONE:
58  {
59  NdArray<uint32> count = { inArray.size() -
60  static_cast<uint32>(stl_algorithms::count(inArray.cbegin(), inArray.cend(), dtype{ 0 })) };
61  return count;
62  }
63  case Axis::COL:
64  {
65  Shape inShape = inArray.shape();
66 
67  NdArray<uint32> returnArray(1, inShape.rows);
68  for (uint32 row = 0; row < inShape.rows; ++row)
69  {
70  returnArray(0, row) = inShape.cols -
71  static_cast<uint32>(stl_algorithms::count(inArray.cbegin(row), inArray.cend(row), dtype{ 0 }));
72  }
73 
74  return returnArray;
75  }
76  case Axis::ROW:
77  {
78  NdArray<dtype> inArrayTranspose = inArray.transpose();
79  Shape inShapeTransposed = inArrayTranspose.shape();
80  NdArray<uint32> returnArray(1, inShapeTransposed.rows);
81  for (uint32 row = 0; row < inShapeTransposed.rows; ++row)
82  {
83  returnArray(0, row) = inShapeTransposed.cols -
84  static_cast<uint32>(stl_algorithms::count(inArrayTranspose.cbegin(row), inArrayTranspose.cend(row), dtype{ 0 }));
85  }
86 
87  return returnArray;
88  }
89  default:
90  {
91  // this isn't actually possible, just putting this here to get rid
92  // of the compiler warning.
93  return NdArray<uint32>(0);
94  }
95  }
96  }
97 }
StaticAsserts.hpp
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
nc::Axis::NONE
@ NONE
nc::Axis::ROW
@ ROW
STATIC_ASSERT_ARITHMETIC_OR_COMPLEX
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:51
nc::NdArray::transpose
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4591
nc::NdArray
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:75
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
nc::count_nonzero
NdArray< uint32 > count_nonzero(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: count_nonzero.hpp:51
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
nc::NdArray::size
size_type size() const noexcept
Definition: NdArrayCore.hpp:4310
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:46
nc::NdArray::cend
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1491
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:47
nc::stl_algorithms::count
std::iterator_traits< InputIt >::difference_type count(InputIt first, InputIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:116
Shape.hpp
nc
Definition: Coordinate.hpp:45
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
nc::NdArray::cbegin
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1147
StlAlgorithms.hpp
Types.hpp
nc::Axis::COL
@ COL