NumCpp  2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
norm.hpp
Go to the documentation of this file.
1 #pragma once
29 
31 #include "NumCpp/Core/Types.hpp"
32 #include "NumCpp/NdArray.hpp"
33 #include "NumCpp/Utils/sqr.hpp"
34 
35 #include <algorithm>
36 #include <cmath>
37 #include <complex>
38 
39 namespace nc
40 {
41  //============================================================================
42  // Method Description:
51  template<typename dtype>
52  NdArray<double> norm(const NdArray<dtype>& inArray, Axis inAxis = Axis::NONE)
53  {
55 
56  double sumOfSquares = 0.0;
57  const auto function = [&sumOfSquares](dtype value) -> void
58  {
59  sumOfSquares += utils::sqr(static_cast<double>(value));
60  };
61 
62  switch (inAxis)
63  {
64  case Axis::NONE:
65  {
66  std::for_each(inArray.cbegin(), inArray.cend(), function);
67 
68  NdArray<double> returnArray = { std::sqrt(sumOfSquares) };
69  return returnArray;
70  }
71  case Axis::COL:
72  {
73  NdArray<double> returnArray(1, inArray.numRows());
74  for (uint32 row = 0; row < inArray.numRows(); ++row)
75  {
76  sumOfSquares = 0.0;
77  std::for_each(inArray.cbegin(row), inArray.cend(row), function);
78  returnArray(0, row) = std::sqrt(sumOfSquares);
79  }
80 
81  return returnArray;
82  }
83  case Axis::ROW:
84  {
85  NdArray<dtype> transposedArray = inArray.transpose();
86  NdArray<double> returnArray(1, transposedArray.numRows());
87  for (uint32 row = 0; row < transposedArray.numRows(); ++row)
88  {
89  sumOfSquares = 0.0;
90  std::for_each(transposedArray.cbegin(row), transposedArray.cend(row), function);
91  returnArray(0, row) = std::sqrt(sumOfSquares);
92  }
93 
94  return returnArray;
95  }
96  default:
97  {
98  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
99  return {}; // get rid of compiler warning
100  }
101  }
102  }
103 
104  //============================================================================
105  // Method Description:
114  template<typename dtype>
115  NdArray<std::complex<double>> norm(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::NONE)
116  {
118 
119  std::complex<double> sumOfSquares(0.0, 0.0);
120  const auto function = [&sumOfSquares](const std::complex<dtype>& value) -> void
121  {
122  sumOfSquares += utils::sqr(complex_cast<double>(value));
123  };
124 
125  switch (inAxis)
126  {
127  case Axis::NONE:
128  {
129  std::for_each(inArray.cbegin(), inArray.cend(), function);
130 
131  NdArray<std::complex<double>> returnArray = { std::sqrt(sumOfSquares) };
132  return returnArray;
133  }
134  case Axis::COL:
135  {
136  NdArray<std::complex<double>> returnArray(1, inArray.numRows());
137  for (uint32 row = 0; row < inArray.numRows(); ++row)
138  {
139  sumOfSquares = std::complex<double>(0.0, 0.0);
140  std::for_each(inArray.cbegin(row), inArray.cend(row), function);
141  returnArray(0, row) = std::sqrt(sumOfSquares);
142  }
143 
144  return returnArray;
145  }
146  case Axis::ROW:
147  {
148  NdArray<std::complex<dtype>> transposedArray = inArray.transpose();
149  NdArray<std::complex<double>> returnArray(1, transposedArray.numRows());
150  for (uint32 row = 0; row < transposedArray.numRows(); ++row)
151  {
152  sumOfSquares = std::complex<double>(0.0, 0.0);
153  std::for_each(transposedArray.cbegin(row), transposedArray.cend(row), function);
154  returnArray(0, row) = std::sqrt(sumOfSquares);
155  }
156 
157  return returnArray;
158  }
159  default:
160  {
161  THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
162  return {}; // get rid of compiler warning
163  }
164  }
165  }
166 } // namespace nc
StaticAsserts.hpp
nc::Axis::NONE
@ NONE
STATIC_ASSERT_ARITHMETIC
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
nc::Axis::ROW
@ ROW
nc::sqrt
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:50
nc::NdArray::transpose
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4652
nc::NdArray< double >
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:40
NdArray.hpp
nc::NdArray::cend
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1487
nc::Axis
Axis
Enum To describe an axis.
Definition: Types.hpp:46
nc
Definition: Coordinate.hpp:44
sqr.hpp
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
nc::NdArray::cbegin
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1143
nc::utils::sqr
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:44
Types.hpp
nc::NdArray::numRows
uint32 numRows() const noexcept
Definition: NdArrayCore.hpp:3488
nc::Axis::COL
@ COL
nc::norm
NdArray< double > norm(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: norm.hpp:52