NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
average.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <complex>
31
37#include "NumCpp/Core/Shape.hpp"
38#include "NumCpp/Core/Types.hpp"
40#include "NumCpp/NdArray.hpp"
41
42namespace nc
43{
44 //============================================================================
45 // Method Description:
54 template<typename dtype>
55 auto average(const NdArray<dtype>& inArray, Axis inAxis = Axis::NONE)
56 {
57 return mean(inArray, inAxis);
58 }
59
60 //============================================================================
61 // Method Description:
71 template<typename dtype>
72 NdArray<double> average(const NdArray<dtype>& inArray, const NdArray<dtype>& inWeights, Axis inAxis = Axis::NONE)
73 {
75
76 switch (inAxis)
77 {
78 case Axis::NONE:
79 {
80 if (inWeights.shape() != inArray.shape())
81 {
82 THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
83 }
84
85 NdArray<double> weightedArray(inArray.shape());
87 inArray.cend(),
88 inWeights.cbegin(),
89 weightedArray.begin(),
90 std::multiplies<double>()); // NOLINT(modernize-use-transparent-functors)
91
92 double sum = std::accumulate(weightedArray.begin(), weightedArray.end(), 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);
111 inArray.cend(row),
112 inWeights.cbegin(),
113 weightedArray.begin(),
114 std::multiplies<double>()); // NOLINT(modernize-use-transparent-functors)
115
116 double sum = std::accumulate(weightedArray.begin(), weightedArray.end(), 0.);
117 returnArray(0, row) = sum / weightSum;
118 }
119
120 return returnArray;
121 }
122 case Axis::ROW:
123 {
124 return average(inArray.transpose(), inWeights, Axis::COL);
125 }
126 default:
127 {
128 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
129 return {};
130 }
131 }
132 }
133
134 //============================================================================
135 // Method Description:
145 template<typename dtype>
146 NdArray<std::complex<double>>
147 average(const NdArray<std::complex<dtype>>& inArray, const NdArray<dtype>& inWeights, Axis inAxis = Axis::NONE)
148 {
150
151 const auto multiplies = [](const std::complex<dtype>& lhs, dtype rhs) -> std::complex<double>
152 { return complex_cast<double>(lhs) * static_cast<double>(rhs); };
153
154 switch (inAxis)
155 {
156 case Axis::NONE:
157 {
158 if (inWeights.shape() != inArray.shape())
159 {
160 THROW_INVALID_ARGUMENT_ERROR("input array and weight values are not consistant.");
161 }
162
163 NdArray<std::complex<double>> weightedArray(inArray.shape());
164 stl_algorithms::transform(inArray.cbegin(),
165 inArray.cend(),
166 inWeights.cbegin(),
167 weightedArray.begin(),
168 multiplies);
169
170 std::complex<double> sum =
171 std::accumulate(weightedArray.begin(), weightedArray.end(), std::complex<double>(0.));
172 NdArray<std::complex<double>> returnArray = { sum /= inWeights.template astype<double>().sum().item() };
173
174 return returnArray;
175 }
176 case Axis::COL:
177 {
178 const Shape arrayShape = inArray.shape();
179 if (inWeights.size() != arrayShape.cols)
180 {
181 THROW_INVALID_ARGUMENT_ERROR("input array and weights value are not consistant.");
182 }
183
184 double weightSum = inWeights.template astype<double>().sum().item();
185 NdArray<std::complex<double>> returnArray(1, arrayShape.rows);
186 for (uint32 row = 0; row < arrayShape.rows; ++row)
187 {
188 NdArray<std::complex<double>> weightedArray(1, arrayShape.cols);
189 stl_algorithms::transform(inArray.cbegin(row),
190 inArray.cend(row),
191 inWeights.cbegin(),
192 weightedArray.begin(),
193 multiplies);
194
195 const std::complex<double> sum =
196 std::accumulate(weightedArray.begin(), weightedArray.end(), std::complex<double>(0.));
197 returnArray(0, row) = sum / weightSum;
198 }
199
200 return returnArray;
201 }
202 case Axis::ROW:
203 {
204 return average(inArray.transpose(), inWeights, Axis::COL);
205 }
206 default:
207 {
208 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
209 return {}; // get rid of compiler warning
210 }
211 }
212 }
213} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition StaticAsserts.hpp:39
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
const_iterator cbegin() const noexcept
Definition NdArrayCore.hpp:1361
iterator end() noexcept
Definition NdArrayCore.hpp:1619
self_type transpose() const
Definition NdArrayCore.hpp:4959
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
const_iterator cend() const noexcept
Definition NdArrayCore.hpp:1669
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
value_type item() const
Definition NdArrayCore.hpp:3098
self_type sum(Axis inAxis=Axis::NONE) const
Definition NdArrayCore.hpp:4694
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:776
Definition Cartesian.hpp:39
NdArray< double > mean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition mean.hpp:52
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37
std::complex< Out > complex_cast(const std::complex< In > &value) noexcept
Definition StdComplexOperators.hpp:111
auto average(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition average.hpp:55
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition sum.hpp:45
NdArray< dtypeOut > astype(const NdArray< dtype > inArray)
Definition astype.hpp:43
std::uint32_t uint32
Definition Types.hpp:40