| 1 |
from pypy.interpreter.error import OperationError, oefmt |
| 2 |
from pypy.interpreter.gateway import unwrap_spec |
| 3 |
from pypy.module.micronumpy import loop, descriptor, support |
| 4 |
from pypy.module.micronumpy import constants as NPY |
| 5 |
from pypy.module.micronumpy.base import convert_to_array, W_NDimArray |
| 6 |
from pypy.module.micronumpy.converters import clipmode_converter |
| 7 |
from pypy.module.micronumpy.strides import ( |
| 8 |
Chunk, new_view, shape_agreement, shape_agreement_multiple) |
| 9 |
from .casting import find_binop_result_dtype, find_result_type |
| 10 |
|
| 11 |
|
| 12 |
def where(space, w_arr, w_x=None, w_y=None): |
| 13 |
"""where(condition, [x, y]) |
| 14 |
|
| 15 |
Return elements, either from `x` or `y`, depending on `condition`. |
| 16 |
|
| 17 |
If only `condition` is given, return ``condition.nonzero()``. |
| 18 |
|
| 19 |
Parameters |
| 20 |
---------- |
| 21 |
condition : array_like, bool |
| 22 |
When True, yield `x`, otherwise yield `y`. |
| 23 |
x, y : array_like, optional |
| 24 |
Values from which to choose. `x` and `y` need to have the same |
| 25 |
shape as `condition`. |
| 26 |
|
| 27 |
Returns |
| 28 |
------- |
| 29 |
out : ndarray or tuple of ndarrays |
| 30 |
If both `x` and `y` are specified, the output array contains |
| 31 |
elements of `x` where `condition` is True, and elements from |
| 32 |
`y` elsewhere. |
| 33 |
|
| 34 |
If only `condition` is given, return the tuple |
| 35 |
``condition.nonzero()``, the indices where `condition` is True. |
| 36 |
|
| 37 |
See Also |
| 38 |
-------- |
| 39 |
nonzero, choose |
| 40 |
|
| 41 |
Notes |
| 42 |
----- |
| 43 |
If `x` and `y` are given and input arrays are 1-D, `where` is |
| 44 |
equivalent to:: |
| 45 |
|
| 46 |
[xv if c else yv for (c,xv,yv) in zip(condition,x,y)] |
| 47 |
|
| 48 |
Examples |
| 49 |
-------- |
| 50 |
>>> np.where([[True, False], [True, True]], |
| 51 |
... [[1, 2], [3, 4]], |
| 52 |
... [[9, 8], [7, 6]]) |
| 53 |
array([[1, 8], |
| 54 |
[3, 4]]) |
| 55 |
|
| 56 |
>>> np.where([[0, 1], [1, 0]]) |
| 57 |
(array([0, 1]), array([1, 0])) |
| 58 |
|
| 59 |
>>> x = np.arange(9.).reshape(3, 3) |
| 60 |
>>> np.where( x > 5 ) |
| 61 |
(array([2, 2, 2]), array([0, 1, 2])) |
| 62 |
>>> x[np.where( x > 3.0 )] # Note: result is 1D. |
| 63 |
array([ 4., 5., 6., 7., 8.]) |
| 64 |
>>> np.where(x < 5, x, -1) # Note: broadcasting. |
| 65 |
array([[ 0., 1., 2.], |
| 66 |
[ 3., 4., -1.], |
| 67 |
[-1., -1., -1.]]) |
| 68 |
|
| 69 |
|
| 70 |
NOTE: support for not passing x and y is unsupported |
| 71 |
""" |
| 72 |
if space.is_none(w_y): |
| 73 |
if space.is_none(w_x): |
| 74 |
arr = convert_to_array(space, w_arr) |
| 75 |
return arr.descr_nonzero(space) |
| 76 |
raise oefmt(space.w_ValueError, |
| 77 |
"Where should be called with either 1 or 3 arguments") |
| 78 |
if space.is_none(w_x): |
| 79 |
raise oefmt(space.w_ValueError, |
| 80 |
"Where should be called with either 1 or 3 arguments") |
| 81 |
arr = convert_to_array(space, w_arr) |
| 82 |
x = convert_to_array(space, w_x) |
| 83 |
y = convert_to_array(space, w_y) |
| 84 |
if x.is_scalar() and y.is_scalar() and arr.is_scalar(): |
| 85 |
if arr.get_dtype().itemtype.bool(arr.get_scalar_value()): |
| 86 |
return x |
| 87 |
return y |
| 88 |
dtype = find_result_type(space, [x, y], []) |
| 89 |
shape = shape_agreement(space, arr.get_shape(), x) |
| 90 |
shape = shape_agreement(space, shape, y) |
| 91 |
out = W_NDimArray.from_shape(space, shape, dtype) |
| 92 |
return loop.where(space, out, shape, arr, x, y, dtype) |
| 93 |
|
| 94 |
|
| 95 |
def dot(space, w_obj1, w_obj2, w_out=None): |
| 96 |
w_arr = convert_to_array(space, w_obj1) |
| 97 |
if w_arr.is_scalar(): |
| 98 |
return convert_to_array(space, w_obj2).descr_dot(space, w_arr, w_out) |
| 99 |
return w_arr.descr_dot(space, w_obj2, w_out) |
| 100 |
|
| 101 |
|
| 102 |
def concatenate(space, w_args, w_axis=None): |
| 103 |
args_w = space.listview(w_args) |
| 104 |
if len(args_w) == 0: |
| 105 |
raise oefmt(space.w_ValueError, "need at least one array to concatenate") |
| 106 |
args_w = [convert_to_array(space, w_arg) for w_arg in args_w] |
| 107 |
if w_axis is None: |
| 108 |
w_axis = space.newint(0) |
| 109 |
if space.is_none(w_axis): |
| 110 |
args_w = [w_arg.reshape(space, |
| 111 |
space.newlist([w_arg.descr_get_size(space)]), |
| 112 |
w_arg.get_order()) |
| 113 |
for w_arg in args_w] |
| 114 |
w_axis = space.newint(0) |
| 115 |
dtype = args_w[0].get_dtype() |
| 116 |
shape = args_w[0].get_shape()[:] |
| 117 |
ndim = len(shape) |
| 118 |
if ndim == 0: |
| 119 |
raise oefmt(space.w_ValueError, |
| 120 |
"zero-dimensional arrays cannot be concatenated") |
| 121 |
axis = space.int_w(w_axis) |
| 122 |
orig_axis = axis |
| 123 |
if axis < 0: |
| 124 |
axis = ndim + axis |
| 125 |
if ndim == 1 and axis != 0: |
| 126 |
axis = 0 |
| 127 |
if axis < 0 or axis >= ndim: |
| 128 |
raise oefmt(space.w_IndexError, "axis %d out of bounds [0, %d)", |
| 129 |
orig_axis, ndim) |
| 130 |
for arr in args_w[1:]: |
| 131 |
if len(arr.get_shape()) != ndim: |
| 132 |
raise oefmt(space.w_ValueError, |
| 133 |
"all the input arrays must have same number of " |
| 134 |
"dimensions") |
| 135 |
for i, axis_size in enumerate(arr.get_shape()): |
| 136 |
if i == axis: |
| 137 |
shape[i] += axis_size |
| 138 |
elif axis_size != shape[i]: |
| 139 |
raise oefmt(space.w_ValueError, |
| 140 |
"all the input array dimensions except for the " |
| 141 |
"concatenation axis must match exactly") |
| 142 |
|
| 143 |
dtype = find_result_type(space, args_w, []) |
| 144 |
|
| 145 |
res = W_NDimArray.from_shape(space, shape, dtype, NPY.CORDER) |
| 146 |
chunks = [Chunk(0, i, 1, i) for i in shape] |
| 147 |
axis_start = 0 |
| 148 |
for arr in args_w: |
| 149 |
if arr.get_shape()[axis] == 0: |
| 150 |
continue |
| 151 |
chunks[axis] = Chunk(axis_start, axis_start + arr.get_shape()[axis], 1, |
| 152 |
arr.get_shape()[axis]) |
| 153 |
view = new_view(space, res, chunks) |
| 154 |
view.implementation.setslice(space, arr) |
| 155 |
axis_start += arr.get_shape()[axis] |
| 156 |
return res |
| 157 |
|
| 158 |
|
| 159 |
@unwrap_spec(repeats=int) |
| 160 |
def repeat(space, w_arr, repeats, w_axis): |
| 161 |
arr = convert_to_array(space, w_arr) |
| 162 |
if space.is_none(w_axis): |
| 163 |
arr = arr.descr_flatten(space) |
| 164 |
orig_size = arr.get_shape()[0] |
| 165 |
shape = [arr.get_shape()[0] * repeats] |
| 166 |
w_res = W_NDimArray.from_shape(space, shape, arr.get_dtype(), w_instance=arr) |
| 167 |
for i in range(repeats): |
| 168 |
chunks = [Chunk(i, shape[0] - repeats + i, repeats, orig_size)] |
| 169 |
view = new_view(space, w_res, chunks) |
| 170 |
view.implementation.setslice(space, arr) |
| 171 |
else: |
| 172 |
axis = space.int_w(w_axis) |
| 173 |
shape = arr.get_shape()[:] |
| 174 |
chunks = [Chunk(0, i, 1, i) for i in shape] |
| 175 |
orig_size = shape[axis] |
| 176 |
shape[axis] *= repeats |
| 177 |
w_res = W_NDimArray.from_shape(space, shape, arr.get_dtype(), w_instance=arr) |
| 178 |
for i in range(repeats): |
| 179 |
chunks[axis] = Chunk(i, shape[axis] - repeats + i, repeats, |
| 180 |
orig_size) |
| 181 |
view = new_view(space, w_res, chunks) |
| 182 |
view.implementation.setslice(space, arr) |
| 183 |
return w_res |
| 184 |
|
| 185 |
|
| 186 |
def count_nonzero(space, w_obj): |
| 187 |
return space.newint(loop.count_all_true(convert_to_array(space, w_obj))) |
| 188 |
|
| 189 |
|
| 190 |
def choose(space, w_arr, w_choices, w_out, w_mode): |
| 191 |
arr = convert_to_array(space, w_arr) |
| 192 |
choices = [convert_to_array(space, w_item) for w_item |
| 193 |
in space.listview(w_choices)] |
| 194 |
if not choices: |
| 195 |
raise oefmt(space.w_ValueError, "choices list cannot be empty") |
| 196 |
if space.is_none(w_out): |
| 197 |
w_out = None |
| 198 |
elif not isinstance(w_out, W_NDimArray): |
| 199 |
raise oefmt(space.w_TypeError, "return arrays must be of ArrayType") |
| 200 |
shape = shape_agreement_multiple(space, choices + [w_out]) |
| 201 |
out = descriptor.dtype_agreement(space, choices, shape, w_out) |
| 202 |
dtype = out.get_dtype() |
| 203 |
mode = clipmode_converter(space, w_mode) |
| 204 |
loop.choose(space, arr, choices, shape, dtype, out, mode) |
| 205 |
return out |
| 206 |
|
| 207 |
|
| 208 |
def put(space, w_arr, w_indices, w_values, w_mode): |
| 209 |
arr = convert_to_array(space, w_arr) |
| 210 |
mode = clipmode_converter(space, w_mode) |
| 211 |
|
| 212 |
if not w_indices: |
| 213 |
raise oefmt(space.w_ValueError, "indices list cannot be empty") |
| 214 |
if not w_values: |
| 215 |
raise oefmt(space.w_ValueError, "value list cannot be empty") |
| 216 |
|
| 217 |
dtype = arr.get_dtype() |
| 218 |
|
| 219 |
if space.isinstance_w(w_indices, space.w_list): |
| 220 |
indices = space.listview(w_indices) |
| 221 |
else: |
| 222 |
indices = [w_indices] |
| 223 |
|
| 224 |
if space.isinstance_w(w_values, space.w_list): |
| 225 |
values = space.listview(w_values) |
| 226 |
else: |
| 227 |
values = [w_values] |
| 228 |
|
| 229 |
v_idx = 0 |
| 230 |
for idx in indices: |
| 231 |
index = support.index_w(space, idx) |
| 232 |
|
| 233 |
if index < 0 or index >= arr.get_size(): |
| 234 |
if mode == NPY.RAISE: |
| 235 |
raise oefmt(space.w_IndexError, |
| 236 |
"index %d is out of bounds for axis 0 with size %d", |
| 237 |
index, arr.get_size()) |
| 238 |
elif mode == NPY.WRAP: |
| 239 |
index = index % arr.get_size() |
| 240 |
elif mode == NPY.CLIP: |
| 241 |
if index < 0: |
| 242 |
index = 0 |
| 243 |
else: |
| 244 |
index = arr.get_size() - 1 |
| 245 |
else: |
| 246 |
assert False |
| 247 |
|
| 248 |
value = values[v_idx] |
| 249 |
|
| 250 |
if v_idx + 1 < len(values): |
| 251 |
v_idx += 1 |
| 252 |
|
| 253 |
arr.setitem(space, [index], dtype.coerce(space, value)) |
| 254 |
|
| 255 |
|
| 256 |
def diagonal(space, arr, offset, axis1, axis2): |
| 257 |
shape = arr.get_shape() |
| 258 |
shapelen = len(shape) |
| 259 |
if offset < 0: |
| 260 |
offset = -offset |
| 261 |
axis1, axis2 = axis2, axis1 |
| 262 |
size = min(shape[axis1], shape[axis2] - offset) |
| 263 |
dtype = arr.dtype |
| 264 |
if axis1 < axis2: |
| 265 |
shape = (shape[:axis1] + shape[axis1 + 1:axis2] + |
| 266 |
shape[axis2 + 1:] + [size]) |
| 267 |
else: |
| 268 |
shape = (shape[:axis2] + shape[axis2 + 1:axis1] + |
| 269 |
shape[axis1 + 1:] + [size]) |
| 270 |
out = W_NDimArray.from_shape(space, shape, dtype) |
| 271 |
if size == 0: |
| 272 |
return out |
| 273 |
if shapelen == 2: |
| 274 |
|
| 275 |
loop.diagonal_simple(space, arr, out, offset, axis1, axis2, size) |
| 276 |
else: |
| 277 |
loop.diagonal_array(space, arr, out, offset, axis1, axis2, shape) |
| 278 |
return out |