本文整理汇总了Python中pypy.module.micronumpy.base.W_NDimArray.from_shape方法的典型用法代码示例。如果您正苦于以下问题:Python W_NDimArray.from_shape方法的具体用法?Python W_NDimArray.from_shape怎么用?Python W_NDimArray.from_shape使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pypy.module.micronumpy.base.W_NDimArray
的用法示例。
在下文中一共展示了W_NDimArray.from_shape方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: zeros
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def zeros(space, w_shape, w_dtype=None, w_order=None):
dtype = space.interp_w(descriptor.W_Dtype,
space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
if dtype.is_str_or_unicode() and dtype.elsize < 1:
dtype = descriptor.variable_dtype(space, dtype.char + '1')
shape = shape_converter(space, w_shape, dtype)
return W_NDimArray.from_shape(space, shape, dtype=dtype)
示例2: numpify
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def numpify(space, w_object):
"""Convert the object to a W_NumpyObject"""
# XXX: code duplication with _array()
from pypy.module.micronumpy import strides
if isinstance(w_object, W_NumpyObject):
return w_object
# for anything that isn't already an array, try __array__ method first
w_array = try_array_method(space, w_object)
if w_array is not None:
return w_array
shape, elems_w = strides.find_shape_and_elems(space, w_object, None)
dtype = find_dtype_for_seq(space, elems_w, None)
if dtype is None:
dtype = descriptor.get_dtype_cache(space).w_float64dtype
elif dtype.is_str_or_unicode() and dtype.elsize < 1:
# promote S0 -> S1, U0 -> U1
dtype = descriptor.variable_dtype(space, dtype.char + '1')
if len(elems_w) == 1:
return dtype.coerce(space, elems_w[0])
else:
w_arr = W_NDimArray.from_shape(space, shape, dtype)
loop.assign(space, w_arr, elems_w)
return w_arr
示例3: accumulate
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def accumulate(space, func, w_arr, axis, calc_dtype, w_out, identity):
out_iter, out_state = w_out.create_iter()
arr_shape = w_arr.get_shape()
temp_shape = arr_shape[:axis] + arr_shape[axis + 1:]
temp = W_NDimArray.from_shape(space, temp_shape, calc_dtype, w_instance=w_arr)
temp_iter = AxisIter(temp.implementation, w_arr.get_shape(), axis)
temp_state = temp_iter.reset()
arr_iter, arr_state = w_arr.create_iter()
arr_iter.track_index = False
if identity is not None:
identity = identity.convert_to(space, calc_dtype)
shapelen = len(arr_shape)
while not out_iter.done(out_state):
accumulate_driver.jit_merge_point(shapelen=shapelen, func=func,
calc_dtype=calc_dtype)
w_item = arr_iter.getitem(arr_state).convert_to(space, calc_dtype)
arr_state = arr_iter.next(arr_state)
out_indices = out_iter.indices(out_state)
if out_indices[axis] == 0:
if identity is not None:
w_item = func(calc_dtype, identity, w_item)
else:
cur_value = temp_iter.getitem(temp_state)
w_item = func(calc_dtype, cur_value, w_item)
out_iter.setitem(out_state, w_item)
out_state = out_iter.next(out_state)
temp_iter.setitem(temp_state, w_item)
temp_state = temp_iter.next(temp_state)
return w_out
示例4: call2
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def call2(space, shape, func, calc_dtype, res_dtype, w_lhs, w_rhs, out):
# handle array_priority
# w_lhs and w_rhs could be of different ndarray subtypes. Numpy does:
# 1. if __array_priorities__ are equal and one is an ndarray and the
# other is a subtype, flip the order
# 2. elif rhs.__array_priority__ is higher, flip the order
# Now return the subtype of the first one
w_ndarray = space.gettypefor(W_NDimArray)
lhs_type = space.type(w_lhs)
rhs_type = space.type(w_rhs)
lhs_for_subtype = w_lhs
rhs_for_subtype = w_rhs
#it may be something like a FlatIter, which is not an ndarray
if not space.is_true(space.issubtype(lhs_type, w_ndarray)):
lhs_type = space.type(w_lhs.base)
lhs_for_subtype = w_lhs.base
if not space.is_true(space.issubtype(rhs_type, w_ndarray)):
rhs_type = space.type(w_rhs.base)
rhs_for_subtype = w_rhs.base
if space.is_w(lhs_type, w_ndarray) and not space.is_w(rhs_type, w_ndarray):
lhs_for_subtype = rhs_for_subtype
# TODO handle __array_priorities__ and maybe flip the order
if w_lhs.get_size() == 1:
w_left = w_lhs.get_scalar_value().convert_to(space, calc_dtype)
left_iter = left_state = None
else:
w_left = None
left_iter, left_state = w_lhs.create_iter(shape)
left_iter.track_index = False
if w_rhs.get_size() == 1:
w_right = w_rhs.get_scalar_value().convert_to(space, calc_dtype)
right_iter = right_state = None
else:
w_right = None
right_iter, right_state = w_rhs.create_iter(shape)
right_iter.track_index = False
if out is None:
out = W_NDimArray.from_shape(space, shape, res_dtype,
w_instance=lhs_for_subtype)
out_iter, out_state = out.create_iter(shape)
shapelen = len(shape)
while not out_iter.done(out_state):
call2_driver.jit_merge_point(shapelen=shapelen, func=func,
calc_dtype=calc_dtype, res_dtype=res_dtype)
if left_iter:
w_left = left_iter.getitem(left_state).convert_to(space, calc_dtype)
left_state = left_iter.next(left_state)
if right_iter:
w_right = right_iter.getitem(right_state).convert_to(space, calc_dtype)
right_state = right_iter.next(right_state)
out_iter.setitem(out_state, func(calc_dtype, w_left, w_right).convert_to(
space, res_dtype))
out_state = out_iter.next(out_state)
return out
示例5: nonzero
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def nonzero(self, space, index_type):
s = loop.count_all_true_concrete(self)
box = index_type.itemtype.box
nd = len(self.get_shape()) or 1
w_res = W_NDimArray.from_shape(space, [s, nd], index_type)
loop.nonzero(w_res, self, box)
w_res = w_res.implementation.swapaxes(space, w_res, 0, 1)
l_w = [w_res.descr_getitem(space, space.wrap(d)) for d in range(nd)]
return space.newtuple(l_w)
示例6: set_shape
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def set_shape(self, space, orig_array, new_shape):
if not new_shape:
return self
if support.product(new_shape) == 1:
arr = W_NDimArray.from_shape(space, new_shape, self.dtype)
arr_iter = arr.create_iter(new_shape)
arr_iter.setitem(self.value)
return arr.implementation
raise OperationError(space.w_ValueError, space.wrap("total size of the array must be unchanged"))
示例7: concatenate
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
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: empty_like
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def empty_like(space, w_a, w_dtype=None, w_order=None, subok=True):
w_a = convert_to_array(space, w_a)
if space.is_none(w_dtype):
dtype = w_a.get_dtype()
else:
dtype = space.interp_w(descriptor.W_Dtype,
space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
if dtype.is_str_or_unicode() and dtype.elsize < 1:
dtype = descriptor.variable_dtype(space, dtype.char + '1')
return W_NDimArray.from_shape(space, w_a.get_shape(), dtype=dtype,
w_instance=w_a if subok else None)
示例9: tostring
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def tostring(space, arr):
builder = StringBuilder()
iter = arr.create_iter()
w_res_str = W_NDimArray.from_shape(space, [1], arr.get_dtype(), order='C')
itemsize = arr.get_dtype().itemtype.get_element_size()
res_str_casted = rffi.cast(rffi.CArrayPtr(lltype.Char),
w_res_str.implementation.get_storage_as_int(space))
while not iter.done():
w_res_str.implementation.setitem(0, iter.getitem())
for i in range(itemsize):
builder.append(res_str_casted[i])
iter.next()
return builder.build()
示例10: tostring
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def tostring(space, arr):
builder = StringBuilder()
iter, state = arr.create_iter()
w_res_str = W_NDimArray.from_shape(space, [1], arr.get_dtype(), order="C")
itemsize = arr.get_dtype().elsize
with w_res_str.implementation as storage:
res_str_casted = rffi.cast(rffi.CArrayPtr(lltype.Char), support.get_storage_as_int(storage))
while not iter.done(state):
w_res_str.implementation.setitem(0, iter.getitem(state))
for i in range(itemsize):
builder.append(res_str_casted[i])
state = iter.next(state)
return builder.build()
示例11: repeat
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def repeat(space, w_arr, repeats, w_axis):
arr = convert_to_array(space, w_arr)
if space.is_none(w_axis):
arr = arr.descr_flatten(space)
orig_size = arr.get_shape()[0]
shape = [arr.get_shape()[0] * repeats]
w_res = W_NDimArray.from_shape(space, shape, arr.get_dtype(), w_instance=arr)
for i in range(repeats):
Chunks([Chunk(i, shape[0] - repeats + i, repeats, orig_size)]).apply(space, w_res).implementation.setslice(
space, arr
)
else:
axis = space.int_w(w_axis)
shape = arr.get_shape()[:]
chunks = [Chunk(0, i, 1, i) for i in shape]
orig_size = shape[axis]
shape[axis] *= repeats
w_res = W_NDimArray.from_shape(space, shape, arr.get_dtype(), w_instance=arr)
for i in range(repeats):
chunks[axis] = Chunk(i, shape[axis] - repeats + i, repeats, orig_size)
Chunks(chunks).apply(space, w_res).implementation.setslice(space, arr)
return w_res
示例12: descr_getitem
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def descr_getitem(self, space, w_idx):
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')
self.reset()
base = self.base
start, stop, step, length = space.decode_index4(w_idx, base.get_size())
base_iter, base_state = base.create_iter()
base_state = base_iter.next_skip_x(base_state, start)
if length == 1:
return base_iter.getitem(base_state)
res = W_NDimArray.from_shape(space, [length], base.get_dtype(),
base.get_order(), w_instance=base)
return loop.flatiter_getitem(res, base_iter, base_state, step)
示例13: call1
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def call1(space, shape, func, calc_dtype, res_dtype, w_obj, out):
if out is None:
out = W_NDimArray.from_shape(space, shape, res_dtype, w_instance=w_obj)
obj_iter, obj_state = w_obj.create_iter(shape)
out_iter, out_state = out.create_iter(shape)
shapelen = len(shape)
while not out_iter.done(out_state):
call1_driver.jit_merge_point(shapelen=shapelen, func=func,
calc_dtype=calc_dtype, res_dtype=res_dtype)
elem = obj_iter.getitem(obj_state).convert_to(space, calc_dtype)
out_iter.setitem(out_state, func(calc_dtype, elem).convert_to(space, res_dtype))
out_state = out_iter.next(out_state)
obj_state = obj_iter.next(obj_state)
return out
示例14: _fromstring_text
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def _fromstring_text(space, s, count, sep, length, dtype):
sep_stripped = strip_spaces(sep)
skip_bad_vals = len(sep_stripped) == 0
items = []
num_items = 0
idx = 0
while (num_items < count or count == -1) and idx < len(s):
nextidx = s.find(sep, idx)
if nextidx < 0:
nextidx = length
piece = strip_spaces(s[idx:nextidx])
if len(piece) > 0 or not skip_bad_vals:
if len(piece) == 0 and not skip_bad_vals:
val = dtype.itemtype.default_fromstring(space)
else:
try:
val = dtype.coerce(space, space.wrap(piece))
except OperationError as e:
if not e.match(space, space.w_ValueError):
raise
gotit = False
while not gotit and len(piece) > 0:
piece = piece[:-1]
try:
val = dtype.coerce(space, space.wrap(piece))
gotit = True
except OperationError as e:
if not e.match(space, space.w_ValueError):
raise
if not gotit:
val = dtype.itemtype.default_fromstring(space)
nextidx = length
items.append(val)
num_items += 1
idx = nextidx + 1
if count > num_items:
raise oefmt(space.w_ValueError,
"string is smaller than requested size")
a = W_NDimArray.from_shape(space, [num_items], dtype=dtype)
ai, state = a.create_iter()
for val in items:
ai.setitem(state, val)
state = ai.next(state)
return space.wrap(a)
示例15: _zeros_or_empty
# 需要导入模块: from pypy.module.micronumpy.base import W_NDimArray [as 别名]
# 或者: from pypy.module.micronumpy.base.W_NDimArray import from_shape [as 别名]
def _zeros_or_empty(space, w_shape, w_dtype, w_order, zero):
dtype = space.interp_w(descriptor.W_Dtype,
space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
if dtype.is_str_or_unicode() and dtype.elsize < 1:
dtype = descriptor.variable_dtype(space, dtype.char + '1')
shape = shape_converter(space, w_shape, dtype)
for dim in shape:
if dim < 0:
raise OperationError(space.w_ValueError, space.wrap(
"negative dimensions are not allowed"))
try:
support.product_check(shape)
except OverflowError:
raise oefmt(space.w_ValueError, "array is too big.")
return W_NDimArray.from_shape(space, shape, dtype=dtype, zero=zero)