• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python base.W_NDimArray类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pypy.module.micronumpy.base.W_NDimArray的典型用法代码示例。如果您正苦于以下问题:Python W_NDimArray类的具体用法?Python W_NDimArray怎么用?Python W_NDimArray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了W_NDimArray类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:charred,项目名称:pypy,代码行数:29,代码来源:compile.py


示例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)
开发者ID:mozillazg,项目名称:pypy,代码行数:48,代码来源:ctors.py


示例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)
开发者ID:sota,项目名称:pypy,代码行数:7,代码来源:iter.py


示例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)
开发者ID:,项目名称:,代码行数:7,代码来源:


示例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
开发者ID:Qointum,项目名称:pypy,代码行数:31,代码来源:loop.py


示例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
开发者ID:timfel,项目名称:thesis-data,代码行数:25,代码来源:ctors.py


示例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)
开发者ID:timfel,项目名称:thesis-data,代码行数:7,代码来源:strides.py


示例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
开发者ID:bukzor,项目名称:pypy,代码行数:59,代码来源:loop.py


示例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)
开发者ID:bukzor,项目名称:pypy,代码行数:8,代码来源:strides.py


示例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)
开发者ID:pypyjs,项目名称:pypy,代码行数:9,代码来源:concrete.py


示例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"))
开发者ID:sota,项目名称:pypy,代码行数:9,代码来源:scalar.py


示例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)
开发者ID:pypyjs,项目名称:pypy,代码行数:9,代码来源:concrete.py


示例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
开发者ID:mozillazg,项目名称:pypy,代码行数:55,代码来源:arrayops.py


示例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)
开发者ID:,项目名称:,代码行数:11,代码来源:


示例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()
开发者ID:GaussDing,项目名称:pypy,代码行数:13,代码来源:loop.py


示例16: tostring

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()
开发者ID:charred,项目名称:pypy,代码行数:13,代码来源:loop.py


示例17: repeat

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
开发者ID:sota,项目名称:pypy,代码行数:22,代码来源:interp_arrayops.py


示例18: descr_getitem

 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)
开发者ID:yuyichao,项目名称:pypy,代码行数:14,代码来源:flatiter.py


示例19: call1

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
开发者ID:yuyichao,项目名称:pypy,代码行数:14,代码来源:loop.py


示例20: _fromstring_text

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)
开发者ID:mozillazg,项目名称:pypy,代码行数:49,代码来源:ctors.py



注:本文中的pypy.module.micronumpy.base.W_NDimArray类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python descriptor.get_dtype_cache函数代码示例发布时间:2022-05-27
下一篇:
Python base.convert_to_array函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap