NumCpp  2.16.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
nanmean.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31#include <cmath>
32
35#include "NumCpp/Core/Shape.hpp"
36#include "NumCpp/Core/Types.hpp"
38#include "NumCpp/NdArray.hpp"
39
40namespace nc
41{
42 //============================================================================
43 // Method Description:
53 template<typename dtype>
55 {
57
58 switch (inAxis)
59 {
60 case Axis::NONE:
61 {
62 auto sum = static_cast<double>(
63 std::accumulate(inArray.cbegin(),
64 inArray.cend(),
65 0.,
67 { return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2; }));
68
69 const auto numberNonNan =
70 static_cast<double>(std::accumulate(inArray.cbegin(),
71 inArray.cend(),
72 0.,
74 { return std::isnan(inValue2) ? inValue1 : inValue1 + 1; }));
75
77
78 return returnArray;
79 }
80 case Axis::COL:
81 {
82 const Shape inShape = inArray.shape();
84 for (uint32 row = 0; row < inShape.rows; ++row)
85 {
86 auto sum = static_cast<double>(
87 std::accumulate(inArray.cbegin(row),
88 inArray.cend(row),
89 0.,
91 { return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2; }));
92
93 auto numberNonNan = static_cast<double>(
94 std::accumulate(inArray.cbegin(row),
95 inArray.cend(row),
96 0.,
98 { return std::isnan(inValue2) ? inValue1 : inValue1 + 1; }));
99
100 returnArray(0, row) = sum / numberNonNan;
101 }
102
103 return returnArray;
104 }
105 case Axis::ROW:
106 {
107 return nanmean(inArray.transpose(), Axis::COL);
108 }
109 default:
110 {
111 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
112 return {}; // get rid of compiler warning
113 }
114 }
115 }
116} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
#define STATIC_ASSERT_FLOAT(dtype)
Definition StaticAsserts.hpp:50
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:139
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
Definition Cartesian.hpp:40
Axis
Enum To describe an axis.
Definition Enums.hpp:36
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition sum.hpp:46
NdArray< double > nanmean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition nanmean.hpp:54
std::uint32_t uint32
Definition Types.hpp:40