NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
gradient.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <complex>
31
36#include "NumCpp/Core/Shape.hpp"
37#include "NumCpp/Core/Types.hpp"
38#include "NumCpp/NdArray.hpp"
39
40namespace nc
41{
42 //============================================================================
43 // Method Description:
53 template<typename dtype>
55 {
57
58 switch (inAxis)
59 {
60 case Axis::ROW:
61 {
62 const auto inShape = inArray.shape();
63 if (inShape.rows < 2)
64 {
65 THROW_INVALID_ARGUMENT_ERROR("input array must have more than 1 row.");
66 }
67
68 // first do the first and last rows
69 auto returnArray = NdArray<double>(inShape);
70 for (uint32 col = 0; col < inShape.cols; ++col)
71 {
72 returnArray(0, col) = static_cast<double>(inArray(1, col)) - static_cast<double>(inArray(0, col));
73 returnArray(-1, col) =
74 static_cast<double>(inArray(-1, col)) - static_cast<double>(inArray(-2, col));
75 }
76
77 // then rip through the rest of the array
78 for (uint32 col = 0; col < inShape.cols; ++col)
79 {
80 for (uint32 row = 1; row < inShape.rows - 1; ++row)
81 {
82 returnArray(row, col) =
83 (static_cast<double>(inArray(row + 1, col)) - static_cast<double>(inArray(row - 1, col))) /
84 2.;
85 }
86 }
87
88 return returnArray;
89 }
90 case Axis::COL:
91 {
92 const auto inShape = inArray.shape();
93 if (inShape.cols < 2)
94 {
95 THROW_INVALID_ARGUMENT_ERROR("input array must have more than 1 columns.");
96 }
97
98 // first do the first and last columns
99 auto returnArray = NdArray<double>(inShape);
100 for (uint32 row = 0; row < inShape.rows; ++row)
101 {
102 returnArray(row, 0) = static_cast<double>(inArray(row, 1)) - static_cast<double>(inArray(row, 0));
103 returnArray(row, -1) =
104 static_cast<double>(inArray(row, -1)) - static_cast<double>(inArray(row, -2));
105 }
106
107 // then rip through the rest of the array
108 for (uint32 row = 0; row < inShape.rows; ++row)
109 {
110 for (uint32 col = 1; col < inShape.cols - 1; ++col)
111 {
112 returnArray(row, col) =
113 (static_cast<double>(inArray(row, col + 1)) - static_cast<double>(inArray(row, col - 1))) /
114 2.;
115 }
116 }
117
118 return returnArray;
119 }
120 default:
121 {
122 // will return the gradient of the flattened array
123 if (inArray.size() < 2)
124 {
125 THROW_INVALID_ARGUMENT_ERROR("input array must have more than 1 element.");
126 }
127
128 auto returnArray = NdArray<double>(1, inArray.size());
129 returnArray[0] = static_cast<double>(inArray[1]) - static_cast<double>(inArray[0]);
130 returnArray[-1] = static_cast<double>(inArray[-1]) - static_cast<double>(inArray[-2]);
131
132 stl_algorithms::transform(inArray.cbegin() + 2,
133 inArray.cend(),
134 inArray.cbegin(),
135 returnArray.begin() + 1,
136 [](dtype value1, dtype value2) -> double
137 { return (static_cast<double>(value1) - static_cast<double>(value2)) / 2.; });
138
139 return returnArray;
140 }
141 }
142 }
143
144 //============================================================================
145 // Method Description:
155 template<typename dtype>
156 NdArray<std::complex<double>> gradient(const NdArray<std::complex<dtype>>& inArray, Axis inAxis = Axis::ROW)
157 {
159
160 switch (inAxis)
161 {
162 case Axis::ROW:
163 {
164 const auto inShape = inArray.shape();
165 if (inShape.rows < 2)
166 {
167 THROW_INVALID_ARGUMENT_ERROR("input array must have more than 1 row.");
168 }
169
170 // first do the first and last rows
171 auto returnArray = NdArray<std::complex<double>>(inShape);
172 for (uint32 col = 0; col < inShape.cols; ++col)
173 {
174 returnArray(0, col) = complex_cast<double>(inArray(1, col)) - complex_cast<double>(inArray(0, col));
175 returnArray(-1, col) =
176 complex_cast<double>(inArray(-1, col)) - complex_cast<double>(inArray(-2, col));
177 }
178
179 // then rip through the rest of the array
180 for (uint32 col = 0; col < inShape.cols; ++col)
181 {
182 for (uint32 row = 1; row < inShape.rows - 1; ++row)
183 {
184 returnArray(row, col) = (complex_cast<double>(inArray(row + 1, col)) -
185 complex_cast<double>(inArray(row - 1, col))) /
186 2.;
187 }
188 }
189
190 return returnArray;
191 }
192 case Axis::COL:
193 {
194 const auto inShape = inArray.shape();
195 if (inShape.cols < 2)
196 {
197 THROW_INVALID_ARGUMENT_ERROR("input array must have more than 1 columns.");
198 }
199
200 // first do the first and last columns
201 auto returnArray = NdArray<std::complex<double>>(inShape);
202 for (uint32 row = 0; row < inShape.rows; ++row)
203 {
204 returnArray(row, 0) = complex_cast<double>(inArray(row, 1)) - complex_cast<double>(inArray(row, 0));
205 returnArray(row, -1) =
206 complex_cast<double>(inArray(row, -1)) - complex_cast<double>(inArray(row, -2));
207 }
208
209 // then rip through the rest of the array
210 for (uint32 row = 0; row < inShape.rows; ++row)
211 {
212 for (uint32 col = 1; col < inShape.cols - 1; ++col)
213 {
214 returnArray(row, col) = (complex_cast<double>(inArray(row, col + 1)) -
215 complex_cast<double>(inArray(row, col - 1))) /
216 2.;
217 }
218 }
219
220 return returnArray;
221 }
222 default:
223 {
224 // will return the gradient of the flattened array
225 if (inArray.size() < 2)
226 {
227 THROW_INVALID_ARGUMENT_ERROR("input array must have more than 1 element.");
228 }
229
230 auto returnArray = NdArray<std::complex<double>>(1, inArray.size());
231 returnArray[0] = complex_cast<double>(inArray[1]) - complex_cast<double>(inArray[0]);
232 returnArray[-1] = complex_cast<double>(inArray[-1]) - complex_cast<double>(inArray[-2]);
233
235 inArray.cbegin() + 2,
236 inArray.cend(),
237 inArray.cbegin(),
238 returnArray.begin() + 1,
239 [](const std::complex<dtype>& value1, const std::complex<dtype>& value2) -> std::complex<double>
240 { return (complex_cast<double>(value1) - complex_cast<double>(value2)) / 2.; });
241
242 return returnArray;
243 }
244 }
245 }
246} // 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
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
const_iterator cbegin() const noexcept
Definition NdArrayCore.hpp:1361
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
const_iterator cend() const noexcept
Definition NdArrayCore.hpp:1669
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition StlAlgorithms.hpp:776
Definition Cartesian.hpp:39
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
std::complex< Out > complex_cast(const std::complex< In > &value) noexcept
Definition StdComplexOperators.hpp:111
NdArray< double > gradient(const NdArray< dtype > &inArray, Axis inAxis=Axis::ROW)
Definition gradient.hpp:54
std::uint32_t uint32
Definition Types.hpp:40