NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
diff.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <string>
31 
34 #include "NumCpp/Core/Shape.hpp"
35 #include "NumCpp/Core/Types.hpp"
36 #include "NumCpp/NdArray.hpp"
37 
38 namespace nc
39 {
40  //============================================================================
41  // Method Description:
51  template<typename dtype>
52  NdArray<dtype> diff(const NdArray<dtype>& inArray, Axis inAxis = Axis::NONE)
53  {
55 
56  const Shape inShape = inArray.shape();
57 
58  switch (inAxis)
59  {
60  case Axis::NONE:
61  {
62  if (inArray.size() < 2)
63  {
64  return NdArray<dtype>(0);
65  }
66 
67  NdArray<dtype> returnArray(1, inArray.size() - 1);
69  inArray.cend() - 1,
70  inArray.cbegin() + 1,
71  returnArray.begin(),
72  [](dtype inValue1, dtype inValue2) noexcept -> dtype
73  { return inValue2 - inValue1; });
74 
75  return returnArray;
76  }
77  case Axis::COL:
78  {
79  if (inShape.cols < 2)
80  {
81  return NdArray<dtype>(0);
82  }
83 
84  NdArray<dtype> returnArray(inShape.rows, inShape.cols - 1);
85  for (uint32 row = 0; row < inShape.rows; ++row)
86  {
87  stl_algorithms::transform(inArray.cbegin(row),
88  inArray.cend(row) - 1,
89  inArray.cbegin(row) + 1,
90  returnArray.begin(row),
91  [](dtype inValue1, dtype inValue2) noexcept -> dtype
92  { return inValue2 - inValue1; });
93  }
94 
95  return returnArray;
96  }
97  case Axis::ROW:
98  {
99  if (inShape.rows < 2)
100  {
101  return NdArray<dtype>(0);
102  }
103 
104  NdArray<dtype> transArray = inArray.transpose();
105  const Shape transShape = transArray.shape();
106  NdArray<dtype> returnArray(transShape.rows, transShape.cols - 1);
107  for (uint32 row = 0; row < transShape.rows; ++row)
108  {
109  stl_algorithms::transform(transArray.cbegin(row),
110  transArray.cend(row) - 1,
111  transArray.cbegin(row) + 1,
112  returnArray.begin(row),
113  [](dtype inValue1, dtype inValue2) noexcept -> dtype
114  { return inValue2 - inValue1; });
115  }
116 
117  return returnArray.transpose();
118  }
119  default:
120  {
121  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
122  return {}; // get rid of compiler warning
123  }
124  }
125  }
126 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:49
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
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1221
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4276
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4650
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1529
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
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:784
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:47
NdArray< dtype > diff(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: diff.hpp:52
std::uint32_t uint32
Definition: Types.hpp:40