def test_intersections(): assert S.Integers.intersection(S.Reals) == S.Integers assert 5 in S.Integers.intersection(S.Reals) assert 5 in S.Integers.intersection(S.Reals) assert -5 not in S.Naturals.intersection(S.Reals) assert 5.5 not in S.Integers.intersection(S.Reals) assert 5 in S.Integers.intersection(Interval(3, oo)) assert -5 in S.Integers.intersection(Interval(-oo, 3)) assert all(x.is_Integer for x in itertools.islice(S.Integers.intersection(Interval(3, oo)), 10))
def test_intersections(): assert S.Integers.intersect(S.Reals) == S.Integers assert 5 in S.Integers.intersect(S.Reals) assert 5 in S.Integers.intersect(S.Reals) assert -5 not in S.Naturals.intersect(S.Reals) assert 5.5 not in S.Integers.intersect(S.Reals) assert 5 in S.Integers.intersect(Interval(3, oo)) assert -5 in S.Integers.intersect(Interval(-oo, 3)) assert all(x.is_Integer for x in take(10, S.Integers.intersect(Interval(3, oo))))
def test_halfcircle(): r, th = symbols('r, theta', extended_real=True) L = Lambda((r, th), (r*cos(th), r*sin(th))) halfcircle = ImageSet(L, Interval(0, 1)*Interval(0, pi)) assert (1, 0) in halfcircle assert (0, -1) not in halfcircle assert (0, 0) in halfcircle assert not halfcircle.is_iterable
def test_halfcircle(): # This test sometimes works and sometimes doesn't. # It may be an issue with solve? Maybe with using Lambdas/dummys? # I believe the code within fancysets is correct r, th = symbols('r, theta', extended_real=True) L = Lambda((r, th), (r*cos(th), r*sin(th))) halfcircle = ImageSet(L, Interval(0, 1)*Interval(0, pi)) assert (1, 0) in halfcircle assert (0, -1) not in halfcircle assert (0, 0) in halfcircle assert not halfcircle.is_iterable
def _intersect(self, other): from diofant.functions.elementary.integers import floor, ceiling from diofant.functions.elementary.miscellaneous import Min, Max if other.is_Interval: osup = other.sup oinf = other.inf # if other is [0, 10) we can only go up to 9 if osup.is_integer and other.right_open: osup -= 1 if oinf.is_integer and other.left_open: oinf += 1 # Take the most restrictive of the bounds set by the two sets # round inwards inf = ceiling(Max(self.inf, oinf)) sup = floor(Min(self.sup, osup)) # if we are off the sequence, get back on if inf.is_finite and self.inf.is_finite: off = (inf - self.inf) % self.step else: off = S.Zero if off: inf += self.step - off return Range(inf, sup + 1, self.step) if other == S.Naturals: return self._intersect(Interval(1, S.Infinity, False, True)) if other == S.Integers: return self return
def test_naturals(): N = S.Naturals assert 5 in N assert -5 not in N assert 5.5 not in N ni = iter(N) a, b, c, d = next(ni), next(ni), next(ni), next(ni) assert (a, b, c, d) == (1, 2, 3, 4) assert isinstance(a, Basic) assert N.intersect(Interval(-5, 5)) == Range(1, 6) assert N.intersect(Interval(-5, 5, True, True)) == Range(1, 5) assert N.boundary == N assert N.inf == 1 assert N.sup == oo
def test_integers(): Z = S.Integers assert 5 in Z assert -5 in Z assert 5.5 not in Z zi = iter(Z) a, b, c, d = next(zi), next(zi), next(zi), next(zi) assert (a, b, c, d) == (0, 1, -1, 2) assert isinstance(a, Basic) assert Z.intersect(Interval(-5, 5)) == Range(-5, 6) assert Z.intersect(Interval(-5, 5, True, True)) == Range(-4, 5) assert Z.inf == -oo assert Z.sup == oo assert Z.boundary == Z
def _intersect(self, other): from diofant.functions.elementary.integers import floor, ceiling if other is Interval(S.NegativeInfinity, S.Infinity, True, True) or other is S.Reals: return self elif other.is_Interval: s = Range(ceiling(other.left), floor(other.right) + 1) return s.intersect(other) # take out endpoints if open interval return
def test_univariate_relational_as_set(): assert (x > 0).as_set() == Interval(0, oo, True, True) assert (x >= 0).as_set() == Interval(0, oo, False, True) assert (x < 0).as_set() == Interval(-oo, 0, True, True) assert (x <= 0).as_set() == Interval(-oo, 0, True) assert Eq(x, 0).as_set() == FiniteSet(0) assert Ne(x, 0).as_set() == Interval(-oo, 0, True, True) + \ Interval(0, oo, True, True) assert (x**2 >= 4).as_set() == (Interval(-oo, -2, True) + Interval(2, oo, False, True))
def test_range_interval_intersection(): # Intersection with intervals assert FiniteSet(*Range(0, 10, 1).intersect(Interval(2, 6))) == \ FiniteSet(2, 3, 4, 5, 6) # Open Intervals are removed assert (FiniteSet(*Range(0, 10, 1).intersect(Interval(2, 6, True, True))) == FiniteSet(3, 4, 5)) # Try this with large steps assert (FiniteSet(*Range(0, 100, 10).intersect(Interval(15, 55))) == FiniteSet(20, 30, 40, 50)) # Going backwards assert (FiniteSet(*Range(10, -9, -3).intersect(Interval(-5, 6))) == FiniteSet(-5, -2, 1, 4)) assert (FiniteSet(*Range(10, -9, -3).intersect(Interval(-5, 6, True))) == FiniteSet(-2, 1, 4))
def test_integers(): Z = S.Integers assert 5 in Z assert -5 in Z assert 5.5 not in Z zi = iter(Z) a, b, c, d = next(zi), next(zi), next(zi), next(zi) assert (a, b, c, d) == (0, 1, -1, 2) assert isinstance(a, Basic) assert Z.intersection(Interval(-5, 5)) == Range(-5, 6) assert Z.intersection(Interval(-5, 5, True, True)) == Range(-4, 5) assert Z.intersection(Set(x)) == Intersection(Z, Set(x), evaluate=False) assert Z.inf == -oo assert Z.sup == oo assert Z.boundary == Z assert imageset(Lambda((x, y), x * y), Z) == ImageSet(Lambda((x, y), x * y), Z)
def test_sympyissue_11732(): interval12 = Interval(1, 2) finiteset1234 = FiniteSet(1, 2, 3, 4) pointComplex = (1, 2) assert (interval12 in S.Naturals) is False assert (interval12 in S.Naturals0) is False assert (interval12 in S.Integers) is False assert (finiteset1234 in S.Naturals) is False assert (finiteset1234 in S.Naturals0) is False assert (finiteset1234 in S.Integers) is False assert (pointComplex in S.Naturals) is False assert (pointComplex in S.Naturals0) is False assert (pointComplex in S.Integers) is False
def test_ImageSet(): squares = ImageSet(Lambda(x, x**2), S.Naturals) assert 4 in squares assert 5 not in squares assert FiniteSet(*range(10)).intersect(squares) == FiniteSet(1, 4, 9) assert 16 not in squares.intersect(Interval(0, 10)) si = iter(squares) a, b, c, d = next(si), next(si), next(si), next(si) assert (a, b, c, d) == (1, 4, 9, 16) harmonics = ImageSet(Lambda(x, 1/x), S.Naturals) assert Rational(1, 5) in harmonics assert Rational(.25) in harmonics assert 0.25 not in harmonics assert Rational(.3) not in harmonics assert harmonics.is_iterable
def _intersect(self, other): if other.is_Interval: return Intersection(S.Integers, other, Interval(self._inf, S.Infinity, False, True)) return
def bspline_basis(d, knots, n, x, close=True): """The `n`-th B-spline at `x` of degree `d` with knots. B-Splines are piecewise polynomials of degree `d` [1]_. They are defined on a set of knots, which is a sequence of integers or floats. The 0th degree splines have a value of one on a single interval: >>> from diofant import bspline_basis >>> from diofant.abc import x >>> d = 0 >>> knots = range(5) >>> bspline_basis(d, knots, 0, x) Piecewise((1, And(x <= 1, x >= 0)), (0, true)) For a given ``(d, knots)`` there are ``len(knots)-d-1`` B-splines defined, that are indexed by ``n`` (starting at 0). Here is an example of a cubic B-spline: >>> bspline_basis(3, range(5), 0, x) Piecewise((x**3/6, And(x < 1, x >= 0)), (-x**3/2 + 2*x**2 - 2*x + 2/3, And(x < 2, x >= 1)), (x**3/2 - 4*x**2 + 10*x - 22/3, And(x < 3, x >= 2)), (-x**3/6 + 2*x**2 - 8*x + 32/3, And(x <= 4, x >= 3)), (0, true)) By repeating knot points, you can introduce discontinuities in the B-splines and their derivatives: >>> d = 1 >>> knots = [0,0,2,3,4] >>> bspline_basis(d, knots, 0, x) Piecewise((-x/2 + 1, And(x <= 2, x >= 0)), (0, true)) It is quite time consuming to construct and evaluate B-splines. If you need to evaluate a B-splines many times, it is best to lambdify them first: >>> from diofant import lambdify >>> d = 3 >>> knots = range(10) >>> b0 = bspline_basis(d, knots, 0, x) >>> f = lambdify(x, b0) >>> y = f(0.5) See Also ======== diofant.functions.special.bsplines.bspline_basis_set References ========== .. [1] http://en.wikipedia.org/wiki/B-spline """ knots = [sympify(k) for k in knots] d = int(d) n = int(n) n_knots = len(knots) n_intervals = n_knots - 1 if n + d + 1 > n_intervals: raise ValueError('n + d + 1 must not exceed len(knots) - 1') if d == 0: result = Piecewise( (S.One, Interval(knots[n], knots[n + 1], False, not close).contains(x)), (0, True)) elif d > 0: denom = knots[n + d + 1] - knots[n + 1] if denom != S.Zero: B = (knots[n + d + 1] - x) / denom b2 = bspline_basis(d - 1, knots, n + 1, x, close) else: b2 = B = S.Zero denom = knots[n + d] - knots[n] if denom != S.Zero: A = (x - knots[n]) / denom b1 = bspline_basis(d - 1, knots, n, x, close and (B == S.Zero or b2 == S.Zero)) else: b1 = A = S.Zero result = _add_splines(A, b1, B, b2) else: raise ValueError('degree must be non-negative: %r' % n) return result
def test_core_interval(): for c in (Interval, Interval(0, 2)): check(c)
def __new__(cls): return Interval.__new__(cls, -S.Infinity, S.Infinity, True, True)
def test_multivariate_relational_as_set(): assert (x*y >= 0).as_set() == Interval(0, oo)*Interval(0, oo) + \ Interval(-oo, 0)*Interval(-oo, 0)