NumCpp  2.12.1
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
30#include <string>
31
34#include "NumCpp/Core/Shape.hpp"
35#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/NdArray.hpp"
37
38namespace nc
39{
40 //============================================================================
41 // Method Description:
51 template<typename dtype>
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 {
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 return diff(inArray.transpose(), Axis::COL).transpose();
100 }
101 default:
102 {
103 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
104 return {}; // get rid of compiler warning
105 }
106 }
107 }
108} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC_OR_COMPLEX(dtype)
Definition: StaticAsserts.hpp:56
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:139
size_type size() const noexcept
Definition: NdArrayCore.hpp:4524
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1365
self_type transpose() const
Definition: NdArrayCore.hpp:4882
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4511
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1673
iterator begin() noexcept
Definition: NdArrayCore.hpp:1315
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:775
Definition: Cartesian.hpp:40
Axis
Enum To describe an axis.
Definition: Enums.hpp:36
NdArray< dtype > diff(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: diff.hpp:52
std::uint32_t uint32
Definition: Types.hpp:40