NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
trapz.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:
50 template<typename dtype>
51 NdArray<double> trapz(const NdArray<dtype>& inArray, double dx = 1., Axis inAxis = Axis::NONE)
52 {
54
55 const Shape inShape = inArray.shape();
56 switch (inAxis)
57 {
58 case Axis::NONE:
59 {
60 double sum = 0.;
61 for (uint32 i = 0; i < inArray.size() - 1; ++i)
62 {
63 sum += static_cast<double>(inArray[i + 1] - inArray[i]) / 2. + static_cast<double>(inArray[i]);
64 }
65
66 NdArray<double> returnArray = { sum * dx };
67 return returnArray;
68 }
69 case Axis::COL:
70 {
71 NdArray<double> returnArray(inShape.rows, 1);
72 for (uint32 row = 0; row < inShape.rows; ++row)
73 {
74 double sum = 0;
75 for (uint32 col = 0; col < inShape.cols - 1; ++col)
76 {
77 sum += static_cast<double>(inArray(row, col + 1) - inArray(row, col)) / 2. +
78 static_cast<double>(inArray(row, col));
79 }
80
81 returnArray[row] = sum * dx;
82 }
83
84 return returnArray;
85 }
86 case Axis::ROW:
87 {
88 return trapz(inArray.transpose(), dx, Axis::COL);
89 }
90 default:
91 {
92 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
93 return {}; // get rid of compiler warning
94 }
95 }
96 }
97
98 //============================================================================
99 // Method Description:
110 template<typename dtype>
111 NdArray<double> trapz(const NdArray<dtype>& inArrayY, const NdArray<dtype>& inArrayX, Axis inAxis = Axis::NONE)
112 {
113 const Shape inShapeY = inArrayY.shape();
114 const Shape inShapeX = inArrayX.shape();
115
116 if (inShapeY != inShapeX)
117 {
118 THROW_INVALID_ARGUMENT_ERROR("input x and y arrays should be the same shape.");
119 }
120
121 switch (inAxis)
122 {
123 case Axis::NONE:
124 {
125 double sum = 0.;
126 for (uint32 i = 0; i < inArrayY.size() - 1; ++i)
127 {
128 const auto dx = static_cast<double>(inArrayX[i + 1] - inArrayX[i]);
129 sum += dx *
130 (static_cast<double>(inArrayY[i + 1] - inArrayY[i]) / 2. + static_cast<double>(inArrayY[i]));
131 }
132
133 NdArray<double> returnArray = { sum };
134 return returnArray;
135 }
136 case Axis::COL:
137 {
138 NdArray<double> returnArray(inShapeY.rows, 1);
139 for (uint32 row = 0; row < inShapeY.rows; ++row)
140 {
141 double sum = 0;
142 for (uint32 col = 0; col < inShapeY.cols - 1; ++col)
143 {
144 const auto dx = static_cast<double>(inArrayX(row, col + 1) - inArrayX(row, col));
145 sum += dx * (static_cast<double>(inArrayY(row, col + 1) - inArrayY(row, col)) / 2. +
146 static_cast<double>(inArrayY(row, col)));
147 }
148
149 returnArray[row] = sum;
150 }
151
152 return returnArray;
153 }
154 case Axis::ROW:
155 {
156 return trapz(inArrayY.transpose(), inArrayX.transpose(), Axis::COL);
157 }
158 default:
159 {
160 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
161 return {}; // get rid of compiler warning
162 }
163 }
164 }
165} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
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
self_type transpose() const
Definition NdArrayCore.hpp:4959
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
uint32 rows
Definition Core/shape.hpp:44
uint32 cols
Definition Core/shape.hpp:45
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 > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition sum.hpp:45
NdArray< double > trapz(const NdArray< dtype > &inArray, double dx=1., Axis inAxis=Axis::NONE)
Definition trapz.hpp:51
std::uint32_t uint32
Definition Types.hpp:40