NumCpp  2.4.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 
31 #include "NumCpp/Core/Shape.hpp"
32 #include "NumCpp/Core/Types.hpp"
33 #include "NumCpp/NdArray.hpp"
34 
35 #include <cmath>
36 
37 namespace nc
38 {
39  //============================================================================
40  // Method Description:
52  template<typename dtype>
53  NdArray<dtype> roll(const NdArray<dtype>& inArray, int32 inShift, Axis inAxis = Axis::NONE)
54  {
55  switch (inAxis)
56  {
57  case Axis::NONE:
58  {
59  uint32 shift = std::abs(inShift) % inArray.size();
60  if (inShift > 0)
61  {
62  shift = inArray.size() - shift;
63  }
64 
65  NdArray<dtype> returnArray(inArray);
66  stl_algorithms::rotate(returnArray.begin(), returnArray.begin() + shift, returnArray.end());
67 
68  return returnArray;
69  }
70  case Axis::COL:
71  {
72  const Shape inShape = inArray.shape();
73 
74  uint32 shift = std::abs(inShift) % inShape.cols;
75  if (inShift > 0)
76  {
77  shift = inShape.cols - shift;
78  }
79 
80  NdArray<dtype> returnArray(inArray);
81  for (uint32 row = 0; row < inShape.rows; ++row)
82  {
83  stl_algorithms::rotate(returnArray.begin(row), returnArray.begin(row) + shift, returnArray.end(row));
84  }
85 
86  return returnArray;
87  }
88  case Axis::ROW:
89  {
90  const Shape inShape = inArray.shape();
91 
92  uint32 shift = std::abs(inShift) % inShape.rows;
93  if (inShift > 0)
94  {
95  shift = inShape.rows - shift;
96  }
97 
98  NdArray<dtype> returnArray = inArray.transpose();
99  for (uint32 row = 0; row < inShape.cols; ++row)
100  {
101  stl_algorithms::rotate(returnArray.begin(row), returnArray.begin(row) + shift, returnArray.end(row));
102  }
103 
104  return returnArray.transpose();
105  }
106  default:
107  {
108  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
109  return {}; // get rid of compiler warning
110  }
111  }
112  }
113 } // namespace nc
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4356
nc::Axis::NONE
@ NONE
nc::int32
std::int32_t int32
Definition: Types.hpp:36
nc::Axis::ROW
@ ROW
nc::NdArray::transpose
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4652
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::roll
NdArray< dtype > roll(const NdArray< dtype > &inArray, int32 inShift, Axis inAxis=Axis::NONE)
Definition: roll.hpp:53
nc::NdArray::end
iterator end() noexcept
Definition: NdArrayCore.hpp:1431
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
Shape.hpp
nc
Definition: Coordinate.hpp:44
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:44
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
StlAlgorithms.hpp
nc::stl_algorithms::rotate
void rotate(ForwardIt first, ForwardIt firstN, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:471
Types.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1087
nc::Axis::COL
@ COL
nc::abs
auto abs(dtype inValue) noexcept
Definition: abs.hpp:51