Пример #1
0
def repeat_to_match_shape(g, shape, dtype, axis, keepdims):
    """Returns the array g repeated along axis to fit vector space vs.
    Also returns the number of repetitions of the array."""
    with ua.set_backend(numpy_backend, coerce=True):
        if shape == ():
            return g, 1
        axis = list(axis) if isinstance(axis, tuple) else axis
        new_shape = np.array(shape, dtype=int)
        new_shape[axis] = 1
        num_reps = np.prod(np.array(shape)[axis])
    return np.broadcast_to(np.reshape(g, new_shape), shape), num_reps
Пример #2
0
    def var(self, value):
        if not isinstance(value, Variable):
            raise ValueError("var must be a Variable")

        if self._var is not None:
            raise ValueError("variable already set")

        self._var = value
        self.diffs[value] = np.array(1)
Пример #3
0
    def jvp(g):
        if axis is None:
            num_reps = np.size(g)
        elif isinstance(axis, int):
            num_reps = np.shape(g)[axis]
        elif isinstance(axis, tuple):
            num_reps = np.prod(np.array(np.shape(g))[list(axis)])

        if num_reps <= 1:
            return np.zeros_like(ans)
        x_minus_mean = np.conj(x - np.mean(x, axis=axis, keepdims=True))
        return np.sum(np.real(g * x_minus_mean), axis=axis,
                      keepdims=keepdims) / ((num_reps - ddof) * ans)
Пример #4
0
            raise
        pytest.xfail(
            reason="The backend has no implementation for this ufunc.")

    assert all(isinstance(arr, types) for arr in ret)

    for arr in ret:
        if isinstance(arr, da.Array):
            arr.compute()


@pytest.mark.parametrize(
    "method, args, kwargs",
    [
        (np.empty, (2, ), {}),
        (np.empty_like, (np.array([1, 2, 3]), ), {}),
        (np.eye, (2, ), {}),
        (np.full, ((1, 2, 3), 1.3), {}),
        (np.ones, ((1, 2, 3), ), {}),
        (np.zeros, ((1, 2, 3), ), {}),
    ],
)
def test_array_creation(backend, method, args, kwargs):
    backend, types = backend
    for dtype in dtypes:
        try:
            with ua.set_backend(backend, coerce=True):
                kwargs["dtype"] = dtype
                ret = method(*args, **kwargs)
        except ua.BackendNotImplementedError:
            if backend in FULLY_TESTED_BACKENDS and (backend,
Пример #5
0
        (np.conjugate, ([1.0 + 2.0j, -1.0 - 1j],), {}, [1.0 - 2.0j, -1.0 + 1j]),
        (np.conj, ([1.0 + 2.0j, -1.0 - 1j],), {}, [1.0 - 2.0j, -1.0 + 1j]),
        (np.exp, ([0, 1, 2],), {}, [1.0, 2.718281828459045, 7.38905609893065]),
        (np.exp2, ([3, 4],), {}, [8, 16]),
        (np.log, ([1.0, np.e, np.e ** 2],), {}, [0.0, 1.0, 2.0]),
        (np.log2, ([1, 2, 2 ** 4],), {}, [0.0, 1.0, 4.0]),
        (np.log10, ([1e-5, -3.0],), {}, [-5.0, np.NaN]),
        (np.sqrt, ([1, 4, 9],), {}, [1, 2, 3]),
        (np.square, ([2, 3, 4],), {}, [4, 9, 16]),
        (np.cbrt, ([1, 8, 27],), {}, [1.0, 2.0, 3.0]),
        (np.reciprocal, ([1.0, 2.0, 4.0],), {}, [1.0, 0.5, 0.25]),
        (
            np.broadcast_to,
            ([1, 2, 3], (3, 3)),
            {},
            np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]]),
        ),
        (
            np.degrees,
            ([0.0, 0.52359878, 1.04719755, 1.57079633],),
            {},
            [0.0, 30.0, 60.0, 90.0],
        ),
        (
            np.radians,
            ([0.0, 30.0, 60.0, 90.0],),
            {},
            [0.0, 0.52359878, 1.04719755, 1.57079633],
        ),
    ],
)