NumCpp  2.18.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 <cmath>
31
33#include "NumCpp/Core/Shape.hpp"
34#include "NumCpp/Core/Types.hpp"
35#include "NumCpp/NdArray.hpp"
36
37namespace nc
38{
39 //============================================================================
40 // Method Description:
50 template<typename dtype>
52 {
54
55 switch (inAxis)
56 {
57 case Axis::NONE:
58 {
59 auto sum = static_cast<double>(
60 std::accumulate(inArray.cbegin(),
61 inArray.cend(),
62 0.,
63 [](dtype inValue1, dtype inValue2) -> dtype
64 { return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2; }));
65
66 const auto numberNonNan =
67 static_cast<double>(std::accumulate(inArray.cbegin(),
68 inArray.cend(),
69 0.,
70 [](dtype inValue1, dtype inValue2) -> dtype
71 { return std::isnan(inValue2) ? inValue1 : inValue1 + 1; }));
72
73 NdArray<double> returnArray = { sum /= numberNonNan };
74
75 return returnArray;
76 }
77 case Axis::COL:
78 {
79 const Shape inShape = inArray.shape();
80 NdArray<double> returnArray(1, inShape.rows);
81 for (uint32 row = 0; row < inShape.rows; ++row)
82 {
83 auto sum = static_cast<double>(
84 std::accumulate(inArray.cbegin(row),
85 inArray.cend(row),
86 0.,
87 [](dtype inValue1, dtype inValue2) -> dtype
88 { return std::isnan(inValue2) ? inValue1 : inValue1 + inValue2; }));
89
90 auto numberNonNan = static_cast<double>(
91 std::accumulate(inArray.cbegin(row),
92 inArray.cend(row),
93 0.,
94 [](dtype inValue1, dtype inValue2) -> dtype
95 { return std::isnan(inValue2) ? inValue1 : inValue1 + 1; }));
96
97 returnArray(0, row) = sum / numberNonNan;
98 }
99
100 return returnArray;
101 }
102 case Axis::ROW:
103 {
104 return nanmean(inArray.transpose(), Axis::COL);
105 }
106 default:
107 {
108 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
109 return {}; // get rid of compiler warning
110 }
111 }
112 }
113} // 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:135
const_iterator cbegin() const noexcept
Definition NdArrayCore.hpp:1361
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
A Shape Class for NdArrays.
Definition Core/shape.hpp:41
uint32 rows
Definition Core/shape.hpp:44
Definition Cartesian.hpp:39
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
NdArray< dtype > sum(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition sum.hpp:45
NdArray< double > nanmean(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition nanmean.hpp:51
std::uint32_t uint32
Definition Types.hpp:40