jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def perform(self, node, inp, out_):
        x, = inp
        out, = out_
        outdim = self.outdim
        if outdim == 1:
            try:
                out[0] = x.reshape(x.size)
            except AttributeError:
                out[0] = x.reshape((np.prod(x.shape),))
        elif outdim == len(x.shape):
            out[0] = x
        else:
            newshape = (x.shape[:outdim - 1] +
                        (np.prod(x.shape[outdim - 1:]),))
            out[0] = x.reshape(newshape)
//  [python]
def test_LinearFunction(self):
        W = numpy.random.uniform(-1, 1, (1, numpy.prod(self.x.shape[1:])))

        class Link(chainer.Chain):
            def __call__(self, x):
                return F.linear(x, W)

        assert_export_import_match(Link(), self.x)
//  [python]
def test_check_mol_mp2(self):
        # treating supercell at gamma point
        supcell = super_cell(cell,nmp)
        mf  = scf.RHF(supcell,exxdiv=None).density_fit()
        ehf  = mf.kernel()
        myadc  = mol_adc.ADC(mf)
        e_mp, t1, t2 = myadc.kernel_gs()
        e_mp = e_mp/(n.prod(nmp))

        self.assertAlmostEqual(e_mp, -0.02095766698, 4)
//  [python]
def test_check_mol_mp2(self):
        # treating supercell at gamma point
        supcell = super_cell(cell,nmp)
        mf  = scf.RHF(supcell,exxdiv=None).density_fit()
        ehf  = mf.kernel()
        myadc  = mol_adc.ADC(mf)
        e_mp, t1, t2 = myadc.kernel_gs()
        e_mp = e_mp/(numpy.prod(nmp))

        self.assertAlmostEqual(e_mp, -0.02095766698, 4)
//  [python]
def make_array_interface(
    ptr: CNumericPtr, shape: Tuple[int, ...], dtype: Type[np.number], is_cuda: bool
) -> Dict[str, Union[int, tuple, None]]:
    """Make an __(cuda)_array_interface__ from a pointer."""
    # Use an empty array to handle typestr and descr
    if is_cuda:
        empty = import_cupy().empty(shape=(0,), dtype=dtype)
        array = empty.__cuda_array_interface__  # pylint: disable=no-member
    else:
        empty = np.empty(shape=(0,), dtype=dtype)
        array = empty.__array_interface__  # pylint: disable=no-member

    addr = ctypes.cast(ptr, ctypes.c_void_p).value
    length = int(reduce(lambda x, y: x * y, shape))
    # Handle empty dataset.
    assert addr is not None or length == 0

    if addr is None:
        return array

    array["data"] = (addr, True)
    if is_cuda:
        array["stream"] = 2
    array["shape"] = shape
    array["strides"] = None
    return array
//  [python]
def make_array_interface(
    ptr: CNumericPtr, shape: Tuple[int, ...], dtype: Type[np.number], is_cuda: bool
) -> Dict[str, Union[int, tuple, None]]:
    """Make an __(cuda)_array_interface__ from a pointer."""
    # Use an empty array to handle typestr and descr
    if is_cuda:
        empty = import_cupy().empty(shape=(0,), dtype=dtype)
        array = empty.__cuda_array_interface__  # pylint: disable=no-member
    else:
        empty = np.empty(shape=(0,), dtype=dtype)
        array = empty.__array_interface__  # pylint: disable=no-member

    addr = ctypes.cast(ptr, ctypes.c_void_p).value
    length = int(np.prod(shape))
    # Handle empty dataset.
    assert addr is not None or length == 0

    if addr is None:
        return array

    array["data"] = (addr, True)
    if is_cuda:
        array["stream"] = 2
    array["shape"] = shape
    array["strides"] = None
    return array
//  [python]
def perform(self, node, inp, outs, params):
        x, = inp
        axes = self.axis
        max_idx, = outs
        if axes is None:
            axes = tuple(range(x.ndim))

        # Numpy does not support multiple axes for argmax
        # Work around
        keep_axes = np.array([i for i in range(x.ndim) if i not in axes],
                             dtype='int64')
        # Not-reduced axes in front
        transposed_x = np.transpose(x, np.concatenate((keep_axes,
                                                       axes)))
        kept_shape = transposed_x.shape[:len(keep_axes)]
        reduced_shape = transposed_x.shape[len(keep_axes):]
        new_shape = kept_shape + (reduce(lambda x, y: x*y, reduced_shape),)
        reshaped_x = transposed_x.reshape(new_shape)

        max_idx[0] = theano._asarray(np.argmax(reshaped_x, axis=-1),
                                     dtype='int64')
//  [python]
def perform(self, node, inp, outs, params):
        x, = inp
        axes = self.axis
        max_idx, = outs
        if axes is None:
            axes = tuple(range(x.ndim))

        # Numpy does not support multiple axes for argmax
        # Work around
        keep_axes = np.array([i for i in range(x.ndim) if i not in axes],
                             dtype='int64')
        # Not-reduced axes in front
        transposed_x = np.transpose(x, np.concatenate((keep_axes,
                                                       axes)))
        kept_shape = transposed_x.shape[:len(keep_axes)]
        reduced_shape = transposed_x.shape[len(keep_axes):]
        new_shape = kept_shape + (np.prod(reduced_shape),)
        reshaped_x = transposed_x.reshape(new_shape)

        max_idx[0] = theano._asarray(np.argmax(reshaped_x, axis=-1),
                                     dtype='int64')
//  [python]
def perform(self, node, inp, outs, params):
        x = inp[0]
        axes = params
        max, max_idx = outs
        if axes is None:
            axes = tuple(range(x.ndim))
        else:
            axes = tuple(int(ax) for ax in axes)
        max[0] = theano._asarray(np.max(x, axes),
                                 dtype=node.outputs[0].dtype)
        # Numpy does not support multiple axes for argmax
        # Work around
        keep_axes = np.array([i for i in range(x.ndim) if i not in axes],
                             dtype='int64')
        # Not-reduced axes in front
        transposed_x = np.transpose(x, np.concatenate((keep_axes, axes)))
        kept_shape = transposed_x.shape[:len(keep_axes)]
        reduced_shape = transposed_x.shape[len(keep_axes):]

        # Numpy.prod returns 1.0 when arg is empty, so we cast it to int64
        # Otherwise reshape would complain citing float arg
        new_shape = kept_shape + (np.multiply.reduce(reduced_shape, dtype='int64'),)
        reshaped_x = transposed_x.reshape(new_shape)

        max_idx[0] = theano._asarray(np.argmax(reshaped_x, axis=-1),
                                     dtype='int64')
