NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
diff.hpp
Go to the documentation of this file.
1 
28 #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:
52  template<typename dtype>
53  NdArray<dtype> diff(const NdArray<dtype>& inArray, Axis inAxis = Axis::NONE)
54  {
56 
57  const Shape inShape = inArray.shape();
58 
59  switch (inAxis)
60  {
61  case Axis::NONE:
62  {
63  if (inArray.size() < 2)
64  {
65  return NdArray<dtype>(0);
66  }
67 
68  NdArray<dtype> returnArray(1, inArray.size() - 1);
69  stl_algorithms::transform(inArray.cbegin(), inArray.cend() - 1, inArray.cbegin() + 1, returnArray.begin(),
70  [](dtype inValue1, dtype inValue2) noexcept -> dtype
71  {
72  return inValue2 - inValue1;
73  });
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), inArray.cend(row) - 1, inArray.cbegin(row) + 1, returnArray.begin(row),
88  [](dtype inValue1, dtype inValue2) noexcept -> dtype
89  {
90  return inValue2 - inValue1;
91  });
92  }
93 
94  return returnArray;
95  }
96  case Axis::ROW:
97  {
98  if (inShape.rows < 2)
99  {
100  return NdArray<dtype>(0);
101  }
102 
103  NdArray<dtype> transArray = inArray.transpose();
104  const Shape transShape = transArray.shape();
105  NdArray<dtype> returnArray(transShape.rows, transShape.cols - 1);
106  for (uint32 row = 0; row < transShape.rows; ++row)
107  {
108  stl_algorithms::transform(transArray.cbegin(row), transArray.cend(row) - 1, transArray.cbegin(row) + 1, returnArray.begin(row),
109  [](dtype inValue1, dtype inValue2) noexcept -> dtype
110  {
111  return inValue2 - inValue1;
112  });
113  }
114 
115  return returnArray.transpose();
116  }
117  default:
118  {
119  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
120  return {}; // get rid of compiler warning
121  }
122  }
123  }
124 } // 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:4497
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1270
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4483
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4841
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1614
iterator begin() noexcept
Definition: NdArrayCore.hpp:1214
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:53
std::uint32_t uint32
Definition: Types.hpp:40