NumCpp  2.7.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 
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Types.hpp"
34 #include "NumCpp/NdArray.hpp"
35 
36 #include <string>
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);
68  stl_algorithms::transform(inArray.cbegin(), inArray.cend() - 1, inArray.cbegin() + 1, returnArray.begin(),
69  [](dtype inValue1, dtype inValue2) noexcept -> dtype
70  {
71  return inValue2 - inValue1;
72  });
73 
74  return returnArray;
75  }
76  case Axis::COL:
77  {
78  if (inShape.cols < 2)
79  {
80  return NdArray<dtype>(0);
81  }
82 
83  NdArray<dtype> returnArray(inShape.rows, inShape.cols - 1);
84  for (uint32 row = 0; row < inShape.rows; ++row)
85  {
86  stl_algorithms::transform(inArray.cbegin(row), inArray.cend(row) - 1, inArray.cbegin(row) + 1, returnArray.begin(row),
87  [](dtype inValue1, dtype inValue2) noexcept -> dtype
88  {
89  return inValue2 - inValue1;
90  });
91  }
92 
93  return returnArray;
94  }
95  case Axis::ROW:
96  {
97  if (inShape.rows < 2)
98  {
99  return NdArray<dtype>(0);
100  }
101 
102  NdArray<dtype> transArray = inArray.transpose();
103  const Shape transShape = transArray.shape();
104  NdArray<dtype> returnArray(transShape.rows, transShape.cols - 1);
105  for (uint32 row = 0; row < transShape.rows; ++row)
106  {
107  stl_algorithms::transform(transArray.cbegin(row), transArray.cend(row) - 1, transArray.cbegin(row) + 1, returnArray.begin(row),
108  [](dtype inValue1, dtype inValue2) noexcept -> dtype
109  {
110  return inValue2 - inValue1;
111  });
112  }
113 
114  return returnArray.transpose();
115  }
116  default:
117  {
118  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
119  return {}; // get rid of compiler warning
120  }
121  }
122  }
123 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:50
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:4296
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1216
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4629
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1524
iterator begin() noexcept
Definition: NdArrayCore.hpp:1166
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:702
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:46
NdArray< dtype > diff(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: diff.hpp:52
std::uint32_t uint32
Definition: Types.hpp:40