//  [python]
def perform(self, node, inp, outs, params):
        x = inp[0]
        axes = params
        max, max_idx = outs
        if axes is None:
            axes = tuple(range(x.ndim))
        else:
            axes = tuple(int(ax) for ax in axes)
        max[0] = theano._asarray(np.max(x, axes),
                                 dtype=node.outputs[0].dtype)
        # Numpy does not support multiple axes for argmax
        # Work around
        keep_axes = np.array([i for i in range(x.ndim) if i not in axes],
                             dtype='int64')
        # Not-reduced axes in front
        transposed_x = np.transpose(x, np.concatenate((keep_axes, axes)))
        kept_shape = transposed_x.shape[:len(keep_axes)]
        reduced_shape = transposed_x.shape[len(keep_axes):]

        # Numpy.prod returns 1.0 when arg is empty, so we cast it to int64
        # Otherwise reshape would complain citing float arg
        new_shape = kept_shape + (np.prod(reduced_shape, dtype='int64'),)
        reshaped_x = transposed_x.reshape(new_shape)

        max_idx[0] = theano._asarray(np.argmax(reshaped_x, axis=-1),
                                     dtype='int64')
//  [python]
mg.barrier()
regrid = esmpy.Regrid(srcfield, dstfield, filename=filename,
                     regrid_method=esmpy.RegridMethod.BILINEAR,
                     unmapped_action=esmpy.UnmappedAction.IGNORE)


# # create a regrid object from file
regrid = esmpy.RegridFromFile(srcfield, dstfield, filename)

# calculate the regridding from source to destination field
dstfield = regrid(srcfield, dstfield)

# compute the mean relative error
num_nodes = numpy.prod(xctfield.data.shape[:])
//  [python]
def test_prod(self):
        arr = [[1, 2, 3, 4],
               [5, 6, 7, 9],
               [10, 3, 4, 5]]
        tgt = [24, 1890, 600]

        assert_equal(np.prod(arr, axis=-1), tgt)
//  [python]
def __get_n_weights(
            self, pre_shape: Tuple[int, ...],
            post_n_atoms: int) -> int:
        """
        Get the expected number of weights.
        """
        shape = self.__get_pre_in_post_shape(pre_shape)
        return numpy.prod(shape) * post_n_atoms
//  [python]
def test_return_type(self):
        class C(np.ndarray):
            pass

        for view in (C, np.ndarray):
            for nd in range(1, 4):
                shape = tuple(range(2, 2+nd))
                x = np.arange(np.prod(shape)).reshape(shape).view(view)
                for nzx in (np.nonzero(x), x.nonzero()):
                    for nzx_i in nzx:
                        assert_(type(nzx_i) is np.ndarray)
                        assert_(nzx_i.flags.writeable)
//  [python]
def img_to_bytes(image,
                 img_format = 'PNG'):
  """Convert image to bytes for storing in tf.Example proto.

  Args:
   image: Either numpy array or PIL image to be serialized.
   img_format: Image serailization format.

  Returns:
    Serialized image.
  Raises:
    ValueError: if input image is empty.
  """
  if np.shape(image) == 0:
    raise ValueError('Cannot serialize an empty image.')
  if not isinstance(image, Image.Image):
    if not np.issubdtype(image.dtype, np.integer):
      image = (image * 255).astype(np.uint8)
    image = Image.fromarray(image)
  image_bytes = io.BytesIO()
  image.save(image_bytes, format=img_format)
  return image_bytes.getvalue()
//  [python]
def img_to_bytes(image,
                 img_format = 'PNG'):
  """Convert image to bytes for storing in tf.Example proto.

  Args:
   image: Either numpy array or PIL image to be serialized.
   img_format: Image serailization format.

  Returns:
    Serialized image.
  Raises:
    ValueError: if input image is empty.
  """
  if np.prod(np.shape(image)) == 0:
    raise ValueError('Cannot serialize an empty image.')
  if not isinstance(image, Image.Image):
    if not np.issubdtype(image.dtype, np.integer):
      image = (image * 255).astype(np.uint8)
    image = Image.fromarray(image)
  image_bytes = io.BytesIO()
  image.save(image_bytes, format=img_format)
  return image_bytes.getvalue()
//  [python]
def test_mean_values(self):
        for mat in [self.rmat, self.cmat, self.omat]:
            for axis in [0, 1]:
                tgt = mat.sum(axis=axis)
                res = _mean(mat, axis=axis) * mat.shape[axis]
                assert_almost_equal(res, tgt)
            for axis in [None]:
                tgt = mat.sum(axis=axis)
                res = _mean(mat, axis=axis) * n.prod(mat.shape)
                assert_almost_equal(res, tgt)
//  [python]
def test_mean_values(self):
        for mat in [self.rmat, self.cmat, self.omat]:
            for axis in [0, 1]:
                tgt = mat.sum(axis=axis)
                res = _mean(mat, axis=axis) * mat.shape[axis]
                assert_almost_equal(res, tgt)
            for axis in [None]:
                tgt = mat.sum(axis=axis)
                res = _mean(mat, axis=axis) * np.prod(mat.shape)
                assert_almost_equal(res, tgt)
