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