NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
normalize.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <complex>
31
33#include "NumCpp/Core/Types.hpp"
35#include "NumCpp/NdArray.hpp"
36
37namespace nc
38{
39 //============================================================================
40 // Method Description:
48 template<typename dtype>
50 {
52
53 switch (inAxis)
54 {
55 case Axis::NONE:
56 {
57 return inArray / norm(inArray, Axis::NONE);
58 }
59 case Axis::COL:
60 {
61 NdArray<double> returnArray(inArray.shape());
62 const auto cSlice = returnArray.cSlice();
63 for (uint32 row = 0; row < inArray.numRows(); ++row)
64 {
65 returnArray.put(row, cSlice, normalize(inArray.row(row), Axis::NONE));
66 }
67
68 return returnArray;
69 }
70 case Axis::ROW:
71 {
72 return normalize(inArray.transpose(), Axis::COL).transpose();
73 }
74 default:
75 {
76 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
77 return {}; // get rid of compiler warning
78 }
79 }
80 }
81
82 //============================================================================
83 // Method Description:
91 template<typename dtype>
92 NdArray<std::complex<double>> normalize(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::NONE)
93 {
95
96 switch (inAxis)
97 {
98 case Axis::NONE:
99 {
100 return inArray / norm(inArray, Axis::NONE);
101 }
102 case Axis::COL:
103 {
104 NdArray<std::complex<double>> returnArray(inArray.shape());
105 const auto cSlice = returnArray.cSlice();
106 for (uint32 row = 0; row < inArray.numRows(); ++row)
107 {
108 returnArray.put(row, cSlice, normalize(inArray.row(row), Axis::NONE));
109 }
110
111 return returnArray;
112 }
113 case Axis::ROW:
114 {
115 return normalize(inArray.transpose(), Axis::COL).transpose();
116 }
117 default:
118 {
119 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
120 return {}; // get rid of compiler warning
121 }
122 }
123 }
124} // 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:135
self_type transpose() const
Definition NdArrayCore.hpp:4959
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
size_type numRows() const noexcept
Definition NdArrayCore.hpp:3553
self_type row(size_type inRow) const
Definition NdArrayCore.hpp:4554
Slice cSlice(index_type inStartIdx=0, size_type inStepSize=1) const
Definition NdArrayCore.hpp:1004
self_type & put(index_type inIndex, const value_type &inValue)
Definition NdArrayCore.hpp:3769
Definition Cartesian.hpp:39
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
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37
NdArray< double > normalize(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition normalize.hpp:49
std::uint32_t uint32
Definition Types.hpp:40