NumCpp  2.8.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 
30 #include <complex>
31 #include <string>
32 
38 #include "NumCpp/Core/Shape.hpp"
39 #include "NumCpp/Core/Types.hpp"
41 #include "NumCpp/NdArray.hpp"
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());
88  inArray.cend(),
89  inWeights.cbegin(),
90  weightedArray.begin(),
91  std::multiplies<double>());
92 
93  double sum = std::accumulate(weightedArray.begin(), weightedArray.end(), 0.0);
94  NdArray<double> returnArray = { sum /= inWeights.template astype<double>().sum().item() };
95 
96  return returnArray;
97  }
98  case Axis::COL:
99  {
100  const Shape arrayShape = inArray.shape();
101  if (inWeights.size() != arrayShape.cols)
102  {
103  THROW_INVALID_ARGUMENT_ERROR("input array and weights value are not consistant.");
104  }
105 
106  double weightSum = inWeights.template astype<double>().sum().item();
107  NdArray<double> returnArray(1, arrayShape.rows);
108  for (uint32 row = 0; row < arrayShape.rows; ++row)
109  {
110  NdArray<double> weightedArray(1, arrayShape.cols);
111  stl_algorithms::transform(inArray.cbegin(row),
112  inArray.cend(row),
113  inWeights.cbegin(),
114  weightedArray.begin(),
115  std::multiplies<double>());
116 
117  double sum = std::accumulate(weightedArray.begin(), weightedArray.end(), 0.0);
118  returnArray(0, row) = sum / weightSum;
119  }
120 
121  return returnArray;
122  }
123  case Axis::ROW:
124  {
125  if (inWeights.size() != inArray.shape().rows)
126  {
127  THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
128  }
129 
130  NdArray<dtype> transposedArray = inArray.transpose();
131 
132  const Shape transShape = transposedArray.shape();
133  double weightSum = inWeights.template astype<double>().sum().item();
134  NdArray<double> returnArray(1, transShape.rows);
135  for (uint32 row = 0; row < transShape.rows; ++row)
136  {
137  NdArray<double> weightedArray(1, transShape.cols);
138  stl_algorithms::transform(transposedArray.cbegin(row),
139  transposedArray.cend(row),
140  inWeights.cbegin(),
141  weightedArray.begin(),
142  std::multiplies<double>());
143 
144  double sum = std::accumulate(weightedArray.begin(), weightedArray.end(), 0.0);
145  returnArray(0, row) = sum / weightSum;
146  }
147 
148  return returnArray;
149  }
150  default:
151  {
152  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
153  return {};
154  }
155  }
156  }
157 
158  //============================================================================
159  // Method Description:
169  template<typename dtype>
170  NdArray<std::complex<double>>
171  average(const NdArray<std::complex<dtype>>& inArray, const NdArray<dtype>& inWeights, Axis inAxis = Axis::NONE)
172  {
174 
175  const auto multiplies = [](const std::complex<dtype>& lhs, dtype rhs) -> std::complex<double>
176  { return complex_cast<double>(lhs) * static_cast<double>(rhs); };
177 
178  switch (inAxis)
179  {
180  case Axis::NONE:
181  {
182  if (inWeights.shape() != inArray.shape())
183  {
184  THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
185  }
186 
187  NdArray<std::complex<double>> weightedArray(inArray.shape());
188  stl_algorithms::transform(inArray.cbegin(),
189  inArray.cend(),
190  inWeights.cbegin(),
191  weightedArray.begin(),
192  multiplies);
193 
194  std::complex<double> sum =
195  std::accumulate(weightedArray.begin(), weightedArray.end(), std::complex<double>(0.0));
196  NdArray<std::complex<double>> returnArray = { sum /= inWeights.template astype<double>().sum().item() };
197 
198  return returnArray;
199  }
200  case Axis::COL:
201  {
202  const Shape arrayShape = inArray.shape();
203  if (inWeights.size() != arrayShape.cols)
204  {
205  THROW_INVALID_ARGUMENT_ERROR("input array and weights value are not consistant.");
206  }
207 
208  double weightSum = inWeights.template astype<double>().sum().item();
209  NdArray<std::complex<double>> returnArray(1, arrayShape.rows);
210  for (uint32 row = 0; row < arrayShape.rows; ++row)
211  {
212  NdArray<std::complex<double>> weightedArray(1, arrayShape.cols);
213  stl_algorithms::transform(inArray.cbegin(row),
214  inArray.cend(row),
215  inWeights.cbegin(),
216  weightedArray.begin(),
217  multiplies);
218 
219  const std::complex<double> sum =
220  std::accumulate(weightedArray.begin(), weightedArray.end(), std::complex<double>(0.0));
221  returnArray(0, row) = sum / weightSum;
222  }
223 
224  return returnArray;
225  }
226  case Axis::ROW:
227  {
228  if (inWeights.size() != inArray.shape().rows)
229  {
230  THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
231  }
232 
233  NdArray<std::complex<dtype>> transposedArray = inArray.transpose();
234 
235  const Shape transShape = transposedArray.shape();
236  double weightSum = inWeights.template astype<double>().sum().item();
237  NdArray<std::complex<double>> returnArray(1, transShape.rows);
238  for (uint32 row = 0; row < transShape.rows; ++row)
239  {
240  NdArray<std::complex<double>> weightedArray(1, transShape.cols);
241  stl_algorithms::transform(transposedArray.cbegin(row),
242  transposedArray.cend(row),
243  inWeights.cbegin(),
244  weightedArray.begin(),
245  multiplies);
246 
247  const std::complex<double> sum =
248  std::accumulate(weightedArray.begin(), weightedArray.end(), std::complex<double>(0.0));
249  returnArray(0, row) = sum / weightSum;
250  }
251 
252  return returnArray;
253  }
254  default:
255  {
256  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
257  return {}; // get rid of compiler warning
258  }
259  }
260  }
261 } // 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:4289
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1221
iterator end() noexcept
Definition: NdArrayCore.hpp:1479
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4276
NdArray< dtype > sum(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:4382
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4650
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1529
iterator begin() noexcept
Definition: NdArrayCore.hpp:1171
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:784
Definition: Coordinate.hpp:45
Axis
Enum To describe an axis.
Definition: Types.hpp:47
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