NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
average.hpp
Go to the documentation of this file.
1 
28 #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:
56  template<typename dtype>
57  auto average(const NdArray<dtype>& inArray, Axis inAxis = Axis::NONE)
58  {
59  return mean(inArray, inAxis);
60  }
61 
62  //============================================================================
63  // Method Description:
74  template<typename dtype>
75  NdArray<double> average(const NdArray<dtype>& inArray, const NdArray<dtype>& inWeights, Axis inAxis = Axis::NONE)
76  {
78 
79  switch (inAxis)
80  {
81  case Axis::NONE:
82  {
83  if (inWeights.shape() != inArray.shape())
84  {
85  THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
86  }
87 
88  NdArray<double> weightedArray(inArray.shape());
89  stl_algorithms::transform(inArray.cbegin(), inArray.cend(), inWeights.cbegin(),
90  weightedArray.begin(), std::multiplies<double>());
91 
92  double sum = std::accumulate(weightedArray.begin(), weightedArray.end(), 0.0);
93  NdArray<double> returnArray = { sum /= inWeights.template astype<double>().sum().item() };
94 
95  return returnArray;
96  }
97  case Axis::COL:
98  {
99  const Shape arrayShape = inArray.shape();
100  if (inWeights.size() != arrayShape.cols)
101  {
102  THROW_INVALID_ARGUMENT_ERROR("input array and weights value are not consistant.");
103  }
104 
105  double weightSum = inWeights.template astype<double>().sum().item();
106  NdArray<double> returnArray(1, arrayShape.rows);
107  for (uint32 row = 0; row < arrayShape.rows; ++row)
108  {
109  NdArray<double> weightedArray(1, arrayShape.cols);
110  stl_algorithms::transform(inArray.cbegin(row), inArray.cend(row), inWeights.cbegin(),
111  weightedArray.begin(), std::multiplies<double>());
112 
113  double sum = std::accumulate(weightedArray.begin(), weightedArray.end(), 0.0);
114  returnArray(0, row) = sum / weightSum;
115  }
116 
117  return returnArray;
118  }
119  case Axis::ROW:
120  {
121  if (inWeights.size() != inArray.shape().rows)
122  {
123  THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
124  }
125 
126  NdArray<dtype> transposedArray = inArray.transpose();
127 
128  const Shape transShape = transposedArray.shape();
129  double weightSum = inWeights.template astype<double>().sum().item();
130  NdArray<double> returnArray(1, transShape.rows);
131  for (uint32 row = 0; row < transShape.rows; ++row)
132  {
133  NdArray<double> weightedArray(1, transShape.cols);
134  stl_algorithms::transform(transposedArray.cbegin(row), transposedArray.cend(row), inWeights.cbegin(),
135  weightedArray.begin(), std::multiplies<double>());
136 
137  double sum = std::accumulate(weightedArray.begin(), weightedArray.end(), 0.0);
138  returnArray(0, row) = sum / weightSum;
139  }
140 
141  return returnArray;
142  }
143  default:
144  {
145  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
146  return {};
147  }
148  }
149  }
150 
151  //============================================================================
152  // Method Description:
163  template<typename dtype>
164  NdArray<std::complex<double>> average(const NdArray<std::complex<dtype>>& inArray,
165  const NdArray<dtype>& inWeights, Axis inAxis = Axis::NONE)
166  {
168 
169  const auto multiplies = [](const std::complex<dtype>& lhs, dtype rhs) -> std::complex<double>
170  {
171  return complex_cast<double>(lhs) * static_cast<double>(rhs);
172  };
173 
174  switch (inAxis)
175  {
176  case Axis::NONE:
177  {
178  if (inWeights.shape() != inArray.shape())
179  {
180  THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
181  }
182 
183  NdArray<std::complex<double>> weightedArray(inArray.shape());
184  stl_algorithms::transform(inArray.cbegin(), inArray.cend(), inWeights.cbegin(),
185  weightedArray.begin(), multiplies);
186 
187  std::complex<double> sum = std::accumulate(weightedArray.begin(), weightedArray.end(), std::complex<double>(0.0));
188  NdArray<std::complex<double>> returnArray = { sum /= inWeights.template astype<double>().sum().item() };
189 
190  return returnArray;
191  }
192  case Axis::COL:
193  {
194  const Shape arrayShape = inArray.shape();
195  if (inWeights.size() != arrayShape.cols)
196  {
197  THROW_INVALID_ARGUMENT_ERROR("input array and weights value are not consistant.");
198  }
199 
200  double weightSum = inWeights.template astype<double>().sum().item();
201  NdArray<std::complex<double>> returnArray(1, arrayShape.rows);
202  for (uint32 row = 0; row < arrayShape.rows; ++row)
203  {
204  NdArray<std::complex<double>> weightedArray(1, arrayShape.cols);
205  stl_algorithms::transform(inArray.cbegin(row), inArray.cend(row), inWeights.cbegin(),
206  weightedArray.begin(), multiplies);
207 
208  const std::complex<double> sum = std::accumulate(weightedArray.begin(), weightedArray.end(),
209  std::complex<double>(0.0));
210  returnArray(0, row) = sum / weightSum;
211  }
212 
213  return returnArray;
214  }
215  case Axis::ROW:
216  {
217  if (inWeights.size() != inArray.shape().rows)
218  {
219  THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
220  }
221 
222  NdArray<std::complex<dtype>> transposedArray = inArray.transpose();
223 
224  const Shape transShape = transposedArray.shape();
225  double weightSum = inWeights.template astype<double>().sum().item();
226  NdArray<std::complex<double>> returnArray(1, transShape.rows);
227  for (uint32 row = 0; row < transShape.rows; ++row)
228  {
229  NdArray<std::complex<double>> weightedArray(1, transShape.cols);
230  stl_algorithms::transform(transposedArray.cbegin(row), transposedArray.cend(row), inWeights.cbegin(),
231  weightedArray.begin(), multiplies);
232 
233  const std::complex<double> sum = std::accumulate(weightedArray.begin(), weightedArray.end(),
234  std::complex<double>(0.0));
235  returnArray(0, row) = sum / weightSum;
236  }
237 
238  return returnArray;
239  }
240  default:
241  {
242  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
243  return {}; // get rid of compiler warning
244  }
245  }
246  }
247 } // 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:4497
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1270
iterator end() noexcept
Definition: NdArrayCore.hpp:1558
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4483
NdArray< dtype > sum(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:4598
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4841
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1614
iterator begin() noexcept
Definition: NdArrayCore.hpp:1214
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:57
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sum.hpp:47
NdArray< double > mean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: mean.hpp:53
std::uint32_t uint32
Definition: Types.hpp:40