NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
NdArrayBroadcast.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <utility>
31
34#include "NumCpp/Core/Types.hpp"
36
38{
39 //============================================================================
40 // Method Description:
50 template<typename dtypeIn1, typename dtypeIn2, typename Function, typename... AdditionalFunctionArgs>
52 const NdArray<dtypeIn2>& inArray2,
53 const Function& function,
54 const AdditionalFunctionArgs&&... additionalFunctionArgs)
55 {
56 if (inArray1.shape() == inArray2.shape())
57 {
59 inArray1.cbegin(),
60 inArray1.cend(),
61 inArray2.cbegin(),
62 inArray1.begin(),
63 [&function, &additionalFunctionArgs...](const auto& inValue1, const auto& inValue2) -> dtypeIn1
64 {
65 return function(inValue1,
66 inValue2,
67 std::forward<AdditionalFunctionArgs>(additionalFunctionArgs)...);
68 });
69 }
70 else if (inArray2.isscalar())
71 {
72 const auto value = inArray2.item();
73 stl_algorithms::transform(
74 inArray1.cbegin(),
75 inArray1.cend(),
76 inArray1.begin(),
77 [&value, &function, &additionalFunctionArgs...](const auto& inValue) -> dtypeIn1
78 { return function(inValue, value, std::forward<AdditionalFunctionArgs>(additionalFunctionArgs)...); });
79 }
80 else if (inArray2.isflat())
81 {
82 if (inArray2.numRows() > 1 && inArray2.numRows() == inArray1.numRows())
83 {
84 for (uint32 row = 0; row < inArray1.numRows(); ++row)
85 {
86 const auto value = inArray2[row];
88 inArray1.cbegin(row),
89 inArray1.cend(row),
90 inArray1.begin(row),
91 [&value, &function, &additionalFunctionArgs...](const auto& inValue) -> dtypeIn1
92 {
93 return function(inValue,
94 value,
95 std::forward<AdditionalFunctionArgs>(additionalFunctionArgs)...);
96 });
97 }
98 }
99 else if (inArray2.numCols() > 1 && inArray2.numCols() == inArray1.numCols())
100 {
101 for (uint32 col = 0; col < inArray1.numCols(); ++col)
102 {
103 const auto value = inArray2[col];
105 inArray1.ccolbegin(col),
106 inArray1.ccolend(col),
107 inArray1.colbegin(col),
108 [&value, &function, &additionalFunctionArgs...](const auto& inValue) -> dtypeIn1
109 {
110 return function(inValue,
111 value,
112 std::forward<AdditionalFunctionArgs>(additionalFunctionArgs)...);
113 });
114 }
115 }
116 else
117 {
118 THROW_INVALID_ARGUMENT_ERROR("operands could not be broadcast together");
119 }
120 }
121 else
122 {
123 THROW_INVALID_ARGUMENT_ERROR("operands could not be broadcast together");
124 }
125
126 return inArray1;
127 }
128
129 //============================================================================
130 // Method Description:
140 template<typename dtypeOut,
141 typename dtypeIn1,
142 typename dtypeIn2,
143 typename Function,
144 typename... AdditionalFunctionArgs>
146 const NdArray<dtypeIn2>& inArray2,
147 const Function& function,
148 const AdditionalFunctionArgs&&... additionalFunctionArgs)
149 {
150 if (inArray1.shape() == inArray2.shape())
151 {
152 return [&inArray1, &inArray2, &function, &additionalFunctionArgs...]
153 {
154 NdArray<dtypeOut> returnArray(inArray1.shape());
156 inArray1.cbegin(),
157 inArray1.cend(),
158 inArray2.cbegin(),
159 returnArray.begin(),
160 [&function, &additionalFunctionArgs...](const auto& inValue1, const auto& inValue2) -> dtypeOut
161 {
162 return function(inValue1,
163 inValue2,
164 std::forward<AdditionalFunctionArgs>(additionalFunctionArgs)...);
165 });
166
167 return returnArray;
168 }();
169 }
170 else if (inArray1.isscalar())
171 {
172 const auto value = inArray1.item();
173 return [&inArray2, &value, &function, &additionalFunctionArgs...]
174 {
175 NdArray<dtypeOut> returnArray(inArray2.shape());
177 inArray2.cbegin(),
178 inArray2.cend(),
179 returnArray.begin(),
180 [&value, &function, &additionalFunctionArgs...](const auto& inValue) -> dtypeOut
181 {
182 return function(inValue,
183 value,
184 std::forward<AdditionalFunctionArgs>(additionalFunctionArgs)...);
185 });
186 return returnArray;
187 }();
188 }
189 else if (inArray2.isscalar())
190 {
191 const auto value = inArray2.item();
192 return [&inArray1, &value, &function, &additionalFunctionArgs...]
193 {
194 NdArray<dtypeOut> returnArray(inArray1.shape());
196 inArray1.cbegin(),
197 inArray1.cend(),
198 returnArray.begin(),
199 [&value, &function, &additionalFunctionArgs...](const auto& inValue) -> dtypeOut
200 {
201 return function(inValue,
202 value,
203 std::forward<AdditionalFunctionArgs>(additionalFunctionArgs)...);
204 });
205 return returnArray;
206 }();
207 }
208 else if (inArray1.isflat() && inArray2.isflat())
209 {
210 return [&inArray1, &inArray2, &function, &additionalFunctionArgs...]
211 {
212 const auto numRows = std::max(inArray1.numRows(), inArray2.numRows());
213 const auto numCols = std::max(inArray1.numCols(), inArray2.numCols());
214 NdArray<dtypeOut> returnArray(numRows, numCols);
215 if (inArray1.numRows() > 1)
216 {
217 for (uint32 row = 0; row < inArray1.numRows(); ++row)
218 {
219 for (uint32 col = 0; col < inArray2.numCols(); ++col)
220 {
221 returnArray(row, col) =
222 function(inArray1[row],
223 inArray2[col],
224 std::forward<AdditionalFunctionArgs>(additionalFunctionArgs)...);
225 }
226 }
227 }
228 else
229 {
230 for (uint32 row = 0; row < inArray2.numRows(); ++row)
231 {
232 for (uint32 col = 0; col < inArray1.numCols(); ++col)
233 {
234 returnArray(row, col) =
235 function(inArray1[col],
236 inArray2[row],
237 std::forward<AdditionalFunctionArgs>(additionalFunctionArgs)...);
238 }
239 }
240 }
241 return returnArray;
242 }();
243 }
244 else if (inArray1.isflat())
245 {
246 return broadcaster<dtypeOut>(inArray2,
247 inArray1,
248 function,
249 std::forward<AdditionalFunctionArgs>(additionalFunctionArgs)...);
250 }
251 else if (inArray2.isflat())
252 {
253 if (inArray2.numRows() > 1 && inArray2.numRows() == inArray1.numRows())
254 {
255 return [&inArray1, &inArray2, &function, &additionalFunctionArgs...]
256 {
257 NdArray<dtypeOut> returnArray(inArray1.shape());
258 for (uint32 row = 0; row < inArray1.numRows(); ++row)
259 {
260 const auto value = inArray2[row];
262 inArray1.cbegin(row),
263 inArray1.cend(row),
264 returnArray.begin(row),
265 [&value, &function, &additionalFunctionArgs...](const auto& inValue) -> dtypeOut
266 {
267 return function(inValue,
268 value,
269 std::forward<AdditionalFunctionArgs>(additionalFunctionArgs)...);
270 });
271 }
272 return returnArray;
273 }();
274 }
275 else if (inArray2.numCols() > 1 && inArray2.numCols() == inArray1.numCols())
276 {
277 return broadcaster<dtypeOut>(inArray1.transpose(),
278 inArray2.transpose(),
279 function,
280 std::forward<AdditionalFunctionArgs>(additionalFunctionArgs)...)
281 .transpose();
282 }
283 else
284 {
285 THROW_INVALID_ARGUMENT_ERROR("operands could not be broadcast together");
286 }
287 }
288 else
289 {
290 THROW_INVALID_ARGUMENT_ERROR("operands could not be broadcast together");
291 }
292
293 return {}; // get rid of compiler warning
294 }
295} // namespace nc::broadcast
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
const_iterator cbegin() const noexcept
Definition NdArrayCore.hpp:1361
self_type transpose() const
Definition NdArrayCore.hpp:4959
bool isflat() const noexcept
Definition NdArrayCore.hpp:3021
size_type numCols() const noexcept
Definition NdArrayCore.hpp:3541
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
size_type numRows() const noexcept
Definition NdArrayCore.hpp:3553
const_iterator cend() const noexcept
Definition NdArrayCore.hpp:1669
iterator begin() noexcept
Definition NdArrayCore.hpp:1311
bool isscalar() const noexcept
Definition NdArrayCore.hpp:3032
value_type item() const
Definition NdArrayCore.hpp:3098
Definition NdArrayBroadcast.hpp:38
NdArray< dtypeIn1 > & broadcaster(NdArray< dtypeIn1 > &inArray1, const NdArray< dtypeIn2 > &inArray2, const Function &function, const AdditionalFunctionArgs &&... additionalFunctionArgs)
Definition NdArrayBroadcast.hpp:51
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition StlAlgorithms.hpp:776
std::uint32_t uint32
Definition Types.hpp:40