NumCpp  2.14.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
66 return returnArray;
67 }
68 case Axis::COL:
69 {
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>
103 {
105
106 std::complex<double> sumOfSquares(0., 0.);
107 const auto function = [&sumOfSquares](const std::complex<dtype>& value) -> void
109
110 switch (inAxis)
111 {
112 case Axis::NONE:
113 {
114 std::for_each(inArray.cbegin(), inArray.cend(), function);
115
117 return returnArray;
118 }
119 case Axis::COL:
120 {
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
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:139
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
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59
std::uint32_t uint32
Definition Types.hpp:40