NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
roll.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <cmath>
31 
33 #include "NumCpp/Core/Shape.hpp"
34 #include "NumCpp/Core/Types.hpp"
35 #include "NumCpp/NdArray.hpp"
36 
37 namespace nc
38 {
39  //============================================================================
40  // Method Description:
51  template<typename dtype>
52  NdArray<dtype> roll(const NdArray<dtype>& inArray, int32 inShift, Axis inAxis = Axis::NONE)
53  {
54  switch (inAxis)
55  {
56  case Axis::NONE:
57  {
58  uint32 shift = std::abs(inShift) % inArray.size();
59  if (inShift > 0)
60  {
61  shift = inArray.size() - shift;
62  }
63 
64  NdArray<dtype> returnArray(inArray);
65  stl_algorithms::rotate(returnArray.begin(), returnArray.begin() + shift, returnArray.end());
66 
67  return returnArray;
68  }
69  case Axis::COL:
70  {
71  const Shape inShape = inArray.shape();
72 
73  uint32 shift = std::abs(inShift) % inShape.cols;
74  if (inShift > 0)
75  {
76  shift = inShape.cols - shift;
77  }
78 
79  NdArray<dtype> returnArray(inArray);
80  for (uint32 row = 0; row < inShape.rows; ++row)
81  {
82  stl_algorithms::rotate(returnArray.begin(row),
83  returnArray.begin(row) + shift,
84  returnArray.end(row));
85  }
86 
87  return returnArray;
88  }
89  case Axis::ROW:
90  {
91  const Shape inShape = inArray.shape();
92 
93  uint32 shift = std::abs(inShift) % inShape.rows;
94  if (inShift > 0)
95  {
96  shift = inShape.rows - shift;
97  }
98 
99  NdArray<dtype> returnArray = inArray.transpose();
100  for (uint32 row = 0; row < inShape.cols; ++row)
101  {
102  stl_algorithms::rotate(returnArray.begin(row),
103  returnArray.begin(row) + shift,
104  returnArray.end(row));
105  }
106 
107  return returnArray.transpose();
108  }
109  default:
110  {
111  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
112  return {}; // get rid of compiler warning
113  }
114  }
115  }
116 } // 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:4289
iterator end() noexcept
Definition: NdArrayCore.hpp:1479
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4276
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4650
iterator begin() noexcept
Definition: NdArrayCore.hpp:1171
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
void rotate(ForwardIt first, ForwardIt firstN, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:510
Definition: Coordinate.hpp:45
NdArray< dtype > roll(const NdArray< dtype > &inArray, int32 inShift, Axis inAxis=Axis::NONE)
Definition: roll.hpp:52
Axis
Enum To describe an axis.
Definition: Types.hpp:47
auto abs(dtype inValue) noexcept
Definition: abs.hpp:49
std::int32_t int32
Definition: Types.hpp:36
std::uint32_t uint32
Definition: Types.hpp:40