NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
deleteIndices.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <string>
31 #include <vector>
32 
34 #include "NumCpp/Core/Shape.hpp"
35 #include "NumCpp/Core/Slice.hpp"
37 #include "NumCpp/NdArray.hpp"
38 
39 namespace nc
40 {
41  //============================================================================
42  // Method Description:
50  template<typename dtype>
51  NdArray<dtype>
52  deleteIndices(const NdArray<dtype>& inArray, const NdArray<uint32>& inArrayIdxs, Axis inAxis = Axis::NONE)
53  {
54  // make sure that the indices are unique first
55  NdArray<uint32> indices = unique(inArrayIdxs);
56 
57  switch (inAxis)
58  {
59  case Axis::NONE:
60  {
61  std::vector<dtype> values;
62  for (uint32 i = 0; i < inArray.size(); ++i)
63  {
64  if (indices.contains(i).item())
65  {
66  continue;
67  }
68 
69  values.push_back(inArray[i]);
70  }
71 
72  return NdArray<dtype>(values);
73  }
74  case Axis::ROW:
75  {
76  const Shape inShape = inArray.shape();
77  if (indices.max().item() >= inShape.rows)
78  {
79  THROW_INVALID_ARGUMENT_ERROR("input index value is greater than the number of rows in the array.");
80  }
81 
82  const uint32 numNewRows = inShape.rows - indices.size();
83  NdArray<dtype> returnArray(numNewRows, inShape.cols);
84 
85  uint32 rowCounter = 0;
86  for (uint32 row = 0; row < inShape.rows; ++row)
87  {
88  if (indices.contains(row).item())
89  {
90  continue;
91  }
92 
93  for (uint32 col = 0; col < inShape.cols; ++col)
94  {
95  returnArray(rowCounter, col) = inArray(row, col);
96  }
97  ++rowCounter;
98  }
99 
100  return returnArray;
101  }
102  case Axis::COL:
103  {
104  const Shape inShape = inArray.shape();
105  if (indices.max().item() >= inShape.cols)
106  {
107  THROW_INVALID_ARGUMENT_ERROR("input index value is greater than the number of cols in the array.");
108  }
109 
110  const uint32 numNewCols = inShape.cols - indices.size();
111  NdArray<dtype> returnArray(inShape.rows, numNewCols);
112 
113  for (uint32 row = 0; row < inShape.rows; ++row)
114  {
115  uint32 colCounter = 0;
116  for (uint32 col = 0; col < inShape.cols; ++col)
117  {
118  if (indices.contains(col).item())
119  {
120  continue;
121  }
122 
123  returnArray(row, colCounter++) = inArray(row, col);
124  }
125  }
126 
127  return returnArray;
128  }
129  default:
130  {
131  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
132  return {}; // get rid of compiler warning
133  }
134  }
135  }
136 
137  //============================================================================
138  // Method Description:
146  template<typename dtype>
147  NdArray<dtype> deleteIndices(const NdArray<dtype>& inArray, const Slice& inIndicesSlice, Axis inAxis = Axis::NONE)
148  {
149  Slice sliceCopy(inIndicesSlice);
150 
151  switch (inAxis)
152  {
153  case Axis::NONE:
154  {
155  sliceCopy.makePositiveAndValidate(inArray.size());
156  break;
157  }
158  case Axis::ROW:
159  {
160  sliceCopy.makePositiveAndValidate(inArray.shape().cols);
161  break;
162  }
163  case Axis::COL:
164  {
165  sliceCopy.makePositiveAndValidate(inArray.shape().rows);
166  break;
167  }
168  }
169 
170  std::vector<uint32> indices;
171  for (auto i = static_cast<uint32>(sliceCopy.start); i < static_cast<uint32>(sliceCopy.stop);
172  i += sliceCopy.step)
173  {
174  indices.push_back(i);
175  }
176 
177  return deleteIndices(inArray, NdArray<uint32>(indices), inAxis);
178  }
179 
180  //============================================================================
181  // Method Description:
189  template<typename dtype>
191  {
192  NdArray<uint32> inIndices = { inIndex };
193  return deleteIndices(inArray, inIndices, inAxis);
194  }
195 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
size_type size() const noexcept
Definition: NdArrayCore.hpp:4105
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4092
NdArray< dtype > max(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:2828
value_type item() const
Definition: NdArrayCore.hpp:2809
NdArray< bool > contains(value_type inValue, Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:2276
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
A Class for slicing into NdArrays.
Definition: Slice.hpp:44
int32 step
Definition: Slice.hpp:49
int32 start
Definition: Slice.hpp:47
void makePositiveAndValidate(uint32 inArraySize)
Definition: Slice.hpp:141
int32 stop
Definition: Slice.hpp:48
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:47
NdArray< dtype > unique(const NdArray< dtype > &inArray)
Definition: unique.hpp:54
NdArray< dtype > deleteIndices(const NdArray< dtype > &inArray, const NdArray< uint32 > &inArrayIdxs, Axis inAxis=Axis::NONE)
Definition: deleteIndices.hpp:52
std::uint32_t uint32
Definition: Types.hpp:40