NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
where.hpp
Go to the documentation of this file.
1
28#pragma once
29
31#include "NumCpp/Core/Shape.hpp"
32#include "NumCpp/NdArray.hpp"
33
34namespace nc
35{
36 //============================================================================
37 // Method Description:
49 template<typename dtype>
50 NdArray<dtype> where(const NdArray<bool>& inMask, const NdArray<dtype>& inA, const NdArray<dtype>& inB)
51 {
52 const auto shapeMask = inMask.shape();
53 const auto shapeA = inA.shape();
54 if (shapeA != inB.shape())
55 {
56 THROW_INVALID_ARGUMENT_ERROR("input inA and inB must be the same shapes.");
57 }
58
59 if (shapeMask != shapeA)
60 {
61 THROW_INVALID_ARGUMENT_ERROR("input inMask must be the same shape as the input arrays.");
62 }
63
64 auto outArray = NdArray<dtype>(shapeMask);
65
66 uint32 idx = 0;
67 for (auto maskValue : inMask)
68 {
69 if (maskValue)
70 {
71 outArray[idx] = inA[idx];
72 }
73 else
74 {
75 outArray[idx] = inB[idx];
76 }
77 ++idx;
78 }
79
80 return outArray;
81 }
82
83 //============================================================================
84 // Method Description:
96 template<typename dtype>
97 NdArray<dtype> where(const NdArray<bool>& inMask, const NdArray<dtype>& inA, dtype inB)
98 {
99 const auto shapeMask = inMask.shape();
100 const auto shapeA = inA.shape();
101 if (shapeMask != shapeA)
102 {
103 THROW_INVALID_ARGUMENT_ERROR("input inMask must be the same shape as the input arrays.");
104 }
105
106 auto outArray = NdArray<dtype>(shapeMask);
107
108 uint32 idx = 0;
109 for (auto maskValue : inMask)
110 {
111 if (maskValue)
112 {
113 outArray[idx] = inA[idx];
114 }
115 else
116 {
117 outArray[idx] = inB;
118 }
119 ++idx;
120 }
121
122 return outArray;
123 }
124
125 //============================================================================
126 // Method Description:
138 template<typename dtype>
139 NdArray<dtype> where(const NdArray<bool>& inMask, dtype inA, const NdArray<dtype>& inB)
140 {
141 const auto shapeMask = inMask.shape();
142 const auto shapeB = inB.shape();
143 if (shapeMask != shapeB)
144 {
145 THROW_INVALID_ARGUMENT_ERROR("input inMask must be the same shape as the input arrays.");
146 }
147
148 auto outArray = NdArray<dtype>(shapeMask);
149
150 uint32 idx = 0;
151 for (auto maskValue : inMask)
152 {
153 if (maskValue)
154 {
155 outArray[idx] = inA;
156 }
157 else
158 {
159 outArray[idx] = inB[idx];
160 }
161 ++idx;
162 }
163
164 return outArray;
165 }
166
167 //============================================================================
168 // Method Description:
180 template<typename dtype>
181 NdArray<dtype> where(const NdArray<bool>& inMask, dtype inA, dtype inB)
182 {
183 auto outArray = NdArray<dtype>(inMask.shape());
184
185 uint32 idx = 0;
186 for (auto maskValue : inMask)
187 {
188 if (maskValue)
189 {
190 outArray[idx] = inA;
191 }
192 else
193 {
194 outArray[idx] = inB;
195 }
196 ++idx;
197 }
198
199 return outArray;
200 }
201} // namespace nc
#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 Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
Definition Cartesian.hpp:39
NdArray< dtype > where(const NdArray< bool > &inMask, const NdArray< dtype > &inA, const NdArray< dtype > &inB)
Definition where.hpp:50
std::uint32_t uint32
Definition Types.hpp:40