NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
centerOfMass.hpp
Go to the documentation of this file.
1
28#pragma once
29
32#include "NumCpp/Core/Shape.hpp"
33#include "NumCpp/Core/Types.hpp"
34#include "NumCpp/NdArray.hpp"
35
36namespace nc
37{
38 //============================================================================
39 // Method Description:
46 template<typename dtype>
48 {
50
51 const Shape shape = inArray.shape();
52
53 switch (inAxis)
54 {
55 case Axis::NONE:
56 {
57 double inten = 0.;
58 double rowCenter = 0.;
59 double colCenter = 0.;
60
61 for (uint32 row = 0; row < shape.rows; ++row)
62 {
63 for (uint32 col = 0; col < shape.cols; ++col)
64 {
65 const auto pixelValue = static_cast<double>(inArray(row, col));
66
67 inten += pixelValue;
68 rowCenter += pixelValue * static_cast<double>(row);
69 colCenter += pixelValue * static_cast<double>(col);
70 }
71 }
72
73 rowCenter /= inten;
74 colCenter /= inten;
75
76 return { rowCenter, colCenter };
77 }
78 case Axis::ROW:
79 {
80 NdArray<double> returnArray(1, shape.cols);
81 returnArray.zeros();
82
83 const NdArray<double> inten = inArray.template astype<double>().sum(inAxis);
84
85 for (uint32 colIdx = 0; colIdx < shape.cols; ++colIdx)
86 {
87 for (uint32 rowIdx = 0; rowIdx < shape.rows; ++rowIdx)
88 {
89 returnArray(0, colIdx) +=
90 static_cast<double>(inArray(rowIdx, colIdx)) * static_cast<double>(rowIdx);
91 }
92
93 returnArray(0, colIdx) /= inten[colIdx];
94 }
95
96 return returnArray;
97 }
98 case Axis::COL:
99 {
100 NdArray<double> returnArray(1, shape.rows);
101 returnArray.zeros();
102
103 const NdArray<double> inten = inArray.template astype<double>().sum(inAxis);
104
105 for (uint32 rowIdx = 0; rowIdx < shape.rows; ++rowIdx)
106 {
107 for (uint32 colIdx = 0; colIdx < shape.cols; ++colIdx)
108 {
109 returnArray(0, rowIdx) +=
110 static_cast<double>(inArray(rowIdx, colIdx)) * static_cast<double>(colIdx);
111 }
112
113 returnArray(0, rowIdx) /= inten[rowIdx];
114 }
115
116 return returnArray;
117 }
118 default:
119 {
120 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
121 return {}; // get rid of compiler warning
122 }
123 }
124 }
125} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:39
self_type & zeros() noexcept
Definition: NdArrayCore.hpp:4900
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4511
self_type sum(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:4618
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
uint32 rows
Definition: Core/Shape.hpp:44
uint32 cols
Definition: Core/Shape.hpp:45
Definition: Cartesian.hpp:40
Axis
Enum To describe an axis.
Definition: Enums.hpp:36
NdArray< double > centerOfMass(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: centerOfMass.hpp:47
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition: Functions/Shape.hpp:42
std::uint32_t uint32
Definition: Types.hpp:40