本文整理汇总了Python中pypy.module.micronumpy.base.convert_to_array函数的典型用法代码示例。如果您正苦于以下问题:Python convert_to_array函数的具体用法?Python convert_to_array怎么用?Python convert_to_array使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了convert_to_array函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: choose
def choose(space, w_arr, w_choices, w_out, w_mode):
arr = convert_to_array(space, w_arr)
choices = [convert_to_array(space, w_item) for w_item in space.listview(w_choices)]
if not choices:
raise OperationError(space.w_ValueError, space.wrap("choices list cannot be empty"))
if space.is_none(w_out):
w_out = None
elif not isinstance(w_out, W_NDimArray):
raise OperationError(space.w_TypeError, space.wrap("return arrays must be of ArrayType"))
shape = shape_agreement_multiple(space, choices + [w_out])
out = interp_dtype.dtype_agreement(space, choices, shape, w_out)
dtype = out.get_dtype()
mode = clipmode_converter(space, w_mode)
loop.choose(space, arr, choices, shape, dtype, out, mode)
return out
示例2: __init__
def __init__(self, space, args):
num_args = len(args)
if not (2 <= num_args <= NPY.MAXARGS):
raise oefmt(space.w_ValueError,
"Need at least two and fewer than (%d) array objects.", NPY.MAXARGS)
self.seq = [convert_to_array(space, w_elem)
for w_elem in args]
self.op_flags = parse_op_arg(space, 'op_flags', space.w_None,
len(self.seq), parse_op_flag)
self.shape = shape_agreement_multiple(space, self.seq, shape=None)
self.order = NPY.CORDER
self.iters = []
self.index = 0
try:
self.size = support.product_check(self.shape)
except OverflowError as e:
raise oefmt(space.w_ValueError, "broadcast dimensions too large.")
for i in range(len(self.seq)):
it = self.get_iter(space, i)
it.contiguous = False
self.iters.append((it, it.reset()))
self.done = False
pass
示例3: descr_setitem
def descr_setitem(self, space, w_idx, w_value):
if not (space.isinstance_w(w_idx, space.w_int) or
space.isinstance_w(w_idx, space.w_slice)):
raise oefmt(space.w_IndexError, 'unsupported iterator index')
base = self.base
start, stop, step, length = space.decode_index4(w_idx, base.get_size())
arr = convert_to_array(space, w_value)
loop.flatiter_setitem(space, self.base, arr, start, step, length)
示例4: min_scalar_type
def min_scalar_type(space, w_a):
w_array = convert_to_array(space, w_a)
dtype = w_array.get_dtype()
if w_array.is_scalar() and dtype.is_number():
num, alt_num = w_array.get_scalar_value().min_dtype()
return num2dtype(space, num)
else:
return dtype
示例5: descr_getitem
def descr_getitem(self, space, w_item):
from pypy.module.micronumpy.base import convert_to_array
if space.is_w(w_item, space.w_Ellipsis) or \
(space.isinstance_w(w_item, space.w_tuple) and
space.len_w(w_item) == 0):
return convert_to_array(space, self)
raise OperationError(space.w_IndexError, space.wrap(
"invalid index to scalar variable"))
示例6: descr_setitem
def descr_setitem(self, space, orig_arr, w_index, w_value):
try:
item = self._single_item_index(space, w_index)
self.setitem(item, self.dtype.coerce(space, w_value))
except IndexError:
w_value = convert_to_array(space, w_value)
chunks = self._prepare_slice_args(space, w_index)
view = chunks.apply(space, orig_arr)
view.implementation.setslice(space, w_value)
示例7: concatenate
def concatenate(space, w_args, w_axis=None):
args_w = space.listview(w_args)
if len(args_w) == 0:
raise oefmt(space.w_ValueError, "need at least one array to concatenate")
args_w = [convert_to_array(space, w_arg) for w_arg in args_w]
if w_axis is None:
w_axis = space.wrap(0)
if space.is_none(w_axis):
args_w = [w_arg.reshape(space,
space.newlist([w_arg.descr_get_size(space)]),
w_arg.get_order())
for w_arg in args_w]
w_axis = space.wrap(0)
dtype = args_w[0].get_dtype()
shape = args_w[0].get_shape()[:]
ndim = len(shape)
if ndim == 0:
raise oefmt(space.w_ValueError,
"zero-dimensional arrays cannot be concatenated")
axis = space.int_w(w_axis)
orig_axis = axis
if axis < 0:
axis = ndim + axis
if ndim == 1 and axis != 0:
axis = 0
if axis < 0 or axis >= ndim:
raise oefmt(space.w_IndexError, "axis %d out of bounds [0, %d)",
orig_axis, ndim)
for arr in args_w[1:]:
if len(arr.get_shape()) != ndim:
raise oefmt(space.w_ValueError,
"all the input arrays must have same number of "
"dimensions")
for i, axis_size in enumerate(arr.get_shape()):
if i == axis:
shape[i] += axis_size
elif axis_size != shape[i]:
raise oefmt(space.w_ValueError,
"all the input array dimensions except for the "
"concatenation axis must match exactly")
dtype = find_result_type(space, args_w, [])
# concatenate does not handle ndarray subtypes, it always returns a ndarray
res = W_NDimArray.from_shape(space, shape, dtype, NPY.CORDER)
chunks = [Chunk(0, i, 1, i) for i in shape]
axis_start = 0
for arr in args_w:
if arr.get_shape()[axis] == 0:
continue
chunks[axis] = Chunk(axis_start, axis_start + arr.get_shape()[axis], 1,
arr.get_shape()[axis])
view = new_view(space, res, chunks)
view.implementation.setslice(space, arr)
axis_start += arr.get_shape()[axis]
return res
示例8: call
def call(self, space, args_w):
w_obj = args_w[0]
out = None
if len(args_w) > 1:
out = args_w[1]
if space.is_w(out, space.w_None):
out = None
w_obj = convert_to_array(space, w_obj)
dtype = w_obj.get_dtype()
if dtype.is_flexible_type():
raise OperationError(space.w_TypeError,
space.wrap('Not implemented for this type'))
if (self.int_only and not dtype.is_int_type() or
not self.allow_bool and dtype.is_bool_type() or
not self.allow_complex and dtype.is_complex_type()):
raise OperationError(space.w_TypeError, space.wrap(
"ufunc %s not supported for the input type" % self.name))
calc_dtype = find_unaryop_result_dtype(space,
w_obj.get_dtype(),
promote_to_float=self.promote_to_float,
promote_bools=self.promote_bools)
if out is not None:
if not isinstance(out, W_NDimArray):
raise OperationError(space.w_TypeError, space.wrap(
'output must be an array'))
res_dtype = out.get_dtype()
#if not w_obj.get_dtype().can_cast_to(res_dtype):
# raise operationerrfmt(space.w_TypeError,
# "Cannot cast ufunc %s output from dtype('%s') to dtype('%s') with casting rule 'same_kind'", self.name, w_obj.get_dtype().name, res_dtype.name)
elif self.bool_result:
res_dtype = interp_dtype.get_dtype_cache(space).w_booldtype
else:
res_dtype = calc_dtype
if self.complex_to_float and calc_dtype.is_complex_type():
if calc_dtype.name == 'complex64':
res_dtype = interp_dtype.get_dtype_cache(space).w_float32dtype
else:
res_dtype = interp_dtype.get_dtype_cache(space).w_float64dtype
if w_obj.is_scalar():
w_val = self.func(calc_dtype,
w_obj.get_scalar_value().convert_to(calc_dtype))
if out is None:
return w_val
if out.is_scalar():
out.set_scalar_value(w_val)
else:
out.fill(res_dtype.coerce(space, w_val))
return out
shape = shape_agreement(space, w_obj.get_shape(), out,
broadcast_down=False)
return loop.call1(space, shape, self.func, calc_dtype, res_dtype,
w_obj, out)
示例9: put
def put(space, w_arr, w_indices, w_values, mode='raise'):
from pypy.module.micronumpy import constants
from pypy.module.micronumpy.support import int_w
arr = convert_to_array(space, w_arr)
if mode not in constants.MODES:
raise OperationError(space.w_ValueError,
space.wrap("mode %s not known" % (mode,)))
if not w_indices:
raise OperationError(space.w_ValueError,
space.wrap("indice list cannot be empty"))
if not w_values:
raise OperationError(space.w_ValueError,
space.wrap("value list cannot be empty"))
dtype = arr.get_dtype()
if space.isinstance_w(w_indices, space.w_list):
indices = space.listview(w_indices)
else:
indices = [w_indices]
if space.isinstance_w(w_values, space.w_list):
values = space.listview(w_values)
else:
values = [w_values]
v_idx = 0
for idx in indices:
index = int_w(space, idx)
if index < 0 or index >= arr.get_size():
if constants.MODES[mode] == constants.MODE_RAISE:
raise OperationError(space.w_ValueError, space.wrap(
"invalid entry in choice array"))
elif constants.MODES[mode] == constants.MODE_WRAP:
index = index % arr.get_size()
else:
assert constants.MODES[mode] == constants.MODE_CLIP
if index < 0:
index = 0
else:
index = arr.get_size() - 1
value = values[v_idx]
if v_idx + 1 < len(values):
v_idx += 1
arr.setitem(space, [index], dtype.coerce(space, value))
示例10: set_real
def set_real(self, space, orig_array, w_val):
w_arr = convert_to_array(space, w_val)
dtype = self.dtype.float_type or self.dtype
if len(w_arr.get_shape()) > 0:
raise OperationError(space.w_ValueError, space.wrap(
"could not broadcast input array from shape " +
"(%s) into shape ()" % (
','.join([str(x) for x in w_arr.get_shape()],))))
if self.dtype.is_complex_type():
self.value = self.dtype.itemtype.composite(
w_arr.get_scalar_value().convert_to(dtype),
self.value.convert_imag_to(dtype))
else:
self.value = w_arr.get_scalar_value()
示例11: put
def put(space, w_arr, w_indices, w_values, w_mode):
from pypy.module.micronumpy.support import index_w
arr = convert_to_array(space, w_arr)
mode = clipmode_converter(space, w_mode)
if not w_indices:
raise OperationError(space.w_ValueError, space.wrap("indice list cannot be empty"))
if not w_values:
raise OperationError(space.w_ValueError, space.wrap("value list cannot be empty"))
dtype = arr.get_dtype()
if space.isinstance_w(w_indices, space.w_list):
indices = space.listview(w_indices)
else:
indices = [w_indices]
if space.isinstance_w(w_values, space.w_list):
values = space.listview(w_values)
else:
values = [w_values]
v_idx = 0
for idx in indices:
index = index_w(space, idx)
if index < 0 or index >= arr.get_size():
if mode == NPY_RAISE:
raise OperationError(
space.w_IndexError,
space.wrap("index %d is out of bounds for axis 0 with size %d" % (index, arr.get_size())),
)
elif mode == NPY_WRAP:
index = index % arr.get_size()
elif mode == NPY_CLIP:
if index < 0:
index = 0
else:
index = arr.get_size() - 1
else:
assert False
value = values[v_idx]
if v_idx + 1 < len(values):
v_idx += 1
arr.setitem(space, [index], dtype.coerce(space, value))
示例12: set_imag
def set_imag(self, space, orig_array, w_val):
# Only called on complex dtype
assert self.dtype.is_complex_type()
w_arr = convert_to_array(space, w_val)
dtype = self.dtype.float_type
if len(w_arr.get_shape()) > 0:
raise OperationError(
space.w_ValueError,
space.wrap(
"could not broadcast input array from shape "
+ "(%s) into shape ()" % (",".join([str(x) for x in w_arr.get_shape()]))
),
)
self.value = self.dtype.itemtype.composite(
self.value.convert_real_to(dtype), w_arr.get_scalar_value().convert_to(dtype)
)
示例13: call_many_to_many
def call_many_to_many(space, shape, func, in_dtypes, out_dtypes, in_args, out_args):
# out must hav been built. func needs no calc_type, is usually an
# external ufunc
nin = len(in_args)
in_iters = [None] * nin
in_states = [None] * nin
nout = len(out_args)
out_iters = [None] * nout
out_states = [None] * nout
for i in range(nin):
in_i = in_args[i]
assert isinstance(in_i, W_NDimArray)
in_iter, in_state = in_i.create_iter(shape)
in_iters[i] = in_iter
in_states[i] = in_state
for i in range(nout):
out_i = out_args[i]
assert isinstance(out_i, W_NDimArray)
out_iter, out_state = out_i.create_iter(shape)
out_iters[i] = out_iter
out_states[i] = out_state
shapelen = len(shape)
vals = [None] * nin
test_iter, test_state = in_iters[-1], in_states[-1]
if nout > 0:
test_iter, test_state = out_iters[0], out_states[0]
while not test_iter.done(test_state):
call_many_to_many_driver.jit_merge_point(shapelen=shapelen, func=func,
in_dtypes=in_dtypes, out_dtypes=out_dtypes,
nin=nin, nout=nout)
for i in range(nin):
vals[i] = in_dtypes[i].coerce(space, in_iters[i].getitem(in_states[i]))
w_arglist = space.newlist(vals)
w_outvals = space.call_args(func, Arguments.frompacked(space, w_arglist))
# w_outvals should be a tuple, but func can return a single value as well
if space.isinstance_w(w_outvals, space.w_tuple):
batch = space.listview(w_outvals)
for i in range(len(batch)):
out_iters[i].setitem(out_states[i], out_dtypes[i].coerce(space, batch[i]))
out_states[i] = out_iters[i].next(out_states[i])
elif nout > 0:
out_iters[0].setitem(out_states[0], out_dtypes[0].coerce(space, w_outvals))
out_states[0] = out_iters[0].next(out_states[0])
for i in range(nin):
in_states[i] = in_iters[i].next(in_states[i])
test_state = test_iter.next(test_state)
return space.newtuple([convert_to_array(space, o) for o in out_args])
示例14: put
def put(space, w_arr, w_indices, w_values, w_mode):
arr = convert_to_array(space, w_arr)
mode = clipmode_converter(space, w_mode)
if not w_indices:
raise oefmt(space.w_ValueError, "indices list cannot be empty")
if not w_values:
raise oefmt(space.w_ValueError, "value list cannot be empty")
dtype = arr.get_dtype()
if space.isinstance_w(w_indices, space.w_list):
indices = space.listview(w_indices)
else:
indices = [w_indices]
if space.isinstance_w(w_values, space.w_list):
values = space.listview(w_values)
else:
values = [w_values]
v_idx = 0
for idx in indices:
index = support.index_w(space, idx)
if index < 0 or index >= arr.get_size():
if mode == NPY.RAISE:
raise oefmt(space.w_IndexError,
"index %d is out of bounds for axis 0 with size %d",
index, arr.get_size())
elif mode == NPY.WRAP:
index = index % arr.get_size()
elif mode == NPY.CLIP:
if index < 0:
index = 0
else:
index = arr.get_size() - 1
else:
assert False
value = values[v_idx]
if v_idx + 1 < len(values):
v_idx += 1
arr.setitem(space, [index], dtype.coerce(space, value))
示例15: descr_setitem
def descr_setitem(self, space, w_idx, w_value):
if not (space.isinstance_w(w_idx, space.w_int) or space.isinstance_w(w_idx, space.w_slice)):
raise oefmt(space.w_IndexError, "unsupported iterator index")
start, stop, step, length = space.decode_index4(w_idx, self.iter.size)
try:
state = self.iter.goto(start)
dtype = self.base.get_dtype()
if length == 1:
try:
val = dtype.coerce(space, w_value)
except OperationError:
raise oefmt(space.w_ValueError, "Error setting single item of array.")
self.iter.setitem(state, val)
return
arr = convert_to_array(space, w_value)
loop.flatiter_setitem(space, dtype, arr, self.iter, state, step, length)
finally:
self.iter.reset(self.state, mutate=True)