NumCpp  2.4.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:
51  template<typename dtype>
52  NdArray<dtype> 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 
130  }
131  default:
132  {
133  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
134  return {}; // get rid of compiler warning
135  }
136  }
137  }
138 
139  //============================================================================
140  // Method Description:
149  template<typename dtype>
150  NdArray<dtype> deleteIndices(const NdArray<dtype>& inArray, const Slice& inIndicesSlice, Axis inAxis = Axis::NONE)
151  {
152  Slice sliceCopy(inIndicesSlice);
153 
154  switch (inAxis)
155  {
156  case Axis::NONE:
157  {
158  sliceCopy.makePositiveAndValidate(inArray.size());
159  break;
160  }
161  case Axis::ROW:
162  {
163  sliceCopy.makePositiveAndValidate(inArray.shape().cols);
164  break;
165  }
166  case Axis::COL:
167  {
168  sliceCopy.makePositiveAndValidate(inArray.shape().rows);
169  break;
170  }
171  }
172 
173  std::vector<uint32> indices;
174  for (auto i = static_cast<uint32>(sliceCopy.start); i < static_cast<uint32>(sliceCopy.stop); i += sliceCopy.step)
175  {
176  indices.push_back(i);
177  }
178 
179  return deleteIndices(inArray, NdArray<uint32>(indices), inAxis);
180  }
181 
182  //============================================================================
183  // Method Description:
192  template<typename dtype>
194  {
195  NdArray<uint32> inIndices = { inIndex };
196  return deleteIndices(inArray, inIndices, inAxis);
197  }
198 } // namespace nc
nc::NdArray::item
value_type item() const
Definition: NdArrayCore.hpp:2975
nc::NdArray::contains
NdArray< bool > contains(value_type inValue, Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:2365
nc::Slice::stop
int32 stop
Definition: Slice.hpp:48
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4356
unique.hpp
nc::Axis::NONE
@ NONE
nc::Slice::makePositiveAndValidate
void makePositiveAndValidate(uint32 inArraySize)
Definition: Slice.hpp:137
Error.hpp
nc::Axis::ROW
@ ROW
nc::NdArray::max
NdArray< dtype > max(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:2996
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:40
nc::Slice::start
int32 start
Definition: Slice.hpp:47
nc::unique
NdArray< dtype > unique(const NdArray< dtype > &inArray)
Definition: unique.hpp:56
nc::NdArray::size
size_type size() const noexcept
Definition: NdArrayCore.hpp:4370
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:45
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:46
nc::Slice::step
int32 step
Definition: Slice.hpp:49
Shape.hpp
nc
Definition: Coordinate.hpp:44
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:44
nc::deleteIndices
NdArray< dtype > deleteIndices(const NdArray< dtype > &inArray, const NdArray< uint32 > &inArrayIdxs, Axis inAxis=Axis::NONE)
Definition: deleteIndices.hpp:52
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
nc::Axis::COL
@ COL
nc::Slice
A Class for slicing into NdArrays.
Definition: Slice.hpp:43
Slice.hpp