def test_intersections(): 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(): # 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', real=True) L = Lambda((r, th), (r * cos(th), r * sin(th))) halfcircle = TransformationSet(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_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.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
def _intersect(self, other): 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 off = (inf - self.inf) % self.step if off: inf += self.step - off return Range(inf, sup + 1, self.step) if other == S.Naturals: return self._intersect(Interval(1, oo)) if other == S.Integers: return self return None
def test_univariate_relational_as_set(): assert (x > 0).as_set() == Interval(0, oo, True, True) assert (x >= 0).as_set() == Interval(0, oo) assert (x < 0).as_set() == Interval(-oo, 0, True, True) assert (x <= 0).as_set() == Interval(-oo, 0) 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) + Interval(2, oo)
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_TransformationSet(): squares = TransformationSet(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 = TransformationSet(Lambda(x, 1 / x), S.Naturals) assert Rational(1, 5) in harmonics assert .25 in harmonics assert .3 not in harmonics assert harmonics.is_iterable
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 sympy import bspline_basis >>> from sympy.abc import x >>> d = 0 >>> knots = range(5) >>> bspline_basis(d, knots, 0, x) Piecewise((1, [0, 1]), (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, [0, 1)), (2/3 - 2*x + 2*x**2 - x**3/2, [1, 2)), (-22/3 + 10*x - 4*x**2 + x**3/2, [2, 3)), (32/3 - 8*x + 2*x**2 - x**3/6, [3, 4]), (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((1 - x/2, [0, 2]), (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 sympy import lambdify >>> d = 3 >>> knots = range(10) >>> b0 = bspline_basis(d, knots, 0, x) >>> f = lambdify(x, b0) >>> y = f(0.5) [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, True)), (0, True)) elif d > 0: denom = knots[n + d] - knots[n] if denom != S.Zero: A = (x - knots[n]) / denom b1 = bspline_basis(d - 1, knots, n, x, close=False) else: b1 = A = S.Zero 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=False) else: b2 = B = S.Zero result = _add_splines(A, b1, B, b2) else: raise ValueError('degree must be non-negative: %r' % n) if close: final_ec_pair = result.args[-2] final_cond = final_ec_pair.cond final_expr = final_ec_pair.expr new_args = final_cond.args[:3] + (False, ) new_ec_pair = ExprCondPair(final_expr, Interval(*new_args)) new_args = result.args[:-2] + (new_ec_pair, result.args[-1]) result = Piecewise(*new_args) return result
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 sympy import bspline_basis >>> from sympy.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 sympy import lambdify >>> d = 3 >>> knots = range(10) >>> b0 = bspline_basis(d, knots, 0, x) >>> f = lambdify(x, b0) >>> y = f(0.5) See Also ======== bsplines_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_sympy__core__sets__Union(): from sympy.core.sets import Union, Interval assert _test_args(Union(Interval(0, 1), Interval(2, 3)))
def test_sympy__core__sets__ProductSet(): from sympy.core.sets import ProductSet, Interval assert _test_args(ProductSet(Interval(0, 1), Interval(0, 1)))
def test_sympy__core__sets__Interval(): from sympy.core.sets import Interval assert _test_args(Interval(0, 1))
def test_sympy__physics__quantum__hilbert__L2(): from sympy.physics.quantum.hilbert import L2 from sympy import oo, Interval assert _test_args(L2(Interval(0, oo)))
def __new__(cls): return Interval.__new__(cls, -S.Infinity, S.Infinity)
def _intersect(self, other): if other.is_Interval: return Intersection(S.Integers, other, Interval(self._inf, S.Infinity)) return None
def __new__(cls): return Interval.__new__(cls, -oo, oo)
def test_multivariate_relational_as_set(): assert (x*y >= 0).as_set() == Interval(0, oo)*Interval(0, oo) + \ Interval(-oo, 0)*Interval(-oo, 0)
def test_core_interval(): for c in (Interval, Interval(0, 2)): check(c)
def _intersect(self, other): if other.is_Interval: return Intersection(S.Integers, other, Interval(1, oo)) return None