NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
normalize.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"
37#include "NumCpp/NdArray.hpp"
38#include "NumCpp/Utils/sqr.hpp"
39
40namespace nc
41{
42 //============================================================================
43 // Method Description:
51 template<typename dtype>
53 {
55
56 switch (inAxis)
57 {
58 case Axis::NONE:
59 {
60 return inArray / norm(inArray, Axis::NONE);
61 }
62 case Axis::COL:
63 {
64 NdArray<double> returnArray(inArray.shape());
65 const auto cSlice = returnArray.cSlice();
66 for (uint32 row = 0; row < inArray.numRows(); ++row)
67 {
68 returnArray.put(row, cSlice, normalize(inArray.row(row), Axis::NONE));
69 }
70
71 return returnArray;
72 }
73 case Axis::ROW:
74 {
75 return normalize(inArray.transpose(), Axis::COL).transpose();
76 }
77 default:
78 {
79 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
80 return {}; // get rid of compiler warning
81 }
82 }
83 }
84
85 //============================================================================
86 // Method Description:
94 template<typename dtype>
95 NdArray<std::complex<double>> normalize(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::NONE)
96 {
98
99 switch (inAxis)
100 {
101 case Axis::NONE:
102 {
103 return inArray / norm(inArray, Axis::NONE);
104 }
105 case Axis::COL:
106 {
107 NdArray<std::complex<double>> returnArray(inArray.shape());
108 const auto cSlice = returnArray.cSlice();
109 for (uint32 row = 0; row < inArray.numRows(); ++row)
110 {
111 returnArray.put(row, cSlice, normalize(inArray.row(row), Axis::NONE));
112 }
113
114 return returnArray;
115 }
116 case Axis::ROW:
117 {
118 return normalize(inArray.transpose(), Axis::COL).transpose();
119 }
120 default:
121 {
122 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
123 return {}; // get rid of compiler warning
124 }
125 }
126 }
127} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:39
self_type transpose() const
Definition: NdArrayCore.hpp:4882
const Shape & shape() const noexcept
Definition: NdArrayCore.hpp:4511
size_type numRows() const noexcept
Definition: NdArrayCore.hpp:3477
self_type row(size_type inRow) const
Definition: NdArrayCore.hpp:4478
Slice cSlice(index_type inStartIdx=0, size_type inStepSize=1) const
Definition: NdArrayCore.hpp:1008
self_type & put(index_type inIndex, const value_type &inValue)
Definition: NdArrayCore.hpp:3693
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< double > normalize(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: normalize.hpp:52
std::uint32_t uint32
Definition: Types.hpp:40