NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
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
36namespace nc
37{
38 //============================================================================
39 // Method Description:
49 template<typename dtype>
51 {
53
54 const Shape inShape = inArray.shape();
55
56 switch (inAxis)
57 {
58 case Axis::NONE:
59 {
60 if (inArray.size() < 2)
61 {
62 return NdArray<dtype>(0);
63 }
64
65 NdArray<dtype> returnArray(1, inArray.size() - 1);
67 inArray.cend() - 1,
68 inArray.cbegin() + 1,
69 returnArray.begin(),
70 [](dtype inValue1, dtype inValue2) noexcept -> dtype
71 { return inValue2 - inValue1; });
72
73 return returnArray;
74 }
75 case Axis::COL:
76 {
77 if (inShape.cols < 2)
78 {
79 return NdArray<dtype>(0);
80 }
81
82 NdArray<dtype> returnArray(inShape.rows, inShape.cols - 1);
83 for (uint32 row = 0; row < inShape.rows; ++row)
84 {
86 inArray.cend(row) - 1,
87 inArray.cbegin(row) + 1,
88 returnArray.begin(row),
89 [](dtype inValue1, dtype inValue2) noexcept -> dtype
90 { return inValue2 - inValue1; });
91 }
92
93 return returnArray;
94 }
95 case Axis::ROW:
96 {
97 return diff(inArray.transpose(), Axis::COL).transpose();
98 }
99 default:
100 {
101 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
102 return {}; // get rid of compiler warning
103 }
104 }
105 }
106} // 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:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
const_iterator cbegin() const noexcept
Definition NdArrayCore.hpp:1361
self_type transpose() const
Definition NdArrayCore.hpp:4959
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
const_iterator cend() const noexcept
Definition NdArrayCore.hpp:1669
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
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:776
Definition Cartesian.hpp:39
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37
NdArray< dtype > diff(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition diff.hpp:50
std::uint32_t uint32
Definition Types.hpp:40