NumCpp  2.7.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 
31 #include "NumCpp/Core/Shape.hpp"
32 #include "NumCpp/Core/Slice.hpp"
34 #include "NumCpp/NdArray.hpp"
35 
36 #include <string>
37 #include <vector>
38 
39 namespace nc
40 {
41  //============================================================================
42  // Method Description:
50  template<typename dtype>
51  NdArray<dtype> deleteIndices(const NdArray<dtype>& inArray, const NdArray<uint32>& inArrayIdxs, Axis inAxis = Axis::NONE)
52  {
53  // make sure that the indices are unique first
54  NdArray<uint32> indices = unique(inArrayIdxs);
55 
56  switch (inAxis)
57  {
58  case Axis::NONE:
59  {
60  std::vector<dtype> values;
61  for (uint32 i = 0; i < inArray.size(); ++i)
62  {
63  if (indices.contains(i).item())
64  {
65  continue;
66  }
67 
68  values.push_back(inArray[i]);
69  }
70 
71  return NdArray<dtype>(values);
72  }
73  case Axis::ROW:
74  {
75  const Shape inShape = inArray.shape();
76  if (indices.max().item() >= inShape.rows)
77  {
78  THROW_INVALID_ARGUMENT_ERROR("input index value is greater than the number of rows in the array.");
79  }
80 
81  const uint32 numNewRows = inShape.rows - indices.size();
82  NdArray<dtype> returnArray(numNewRows, inShape.cols);
83 
84  uint32 rowCounter = 0;
85  for (uint32 row = 0; row < inShape.rows; ++row)
86  {
87  if (indices.contains(row).item())
88  {
89  continue;
90  }
91 
92  for (uint32 col = 0; col < inShape.cols; ++col)
93  {
94  returnArray(rowCounter, col) = inArray(row, col);
95  }
96  ++rowCounter;
97  }
98 
99  return returnArray;
100  }
101  case Axis::COL:
102  {
103  const Shape inShape = inArray.shape();
104  if (indices.max().item() >= inShape.cols)
105  {
106  THROW_INVALID_ARGUMENT_ERROR("input index value is greater than the number of cols in the array.");
107  }
108 
109  const uint32 numNewCols = inShape.cols - indices.size();
110  NdArray<dtype> returnArray(inShape.rows, numNewCols);
111 
112  for (uint32 row = 0; row < inShape.rows; ++row)
113  {
114  uint32 colCounter = 0;
115  for (uint32 col = 0; col < inShape.cols; ++col)
116  {
117  if (indices.contains(col).item())
118  {
119  continue;
120  }
121 
122  returnArray(row, colCounter++) = inArray(row, col);
123  }
124  }
125 
126  return returnArray;
127 
128 
129  }
130  default:
131  {
132  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
133  return {}; // get rid of compiler warning
134  }
135  }
136  }
137 
138  //============================================================================
139  // Method Description:
147  template<typename dtype>
148  NdArray<dtype> deleteIndices(const NdArray<dtype>& inArray, const Slice& inIndicesSlice, Axis inAxis = Axis::NONE)
149  {
150  Slice sliceCopy(inIndicesSlice);
151 
152  switch (inAxis)
153  {
154  case Axis::NONE:
155  {
156  sliceCopy.makePositiveAndValidate(inArray.size());
157  break;
158  }
159  case Axis::ROW:
160  {
161  sliceCopy.makePositiveAndValidate(inArray.shape().cols);
162  break;
163  }
164  case Axis::COL:
165  {
166  sliceCopy.makePositiveAndValidate(inArray.shape().rows);
167  break;
168  }
169  }
170 
171  std::vector<uint32> indices;
172  for (auto i = static_cast<uint32>(sliceCopy.start); i < static_cast<uint32>(sliceCopy.stop); 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:4296
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
NdArray< dtype > max(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:2950
value_type item() const
Definition: NdArrayCore.hpp:2931
NdArray< bool > contains(value_type inValue, Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:2345
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:137
int32 stop
Definition: Slice.hpp:48
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:46
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:51
std::uint32_t uint32
Definition: Types.hpp:40