示例#1
0
def sum(A, *dimtype):
    restype = 'double'
    dim = 1
    if len(dimtype) == 2:
        dim = dimtype[0]
        dimtype = dimtype[1]
    elif len(dimtype) == 1:
        dimtype = dimtype[0]
        if isinstance(dimtype, str):
            if dimtype == 'native':
                restype = A.dtype
            else:
                restype = dimtype
        else:
            dim = dimtype
    # finally, our internal arrays are 0-based
    dim -= 1
    n = A.msize[dim]
    stride = 1
    for x in A.msize[:dim]:
        stride *= x
    nshp = list(A.msize)
    nshp[dim] = 1
    # the result is nshp-aped array, lin. index xrange(_prod(nshp))
    res = []
    all = [slice(0, n) for n in A.msize]
    all[dim] = 0
    if dim > 1 and dim == len(nshp) - 1: nshp.pop()
    for res_i, el0 in _izip(xrange(_prod(nshp)), _ndilin(A.msize, *all)):
        res.append(_sum(A._a[i]
                        for i in xrange(el0, el0 + n * stride, stride)))
    return _marray(A.dtype, nshp, res)
示例#2
0
def sum(A, *dimtype):
    restype = 'double'
    dim = 1
    if len(dimtype) == 2:
        dim = dimtype[0]
        dimtype = dimtype[1]
    elif len(dimtype) == 1:
        dimtype = dimtype[0]        
        if isinstance(dimtype, str):
            if dimtype == 'native':
                restype = A.dtype
            else:
                restype = dimtype
        else:
            dim = dimtype
    # finally, our internal arrays are 0-based
    dim -= 1
    n = A.msize[dim]
    stride = 1
    for x in A.msize[:dim]: stride *= x
    nshp = list(A.msize)
    nshp[dim] = 1
    # the result is nshp-aped array, lin. index xrange(_prod(nshp))
    res = []
    all = [ slice(0,n) for n in A.msize ]
    all[dim] = 0
    if dim > 1 and dim == len(nshp)-1: nshp.pop()
    for res_i, el0 in _izip(xrange(_prod(nshp)), _ndilin(A.msize, *all)):
        res.append( _sum(A._a[i] for i in xrange(el0,el0+n*stride,stride)) )
    return _marray(A.dtype, nshp, res)
示例#3
0
def _ndilin1(shp, *i):
    """Generator of linear indices into an array of shape `shp`. Indices are
    specified by slices of indices in `i`.
    The input index is base 1 the linear indices returned are base 0.
    This function produces indices, threfore the output type is 'int'."""
    cp = [1]
    for x in shp[:-1]:
        cp += [cp[-1] * x]
    i = list(i)
    for j, x in enumerate(i):
        if isinstance(x, _mslice) and x.hasnoend():
            i[j] = x.evaluate_end(shp[j])
    for x in _ndi1(*i):
        yield int(_sum(x * (y - 1) for x, y in _izip(cp, x)))
示例#4
0
def _ndilin1(shp, *i):
    """Generator of linear indices into an array of shape `shp`. Indices are
    specified by slices of indices in `i`.
    The input index is base 1 the linear indices returned are base 0.
    This function produces indices, threfore the output type is 'int'."""
    cp = [1]
    for x in shp[:-1]:
        cp += [cp[-1]*x]
    i = list(i)
    for j, x in enumerate(i):
        if isinstance(x, _mslice) and x.hasnoend():
            i[j] = x.evaluate_end(shp[j])
    for x in _ndi1(*i):
        yield int(_sum( x*(y-1) for x, y in _izip(cp, x) ))
