NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
norm.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31#include <cmath>
32#include <complex>
33
35#include "NumCpp/Core/Types.hpp"
36#include "NumCpp/NdArray.hpp"
37#include "NumCpp/Utils/sqr.hpp"
38
39namespace nc
40{
41 //============================================================================
42 // Method Description:
50 template<typename dtype>
52 {
54
55 double sumOfSquares = 0.;
56 const auto function = [&sumOfSquares](dtype value) -> void
57 { sumOfSquares += utils::sqr(static_cast<double>(value)); };
58
59 switch (inAxis)
60 {
61 case Axis::NONE:
62 {
63 std::for_each(inArray.cbegin(), inArray.cend(), function);
64
65 NdArray<double> returnArray = { std::sqrt(sumOfSquares) };
66 return returnArray;
67 }
68 case Axis::COL:
69 {
70 NdArray<double> returnArray(1, inArray.numRows());
71 for (uint32 row = 0; row < inArray.numRows(); ++row)
72 {
73 sumOfSquares = 0.;
74 std::for_each(inArray.cbegin(row), inArray.cend(row), function);
75 returnArray(0, row) = std::sqrt(sumOfSquares);
76 }
77
78 return returnArray;
79 }
80 case Axis::ROW:
81 {
82 return norm(inArray.transpose(), Axis::COL);
83 }
84 default:
85 {
86 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
87 return {}; // get rid of compiler warning
88 }
89 }
90 }
91
92 //============================================================================
93 // Method Description:
101 template<typename dtype>
102 NdArray<std::complex<double>> norm(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::NONE)
103 {
105
106 std::complex<double> sumOfSquares(0., 0.);
107 const auto function = [&sumOfSquares](const std::complex<dtype>& value) -> void
108 { sumOfSquares += utils::sqr(complex_cast<double>(value)); };
109
110 switch (inAxis)
111 {
112 case Axis::NONE:
113 {
114 std::for_each(inArray.cbegin(), inArray.cend(), function);
115
116 NdArray<std::complex<double>> returnArray = { std::sqrt(sumOfSquares) };
117 return returnArray;
118 }
119 case Axis::COL:
120 {
121 NdArray<std::complex<double>> returnArray(1, inArray.numRows());
122 for (uint32 row = 0; row < inArray.numRows(); ++row)
123 {
124 sumOfSquares = std::complex<double>(0., 0.);
125 std::for_each(inArray.cbegin(row), inArray.cend(row), function);
126 returnArray(0, row) = std::sqrt(sumOfSquares);
127 }
128
129 return returnArray;
130 }
131 case Axis::ROW:
132 {
133 return norm(inArray.transpose(), Axis::COL);
134 }
135 default:
136 {
137 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
138 return {}; // get rid of compiler warning
139 }
140 }
141 }
142} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:39
const_iterator cbegin() const noexcept
Definition: NdArrayCore.hpp:1365
self_type transpose() const
Definition: NdArrayCore.hpp:4882
size_type numRows() const noexcept
Definition: NdArrayCore.hpp:3477
const_iterator cend() const noexcept
Definition: NdArrayCore.hpp:1673
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:225
constexpr dtype sqr(dtype inValue) noexcept
Definition: sqr.hpp:42
Definition: Cartesian.hpp:40
NdArray< double > norm(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: norm.hpp:51
Axis
Enum To describe an axis.
Definition: Enums.hpp:36
auto sqrt(dtype inValue) noexcept
Definition: sqrt.hpp:48
std::uint32_t uint32
Definition: Types.hpp:40