NumCpp  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  // this isn't actually possible, just putting this here to get rid
135  // of the compiler warning.
136  return NdArray<dtype>(0);
137  }
138  }
139  }
140 
141  //============================================================================
142  // Method Description:
151  template<typename dtype>
152  NdArray<dtype> deleteIndices(const NdArray<dtype>& inArray, const Slice& inIndicesSlice, Axis inAxis = Axis::NONE)
153  {
154  Slice sliceCopy(inIndicesSlice);
155 
156  switch (inAxis)
157  {
158  case Axis::NONE:
159  {
160  sliceCopy.makePositiveAndValidate(inArray.size());
161  break;
162  }
163  case Axis::ROW:
164  {
165  sliceCopy.makePositiveAndValidate(inArray.shape().cols);
166  break;
167  }
168  case Axis::COL:
169  {
170  sliceCopy.makePositiveAndValidate(inArray.shape().rows);
171  break;
172  }
173  }
174 
175  std::vector<uint32> indices;
176  for (uint32 i = static_cast<uint32>(sliceCopy.start); i < static_cast<uint32>(sliceCopy.stop); i += sliceCopy.step)
177  {
178  indices.push_back(i);
179  }
180 
181  return deleteIndices(inArray, NdArray<uint32>(indices), inAxis);
182  }
183 
184  //============================================================================
185  // Method Description:
194  template<typename dtype>
196  {
197  NdArray<uint32> inIndices = { inIndex };
198  return deleteIndices(inArray, inIndices, inAxis);
199  }
200 }
nc::NdArray::item
value_type item() const
Definition: NdArrayCore.hpp:2950
nc::NdArray::contains
NdArray< bool > contains(value_type inValue, Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:2343
nc::Slice::stop
int32 stop
Definition: Slice.hpp:49
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
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:2971
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:4310
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