本文整理汇总了Python中pypy.module.micronumpy.support.product函数的典型用法代码示例。如果您正苦于以下问题:Python product函数的具体用法?Python product怎么用?Python product使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了product函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_iter
def create_iter(self, shape=None, backward_broadcast=False):
if shape is not None and \
support.product(shape) > support.product(self.get_shape()):
r = calculate_broadcast_strides(self.get_strides(),
self.get_backstrides(),
self.get_shape(), shape,
backward_broadcast)
i = ArrayIter(self, support.product(shape), shape, r[0], r[1])
else:
i = ArrayIter(self, self.get_size(), self.shape,
self.strides, self.backstrides)
return i, i.reset()
示例2: from_shape_and_storage
def from_shape_and_storage(space, shape, storage, dtype, storage_bytes=-1,
order='C', owning=False, w_subtype=None,
w_base=None, writable=True, strides=None, start=0):
from pypy.module.micronumpy import concrete
from pypy.module.micronumpy.strides import (calc_strides,
calc_backstrides)
isize = dtype.elsize
if storage_bytes > 0 :
totalsize = support.product(shape) * isize
if totalsize > storage_bytes:
raise OperationError(space.w_TypeError, space.wrap(
"buffer is too small for requested array"))
else:
storage_bytes = support.product(shape) * isize
if strides is None:
strides, backstrides = calc_strides(shape, dtype, order)
else:
if len(strides) != len(shape):
raise oefmt(space.w_ValueError,
'strides, if given, must be the same length as shape')
for i in range(len(strides)):
if strides[i] < 0 or strides[i]*shape[i] > storage_bytes:
raise oefmt(space.w_ValueError,
'strides is incompatible with shape of requested '
'array and size of buffer')
backstrides = calc_backstrides(strides, shape)
if w_base is not None:
if owning:
raise OperationError(space.w_ValueError,
space.wrap("Cannot have owning=True when specifying a buffer"))
if writable:
impl = concrete.ConcreteArrayWithBase(shape, dtype, order,
strides, backstrides, storage, w_base,
start=start)
else:
impl = concrete.ConcreteNonWritableArrayWithBase(shape, dtype, order,
strides, backstrides,
storage, w_base)
elif owning:
# Will free storage when GCd
impl = concrete.ConcreteArray(shape, dtype, order, strides,
backstrides, storage=storage)
else:
impl = concrete.ConcreteArrayNotOwning(shape, dtype, order, strides,
backstrides, storage)
if w_subtype:
w_ret = space.allocate_instance(W_NDimArray, w_subtype)
W_NDimArray.__init__(w_ret, impl)
space.call_method(w_ret, '__array_finalize__', w_subtype)
return w_ret
return W_NDimArray(impl)
示例3: test_iterator_step
def test_iterator_step(self):
#iteration in C order with #contiguous layout => strides[-1] is 1
#skip less than the shape
shape = [3, 5]
strides = [5, 1]
backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
assert backstrides == [10, 4]
i = ArrayIter(MockArray, support.product(shape), shape,
strides, backstrides)
s = i.reset()
s = i.next_skip_x(s, 2)
s = i.next_skip_x(s, 2)
s = i.next_skip_x(s, 2)
assert s.offset == 6
assert not i.done(s)
assert s.indices == [1,1]
#And for some big skips
s = i.next_skip_x(s, 5)
assert s.offset == 11
assert s.indices == [2,1]
s = i.next_skip_x(s, 5)
# Note: the offset does not overflow but recycles,
# this is good for broadcast
assert s.offset == 1
assert s.indices == [0,1]
assert i.done(s)
#Now what happens if the array is transposed? strides[-1] != 1
# therefore layout is non-contiguous
strides = [1, 3]
backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
assert backstrides == [2, 12]
i = ArrayIter(MockArray, support.product(shape), shape,
strides, backstrides)
s = i.reset()
s = i.next_skip_x(s, 2)
s = i.next_skip_x(s, 2)
s = i.next_skip_x(s, 2)
assert s.offset == 4
assert s.indices == [1,1]
assert not i.done(s)
s = i.next_skip_x(s, 5)
assert s.offset == 5
assert s.indices == [2,1]
assert not i.done(s)
s = i.next_skip_x(s, 5)
assert s.indices == [0,1]
assert s.offset == 3
assert i.done(s)
示例4: create_iter
def create_iter(self, shape=None, backward_broadcast=False, require_index=False):
if shape is not None and \
support.product(shape) > support.product(self.get_shape()):
r = calculate_broadcast_strides(self.get_strides(),
self.get_backstrides(),
self.get_shape(), shape,
backward_broadcast)
return iter.MultiDimViewIterator(self.parent, self.dtype,
self.start, r[0], r[1], shape)
if len(self.get_shape()) == 1:
return iter.OneDimViewIterator(self.parent, self.dtype, self.start,
self.get_strides(), self.get_shape())
return iter.MultiDimViewIterator(self.parent, self.dtype, self.start,
self.get_strides(),
self.get_backstrides(), self.get_shape())
示例5: __init__
def __init__(self, shape, dtype, order, strides, backstrides,
storage=lltype.nullptr(RAW_STORAGE), zero=True):
if storage == lltype.nullptr(RAW_STORAGE):
storage = dtype.itemtype.malloc(support.product(shape) *
dtype.elsize, zero=zero)
ConcreteArrayNotOwning.__init__(self, shape, dtype, order, strides, backstrides,
storage)
示例6: get_iter
def get_iter(self, space, i):
arr = self.seq[i]
imp = arr.implementation
if arr.is_scalar():
return ConcreteIter(imp, 1, [], [], [], self.op_flags[i], self)
shape = self.shape
if self.external_loop and len(self.seq) < 2 and self.buffered:
# Special case, always return a memory-ordered iterator
stride = imp.dtype.elsize
backstride = imp.size * stride - stride
return ConcreteIter(
imp, imp.get_size(), [support.product(shape)], [stride], [backstride], self.op_flags[i], self
)
backward = imp.order != self.order
# XXX cleanup needed
strides = imp.strides
backstrides = imp.backstrides
if self.allow_backward:
if (abs(imp.strides[0]) < abs(imp.strides[-1]) and not backward) or (
abs(imp.strides[0]) > abs(imp.strides[-1]) and backward
):
# flip the strides. Is this always true for multidimension?
strides = imp.strides[:]
backstrides = imp.backstrides[:]
shape = imp.shape[:]
strides.reverse()
backstrides.reverse()
shape.reverse()
r = calculate_broadcast_strides(strides, backstrides, imp.shape, shape, backward)
iter_shape = shape
if len(shape) != len(r[0]):
# shape can be shorter when using an external loop, just return a view
iter_shape = imp.shape
return ConcreteIter(imp, imp.get_size(), iter_shape, r[0], r[1], self.op_flags[i], self)
示例7: _zeros_or_empty
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(shape)
except OverflowError:
raise OperationError(space.w_ValueError, space.wrap(
"array is too big."))
return W_NDimArray.from_shape(space, shape, dtype=dtype, zero=zero)
示例8: from_shape
def from_shape(space, shape, dtype, order="C", w_instance=None, zero=True):
from pypy.module.micronumpy import concrete, descriptor, boxes
from pypy.module.micronumpy.strides import calc_strides
if len(shape) > NPY.MAXDIMS:
raise oefmt(space.w_ValueError, "sequence too large; must be smaller than %d", NPY.MAXDIMS)
try:
support.product(shape) * dtype.elsize
except OverflowError as e:
raise oefmt(space.w_ValueError, "array is too big")
strides, backstrides = calc_strides(shape, dtype.base, order)
impl = concrete.ConcreteArray(shape, dtype.base, order, strides, backstrides, zero=zero)
if dtype == descriptor.get_dtype_cache(space).w_objectdtype:
impl.fill(space, boxes.W_ObjectBox(space.w_None))
if w_instance:
return wrap_impl(space, space.type(w_instance), w_instance, impl)
return W_NDimArray(impl)
示例9: test_iterator_basic
def test_iterator_basic(self):
#Let's get started, simple iteration in C order with
#contiguous layout => strides[-1] is 1
shape = [3, 5]
strides = [5, 1]
backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
assert backstrides == [10, 4]
i = ArrayIter(MockArray(shape, strides), support.product(shape), shape,
strides, backstrides)
assert i.contiguous
s = i.reset()
s = i.next(s)
s = i.next(s)
s = i.next(s)
assert s.offset == 3
assert not i.done(s)
assert s._indices == [0,0]
assert i.indices(s) == [0,3]
#cause a dimension overflow
s = i.next(s)
s = i.next(s)
assert s.offset == 5
assert s._indices == [0,3]
assert i.indices(s) == [1,0]
#Now what happens if the array is transposed? strides[-1] != 1
# therefore layout is non-contiguous
strides = [1, 3]
backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
assert backstrides == [2, 12]
i = ArrayIter(MockArray(shape, strides), support.product(shape), shape,
strides, backstrides)
assert not i.contiguous
s = i.reset()
s = i.next(s)
s = i.next(s)
s = i.next(s)
assert s.offset == 9
assert not i.done(s)
assert s._indices == [0,3]
#cause a dimension overflow
s = i.next(s)
s = i.next(s)
assert s.offset == 1
assert s._indices == [1,0]
示例10: 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"))
示例11: __init__
def __init__(self, array, dtype, start, strides, backstrides, shape):
self.indexes = [0] * len(shape)
self.array = array
self.dtype = dtype
self.shape = shape
self.offset = start
self.shapelen = len(shape)
self._done = self.shapelen == 0 or product(shape) == 0
self.strides = strides
self.backstrides = backstrides
self.size = array.size
示例12: AxisIter
def AxisIter(array, shape, axis):
strides = array.get_strides()
backstrides = array.get_backstrides()
if len(shape) == len(strides):
# keepdims = True
strides = strides[:axis] + [0] + strides[axis + 1:]
backstrides = backstrides[:axis] + [0] + backstrides[axis + 1:]
else:
strides = strides[:axis] + [0] + strides[axis:]
backstrides = backstrides[:axis] + [0] + backstrides[axis:]
return ArrayIter(array, support.product(shape), shape, strides, backstrides)
示例13: coalesce_iter
def coalesce_iter(old_iter, op_flags, it, order, flat=True):
"""
We usually iterate through an array one value at a time.
But after coalesce(), getoperand() will return a slice by removing
the fastest varying dimension(s) from the beginning or end of the shape.
If flat is true, then the slice will be 1d, otherwise stack up the shape of
the fastest varying dimension in the slice, so an iterator of a 'C' array
of shape (2,4,3) after two calls to coalesce will iterate 2 times over a slice
of shape (4,3) by setting the offset to the beginning of the data at each iteration
"""
shape = [s + 1 for s in old_iter.shape_m1]
if len(shape) < 1:
return old_iter
strides = old_iter.strides
backstrides = old_iter.backstrides
if order == NPY.FORTRANORDER:
new_shape = shape[1:]
new_strides = strides[1:]
new_backstrides = backstrides[1:]
_stride = old_iter.slice_stride + [strides[0]]
_shape = old_iter.slice_shape + [shape[0]]
_backstride = old_iter.slice_backstride + [strides[0] * (shape[0] - 1)]
fastest = shape[0]
else:
new_shape = shape[:-1]
new_strides = strides[:-1]
new_backstrides = backstrides[:-1]
# use the operand's iterator's rightmost stride,
# even if it is not the fastest (for 'F' or swapped axis)
_stride = [strides[-1]] + old_iter.slice_stride
_shape = [shape[-1]] + old_iter.slice_shape
_backstride = [(shape[-1] - 1) * strides[-1]] + old_iter.slice_backstride
fastest = shape[-1]
if fastest == 0:
return old_iter
if flat:
_shape = [support.product(_shape)]
if len(_stride) > 1:
_stride = [min(_stride[0], _stride[1])]
_backstride = [(shape[0] - 1) * _stride[0]]
return SliceIter(
old_iter.array,
old_iter.size / fastest,
new_shape,
new_strides,
new_backstrides,
_shape,
_stride,
_backstride,
op_flags,
it,
)
示例14: split_iter
def split_iter(arr, axis_flags):
"""Prepare 2 iterators for nested iteration over `arr`.
Arguments:
arr: instance of BaseConcreteArray
axis_flags: list of bools, one for each dimension of `arr`.The inner
iterator operates over the dimensions for which the flag is True
"""
shape = arr.get_shape()
strides = arr.get_strides()
backstrides = arr.get_backstrides()
shapelen = len(shape)
assert len(axis_flags) == shapelen
inner_shape = [-1] * shapelen
inner_strides = [-1] * shapelen
inner_backstrides = [-1] * shapelen
outer_shape = [-1] * shapelen
outer_strides = [-1] * shapelen
outer_backstrides = [-1] * shapelen
for i in range(len(shape)):
if axis_flags[i]:
inner_shape[i] = shape[i]
inner_strides[i] = strides[i]
inner_backstrides[i] = backstrides[i]
outer_shape[i] = 1
outer_strides[i] = 0
outer_backstrides[i] = 0
else:
outer_shape[i] = shape[i]
outer_strides[i] = strides[i]
outer_backstrides[i] = backstrides[i]
inner_shape[i] = 1
inner_strides[i] = 0
inner_backstrides[i] = 0
inner_iter = ArrayIter(arr, support.product(inner_shape),
inner_shape, inner_strides, inner_backstrides)
outer_iter = ArrayIter(arr, support.product(outer_shape),
outer_shape, outer_strides, outer_backstrides)
return inner_iter, outer_iter
示例15: __init__
def __init__(self, shape, dtype, order, strides, backstrides, storage, start=0):
make_sure_not_resized(shape)
make_sure_not_resized(strides)
make_sure_not_resized(backstrides)
self.shape = shape
# already tested for overflow in from_shape_and_storage
self.size = support.product(shape) * dtype.elsize
self.order = order
self.dtype = dtype
self.strides = strides
self.backstrides = backstrides
self.storage = storage
self.start = start
self.gcstruct = V_OBJECTSTORE