//  [python]
def perform(self, node, inp, out_):
        # This don't work as CudaNdarray_Subscript() don't support it.
        #super(GpuAdvancedSubtensor1, self).perform(node, inp, out_)
        x, idx = inp
        out, = out_
        x_orig = x
        # TODO: if more than 3 dims, reshape the inputs even if not all
        # dimensions are c contiguous
        if x.ndim > 3 and x.is_c_contiguous():
            x = x.reshape((x.shape[0], n.prod(x.shape[1:])))
        out_shape = (len(idx),) + x_orig.shape[1:]
        if x.ndim <= 3:
            # CudaNdarray.take only supports ndim <= 3
            if self.perform_using_take is not None:
                assert self.perform_using_take == True, (
                    "GpuAdvancedSubtensor1 used the fast version")
            if idx.dtype != numpy.int64:
                if idx.dtype in [numpy.int8, numpy.int16, numpy.int32,
                                 numpy.int64, numpy.uint8, numpy.uint16,
                                 numpy.uint32]:
                    idx = idx.astype(numpy.int64)
            if not idx.flags.c_contiguous:
                idx = numpy.ascontiguousarray(idx)

            idx = idx.view("float32")
            idx = cuda_ndarray.cuda_ndarray.CudaNdarray(idx)
            if self.max_threads == 0:
                num = theano.sandbox.cuda.use.device_number
                if device_properties(num)['regsPerBlock'] < (8192 * 2):
                    self.max_threads = 256
                else:
                    self.max_threads = 512

            o = x.take(idx,
                       0,  # axis
                       out_[0][0],  # return
                       "raise",
                       self.max_threads)
            if x is not x_orig:
                o = o.reshape(out_shape)
            out[0] = o
        else:
            if self.perform_using_take is not None:
                assert self.perform_using_take == False, (
                    "GpuAdvancedSubtensor1 didn't use the fast version")
            if out_[0][0] is None or out_[0][0].shape != out_shape:
                o = cuda_ndarray.cuda_ndarray.CudaNdarray.zeros(out_shape)
            else:
                o = out_[0][0]
            for (j, i) in enumerate(idx):
                o[j] = x[i]
            out[0] = o
//  [python]
def perform(self, node, inp, out_):
        # This don't work as CudaNdarray_Subscript() don't support it.
        #super(GpuAdvancedSubtensor1, self).perform(node, inp, out_)
        x, idx = inp
        out, = out_
        x_orig = x
        # TODO: if more than 3 dims, reshape the inputs even if not all
        # dimensions are c contiguous
        if x.ndim > 3 and x.is_c_contiguous():
            x = x.reshape((x.shape[0], numpy.prod(x.shape[1:])))
        out_shape = (len(idx),) + x_orig.shape[1:]
        if x.ndim <= 3:
            # CudaNdarray.take only supports ndim <= 3
            if self.perform_using_take is not None:
                assert self.perform_using_take == True, (
                    "GpuAdvancedSubtensor1 used the fast version")
            if idx.dtype != numpy.int64:
                if idx.dtype in [numpy.int8, numpy.int16, numpy.int32,
                                 numpy.int64, numpy.uint8, numpy.uint16,
                                 numpy.uint32]:
                    idx = idx.astype(numpy.int64)
            if not idx.flags.c_contiguous:
                idx = numpy.ascontiguousarray(idx)

            idx = idx.view("float32")
            idx = cuda_ndarray.cuda_ndarray.CudaNdarray(idx)
            if self.max_threads == 0:
                num = theano.sandbox.cuda.use.device_number
                if device_properties(num)['regsPerBlock'] < (8192 * 2):
                    self.max_threads = 256
                else:
                    self.max_threads = 512

            o = x.take(idx,
                       0,  # axis
                       out_[0][0],  # return
                       "raise",
                       self.max_threads)
            if x is not x_orig:
                o = o.reshape(out_shape)
            out[0] = o
        else:
            if self.perform_using_take is not None:
                assert self.perform_using_take == False, (
                    "GpuAdvancedSubtensor1 didn't use the fast version")
            if out_[0][0] is None or out_[0][0].shape != out_shape:
                o = cuda_ndarray.cuda_ndarray.CudaNdarray.zeros(out_shape)
            else:
                o = out_[0][0]
            for (j, i) in enumerate(idx):
                o[j] = x[i]
            out[0] = o
//  [python]
def test_shapes(self, a_shape: tuple[int, ...], b_shape: tuple[int, ...]):
        a_size = np.multiply.reduce(a_shape)
        a = np.arange(a_size).reshape(a_shape).astype(np.float64)
        a_id = id(a)

        b_size = np.multiply.reduce(b_shape)
        b = np.arange(b_size).reshape(b_shape)

        ref = a @ b
        if ref.shape != a_shape:
            with pytest.raises(ValueError):
                a @= b
            return
        else:
            a @= b

        assert id(a) == a_id
        assert a.dtype.type == np.float64
        assert a.shape == a_shape
        np.testing.assert_allclose(a, ref)
//  [python]
def test_shapes(self, a_shape: tuple[int, ...], b_shape: tuple[int, ...]):
        a_size = np.prod(a_shape)
        a = np.arange(a_size).reshape(a_shape).astype(np.float64)
        a_id = id(a)

        b_size = np.prod(b_shape)
        b = np.arange(b_size).reshape(b_shape)

        ref = a @ b
        if ref.shape != a_shape:
            with pytest.raises(ValueError):
                a @= b
            return
        else:
            a @= b

        assert id(a) == a_id
        assert a.dtype.type == np.float64
        assert a.shape == a_shape
        np.testing.assert_allclose(a, ref)
//  [python]
def test_no_suboffsets(self):
        try:
            import _testbuffer
        except ImportError:
            raise pytest.skip("_testbuffer is not available")

        for shape in [(2, 3), (2, 3, 4)]:
            data = list(range(n.prod(shape)))
            buffer = _testbuffer.ndarray(data, shape, format='i',
                                         flags=_testbuffer.ND_PIL)
            msg = "NumPy currently does not support.*suboffsets"
            with pytest.raises(BufferError, match=msg):
                np.asarray(buffer)
            with pytest.raises(BufferError, match=msg):
                np.asarray([buffer])

            # Also check (unrelated and more limited but similar) frombuffer:
            with pytest.raises(BufferError):
                np.frombuffer(buffer)
//  [python]
def test_no_suboffsets(self):
        try:
            import _testbuffer
        except ImportError:
            raise pytest.skip("_testbuffer is not available")

        for shape in [(2, 3), (2, 3, 4)]:
            data = list(range(np.prod(shape)))
            buffer = _testbuffer.ndarray(data, shape, format='i',
                                         flags=_testbuffer.ND_PIL)
            msg = "NumPy currently does not support.*suboffsets"
            with pytest.raises(BufferError, match=msg):
                np.asarray(buffer)
            with pytest.raises(BufferError, match=msg):
                np.asarray([buffer])

            # Also check (unrelated and more limited but similar) frombuffer:
            with pytest.raises(BufferError):
                np.frombuffer(buffer)
