NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
trapz.hpp
Go to the documentation of this file.
1 #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<double> trapz(const NdArray<dtype>& inArray, double dx = 1.0, Axis inAxis = Axis::NONE)
54  {
56 
57  const Shape inShape = inArray.shape();
58  switch (inAxis)
59  {
60  case Axis::COL:
61  {
62  NdArray<double> returnArray(inShape.rows, 1);
63  for (uint32 row = 0; row < inShape.rows; ++row)
64  {
65  double sum = 0;
66  for (uint32 col = 0; col < inShape.cols - 1; ++col)
67  {
68  sum += static_cast<double>(inArray(row, col + 1) - inArray(row, col)) / 2.0 +
69  static_cast<double>(inArray(row, col));
70  }
71 
72  returnArray[row] = sum * dx;
73  }
74 
75  return returnArray;
76  }
77  case Axis::ROW:
78  {
79  NdArray<dtype> arrayTranspose = inArray.transpose();
80  const Shape transShape = arrayTranspose.shape();
81  NdArray<double> returnArray(transShape.rows, 1);
82  for (uint32 row = 0; row < transShape.rows; ++row)
83  {
84  double sum = 0;
85  for (uint32 col = 0; col < transShape.cols - 1; ++col)
86  {
87  sum += static_cast<double>(arrayTranspose(row, col + 1) - arrayTranspose(row, col)) / 2.0 +
88  static_cast<double>(arrayTranspose(row, col));
89  }
90 
91  returnArray[row] = sum * dx;
92  }
93 
94  return returnArray;
95  }
96  case Axis::NONE:
97  {
98  double sum = 0.0;
99  for (uint32 i = 0; i < inArray.size() - 1; ++i)
100  {
101  sum += static_cast<double>(inArray[i + 1] - inArray[i]) / 2.0 + static_cast<double>(inArray[i]);
102  }
103 
104  NdArray<double> returnArray = { sum * dx };
105  return returnArray;
106  }
107  default:
108  {
109  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
110  return {}; // get rid of compiler warning
111  }
112  }
113  }
114 
115  //============================================================================
116  // Method Description:
127  template<typename dtype>
128  NdArray<double> trapz(const NdArray<dtype>& inArrayY, const NdArray<dtype>& inArrayX, Axis inAxis = Axis::NONE)
129  {
130  const Shape inShapeY = inArrayY.shape();
131  const Shape inShapeX = inArrayX.shape();
132 
133  if (inShapeY != inShapeX)
134  {
135  THROW_INVALID_ARGUMENT_ERROR("input x and y arrays should be the same shape.");
136  }
137 
138  switch (inAxis)
139  {
140  case Axis::COL:
141  {
142  NdArray<double> returnArray(inShapeY.rows, 1);
143  for (uint32 row = 0; row < inShapeY.rows; ++row)
144  {
145  double sum = 0;
146  for (uint32 col = 0; col < inShapeY.cols - 1; ++col)
147  {
148  const auto dx = static_cast<double>(inArrayX(row, col + 1) - inArrayX(row, col));
149  sum += dx * (static_cast<double>(inArrayY(row, col + 1) - inArrayY(row, col)) / 2.0 +
150  static_cast<double>(inArrayY(row, col)));
151  }
152 
153  returnArray[row] = sum;
154  }
155 
156  return returnArray;
157  }
158  case Axis::ROW:
159  {
160  NdArray<dtype> arrayYTranspose = inArrayY.transpose();
161  NdArray<dtype> arrayXTranspose = inArrayX.transpose();
162  const Shape transShape = arrayYTranspose.shape();
163  NdArray<double> returnArray(transShape.rows, 1);
164  for (uint32 row = 0; row < transShape.rows; ++row)
165  {
166  double sum = 0;
167  for (uint32 col = 0; col < transShape.cols - 1; ++col)
168  {
169  const auto dx = static_cast<double>(arrayXTranspose(row, col + 1) - arrayXTranspose(row, col));
170  sum += dx * (static_cast<double>(arrayYTranspose(row, col + 1) - arrayYTranspose(row, col)) / 2.0 +
171  static_cast<double>(arrayYTranspose(row, col)));
172  }
173 
174  returnArray[row] = sum;
175  }
176 
177  return returnArray;
178  }
179  case Axis::NONE:
180  {
181  double sum = 0.0;
182  for (uint32 i = 0; i < inArrayY.size() - 1; ++i)
183  {
184  const auto dx = static_cast<double>(inArrayX[i + 1] - inArrayX[i]);
185  sum += dx * (static_cast<double>(inArrayY[i + 1] - inArrayY[i]) / 2.0 + static_cast<double>(inArrayY[i]));
186  }
187 
188  NdArray<double> returnArray = { sum };
189  return returnArray;
190  }
191  default:
192  {
193  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
194  return {}; // get rid of compiler warning
195  }
196  }
197  }
198 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
size_type size() const noexcept
Definition: NdArrayCore.hpp:4296
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4629
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: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:46
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sum.hpp:46
NdArray< double > trapz(const NdArray< dtype > &inArray, double dx=1.0, Axis inAxis=Axis::NONE)
Definition: trapz.hpp:53
std::uint32_t uint32
Definition: Types.hpp:40