本文整理汇总了Python中pypy.module.micronumpy.base.W_NDimArray类的典型用法代码示例。如果您正苦于以下问题:Python W_NDimArray类的具体用法?Python W_NDimArray怎么用?Python W_NDimArray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了W_NDimArray类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: execute
def execute(self, interp):
w_lhs = self.lhs.execute(interp)
if isinstance(self.rhs, SliceConstant):
w_rhs = self.rhs.wrap(interp.space)
else:
w_rhs = self.rhs.execute(interp)
if not isinstance(w_lhs, W_NDimArray):
# scalar
dtype = get_dtype_cache(interp.space).w_float64dtype
w_lhs = W_NDimArray.new_scalar(interp.space, dtype, w_lhs)
assert isinstance(w_lhs, W_NDimArray)
if self.name == '+':
w_res = w_lhs.descr_add(interp.space, w_rhs)
elif self.name == '*':
w_res = w_lhs.descr_mul(interp.space, w_rhs)
elif self.name == '-':
w_res = w_lhs.descr_sub(interp.space, w_rhs)
elif self.name == '->':
if isinstance(w_rhs, FloatObject):
w_rhs = IntObject(int(w_rhs.floatval))
assert isinstance(w_lhs, W_NDimArray)
w_res = w_lhs.descr_getitem(interp.space, w_rhs)
else:
raise NotImplementedError
if (not isinstance(w_res, W_NDimArray) and
not isinstance(w_res, interp_boxes.W_GenericBox)):
dtype = get_dtype_cache(interp.space).w_float64dtype
w_res = W_NDimArray.new_scalar(interp.space, dtype, w_res)
return w_res
示例2: frombuffer
def frombuffer(space, w_buffer, w_dtype=None, count=-1, offset=0):
dtype = space.interp_w(descriptor.W_Dtype,
space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
if dtype.elsize == 0:
raise oefmt(space.w_ValueError, "itemsize cannot be zero in type")
try:
buf = _getbuffer(space, w_buffer)
except OperationError as e:
if not e.match(space, space.w_TypeError):
raise
w_buffer = space.call_method(w_buffer, '__buffer__',
space.newint(space.BUF_FULL_RO))
buf = _getbuffer(space, w_buffer)
ts = buf.getlength()
if offset < 0 or offset > ts:
raise oefmt(space.w_ValueError,
"offset must be non-negative and no greater than "
"buffer length (%d)", ts)
s = ts - offset
if offset:
buf = SubBuffer(buf, offset, s)
n = count
itemsize = dtype.elsize
assert itemsize > 0
if n < 0:
if s % itemsize != 0:
raise oefmt(space.w_ValueError,
"buffer size must be a multiple of element size")
n = s / itemsize
else:
if s < n * itemsize:
raise oefmt(space.w_ValueError,
"buffer is smaller than requested size")
try:
storage = buf.get_raw_address()
except ValueError:
a = W_NDimArray.from_shape(space, [n], dtype=dtype)
loop.fromstring_loop(space, a, dtype, itemsize, buf.as_str())
return a
else:
writable = not buf.readonly
return W_NDimArray.from_shape_and_storage(space, [n], storage, storage_bytes=s,
dtype=dtype, w_base=w_buffer, writable=writable)
示例3: apply
def apply(self, space, orig_arr):
arr = orig_arr.implementation
ofs, subdtype = arr.dtype.fields[self.name]
# strides backstrides are identical, ofs only changes start
return W_NDimArray.new_slice(space, arr.start + ofs, arr.get_strides(),
arr.get_backstrides(),
arr.shape, arr, orig_arr, subdtype)
示例4: zeros
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)
示例5: accumulate
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
示例6: numpify
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
示例7: new_view
def new_view(space, w_arr, chunks):
arr = w_arr.implementation
r = calculate_slice_strides(space, arr.shape, arr.start, arr.get_strides(),
arr.get_backstrides(), chunks)
shape, start, strides, backstrides = r
return W_NDimArray.new_slice(space, start, strides[:], backstrides[:],
shape[:], arr, w_arr)
示例8: call2
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
示例9: apply
def apply(self, space, orig_arr):
arr = orig_arr.implementation
shape = self.extend_shape(arr.shape)
r = calculate_slice_strides(arr.shape, arr.start, arr.get_strides(),
arr.get_backstrides(), self.l)
_, start, strides, backstrides = r
return W_NDimArray.new_slice(space, start, strides[:], backstrides[:],
shape[:], arr, orig_arr)
示例10: swapaxes
def swapaxes(self, space, orig_arr, axis1, axis2):
shape = self.get_shape()[:]
strides = self.get_strides()[:]
backstrides = self.get_backstrides()[:]
shape[axis1], shape[axis2] = shape[axis2], shape[axis1]
strides[axis1], strides[axis2] = strides[axis2], strides[axis1]
backstrides[axis1], backstrides[axis2] = backstrides[axis2], backstrides[axis1]
return W_NDimArray.new_slice(space, self.start, strides,
backstrides, shape, self, orig_arr)
示例11: set_shape
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"))
示例12: nonzero
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)
示例13: 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
示例14: empty_like
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)
示例15: tostring
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()