NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
mean.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <complex>
31#include <numeric>
32
34#include "NumCpp/Core/Shape.hpp"
35#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/NdArray.hpp"
37
38namespace nc
39{
40 //===========================================================================
41 // Method Description:
51 template<typename dtype>
53 {
55
56 switch (inAxis)
57 {
58 case Axis::NONE:
59 {
60 auto sum = std::accumulate(inArray.cbegin(), inArray.cend(), 0.);
61 NdArray<double> returnArray = { sum /= static_cast<double>(inArray.size()) };
62
63 return returnArray;
64 }
65 case Axis::COL:
66 {
67 NdArray<double> returnArray(1, inArray.numRows());
68 for (uint32 row = 0; row < inArray.numRows(); ++row)
69 {
70 auto sum = std::accumulate(inArray.cbegin(row), inArray.cend(row), 0.);
71 returnArray(0, row) = sum / static_cast<double>(inArray.numCols());
72 }
73
74 return returnArray;
75 }
76 case Axis::ROW:
77 {
78 return mean(inArray.transpose(), Axis::COL);
79 }
80 default:
81 {
82 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
83 return {};
84 }
85 }
86 }
87
88 //============================================================================
89 // Method Description:
99 template<typename dtype>
100 NdArray<std::complex<double>> mean(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::NONE)
101 {
103
104 switch (inAxis)
105 {
106 case Axis::NONE:
107 {
108 auto sum = std::accumulate(inArray.cbegin(), inArray.cend(), std::complex<double>(0.));
109 NdArray<std::complex<double>> returnArray = { sum /= std::complex<double>(inArray.size()) };
110
111 return returnArray;
112 }
113 case Axis::COL:
114 {
115 NdArray<std::complex<double>> returnArray(1, inArray.numRows());
116 for (uint32 row = 0; row < inArray.numRows(); ++row)
117 {
118 auto sum = std::accumulate(inArray.cbegin(row), inArray.cend(row), std::complex<double>(0.));
119 returnArray(0, row) = sum / std::complex<double>(inArray.numCols());
120 }
121
122 return returnArray;
123 }
124 case Axis::ROW:
125 {
126 NdArray<std::complex<double>> transposedArray = inArray.transpose();
127 NdArray<std::complex<double>> returnArray(1, transposedArray.numRows());
128 for (uint32 row = 0; row < transposedArray.numRows(); ++row)
129 {
130 auto sum = std::accumulate(transposedArray.cbegin(row),
131 transposedArray.cend(row),
132 std::complex<double>(0.));
133 returnArray(0, row) = sum / std::complex<double>(transposedArray.numCols());
134 }
135
136 return returnArray;
137 }
138 default:
139 {
140 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
141 return {}; // get rid of compiler warning
142 }
143 }
144 }
145} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:39
size_type size() const noexcept
Definition: NdArrayCore.hpp:4524
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1365
self_type transpose() const
Definition: NdArrayCore.hpp:4882
size_type numCols() const noexcept
Definition: NdArrayCore.hpp:3465
size_type numRows() const noexcept
Definition: NdArrayCore.hpp:3477
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1673
Definition: Cartesian.hpp:40
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
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: sum.hpp:46
std::uint32_t uint32
Definition: Types.hpp:40