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