//  [python]
def get_total_subsampling_factor(self):
        """Get total subsampling factor."""
        if self.etype == "custom":
            return self.encoder.conv_subsampling_factor * int(
                n.prod(self.subsample)
            )
        else:
            return self.enc.conv_subsampling_factor * int(n.prod(self.subsample))
//  [python]
def get_total_subsampling_factor(self):
        """Get total subsampling factor."""
        if self.etype == "custom":
            return self.encoder.conv_subsampling_factor * int(
                numpy.prod(self.subsample)
            )
        else:
            return self.enc.conv_subsampling_factor * int(numpy.prod(self.subsample))
//  [python]
def _check_nd_one(self, routine, dtype, shape, axes, overwritable_dtypes,
                      overwrite_x):
        np.random.seed(1234)
        if np.issubdtype(dtype, np.complexfloating):
            data = np.random.randn(*shape) + 1j*np.random.randn(*shape)
        else:
            data = np.random.randn(*shape)
        data = data.astype(dtype)

        def fftshape_iter(shp):
            if len(shp) <= 0:
                yield ()
            else:
                for j in (shp[0]//2, shp[0], shp[0]*2):
                    for rest in fftshape_iter(shp[1:]):
                        yield (j,) + rest

        def part_shape(shape, axes):
            if axes is None:
                return shape
            else:
                return tuple(np.take(shape, axes))

        def should_overwrite(data, shape, axes):
            s = part_shape(data.shape, axes)
            return (overwrite_x and
                    reduce(lambda x, y: x*y, shape) <= reduce(lambda x, y: x*y, s)
                    and dtype in overwritable_dtypes)

        for fftshape in fftshape_iter(part_shape(shape, axes)):
            self._check(data, routine, fftshape, axes,
                        overwrite_x=overwrite_x,
                        should_overwrite=should_overwrite(data, fftshape, axes))
            if data.ndim > 1:
                # check fortran order
                self._check(data.T, routine, fftshape, axes,
                            overwrite_x=overwrite_x,
                            should_overwrite=should_overwrite(
                                data.T, fftshape, axes))
//  [python]
def _check_nd_one(self, routine, dtype, shape, axes, overwritable_dtypes,
                      overwrite_x):
        np.random.seed(1234)
        if np.issubdtype(dtype, np.complexfloating):
            data = np.random.randn(*shape) + 1j*np.random.randn(*shape)
        else:
            data = np.random.randn(*shape)
        data = data.astype(dtype)

        def fftshape_iter(shp):
            if len(shp) <= 0:
                yield ()
            else:
                for j in (shp[0]//2, shp[0], shp[0]*2):
                    for rest in fftshape_iter(shp[1:]):
                        yield (j,) + rest

        def part_shape(shape, axes):
            if axes is None:
                return shape
            else:
                return tuple(np.take(shape, axes))

        def should_overwrite(data, shape, axes):
            s = part_shape(data.shape, axes)
            return (overwrite_x and
                    np.prod(shape) <= np.prod(s)
                    and dtype in overwritable_dtypes)

        for fftshape in fftshape_iter(part_shape(shape, axes)):
            self._check(data, routine, fftshape, axes,
                        overwrite_x=overwrite_x,
                        should_overwrite=should_overwrite(data, fftshape, axes))
            if data.ndim > 1:
                # check fortran order
                self._check(data.T, routine, fftshape, axes,
                            overwrite_x=overwrite_x,
                            should_overwrite=should_overwrite(
                                data.T, fftshape, axes))
//  [python]
def combine(args):
    """
    All linear combination of a list of list.

    Args:
        args (numpy.ndarray):
            List of input arrays.  Components to take linear combination of
            with ``args[i].shape == (N[i], M[i])`` where ``N`` is to be taken
            linear combination of and ``M`` is constant.  ``M[i]`` is set to
            1 if missing.

    Returns:
        (numpy.array):
            Matrix of combinations with
            ``shape == (numpy.prod(N), numpy.sum(M))``.

    Examples:
        >>> A, B = [1,2], [[4,4],[5,6]]
        >>> combine([A, B])
        array([[1, 4, 4],
               [1, 5, 6],
               [2, 4, 4],
               [2, 5, 6]])
    """
    args = [numpy.asarray(arg).reshape(len(arg), -1) for arg in args]
    shapes = [arg.shape for arg in args]

    size = n.prod(shapes, 0)[0] * n.sum(shapes, 0)[1]

    out = args[0]
    for arg in args[1:]:
        out = numpy.hstack(
            [
                numpy.tile(out, len(arg)).reshape(-1, out.shape[1]),
                numpy.tile(arg.T, len(out)).reshape(arg.shape[1], -1).T,
            ]
        )
    return out
//  [python]
def combine(args):
    """
    All linear combination of a list of list.

    Args:
        args (numpy.ndarray):
            List of input arrays.  Components to take linear combination of
            with ``args[i].shape == (N[i], M[i])`` where ``N`` is to be taken
            linear combination of and ``M`` is constant.  ``M[i]`` is set to
            1 if missing.

    Returns:
        (numpy.array):
            Matrix of combinations with
            ``shape == (numpy.prod(N), numpy.sum(M))``.

    Examples:
        >>> A, B = [1,2], [[4,4],[5,6]]
        >>> combine([A, B])
        array([[1, 4, 4],
               [1, 5, 6],
               [2, 4, 4],
               [2, 5, 6]])
    """
    args = [numpy.asarray(arg).reshape(len(arg), -1) for arg in args]
    shapes = [arg.shape for arg in args]

    size = numpy.prod(shapes, 0)[0] * numpy.sum(shapes, 0)[1]

    out = args[0]
    for arg in args[1:]:
        out = numpy.hstack(
            [
                numpy.tile(out, len(arg)).reshape(-1, out.shape[1]),
                numpy.tile(arg.T, len(out)).reshape(arg.shape[1], -1).T,
            ]
        )
    return out
//  [python]
def combine_quadrature(
    abscissas,
    weights,
    domain=(),
):
    """
    Create all linear combinations of all abscissas and weights. If ``domain``
    is provided, also scale from assumed (0, 1) to said domain.

    Args:
        abscissas (List[numpy.ndarray]):
            List of abscissas to be combined.
        weights (List[numpy.ndarray]):
            List of weights to be combined.
        domain (Optional[Tuple[numpy.ndarray, numpy.ndarray]]):
            Domain to scale to.

    Returns:
        (Tuple[numpy.ndarray, numpy.ndarray]):
            Same as ``abscissas`` and ``weights``, but combined and flatten
            such that ``abscissas.shape == (dim, len(weights))``.
    """
    dim = len(abscissas)
    abscissas = combine(abscissas)
    weights = combine(weights)

    if domain:
        abscissas = (domain[1] - domain[0]) * abscissas + domain[0]
        weights = weights * (domain[1] - domain[0])

    abscissas = abscissas.T.reshape(dim, -1)
    weights = n.prod(weights, -1)

    assert len(weights.shape) == 1
    assert abscissas.shape == (dim,) + weights.shape, (abscissas, weights)

    return abscissas, weights
//  [python]
def combine_quadrature(
    abscissas,
    weights,
    domain=(),
):
    """
    Create all linear combinations of all abscissas and weights. If ``domain``
    is provided, also scale from assumed (0, 1) to said domain.

    Args:
        abscissas (List[numpy.ndarray]):
            List of abscissas to be combined.
        weights (List[numpy.ndarray]):
            List of weights to be combined.
        domain (Optional[Tuple[numpy.ndarray, numpy.ndarray]]):
            Domain to scale to.

    Returns:
        (Tuple[numpy.ndarray, numpy.ndarray]):
            Same as ``abscissas`` and ``weights``, but combined and flatten
            such that ``abscissas.shape == (dim, len(weights))``.
    """
    dim = len(abscissas)
    abscissas = combine(abscissas)
    weights = combine(weights)

    if domain:
        abscissas = (domain[1] - domain[0]) * abscissas + domain[0]
        weights = weights * (domain[1] - domain[0])

    abscissas = abscissas.T.reshape(dim, -1)
    weights = numpy.prod(weights, -1)

    assert len(weights.shape) == 1
    assert abscissas.shape == (dim,) + weights.shape, (abscissas, weights)

    return abscissas, weights
//  [python]
def reshape(self, new_shape):
        if not isinstance(new_shape, tuple):
            raise TypeError("new_shape 必须是一个元组(tuple)。")
        
        if not all(isinstance(dim, int) for dim in new_shape):
            raise TypeError("new_shape 中的所有元素都必须是整型(int)。")

        assert np.prod(new_shape) == np.prod(self.data.shape), "新形状的元素总数必须与原始形状相同"
        
        reshaped_data = self.data.reshape(new_shape)
        out = Tensor(reshaped_data, _prev=(self,), _op='reshape')
        
        def _backward():
            # reshape操作的梯度传递只涉及形状的变化，数据本身不改变
            self.grad += out.grad.reshape(self.data.shape)
            
        out._backward = _backward
        return out
//  [python]
def test_accelerate_framework_sgemv_fix(self):

        def aligned_array(shape, align, dtype, order='C'):
            d = dtype(0)
            N = np.prod(shape)
            tmp = np.zeros(N * d.nbytes + align, dtype=np.uint8)
            address = tmp.__array_interface__["data"][0]
            for offset in range(align):
                if (address + offset) % align == 0:
                    break
            tmp = tmp[offset:offset+N*d.nbytes].view(dtype=dtype)
            return tmp.reshape(shape, order=order)

        def as_aligned(arr, align, dtype, order='C'):
            aligned = aligned_array(arr.shape, align, dtype, order)
            aligned[:] = arr[:]
            return aligned

        def assert_dot_close(A, X, desired):
            assert_allclose(np.dot(A, X), desired, rtol=1e-5, atol=1e-7)

        m = aligned_array(100, 15, np.float32)
        s = aligned_array((100, 100), 15, np.float32)
        np.dot(s, m)  # this will always segfault if the bug is present

        testdata = itertools.product((15, 32), (10000,), (200, 89), ('C', 'F'))
        for align, m, n, a_order in testdata:
            # Calculation in double precision
            A_d = np.random.rand(m, n)
            X_d = np.random.rand(n)
            desired = np.dot(A_d, X_d)
            # Calculation with aligned single precision
            A_f = as_aligned(A_d, align, np.float32, order=a_order)
            X_f = as_aligned(X_d, align, np.float32)
            assert_dot_close(A_f, X_f, desired)
            # Strided A rows
            A_d_2 = A_d[::2]
            desired = np.dot(A_d_2, X_d)
            A_f_2 = A_f[::2]
            assert_dot_close(A_f_2, X_f, desired)
            # Strided A columns, strided X vector
            A_d_22 = A_d_2[:, ::2]
            X_d_2 = X_d[::2]
            desired = np.dot(A_d_22, X_d_2)
            A_f_22 = A_f_2[:, ::2]
            X_f_2 = X_f[::2]
            assert_dot_close(A_f_22, X_f_2, desired)
            # Check the strides are as expected
            if a_order == 'F':
                assert_equal(A_f_22.strides, (8, 8 * m))
            else:
                assert_equal(A_f_22.strides, (8 * n, 8))
            assert_equal(X_f_2.strides, (8,))
            # Strides in A rows + cols only
            X_f_2c = as_aligned(X_f_2, align, np.float32)
            assert_dot_close(A_f_22, X_f_2c, desired)
            # Strides just in A cols
            A_d_12 = A_d[:, ::2]
            desired = np.dot(A_d_12, X_d_2)
            A_f_12 = A_f[:, ::2]
            assert_dot_close(A_f_12, X_f_2c, desired)
            # Strides in A cols and X
            assert_dot_close(A_f_12, X_f_2, desired)
//  [python]
def test_tensorinv_singular_input(self, device, dtype):

        def check_singular_input(a_shape, ind):
            prod_ind_end = n.prod(a_shape[ind:])
            a = torch.eye(prod_ind_end, dtype=dtype, device=device)
            a[-1, -1] = 0   # Now `a` is singular
            a = a.reshape(a_shape)
            with self.assertRaisesRegex(RuntimeError, "Failed to invert the input tensor, because it is singular"):
                torch.linalg.tensorinv(a, ind=ind)

        # test for non-invertible input
        check_singular_input((12, 3, 4), ind=1)
        check_singular_input((3, 6, 18), ind=2)
//  [python]
def test_tensorinv_singular_input(self, device, dtype):

        def check_singular_input(a_shape, ind):
            prod_ind_end = np.prod(a_shape[ind:])
            a = torch.eye(prod_ind_end, dtype=dtype, device=device)
            a[-1, -1] = 0   # Now `a` is singular
            a = a.reshape(a_shape)
            with self.assertRaisesRegex(RuntimeError, "Failed to invert the input tensor, because it is singular"):
                torch.linalg.tensorinv(a, ind=ind)

        # test for non-invertible input
        check_singular_input((12, 3, 4), ind=1)
        check_singular_input((3, 6, 18), ind=2)
//  [python]
def offcore_array(
    shape: Union[Tuple[int, ...], Generator[int, None, None]],
    dtype: numpy.dtype,
    force_memmap: bool = False,
    zarr_allowed: bool = False,
    no_memmap_limit: bool = True,
    max_memory_usage_ratio: float = 0.9,
):
    """
    Instanciates an array of given shape and dtype in  'off-core' fashion i.e. not in main memory.
    Right now it simply uses memory mapping on temp file that is deleted after the file is closed

    Parameters
    ----------
    shape : tuple
    dtype : numpy.dtype
    force_memmap : bool
    zarr_allowed : bool
    no_memmap_limit : bool
    max_memory_usage_ratio : float

    """

    with lsection(f"Array of shape: {shape} and dtype: {dtype} requested"):
        size_in_bytes = numpy.prod(shape) * numpy.dtype(dtype).itemsize
        lprint(f'Array requested will be {(size_in_bytes / 1E6)} MB.')

        total_physical_memory_in_bytes = psutil.virtual_memory().total
        total_swap_memory_in_bytes = psutil.swap_memory().total

        total_mem_in_bytes = total_physical_memory_in_bytes + total_swap_memory_in_bytes
        lprint(
            f'There is {int(psutil.virtual_memory().total / 1E6)} MB of physical memory'
        )
        lprint(f'There is {int(psutil.swap_memory().total / 1E6)} MB of swap memory')
        lprint(f'There is {int(total_mem_in_bytes / 1E6)} MB of total memory')

        is_enough_total_memory = (
            size_in_bytes < max_memory_usage_ratio * total_mem_in_bytes
        )

        if not force_memmap and is_enough_total_memory:
            lprint(
                'There is enough physical+swap memory -- we do not need to use a mem mapped array or zarr-backed array.'
            )
            array = numpy.zeros(shape, dtype=dtype)

        elif no_memmap_limit:
            lprint(
                'There is not enough physical+swap memory -- we will use a mem mapped array.'
            )
            temp_file = tempfile.NamedTemporaryFile(dir=OffCore.memmap_directory)
            lprint(
                f'The temporary memory mapped file is at: {temp_file.name} (but you might not be able to see it!)'
            )
            array = numpy.memmap(temp_file, dtype=dtype, mode='w+', shape=shape)

        elif zarr_allowed:
            lprint(
                'There is not enough physical+swap memory -- we will use a zarr-backed array.'
            )
            import zarr

            array = zarr.create(
                shape=shape, dtype=dtype, store=zarr.TempStore("output.zarr")
            )
            # from numcodecs import Blosc
            # compressor = Blosc(cname = 'zstd', clevel = 3, shuffle = Blosc.BITSHUFFLE)
            # array = zarr.zeros((102_0, 200, 210), chunks = (100, 200, 210), compressor = compressor

        return array
//  [python]
def get_conv_2d_filter(filter_shape, param_list = None, masktype = None, name = ""):
	fan_in = n.prod(filter_shape[1:])
	fan_out = (filter_shape[0] * n.prod(filter_shape[2:]))
	w_std = numpy.sqrt(2.0 / (fan_in + fan_out))

	filter_init = uniform(w_std, filter_shape)


	if masktype is not None:
		filter_init *= floatX(numpy.sqrt(2.))

	conv_filter = theano.shared(filter_init, name = name)
	param_list.append(conv_filter)

	if masktype is not None:
		mask = numpy.ones(
			filter_shape,
			dtype=theano.config.floatX
			)

		for i in range(filter_shape[2]):
			for j in range(filter_shape[3]):
				if i > filter_shape[2]//2:
					mask[:,:,i,j] = floatX(0.0)

				if i == filter_shape[2]//2 and j > filter_shape[3]//2:
					mask[:,:,i,j] = floatX(0.0)

		if masktype == 'a':
			mask[:,:,filter_shape[2]//2,filter_shape[3]//2] = floatX(0.0)

		conv_filter = conv_filter*mask

	return conv_filter
//  [python]
def get_conv_2d_filter(filter_shape, param_list = None, masktype = None, name = ""):
	fan_in = numpy.prod(filter_shape[1:])
	fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]))
	w_std = numpy.sqrt(2.0 / (fan_in + fan_out))

	filter_init = uniform(w_std, filter_shape)


	if masktype is not None:
		filter_init *= floatX(numpy.sqrt(2.))

	conv_filter = theano.shared(filter_init, name = name)
	param_list.append(conv_filter)

	if masktype is not None:
		mask = numpy.ones(
			filter_shape,
			dtype=theano.config.floatX
			)

		for i in range(filter_shape[2]):
			for j in range(filter_shape[3]):
				if i > filter_shape[2]//2:
					mask[:,:,i,j] = floatX(0.0)

				if i == filter_shape[2]//2 and j > filter_shape[3]//2:
					mask[:,:,i,j] = floatX(0.0)

		if masktype == 'a':
			mask[:,:,filter_shape[2]//2,filter_shape[3]//2] = floatX(0.0)

		conv_filter = conv_filter*mask

	return conv_filter
//  [python]
def get_size(self, shape_info):
        if shape_info:
            return numpy.prod(shape_info) * numpy.dtype(self.dtype).itemsize
        else:
            return numpy.dtype(self.dtype).itemsize
//  [python]
def test_tensorsolve_errors_and_warnings(self, device, dtype):
        # tensorsolve expects the input that can be reshaped to a square matrix
        a = torch.eye(2 * 3 * 4, dtype=dtype, device=device).reshape((2 * 3, 4, 2, 3, 4))
        b = torch.randn(8, 4, dtype=dtype, device=device)
        self.assertTrue(np.prod(a.shape[2:]) != np.prod(b.shape))
        with self.assertRaisesRegex(RuntimeError, r'Expected self to satisfy the requirement'):
            torch.linalg.tensorsolve(a, b)

        # if non-empty out tensor with wrong shape is passed a warning is given
        out = torch.empty_like(a)
        b = torch.randn(6, 4, dtype=dtype, device=device)
        with warnings.catch_warnings(record=True) as w:
            # Trigger warning
            torch.linalg.tensorsolve(a, b, out=out)
            # Check warning occurs
            self.assertEqual(len(w), 1)
            self.assertTrue("An output with one or more elements was resized" in str(w[-1].message))

        # dtypes should be safely castable
        out = torch.empty_like(a).to(torch.int)
        with self.assertRaisesRegex(RuntimeError, "but got result with dtype Int"):
            torch.linalg.tensorsolve(a, b, out=out)

        # device should match
        if torch.cuda.is_available():
            wrong_device = 'cpu' if self.device_type != 'cpu' else 'cuda'
            out = torch.empty(0, dtype=dtype, device=wrong_device)
            with self.assertRaisesRegex(RuntimeError, "tensors to be on the same device"):
                torch.linalg.tensorsolve(a, b, out=out)
//  [python]
def project_ball(array, epsilon=1, ord=2):
    """
    Compute the orthogonal projection of the input tensor (as vector) onto the L_ord epsilon-ball.

    **Assumes the first dimension to be batch dimension, which is preserved.**

    :param array: array
    :type array: numpy.ndarray
    :param epsilon: radius of ball.
    :type epsilon: float
    :param ord: order of norm
    :type ord: int
    :return: projected vector
    :rtype: torch.autograd.Variable or torch.Tensor
    """

    assert isinstance(array, numpy.ndarray), 'given tensor should be numpy.ndarray'

    if ord == 0:
        raise NotImplementedError

        # not optimal implementation, see torch variant!
        assert epsilon >= 1
        size = array.shape
        flattened_size = n.prod(n.array(size[1:]))

        array = array.reshape(-1, flattened_size)
        sorted = n.sort(array, axis=1)

        k = int(math.ceil(epsilon))
        thresholds = sorted[:, -k]

        mask = (array >= expand_as(thresholds, array)).astype(float)
        array *= mask
    elif ord == 1:
        size = array.shape
        flattened_size = n.prod(n.array(size[1:]))

        array = array.reshape(-1, flattened_size)

        for i in range(array.shape[0]):
            # compute the vector of absolute values
            u = n.abs(array[i])
            # check if v is already a solution
            if u.sum() <= epsilon:
                # L1-norm is <= s
                continue
            # v is not already a solution: optimum lies on the boundary (norm == s)
            # project *u* on the simplex
            #w = project_simplex(u, s=epsilon)
            w = projection_simplex_sort(u, z=epsilon)
            # compute the solution to the original problem on v
            w *= n.sign(array[i])
            array[i] = w

        if len(size) == 4:
            array = array.reshape(-1, size[1], size[2], size[3])
        elif len(size) == 2:
            array = array.reshape(-1, size[1])
    elif ord == 2:
        size = array.shape
        flattened_size = n.prod(n.array(size[1:]))

        array = array.reshape(-1, flattened_size)
        clamped = n.clip(epsilon/n.linalg.norm(array, 2, axis=1), a_min=None, a_max=1)
        clamped = clamped.reshape(-1, 1)

        array = array * clamped
        if len(size) == 4:
            array = array.reshape(-1, size[1], size[2], size[3])
        elif len(size) == 2:
            array = array.reshape(-1, size[1])
    elif ord == float('inf'):
        array = n.clip(array, a_min=-epsilon, a_max=epsilon)
    else:
        raise NotImplementedError()

    return array
//  [python]
def project_ball(array, epsilon=1, ord=2):
    """
    Compute the orthogonal projection of the input tensor (as vector) onto the L_ord epsilon-ball.

    **Assumes the first dimension to be batch dimension, which is preserved.**

    :param array: array
    :type array: numpy.ndarray
    :param epsilon: radius of ball.
    :type epsilon: float
    :param ord: order of norm
    :type ord: int
    :return: projected vector
    :rtype: torch.autograd.Variable or torch.Tensor
    """

    assert isinstance(array, numpy.ndarray), 'given tensor should be numpy.ndarray'

    if ord == 0:
        raise NotImplementedError

        # not optimal implementation, see torch variant!
        assert epsilon >= 1
        size = array.shape
        flattened_size = numpy.prod(numpy.array(size[1:]))

        array = array.reshape(-1, flattened_size)
        sorted = numpy.sort(array, axis=1)

        k = int(math.ceil(epsilon))
        thresholds = sorted[:, -k]

        mask = (array >= expand_as(thresholds, array)).astype(float)
        array *= mask
    elif ord == 1:
        size = array.shape
        flattened_size = numpy.prod(numpy.array(size[1:]))

        array = array.reshape(-1, flattened_size)

        for i in range(array.shape[0]):
            # compute the vector of absolute values
            u = numpy.abs(array[i])
            # check if v is already a solution
            if u.sum() <= epsilon:
                # L1-norm is <= s
                continue
            # v is not already a solution: optimum lies on the boundary (norm == s)
            # project *u* on the simplex
            #w = project_simplex(u, s=epsilon)
            w = projection_simplex_sort(u, z=epsilon)
            # compute the solution to the original problem on v
            w *= numpy.sign(array[i])
            array[i] = w

        if len(size) == 4:
            array = array.reshape(-1, size[1], size[2], size[3])
        elif len(size) == 2:
            array = array.reshape(-1, size[1])
    elif ord == 2:
        size = array.shape
        flattened_size = numpy.prod(numpy.array(size[1:]))

        array = array.reshape(-1, flattened_size)
        clamped = numpy.clip(epsilon/numpy.linalg.norm(array, 2, axis=1), a_min=None, a_max=1)
        clamped = clamped.reshape(-1, 1)

        array = array * clamped
        if len(size) == 4:
            array = array.reshape(-1, size[1], size[2], size[3])
        elif len(size) == 2:
            array = array.reshape(-1, size[1])
    elif ord == float('inf'):
        array = numpy.clip(array, a_min=-epsilon, a_max=epsilon)
    else:
        raise NotImplementedError()

    return array
//  [python]
def compute_local_sums(I, J, filt, stride, padding, win):
    I2 = I * I
    J2 = J * J
    IJ = I * J

    I_sum = F.conv3d(I, filt, stride=stride, padding=padding)
    J_sum = F.conv3d(J, filt, stride=stride, padding=padding)
    I2_sum = F.conv3d(I2, filt, stride=stride, padding=padding)
    J2_sum = F.conv3d(J2, filt, stride=stride, padding=padding)
    IJ_sum = F.conv3d(IJ, filt, stride=stride, padding=padding)

    win_size = n.prod(win)
    u_I = I_sum / win_size
    u_J = J_sum / win_size

    cross = IJ_sum - u_J * I_sum - u_I * J_sum + u_I * u_J * win_size
    I_var = I2_sum - 2 * u_I * I_sum + u_I * u_I * win_size
    J_var = J2_sum - 2 * u_J * J_sum + u_J * u_J * win_size

    return I_var, J_var, cross
//  [python]
def compute_local_sums(I, J, filt, stride, padding, win):
    I2 = I * I
    J2 = J * J
    IJ = I * J

    I_sum = F.conv3d(I, filt, stride=stride, padding=padding)
    J_sum = F.conv3d(J, filt, stride=stride, padding=padding)
    I2_sum = F.conv3d(I2, filt, stride=stride, padding=padding)
    J2_sum = F.conv3d(J2, filt, stride=stride, padding=padding)
    IJ_sum = F.conv3d(IJ, filt, stride=stride, padding=padding)

    win_size = np.prod(win)
    u_I = I_sum / win_size
    u_J = J_sum / win_size

    cross = IJ_sum - u_J * I_sum - u_I * J_sum + u_I * u_J * win_size
    I_var = I2_sum - 2 * u_I * I_sum + u_I * u_I * win_size
    J_var = J2_sum - 2 * u_J * J_sum + u_J * u_J * win_size

    return I_var, J_var, cross
//  [python]
def test_gh_21022():
    # testing for absence of reported error
    source = np.ma.masked_array(data=[-1, -1], mask=True, dtype=np.float64)
    axis = np.array(0)
    result = np.prod(source, axis=axis, keepdims=False)
    result = np.ma.masked_array(result,
                                mask=np.ones(result.shape, dtype=np.bool_))
    array = np.ma.masked_array(data=-1, mask=True, dtype=np.float64)
    copy.deepcopy(array)
    copy.deepcopy(result)
//  [python]
def _compute_l2_norm(val, eps):
        shape = val.shape
        rank = len(shape)
        batch_dims = rank - 3
        if batch_dims == 0:
            square_sum = np.sum(val**2)
            output = val / np.power(square_sum + eps, 0.5)
        else:
            batch_dim_prod = np.multiply.reduce(shape[:batch_dims])
            reshape_val = np.reshape(val, (batch_dim_prod, -1))
            square_sum = np.sum(reshape_val * reshape_val, axis=1, keepdims=True) + eps
            output = reshape_val / np.power(square_sum, 0.5)
            output = np.reshape(output, shape)
        return output
//  [python]
def _compute_l2_norm(val, eps):
        shape = val.shape
        rank = len(shape)
        batch_dims = rank - 3
        if batch_dims == 0:
            square_sum = np.sum(val**2)
            output = val / np.power(square_sum + eps, 0.5)
        else:
            batch_dim_prod = np.prod(shape[:batch_dims])
            reshape_val = np.reshape(val, (batch_dim_prod, -1))
            square_sum = np.sum(reshape_val * reshape_val, axis=1, keepdims=True) + eps
            output = reshape_val / np.power(square_sum, 0.5)
            output = np.reshape(output, shape)
        return output
//  [python]
def _create_dummy_array_for_dot(xp, shape, dtype):
    x = numpy.arange(numpy.prod(shape)).reshape(shape)
    if dtype == 'bool_':
        x = numpy.asarray(x % 2 == 0)
    else:
        x = x.astype(dtype)
    return xp.array(x)
//  [python]
def _np_layer_norm(x, axes, gamma=None, beta=None, epsilon=1e-5):
        rank = len(x.shape)
        axes = [axis + rank if axis < 0 else axis for axis in axes]
        normalized_shape = [x.shape[i] if i in axes else 1 for i in range(rank)]
        gamma = (
            np.ones(shape=normalized_shape)
            if gamma is None
            else np.reshape(gamma, normalized_shape)
        )
        beta = (
            np.zeros(shape=normalized_shape) if beta is None else np.reshape(beta, normalized_shape)
        )
        num = x - np.mean(x, axis=tuple(axes), keepdims=True)
        dem = np.sqrt(
            np.sum(np.square(num), axis=tuple(axes), keepdims=True) / np.multiply.reduce(normalized_shape)
            + epsilon
        )
        return num / dem * gamma + beta
//  [python]
def _np_layer_norm(x, axes, gamma=None, beta=None, epsilon=1e-5):
        rank = len(x.shape)
        axes = [axis + rank if axis < 0 else axis for axis in axes]
        normalized_shape = [x.shape[i] if i in axes else 1 for i in range(rank)]
        gamma = (
            np.ones(shape=normalized_shape)
            if gamma is None
            else np.reshape(gamma, normalized_shape)
        )
        beta = (
            np.zeros(shape=normalized_shape) if beta is None else np.reshape(beta, normalized_shape)
        )
        num = x - np.mean(x, axis=tuple(axes), keepdims=True)
        dem = np.sqrt(
            np.sum(np.square(num), axis=tuple(axes), keepdims=True) / np.prod(normalized_shape)
            + epsilon
        )
        return num / dem * gamma + beta