本文整理汇总了Python中pypy.module.micronumpy.support.product函数的典型用法代码示例。如果您正苦于以下问题:Python product函数的具体用法?Python product怎么用?Python product使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了product函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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()
开发者ID:pypyjs,项目名称:pypy,代码行数:12,代码来源:concrete.py
示例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)
开发者ID:pypyjs,项目名称:pypy,代码行数:51,代码来源:base.py
示例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)
开发者ID:yuyichao,项目名称:pypy,代码行数:49,代码来源:test_iterators.py
示例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())
开发者ID:sota,项目名称:pypy,代码行数:15,代码来源:concrete.py
示例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)
开发者ID:Darriall,项目名称:pypy,代码行数:7,代码来源:concrete.py
示例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)
开发者ID:cimarieta,项目名称:usp,代码行数:34,代码来源:nditer.py
示例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)
开发者ID:Darriall,项目名称:pypy,代码行数:16,代码来源:ctors.py
示例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)
开发者ID:Qointum,项目名称:pypy,代码行数:17,代码来源:base.py
示例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]
开发者ID:abhinavthomas,项目名称:pypy,代码行数:45,代码来源:test_iterators.py
示例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"))
开发者ID:sota,项目名称:pypy,代码行数:9,代码来源:scalar.py
示例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
开发者ID:sota,项目名称:pypy,代码行数:11,代码来源:iter.py
示例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)
开发者ID:Qointum,项目名称:pypy,代码行数:11,代码来源:iterators.py
示例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,
)
开发者ID:cimarieta,项目名称:usp,代码行数:52,代码来源:nditer.py
示例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
开发者ID:Qointum,项目名称:pypy,代码行数:39,代码来源:loop.py
示例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
开发者ID:timfel,项目名称:thesis-data,代码行数:14,代码来源:concrete.py
示例16: __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
if order not in (NPY.CORDER, NPY.FORTRANORDER):
raise oefmt(dtype.itemtype.space.w_ValueError, "ConcreteArrayNotOwning but order is not 0,1 rather %d", order)
self.order = order
self.dtype = dtype
self.strides = strides
self.backstrides = backstrides
self.storage = storage
self.start = start
self.gcstruct = V_OBJECTSTORE
开发者ID:mozillazg,项目名称:pypy,代码行数:16,代码来源:concrete.py
示例17: test_iterator_goto
def test_iterator_goto(self):
shape = [3, 5]
strides = [1, 3]
backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
assert backstrides == [2, 12]
a = MockArray(shape, strides, 42)
i = ArrayIter(a, support.product(shape), shape,
strides, backstrides)
assert not i.contiguous
s = i.reset()
assert s.index == 0
assert s._indices == [0, 0]
assert s.offset == a.start
s = i.goto(11)
assert s.index == 11
assert s._indices is None
assert s.offset == a.start + 5
开发者ID:abhinavthomas,项目名称:pypy,代码行数:17,代码来源:test_iterators.py
示例18: __init__
def __init__(self, shape, dtype, order, strides, backstrides,
storage=lltype.nullptr(RAW_STORAGE), zero=True):
gcstruct = V_OBJECTSTORE
if storage == lltype.nullptr(RAW_STORAGE):
length = support.product(shape)
if dtype.num == NPY.OBJECT:
storage = dtype.itemtype.malloc(length * dtype.elsize, zero=True)
gcstruct = _create_objectstore(storage, length, dtype.elsize)
else:
storage = dtype.itemtype.malloc(length * dtype.elsize, zero=zero)
start = calc_start(shape, strides)
ConcreteArrayNotOwning.__init__(self, shape, dtype, order, strides, backstrides,
storage, start=start)
self.gcstruct = gcstruct
self.flags = NPY.ARRAY_ALIGNED | NPY.ARRAY_WRITEABLE
if is_c_contiguous(self):
self.flags |= NPY.ARRAY_C_CONTIGUOUS
if is_f_contiguous(self):
self.flags |= NPY.ARRAY_F_CONTIGUOUS
开发者ID:pypyjs,项目名称:pypy,代码行数:19,代码来源:concrete.py
示例19: test_one_in_shape
def test_one_in_shape(self):
strides = [16, 4, 8]
shape = [3, 4, 1]
backstrides = [x * (y - 1) for x,y in zip(strides, shape)]
assert backstrides == [32, 12, 0]
i = ArrayIter(MockArray(shape, strides), support.product(shape), shape,
strides, backstrides)
assert not i.contiguous
s = i.reset()
for j in range(3):
s = i.next(s)
assert s.offset == 12
assert not i.done(s)
assert s._indices == [0, 3, 0]
while not i.done(s):
old_indices = s._indices[:]
old_offset = s.offset
s = i.next(s)
assert s.offset == 0
assert s._indices == [0, 0, 0]
assert old_indices == [2, 3, 0]
assert old_offset == 44
开发者ID:abhinavthomas,项目名称:pypy,代码行数:22,代码来源:test_iterators.py
示例20: _array
def _array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False):
from pypy.module.micronumpy import strides
# for anything that isn't already an array, try __array__ method first
if not isinstance(w_object, W_NDimArray):
w_array = try_array_method(space, w_object, w_dtype)
if w_array is not None:
# continue with w_array, but do further operations in place
w_object = w_array
copy = False
dtype = descriptor.decode_w_dtype(space, w_dtype)
if space.is_none(w_order):
order = 'C'
else:
order = space.str_w(w_order)
if order == 'K':
order = 'C'
if order != 'C': # or order != 'F':
raise oefmt(space.w_ValueError, "Unknown order: %s", order)
if isinstance(w_object, W_NDimArray):
if (dtype is None or w_object.get_dtype() is dtype):
if copy and (subok or type(w_object) is W_NDimArray):
return w_object.descr_copy(space, w_order)
elif not copy and (subok or type(w_object) is W_NDimArray):
return w_object
# we have a ndarray, but need to copy or change dtype or create W_NDimArray
if dtype is None:
dtype = w_object.get_dtype()
if dtype != w_object.get_dtype():
# silently reject the copy value
copy = True
if copy:
shape = w_object.get_shape()
_elems_w = w_object.reshape(space, space.wrap(-1))
elems_w = [None] * w_object.get_size()
for i in range(len(elems_w)):
elems_w[i] = _elems_w.descr_getitem(space, space.wrap(i))
elif subok:
raise oefmt(space.w_NotImplementedError,
"array(...copy=False, subok=True) not implemented yet")
else:
sz = support.product(w_object.get_shape()) * dtype.elsize
return W_NDimArray.from_shape_and_storage(space,
w_object.get_shape(),w_object.implementation.storage,
dtype, storage_bytes=sz, w_base=w_object)
else:
# not an array
shape, elems_w = strides.find_shape_and_elems(space, w_object, dtype)
if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1):
dtype = strides.find_dtype_for_seq(space, elems_w, dtype)
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')
w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order)
if len(elems_w) == 1:
w_arr.set_scalar_value(dtype.coerce(space, elems_w[0]))
else:
loop.assign(space, w_arr, elems_w)
return w_arr
开发者ID:bukzor,项目名称:pypy,代码行数:65,代码来源:ctors.py
注:本文中的pypy.module.micronumpy.support.product函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论