NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
average.hpp
Go to the documentation of this file.
1 #pragma once
29 
35 #include "NumCpp/Core/Shape.hpp"
36 #include "NumCpp/Core/Types.hpp"
38 #include "NumCpp/NdArray.hpp"
39 
40 #include <complex>
41 #include <string>
42 
43 namespace nc
44 {
45  //============================================================================
46  // Method Description:
55  template<typename dtype>
56  auto average(const NdArray<dtype>& inArray, Axis inAxis = Axis::NONE)
57  {
58  return mean(inArray, inAxis);
59  }
60 
61  //============================================================================
62  // Method Description:
72  template<typename dtype>
73  NdArray<double> average(const NdArray<dtype>& inArray, const NdArray<dtype>& inWeights, Axis inAxis = Axis::NONE)
74  {
76 
77  switch (inAxis)
78  {
79  case Axis::NONE:
80  {
81  if (inWeights.shape() != inArray.shape())
82  {
83  THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
84  }
85 
86  NdArray<double> weightedArray(inArray.shape());
87  stl_algorithms::transform(inArray.cbegin(), inArray.cend(), inWeights.cbegin(),
88  weightedArray.begin(), std::multiplies<double>());
89 
90  double sum = std::accumulate(weightedArray.begin(), weightedArray.end(), 0.0);
91  NdArray<double> returnArray = { sum /= inWeights.template astype<double>().sum().item() };
92 
93  return returnArray;
94  }
95  case Axis::COL:
96  {
97  const Shape arrayShape = inArray.shape();
98  if (inWeights.size() != arrayShape.cols)
99  {
100  THROW_INVALID_ARGUMENT_ERROR("input array and weights value are not consistant.");
101  }
102 
103  double weightSum = inWeights.template astype<double>().sum().item();
104  NdArray<double> returnArray(1, arrayShape.rows);
105  for (uint32 row = 0; row < arrayShape.rows; ++row)
106  {
107  NdArray<double> weightedArray(1, arrayShape.cols);
108  stl_algorithms::transform(inArray.cbegin(row), inArray.cend(row), inWeights.cbegin(),
109  weightedArray.begin(), std::multiplies<double>());
110 
111  double sum = std::accumulate(weightedArray.begin(), weightedArray.end(), 0.0);
112  returnArray(0, row) = sum / weightSum;
113  }
114 
115  return returnArray;
116  }
117  case Axis::ROW:
118  {
119  if (inWeights.size() != inArray.shape().rows)
120  {
121  THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
122  }
123 
124  NdArray<dtype> transposedArray = inArray.transpose();
125 
126  const Shape transShape = transposedArray.shape();
127  double weightSum = inWeights.template astype<double>().sum().item();
128  NdArray<double> returnArray(1, transShape.rows);
129  for (uint32 row = 0; row < transShape.rows; ++row)
130  {
131  NdArray<double> weightedArray(1, transShape.cols);
132  stl_algorithms::transform(transposedArray.cbegin(row), transposedArray.cend(row), inWeights.cbegin(),
133  weightedArray.begin(), std::multiplies<double>());
134 
135  double sum = std::accumulate(weightedArray.begin(), weightedArray.end(), 0.0);
136  returnArray(0, row) = sum / weightSum;
137  }
138 
139  return returnArray;
140  }
141  default:
142  {
143  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
144  return {};
145  }
146  }
147  }
148 
149  //============================================================================
150  // Method Description:
160  template<typename dtype>
161  NdArray<std::complex<double>> average(const NdArray<std::complex<dtype>>& inArray,
162  const NdArray<dtype>& inWeights, Axis inAxis = Axis::NONE)
163  {
165 
166  const auto multiplies = [](const std::complex<dtype>& lhs, dtype rhs) -> std::complex<double>
167  {
168  return complex_cast<double>(lhs) * static_cast<double>(rhs);
169  };
170 
171  switch (inAxis)
172  {
173  case Axis::NONE:
174  {
175  if (inWeights.shape() != inArray.shape())
176  {
177  THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
178  }
179 
180  NdArray<std::complex<double>> weightedArray(inArray.shape());
181  stl_algorithms::transform(inArray.cbegin(), inArray.cend(), inWeights.cbegin(),
182  weightedArray.begin(), multiplies);
183 
184  std::complex<double> sum = std::accumulate(weightedArray.begin(), weightedArray.end(), std::complex<double>(0.0));
185  NdArray<std::complex<double>> returnArray = { sum /= inWeights.template astype<double>().sum().item() };
186 
187  return returnArray;
188  }
189  case Axis::COL:
190  {
191  const Shape arrayShape = inArray.shape();
192  if (inWeights.size() != arrayShape.cols)
193  {
194  THROW_INVALID_ARGUMENT_ERROR("input array and weights value are not consistant.");
195  }
196 
197  double weightSum = inWeights.template astype<double>().sum().item();
198  NdArray<std::complex<double>> returnArray(1, arrayShape.rows);
199  for (uint32 row = 0; row < arrayShape.rows; ++row)
200  {
201  NdArray<std::complex<double>> weightedArray(1, arrayShape.cols);
202  stl_algorithms::transform(inArray.cbegin(row), inArray.cend(row), inWeights.cbegin(),
203  weightedArray.begin(), multiplies);
204 
205  const std::complex<double> sum = std::accumulate(weightedArray.begin(), weightedArray.end(),
206  std::complex<double>(0.0));
207  returnArray(0, row) = sum / weightSum;
208  }
209 
210  return returnArray;
211  }
212  case Axis::ROW:
213  {
214  if (inWeights.size() != inArray.shape().rows)
215  {
216  THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
217  }
218 
219  NdArray<std::complex<dtype>> transposedArray = inArray.transpose();
220 
221  const Shape transShape = transposedArray.shape();
222  double weightSum = inWeights.template astype<double>().sum().item();
223  NdArray<std::complex<double>> returnArray(1, transShape.rows);
224  for (uint32 row = 0; row < transShape.rows; ++row)
225  {
226  NdArray<std::complex<double>> weightedArray(1, transShape.cols);
227  stl_algorithms::transform(transposedArray.cbegin(row), transposedArray.cend(row), inWeights.cbegin(),
228  weightedArray.begin(), multiplies);
229 
230  const std::complex<double> sum = std::accumulate(weightedArray.begin(), weightedArray.end(),
231  std::complex<double>(0.0));
232  returnArray(0, row) = sum / weightSum;
233  }
234 
235  return returnArray;
236  }
237  default:
238  {
239  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
240  return {}; // get rid of compiler warning
241  }
242  }
243  }
244 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
size_type size() const noexcept
Definition: NdArrayCore.hpp:4296
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1216
iterator end() noexcept
Definition: NdArrayCore.hpp:1474
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4283
NdArray< dtype > sum(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:4392
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4629
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1524
iterator begin() noexcept
Definition: NdArrayCore.hpp:1166
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:702
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:46
auto average(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: average.hpp:56
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sum.hpp:46
NdArray< double > mean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: mean.hpp:52
std::uint32_t uint32
Definition: Types.hpp:40