NumCpp  1.0
A C++ implementation of the Python Numpy library
centerOfMass.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/NdArray.hpp"
32 #include "NumCpp/Core/Shape.hpp"
33 #include "NumCpp/Core/Types.hpp"
36 
37 namespace nc
38 {
39  //============================================================================
40  // Method Description:
47  template<typename dtype>
49  {
51 
52  const Shape shape = inArray.shape();
53 
54  switch (inAxis)
55  {
56  case Axis::NONE:
57  {
58  double inten = 0.0;
59  double rowCenter = 0.0;
60  double colCenter = 0.0;
61 
62  for (uint32 row = 0; row < shape.rows; ++row)
63  {
64  for (uint32 col = 0; col < shape.cols; ++col)
65  {
66  const double pixelValue = static_cast<double>(inArray(row, col));
67 
68  inten += pixelValue;
69  rowCenter += pixelValue * static_cast<double>(row);
70  colCenter += pixelValue * static_cast<double>(col);
71  }
72  }
73 
74  rowCenter /= inten;
75  colCenter /= inten;
76 
77  return { rowCenter, colCenter };
78  }
79  case Axis::ROW:
80  {
81  NdArray<double> returnArray(1, shape.cols);
82  returnArray.zeros();
83 
84  const NdArray<double> inten = inArray.template astype<double>().sum(inAxis);
85 
86  for (uint32 colIdx = 0; colIdx < shape.cols; ++colIdx)
87  {
88  for (uint32 rowIdx = 0; rowIdx < shape.rows; ++rowIdx)
89  {
90  returnArray(0, colIdx) += 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) += static_cast<double>(inArray(rowIdx, colIdx)) * static_cast<double>(colIdx);
110  }
111 
112  returnArray(0, rowIdx) /= inten[rowIdx];
113  }
114 
115  return returnArray;
116  }
117  default:
118  {
119  // this isn't actually possible, just putting this here to get rid
120  // of the compiler warning.
121  return NdArray<double>(0);
122  }
123  }
124  }
125 }
StaticAsserts.hpp
nc::NdArray::shape
Shape shape() const noexcept
Definition: NdArrayCore.hpp:4296
nc::Axis::NONE
@ NONE
STATIC_ASSERT_ARITHMETIC
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:38
nc::Axis::ROW
@ ROW
nc::shape
Shape shape(const NdArray< dtype > &inArray) noexcept
Definition: Functions/Shape.hpp:45
nc::NdArray< double >
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
NdArray.hpp
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
nc::Shape::cols
uint32 cols
Definition: Core/Shape.hpp:46
nc::NdArray::sum
NdArray< dtype > sum(Axis inAxis=Axis::NONE) const
Definition: NdArrayCore.hpp:4411
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:47
nc::NdArray::zeros
NdArray< dtype > & zeros() noexcept
Definition: NdArrayCore.hpp:4609
Shape.hpp
nc
Definition: Coordinate.hpp:45
nc::Shape::rows
uint32 rows
Definition: Core/Shape.hpp:45
StlAlgorithms.hpp
Types.hpp
nc::Axis::COL
@ COL
nc::centerOfMass
NdArray< double > centerOfMass(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: centerOfMass.hpp:48