NumCpp
2.4.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
where.hpp
Go to the documentation of this file.
1
#pragma once
29
30
#include "
NumCpp/Core/Internal/Error.hpp
"
31
#include "
NumCpp/Core/Shape.hpp
"
32
#include "
NumCpp/NdArray.hpp
"
33
34
#include <string>
35
36
namespace
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
nc::NdArray::shape
Shape shape() const noexcept
Definition:
NdArrayCore.hpp:4356
Error.hpp
nc::where
NdArray< dtype > where(const NdArray< bool > &inMask, const NdArray< dtype > &inA, const NdArray< dtype > &inB)
Definition:
where.hpp:52
nc::NdArray< dtype >
nc::uint32
std::uint32_t uint32
Definition:
Types.hpp:40
NdArray.hpp
Shape.hpp
nc
Definition:
Coordinate.hpp:44
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition:
Error.hpp:36
include
NumCpp
Functions
where.hpp
Generated by
1.8.17