NumCpp  1.0
A C++ implementation of the Python Numpy library
diff.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/NdArray.hpp"
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Types.hpp"
36 
37 #include <string>
38 
39 namespace nc
40 {
41  //============================================================================
42  // Method Description:
53  template<typename dtype>
54  NdArray<dtype> diff(const NdArray<dtype>& inArray, Axis inAxis = Axis::NONE)
55  {
57 
58  const Shape inShape = inArray.shape();
59 
60  switch (inAxis)
61  {
62  case Axis::NONE:
63  {
64  if (inArray.size() < 2)
65  {
66  return NdArray<dtype>(0);
67  }
68 
69  NdArray<dtype> returnArray(1, inArray.size() - 1);
70  stl_algorithms::transform(inArray.cbegin(), inArray.cend() - 1, inArray.cbegin() + 1, returnArray.begin(),
71  [](dtype inValue1, dtype inValue2) noexcept -> dtype
72  {
73  return inValue2 - inValue1;
74  });
75 
76  return returnArray;
77  }
78  case Axis::COL:
79  {
80  if (inShape.cols < 2)
81  {
82  return NdArray<dtype>(0);
83  }
84 
85  NdArray<dtype> returnArray(inShape.rows, inShape.cols - 1);
86  for (uint32 row = 0; row < inShape.rows; ++row)
87  {
88  stl_algorithms::transform(inArray.cbegin(row), inArray.cend(row) - 1, inArray.cbegin(row) + 1, returnArray.begin(row),
89  [](dtype inValue1, dtype inValue2) noexcept -> dtype
90  {
91  return inValue2 - inValue1;
92  });
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), transArray.cend(row) - 1, transArray.cbegin(row) + 1, returnArray.begin(row),
110  [](dtype inValue1, dtype inValue2) noexcept -> dtype
111  {
112  return inValue2 - inValue1;
113  });
114  }
115 
116  return returnArray.transpose();
117  }
118  default:
119  {
120  // this isn't actually possible, just putting this here to get rid
121  // of the compiler warning.
122  return NdArray<dtype>(0);
123  }
124  }
125  }
126 }
StaticAsserts.hpp
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
nc::Axis::NONE
@ NONE
nc::Axis::ROW
@ ROW
STATIC_ASSERT_ARITHMETIC_OR_COMPLEX
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:51
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::diff
NdArray< dtype > diff(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: diff.hpp:54
nc::NdArray::size
size_type size() const noexcept
Definition: NdArrayCore.hpp:4310
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:46
nc::NdArray::cend
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1491
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
nc::stl_algorithms::transform
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction) noexcept
Definition: StlAlgorithms.hpp:703
nc::NdArray::cbegin
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1147
StlAlgorithms.hpp
Types.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1091
nc::Axis::COL
@ COL