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