def _findIndices(ArrSize, FilterSize): N = FilterSize.shape[0] n = int(FilterSize.prod()) CumSizeArr = numpy.ones([N], dtype=numpy.int32) CumSizeArr[1:N] = ArrSize[0:N - 1].cumprod() CumSize = numpy.ones([N], dtype=numpy.int32) CumSize[1:N] = FilterSize[0:N - 1].cumprod() vals = numpy.empty((n, N), dtype=numpy.int32) for i in range(N): vals[:, i] = numpy.linspace(0, n - 1, n) vals = vals // CumSize vals = vals % FilterSize CurrPos = summations.sum(vals * CumSizeArr, axis=1) return CurrPos.astype(numpy.int32)
def empty(shape, dtype=float, bohrium=True): """ Return a new matrix of given shape and type, without initializing entries. Parameters ---------- shape : int or tuple of int Shape of the empty matrix. dtype : data-type, optional Desired output data-type. See Also -------- empty_like, zeros Notes ----- The order of the data in memory is always row-major (C-style). `empty`, unlike `zeros`, does not set the matrix values to zero, and may therefore be marginally faster. On the other hand, it requires the user to manually set all the values in the array, and should be used with caution. Examples -------- >>> import numpy.matlib >>> np.matlib.empty((2, 2)) # filled with random data matrix([[ 6.76425276e-320, 9.79033856e-307], [ 7.39337286e-309, 3.22135945e-309]]) #random >>> np.matlib.empty((2, 2), dtype=int) matrix([[ 6600475, 0], [ 6586976, 22740995]]) #random """ if not bohrium: return numpy.empty(shape, dtype=dtype) return bhary.new(shape, dtype)
def diagonal(ary, offset=0, axis1=0, axis2=1): """ Return specified diagonals. If `a` is 2-D, returns the diagonal of `a` with the given offset, i.e., the collection of elements of the form ``a[i, i+offset]``. If `a` has more than two dimensions, then the axes specified by `axis1` and `axis2` are used to determine the 2-D sub-array whose diagonal is returned. The shape of the resulting array can be determined by removing `axis1` and `axis2` and appending an index to the right equal to the size of the resulting diagonals. Parameters ---------- ary : array_like Array from which the diagonals are taken. offset : int, optional Offset of the diagonal from the main diagonal. Can be positive or negative. Defaults to main diagonal (0). axis1 : int, optional Axis to be used as the first axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to first axis (0). axis2 : int, optional Axis to be used as the second axis of the 2-D sub-arrays from which the diagonals should be taken. Defaults to second axis (1). Returns ------- array_of_diagonals : ndarray If `a` is 2-D, a 1-D array containing the diagonal is returned. If the dimension of `a` is larger, then an array of diagonals is returned, "packed" from left-most dimension to right-most (e.g., if `a` is 3-D, then the diagonals are "packed" along rows). Raises ------ ValueError If the dimension of `a` is less than 2. See Also -------- diag : MATLAB work-a-like for 1-D and 2-D arrays. diagflat : Create diagonal arrays. trace : Sum along diagonals. Examples -------- >>> a = np.arange(4).reshape(2,2); a array([[0, 1], [2, 3]]) >>> a.diagonal() array([0, 3]) >>> a.diagonal(1) array([1]) A 3-D example: >>> a = np.arange(8).reshape(2,2,2); a array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) >>> a.diagonal() array([[0, 6], [1, 7]]) """ if axis1 == axis2: raise Exception("axis1 and axis2 cannot be the same\n") if ary.ndim < 2: raise Exception("diagonal requires an array of at least two dimensions\n") # Get all axes except the two which has the diagonal we seek; # these are added later min_axis, max_axis = sorted([axis1, axis2]) tr = list(range(ary.ndim)) del tr[max_axis] del tr[min_axis] # Positive offset means upper diagonals, negative is lower, so we switch # the axes around if negative if offset >= 0: ary = ary.transpose(tr + [axis1, axis2]) else: ary = ary.transpose(tr + [axis2, axis1]) offset = -offset # Calculate how many elements will be in the diagonal diag_size = max(0, min(ary.shape[-2], ary.shape[-1] - offset)) ret_shape = ary.shape[:-2] + (diag_size,) # Return empty array if the diagonal has zero elements if diag_size == 0: return numpy.empty(ret_shape, dtype=ary.dtype) ary = ary[..., :diag_size, offset:(offset + diag_size)] ret_strides = ary.strides[:-2] + (ary.strides[-1] + ary.strides[-2],) return numpy.lib.stride_tricks.as_strided(ary, shape=ret_shape, strides=ret_strides)
def matmul(out, in1, in2): assert(in1.dtype == in2.dtype) if not out: out = np.empty((in1.shape[0], in2.shape[1]), dtype=in1.dtype) assert(in1.dtype == out.dtype) target.matmul(get_bhc(out), get_bhc(in1), get_bhc(in2))