NumCpp  1.0
A C++ implementation of the Python Numpy library
roll.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
35 
36 #include <cmath>
37 
38 namespace nc
39 {
40  //============================================================================
41  // Method Description:
53  template<typename dtype>
54  NdArray<dtype> roll(const NdArray<dtype>& inArray, int32 inShift, Axis inAxis = Axis::NONE)
55  {
56  switch (inAxis)
57  {
58  case Axis::NONE:
59  {
60  uint32 shift = std::abs(inShift) % inArray.size();
61  if (inShift > 0)
62  {
63  shift = inArray.size() - shift;
64  }
65 
66  NdArray<dtype> returnArray(inArray);
67  stl_algorithms::rotate(returnArray.begin(), returnArray.begin() + shift, returnArray.end());
68 
69  return returnArray;
70  }
71  case Axis::COL:
72  {
73  const Shape inShape = inArray.shape();
74 
75  uint32 shift = std::abs(inShift) % inShape.cols;
76  if (inShift > 0)
77  {
78  shift = inShape.cols - shift;
79  }
80 
81  NdArray<dtype> returnArray(inArray);
82  for (uint32 row = 0; row < inShape.rows; ++row)
83  {
84  stl_algorithms::rotate(returnArray.begin(row), returnArray.begin(row) + shift, 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), returnArray.begin(row) + shift, returnArray.end(row));
103  }
104 
105  return returnArray.transpose();
106  }
107  default:
108  {
109  // this isn't actually possible, just putting this here to get rid
110  // of the compiler warning.
111  return NdArray<dtype>(0);
112  }
113  }
114  }
115 }
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
nc::Axis::NONE
@ NONE
nc::int32
std::int32_t int32
Definition: Types.hpp:37
nc::Axis::ROW
@ ROW
nc::NdArray::transpose
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4591
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::roll
NdArray< dtype > roll(const NdArray< dtype > &inArray, int32 inShift, Axis inAxis=Axis::NONE)
Definition: roll.hpp:54
nc::NdArray::end
iterator end() noexcept
Definition: NdArrayCore.hpp:1435
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
Shape.hpp
nc
Definition: Coordinate.hpp:45
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
StlAlgorithms.hpp
nc::stl_algorithms::rotate
void rotate(ForwardIt first, ForwardIt firstN, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:472
Types.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1091
nc::Axis::COL
@ COL
nc::abs
auto abs(dtype inValue) noexcept
Definition: abs.hpp:52