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