def divmod( x1: PolyLike, x2: PolyLike, out: Union[None, ndpoly, Tuple[ndpoly, ...]] = None, where: Optional[numpy.ndarray] = numpy.array(True), **kwargs: Any, ) -> Tuple[ndpoly, ndpoly]: """ Return element-wise quotient and remainder simultaneously. ``numpoly.divmod(x, y)`` is equivalent to ``(x // y, x % y)``, but faster because it avoids redundant work. It is used to implement the Python built-in function ``divmod`` on arrays. Args: x1: Dividend array. x2: Divisor array. If ``x1.shape != x2.shape``, they must be broadcastable to a common shape (which becomes the shape of the output). out: A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or `None`, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs. where: This condition is broadcast over the input. At locations where the condition is True, the `out` array will be set to the ufunc result. Elsewhere, the `out` array will retain its original value. Note that if an uninitialized `out` array is created via the default ``out=None``, locations within it where the condition is False will remain uninitialized. kwargs: Keyword args passed to numpy.ufunc. Returns: Element-wise quotient and remainder resulting from floor division. Raises: numpoly.baseclass.FeatureNotSupported: If either `x1` or `x2` contains indeterminants, numerical division is no longer possible and an error is raised instead. For polynomial division-remainder see ``numpoly.poly_divmod``. Examples: >>> numpoly.divmod([1, 22, 444], 4) (polynomial([0, 5, 111]), polynomial([1, 2, 0])) """ del out x1, x2 = numpoly.align_polynomials(x1, x2) if not x1.isconstant() or not x2.isconstant(): raise numpoly.FeatureNotSupported(DIVMOD_ERROR_MSG) quotient, remainder = numpy.divmod(x1.tonumpy(), x2.tonumpy(), where=where, **kwargs) return numpoly.polynomial(quotient), numpoly.polynomial(remainder)
def test_zeros_like(func_interface): """Tests for numpoly.zeros_like.""" poly = numpoly.polynomial([1, X, 2]) assert_equal(func_interface.zeros_like(poly), [0, 0, 0]) assert_equal(numpoly.zeros_like([1, X, 2]), [0, 0, 0]) poly = numpoly.polynomial([1., X, 2]) assert_equal(func_interface.zeros_like(poly), [0., 0., 0.])
def test_full_like(func_interface): """Tests for numpoly.full_like.""" poly = numpoly.polynomial([1, X, 2]) assert_equal(func_interface.full_like(poly, X), [X, X, X]) assert_equal(numpoly.full_like([1, X, 2], X), [X, X, X]) poly = numpoly.polynomial([1., X, 2]) assert_equal(func_interface.full_like(poly, Y), [1. * Y, Y, Y])
def test_add(interface): """Tests for numpoly.add.""" assert_equal(interface.add(X, 3), 3 + X) assert_equal(interface.add(polynomial([1, X, Y]), 4), [5, 4 + X, 4 + Y]) assert_equal(interface.add(polynomial([0, X]), polynomial([Y, 0])), [Y, X]) assert_equal(interface.add(polynomial([[1, X], [Y, X * Y]]), [2, X]), [[3, 2 * X], [2 + Y, X + X * Y]])
def test_copyto(func_interface): """Tests for numpoly.copyto.""" poly = numpoly.polynomial([1, X, Y]) poly_ref = numpoly.polynomial([1, X, Y]) with raises(ValueError): func_interface.copyto(poly.values, poly_ref, casting="safe") with raises(ValueError): numpoly.copyto(poly.values, [1, 2, 3], casting="safe") with raises(ValueError): numpoly.copyto(X, Y, casting="unsafe") func_interface.copyto(poly, X) assert_equal(poly, [X, X, X]) func_interface.copyto(poly.values, poly_ref, casting="unsafe") assert_equal(poly, poly_ref) func_interface.copyto(poly, 4) assert_equal(poly, [4, 4, 4]) func_interface.copyto(poly.values, poly_ref.values, casting="unsafe") assert_equal(poly, poly_ref) poly = numpoly.polynomial([1, 2, 3]) func_interface.copyto(poly, [3, 2, 1], casting="unsafe") assert_equal(poly, [3, 2, 1]) func_interface.copyto(poly.values, numpoly.polynomial([1, 2, 3]), casting="unsafe") assert_equal(poly, [1, 2, 3]) out = numpy.zeros(3, dtype=float) numpoly.copyto(out, poly, casting="unsafe") assert_equal(out, [1., 2., 3.])
def test_ones_like(func_interface): """Tests for numpoly.ones_like.""" poly = numpoly.polynomial([1, X, 2]) assert_equal(func_interface.ones_like(poly), [1, 1, 1]) assert_equal(numpoly.ones_like([1, X, 2]), [1, 1, 1]) poly = numpoly.polynomial([1., X, 2]) assert_equal(func_interface.ones_like(poly), [1., 1., 1.])
def test_count_nonzero(func_interface): """Tests for numpoly.count_nonzero.""" poly1 = polynomial([[0, Y], [X, 1]]) poly2 = polynomial([[0, Y, X, 0, 0], [3, 0, 0, 2, 19]]) assert_equal(func_interface.count_nonzero(poly1), 3, type_=int) assert_equal(func_interface.count_nonzero(poly1, axis=0), [1, 2]) assert_equal(func_interface.count_nonzero(poly2, axis=0), [1, 1, 1, 1, 1]) assert_equal(func_interface.count_nonzero(X), 1, type_=int)
def test_where(func_interface): """Tests for numpoly.where.""" poly1 = numpoly.polynomial([0, 4, 0]) poly2 = numpoly.polynomial([Y, 0, X]) assert_equal(func_interface.where([0, 1, 0], poly1, poly2), [Y, 4, X]) assert_equal(func_interface.where([1, 0, 1], poly1, poly2), [0, 0, 0]) assert_equal(func_interface.where(poly1)[0], [1]) assert_equal(func_interface.where(poly2)[0], [0, 2])
def test_numpy_allclose(func_interface): poly1 = numpoly.polynomial([1e10 * X, 1e-7]) poly2 = numpoly.polynomial([1.00001e10 * X, 1e-8]) assert not func_interface.allclose(poly1, poly2) poly1 = numpoly.polynomial([1e10 * X, 1e-8]) poly2 = numpoly.polynomial([1.00001e10 * X, 1e-9]) assert func_interface.allclose(poly1, poly2) poly2 = numpoly.polynomial([1e10 * Y, 1e-8]) assert not func_interface.allclose(poly1, poly2)
def test_numpy_logical_and(func_interface): poly1 = numpoly.polynomial([0, X]) poly2 = numpoly.polynomial([1, X]) poly3 = numpoly.polynomial([0, Y]) assert numpy.all(func_interface.logical_and(1, poly1) == [False, True]) assert numpy.all(1 and poly1 == [0, X]) assert numpy.all(func_interface.logical_and(1, poly2) == [True, True]) assert numpy.all(1 and poly2 == [1, X]) assert numpy.all(func_interface.logical_and(poly2, poly3) == [False, True])
def test_numpy_logical_or(func_interface): poly1 = numpoly.polynomial([0, X]) poly2 = numpoly.polynomial([1, X]) poly3 = numpoly.polynomial([0, Y]) assert numpy.all(func_interface.logical_or(1, poly1) == [True, True]) assert numpy.all(1 or poly1 == [1, 1]) assert numpy.all(func_interface.logical_or(0, poly1) == [False, True]) assert numpy.all(0 or poly1 == [0, X]) assert numpy.all(func_interface.logical_or(poly2, poly3) == [True, True])
def test_numpy_isclose(func_interface): poly1 = numpoly.polynomial([1e10 * X, 1e-7]) poly2 = numpoly.polynomial([1.00001e10 * X, 1e-8]) assert numpy.all(func_interface.isclose(poly1, poly2) == [True, False]) poly1 = numpoly.polynomial([1e10 * X, 1e-8]) poly2 = numpoly.polynomial([1.00001e10 * X, 1e-9]) assert numpy.all(func_interface.isclose(poly1, poly2) == [True, True]) poly2 = numpoly.polynomial([1e10 * Y, 1e-8]) assert numpy.all(func_interface.isclose(poly1, poly2) == [False, True])
def test_numpy_add(interface): assert interface.add(X, 3) == 3 + X assert numpy.all( interface.add(polynomial([1, X, Y]), 4) == [5, 4 + X, 4 + Y]) assert numpy.all( interface.add(polynomial([0, X]), polynomial([Y, 0])) == [Y, X]) assert numpy.all( interface.add(polynomial([[1, X], [Y, X * Y]]), [2, X]) == [[3, 2 * X], [2 + Y, X + X * Y]])
def test_numpy_array_str(func_interface): assert str(4 + 6 * X**2) == "4+6*X**2" assert func_interface.array_str(4 + 6 * X**2) == "4+6*X**2" assert str(polynomial([1., -5 * X, 3 - X**2])) == "[1.0 -5.0*X 3.0-X**2]" assert func_interface.array_str(polynomial([1., -5 * X, 3 - X**2 ])) == "[1.0 -5.0*X 3.0-X**2]" assert str(polynomial([[[1, 2], [5, Y]]])) == """\ [[[1 2] [5 Y]]]""" assert func_interface.array_str(polynomial([[[1, 2], [5, Y]]])) == """\
def test_allclose(func_interface): """Tests for numpoly.allclose.""" poly1 = numpoly.polynomial([1e10 * X, 1e-7]) poly2 = numpoly.polynomial([1.00001e10 * X, 1e-8]) assert_equal(func_interface.allclose(poly1, poly2), False, type_=bool) poly1 = numpoly.polynomial([1e10 * X, 1e-8]) poly2 = numpoly.polynomial([1.00001e10 * X, 1e-9]) assert_equal(func_interface.allclose(poly1, poly2), True, type_=bool) poly2 = numpoly.polynomial([1e10 * Y, 1e-8]) assert_equal(func_interface.allclose(poly1, poly2), False, type_=bool)
def test_isclose(func_interface): """Tests for numpoly.isclose.""" poly1 = numpoly.polynomial([1e10 * X, 1e-7]) poly2 = numpoly.polynomial([1.00001e10 * X, 1e-8]) assert_equal(func_interface.isclose(poly1, poly2), [True, False]) poly1 = numpoly.polynomial([1e10 * X, 1e-8]) poly2 = numpoly.polynomial([1.00001e10 * X, 1e-9]) assert_equal(func_interface.isclose(poly1, poly2), [True, True]) poly2 = numpoly.polynomial([1e10 * Y, 1e-8]) assert_equal(func_interface.isclose(poly1, poly2), [False, True])
def test_logical_and(func_interface): """Tests for numpoly.logical_and.""" poly1 = numpoly.polynomial([0, X]) poly2 = numpoly.polynomial([1, X]) poly3 = numpoly.polynomial([0, Y]) assert_equal(func_interface.logical_and(1, poly1), [False, True]) assert_equal(1 and poly1, poly1) assert_equal(func_interface.logical_and(1, poly2), [True, True]) assert_equal(1 and poly2, poly2) assert_equal(func_interface.logical_and(poly2, poly3), [False, True])
def test_logical_or(func_interface): """Tests for numpoly.logical_or.""" poly1 = numpoly.polynomial([0, X]) poly2 = numpoly.polynomial([1, X]) poly3 = numpoly.polynomial([0, Y]) assert_equal(func_interface.logical_or(1, poly1), [True, True]) assert_equal(1 or poly1, 1, type_=int) assert_equal(func_interface.logical_or(0, poly1), [False, True]) assert_equal(0 or poly1, poly1) assert_equal(func_interface.logical_or(poly2, poly3), [True, True])
def test_poly_divide(): const = numpoly.polynomial([1, 2]) poly1 = numpoly.polynomial(X) poly2 = numpoly.polynomial([1, X]) poly3 = numpoly.polynomial([[0, Y], [X, 1]]) assert numpy.all(numpoly.poly_divide(poly1, const) == [X, 0.5 * X]) assert numpy.all(numpoly.poly_divide(const, poly1) == [0, 0]) assert numpy.all(poly1.__truediv__(poly2) == [X, 1]) assert numpy.all(poly2.__rtruediv__(poly3) == [[0, 0], [X, 0]]) assert numpy.all(poly2.__div__(poly1) == [0, 1]) assert numpy.all(poly3.__rdiv__(poly2) == [[0, 0], [0, X]])
def test_not_equal(interface): """Tests for numpoly.not_equal.""" poly = polynomial([[0, 2 + Y], [X, 2]]) assert_equal(([X, 2 + Y] != poly), [[True, False], [False, True]]) assert_equal(interface.not_equal(numpoly.polynomial([X, 2 + Y]), poly), [[True, False], [False, True]]) assert_equal((X != poly), [[True, True], [False, True]]) assert_equal(interface.not_equal(X, poly), [[True, True], [False, True]]) assert_equal(poly != poly.T, [[False, True], [True, False]]) assert_equal(interface.not_equal(poly, poly.T), [[False, True], [True, False]])
def test_true_divide(func_interface): """Tests for numpoly.true_divide.""" poly = polynomial([[0, Y], [X, 1]]) assert_equal(func_interface.true_divide(poly, 2), polynomial([[0, 0.5 * Y], [0.5 * X, 0.5]])) assert_equal(func_interface.true_divide(poly, [1, 2]), [[0, 0.5 * Y], [X, 0.5]]) assert_equal(func_interface.true_divide(poly, [[1, 2], [2, 1]]), [[0, 0.5 * Y], [0.5 * X, 1]]) with raises(numpoly.FeatureNotSupported): func_interface.true_divide(poly, X)
def test_numpy_divide(func_interface): poly = polynomial([[0, Y], [X, 1]]) assert numpy.all(poly / 2 == polynomial([[0, 0.5 * Y], [0.5 * X, 0.5]])) assert numpy.all( func_interface.divide(poly, 2) == [[0, 0.5 * Y], [0.5 * X, 0.5]]) assert numpy.all(poly / [1, 2] == [[0, 0.5 * Y], [X, 0.5]]) assert numpy.all( func_interface.divide(poly, [1, 2]) == [[0, 0.5 * Y], [X, 0.5]]) assert numpy.all(poly / [[1, 2], [2, 1]] == [[0, 0.5 * Y], [0.5 * X, 1]]) assert numpy.all( func_interface.divide(poly, [[1, 2], [2, 1]]) == [[0, 0.5 * Y], [0.5 * X, 1]])
def test_array_str(func_interface): """Tests for numpoly.array_str.""" assert str(polynomial([])).startswith("[]") assert str(4 + 6 * X**2) == "6*q0**2+4" assert func_interface.array_str(4 + 6 * X**2) == "6*q0**2+4" assert str(polynomial([1., -5 * X, 3 - X**2])) == "[1.0 -5.0*q0 -q0**2+3.0]" assert func_interface.array_str(polynomial( [1., -5 * X, 3 - X**2])) == "[1.0 -5.0*q0 -q0**2+3.0]" assert str(polynomial([[[1, 2], [5, Y]]])) == """\ [[[1 2] [5 q1]]]""" assert func_interface.array_str(polynomial([[[1, 2], [5, Y]]])) == """\
def test_diag(func_interface): """Tests for numpoly.diag.""" poly = polynomial([[1, 2, X], [4, Y, 6], [7, 8, X + Y]]) assert_equal(func_interface.diag(poly), [1, Y, X + Y]) assert_equal(func_interface.diag(poly, k=1), [2, 6]) assert_equal(func_interface.diag(poly, k=-1), [4, 8]) poly = polynomial([X, Y]) assert_equal(func_interface.diag(poly), [[X, 0], [0, Y]]) assert_equal(func_interface.diag(poly, k=1), [[0, X, 0], [0, 0, Y], [0, 0, 0]]) assert_equal(func_interface.diag(poly, k=-1), [[0, 0, 0], [X, 0, 0], [0, Y, 0]])
def test_numpy_matmul(func_interface): poly1 = numpoly.polynomial([[0, X], [1, Y]]) poly2 = numpoly.polynomial([X, 2]) assert numpy.all( func_interface.matmul(poly1, poly2) == [[X**2, 2 * X], [X + X * Y, 2 + 2 * Y]]) assert func_interface.matmul(numpy.zeros((9, 5, 7, 4)), numpy.ones( (9, 5, 4, 3))).shape == (9, 5, 7, 3) with raises(ValueError): func_interface.matmul(poly1, 4) with raises(ValueError): func_interface.matmul(3, poly2)
def test_matmul(func_interface): """Tests for numpoly.matmul.""" poly1 = numpoly.polynomial([[0, X], [1, Y]]) poly2 = numpoly.polynomial([X, 2]) assert_equal(func_interface.matmul(poly1, poly2), [[X**2, 2 * X], [X + X * Y, 2 + 2 * Y]]) assert_equal( func_interface.matmul(numpy.ones((2, 5, 6, 4)), numpy.ones( (2, 5, 4, 3))), 4 * numpy.ones((2, 5, 6, 3))) with raises(ValueError): func_interface.matmul(poly1, 4) with raises(ValueError): func_interface.matmul(3, poly2)
def test_equal(interface): """Tests for numpoly.equal.""" poly = polynomial([[0, 2 + Y], [X, 2]]) assert_equal(interface.equal(X, X), True) assert_equal(interface.equal(X, [X]), [True]) assert_equal([X] == X, [True]) assert_equal(interface.equal(X, Y), False) assert_equal(([X, 2 + Y] == poly), [[False, True], [True, False]]) assert_equal(interface.equal(numpoly.polynomial([X, 2 + Y]), poly), [[False, True], [True, False]]) assert_equal((X == poly), [[False, False], [True, False]]) assert_equal(interface.equal(X, poly), [[False, False], [True, False]]) assert_equal(poly == poly.T, [[True, False], [False, True]]) assert_equal(interface.equal(poly, poly.T), [[True, False], [False, True]])
def ones( shape: Union[int, Sequence[int]], dtype: numpy.typing.DTypeLike = float, order: Order = "C", ) -> ndpoly: """ Return a new array of given shape and type, filled with ones. Args: shape: Shape of the new array, e.g., ``(2, 3)`` or ``2``. dtype: The desired data-type for the array, e.g., `numpy.int8`. Default is `numpy.float64`. order: Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. Returns: Array of ones with the given shape, dtype, and order. Examples: >>> numpoly.ones(5) polynomial([1.0, 1.0, 1.0, 1.0, 1.0]) """ return numpoly.polynomial(numpy.ones(shape, dtype=dtype, order=order))
def test_numpy_sum(interface): poly = polynomial([[1, 5 * X], [X + 3, -Y]]) assert interface.sum(poly) == -Y + X * 6 + 4 assert numpy.all(interface.sum(poly, axis=0) == [X + 4, -Y + X * 5]) assert numpy.all( interface.sum(poly, axis=-1, keepdims=True) == [[X * 5 + 1], [X - Y + 3]])
def test_numpy_mean(interface): poly = numpoly.polynomial([[1, 2 * X], [3 * Y + X, 4]]) assert interface.mean(poly) == 1.25 + 0.75 * Y + 0.75 * X assert numpy.all( interface.mean(poly, axis=0) == [0.5 + 1.5 * Y + 0.5 * X, 2.0 + X]) assert numpy.all( interface.mean(poly, axis=1) == [0.5 + X, 2.0 + 1.5 * Y + 0.5 * X])