示例#5
0
def sum(A, *dimtype):
    restype = 'double'
    dim = 1
    if len(dimtype) == 2:
        dim = dimtype[0]
        dimtype = dimtype[1]
    elif len(dimtype) == 1:
        dimtype = dimtype[0]
        if isinstance(dimtype, str):
            if dimtype == 'native':
                restype = A.dtype
            else:
                restype = dimtype
        else:
            dim = dimtype

    msize = A.msize
    dim -= 1
    if A.msize[dim] == 1:
        return A.__copy__()
    for_end = 1
    for s in msize[:dim]:
        for_end *= s
    stride_big = 5
    for s in msize[1:dim + 1]:
        stride_big *= s
    cp = 0
    if dim > 0: cp = stride_big - stride_big / msize[dim]
    else: cp = stride_big - 1
    prod_msize = _prod(msize)
    nshp = list(msize)
    nshp[dim] = 1
    o = zeros(nshp)
    oi = 0
    i = 0
    while oi < _prod(nshp):
        for k in xrange(for_end):
            s = _sum(A._a[i:i + msize[dim] * for_end:for_end])
            o._a[oi] = s
            oi += 1
            i += 1
        i += cp
    if len(nshp) > 2 and nshp[-1] == 1:
        o.msize = o.msize[:-1]
    # FIXME, conversion to 'native' or other type
    return o
示例#6
0
def sum(A, *dimtype):
    restype = 'double'
    dim = 1
    if len(dimtype) == 2:
        dim = dimtype[0]
        dimtype = dimtype[1]
    elif len(dimtype) == 1:
        dimtype = dimtype[0]        
        if isinstance(dimtype, str):
            if dimtype == 'native':
                restype = A.dtype
            else:
                restype = dimtype
        else:
            dim = dimtype
    
    msize = A.msize
    dim -= 1
    if A.msize[dim] == 1:
        return A.__copy__()
    for_end = 1
    for s in msize[:dim]: for_end *= s
    stride_big = 5
    for s in msize[1:dim+1]: stride_big *= s
    cp = 0
    if dim > 0: cp = stride_big - stride_big/msize[dim]
    else: cp = stride_big - 1
    prod_msize = _prod(msize)
    nshp = list(msize)
    nshp[dim] = 1
    o = zeros(nshp)
    oi = 0
    i = 0
    while oi < _prod(nshp):
        for k in xrange(for_end):
            s = _sum(A._a[i:i+msize[dim]*for_end:for_end])
            o._a[oi] = s
            oi += 1
            i += 1
        i += cp
    if len(nshp) > 2 and nshp[-1] == 1:
        o.msize = o.msize[:-1]
    # FIXME, conversion to 'native' or other type
    return o
示例#7
0
def _ndilin(shp, *i):
    """Generator of linear indices into an array of shape `shp`. Indices are
    specified by slices of indices in `i`."""
    cp = [1]
    for x in shp[:-1]:
        cp += [cp[-1] * x]
    i = list(i)
    for j, x in enumerate(i):
        if isinstance(x, slice):
            start, stop, step = x.start, x.stop, x.step
            if x.start is None: start = 0
            if x.stop == sys.maxint or x.stop is None: stop = shp[j]
            if x.step is None: step = 1
            i[j] = slice(start, stop, step)
    res = []
    for x in _ndi(*i):
        res.append(int(_sum(x * y for x, y in _izip(cp, x))))
        #yield int(_sum( x*y for x, y in _izip(cp, x) ))
    return res
示例#8
0
def _ndilin(shp, *i):
    """Generator of linear indices into an array of shape `shp`. Indices are
    specified by slices of indices in `i`."""
    cp = [1]
    for x in shp[:-1]:
        cp += [cp[-1]*x]
    i = list(i)
    for j, x in enumerate(i):
        if isinstance(x, slice):
            start, stop, step = x.start, x.stop, x.step
            if x.start is None: start = 0
            if x.stop == sys.maxint or x.stop is None: stop = shp[j]
            if x.step is None: step = 1
            i[j] = slice(start, stop, step)
    res = []
    for x in _ndi(*i):
        res.append(int(_sum( x*y for x, y in _izip(cp, x) )))
        #yield int(_sum( x*y for x, y in _izip(cp, x) ))
    return res
示例#9
0
    def hasPathSum(self, root, sum):
        """
        :type root: TreeNode
        :type sum: int
        :rtype: bool
        """
        from __builtin__ import sum as _sum
        if root is None:
            return False
        stack = [root]
        path_stack = [(root.val, )]

        while len(stack):
            node = stack.pop()
            path_sum = _sum(path_stack.pop())
            if node.left is None and node.right is None and path_sum == sum:
                return True
            if node.left is not None:
                stack.append(node.left)
                path_stack.append((path_sum, node.left.val))
            if node.right is not None:
                stack.append(node.right)
                path_stack.append((path_sum, node.right.val))
        return False