Subsets of a Universe Defined by Predicates#

class sage.sets.condition_set.ConditionSet(universe, *predicates, names=None, category=None)[source]#

Bases: Set_generic, Set_base, Set_boolean_operators, Set_add_sub_operators, UniqueRepresentation

Set of elements of a universe that satisfy given predicates

INPUT:

  • universe – a set

  • *predicates – callables

  • vars or names – (default: inferred from predicates if any predicate is an element of a CallableSymbolicExpressionRing_class) variables or names of variables

  • category – (default: inferred from universe) a category

EXAMPLES:

sage: Evens = ConditionSet(ZZ, is_even); Evens
{ x ∈ Integer Ring : <function is_even at 0x...>(x) }
sage: 2 in Evens
True
sage: 3 in Evens
False
sage: 2.0 in Evens
True

sage: Odds = ConditionSet(ZZ, is_odd); Odds
{ x ∈ Integer Ring : <function is_odd at 0x...>(x) }
sage: EvensAndOdds = Evens | Odds; EvensAndOdds
Set-theoretic union of
 { x ∈ Integer Ring : <function is_even at 0x...>(x) } and
 { x ∈ Integer Ring : <function is_odd at 0x...>(x) }
sage: 5 in EvensAndOdds
True
sage: 7/2 in EvensAndOdds
False

sage: var('y')                                                                  # needs sage.symbolic
y
sage: SmallOdds = ConditionSet(ZZ, is_odd, abs(y) <= 11, vars=[y]); SmallOdds   # needs sage.symbolic
{ y ∈ Integer Ring : abs(y) <= 11, <function is_odd at 0x...>(y) }

sage: # needs sage.geometry.polyhedron
sage: P = polytopes.cube(); P
A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 8 vertices
sage: P.rename("P")
sage: P_inter_B = ConditionSet(P, lambda x: x.norm() < 1.2); P_inter_B
{ x ∈ P : <function <lambda> at 0x...>(x) }
sage: vector([1, 0, 0]) in P_inter_B
True
sage: vector([1, 1, 1]) in P_inter_B                                            # needs sage.symbolic
False

sage: # needs sage.symbolic
sage: predicate(x, y, z) = sqrt(x^2 + y^2 + z^2) < 1.2; predicate
(x, y, z) |--> sqrt(x^2 + y^2 + z^2) < 1.20000000000000
sage: P_inter_B_again = ConditionSet(P, predicate); P_inter_B_again             # needs sage.geometry.polyhedron
{ (x, y, z) ∈ P : sqrt(x^2 + y^2 + z^2) < 1.20000000000000 }
sage: vector([1, 0, 0]) in P_inter_B_again                                      # needs sage.geometry.polyhedron
True
sage: vector([1, 1, 1]) in P_inter_B_again                                      # needs sage.geometry.polyhedron
False
>>> from sage.all import *
>>> Evens = ConditionSet(ZZ, is_even); Evens
{ x ∈ Integer Ring : <function is_even at 0x...>(x) }
>>> Integer(2) in Evens
True
>>> Integer(3) in Evens
False
>>> RealNumber('2.0') in Evens
True

>>> Odds = ConditionSet(ZZ, is_odd); Odds
{ x ∈ Integer Ring : <function is_odd at 0x...>(x) }
>>> EvensAndOdds = Evens | Odds; EvensAndOdds
Set-theoretic union of
 { x ∈ Integer Ring : <function is_even at 0x...>(x) } and
 { x ∈ Integer Ring : <function is_odd at 0x...>(x) }
>>> Integer(5) in EvensAndOdds
True
>>> Integer(7)/Integer(2) in EvensAndOdds
False

>>> var('y')                                                                  # needs sage.symbolic
y
>>> SmallOdds = ConditionSet(ZZ, is_odd, abs(y) <= Integer(11), vars=[y]); SmallOdds   # needs sage.symbolic
{ y ∈ Integer Ring : abs(y) <= 11, <function is_odd at 0x...>(y) }

>>> # needs sage.geometry.polyhedron
>>> P = polytopes.cube(); P
A 3-dimensional polyhedron in ZZ^3 defined as the convex hull of 8 vertices
>>> P.rename("P")
>>> P_inter_B = ConditionSet(P, lambda x: x.norm() < RealNumber('1.2')); P_inter_B
{ x ∈ P : <function <lambda> at 0x...>(x) }
>>> vector([Integer(1), Integer(0), Integer(0)]) in P_inter_B
True
>>> vector([Integer(1), Integer(1), Integer(1)]) in P_inter_B                                            # needs sage.symbolic
False

>>> # needs sage.symbolic
>>> __tmp__=var("x,y,z"); predicate = symbolic_expression(sqrt(x**Integer(2) + y**Integer(2) + z**Integer(2)) < RealNumber('1.2')).function(x,y,z); predicate
(x, y, z) |--> sqrt(x^2 + y^2 + z^2) < 1.20000000000000
>>> P_inter_B_again = ConditionSet(P, predicate); P_inter_B_again             # needs sage.geometry.polyhedron
{ (x, y, z) ∈ P : sqrt(x^2 + y^2 + z^2) < 1.20000000000000 }
>>> vector([Integer(1), Integer(0), Integer(0)]) in P_inter_B_again                                      # needs sage.geometry.polyhedron
True
>>> vector([Integer(1), Integer(1), Integer(1)]) in P_inter_B_again                                      # needs sage.geometry.polyhedron
False

Iterating over subsets determined by predicates:

sage: Odds = ConditionSet(ZZ, is_odd); Odds
{ x ∈ Integer Ring : <function is_odd at 0x...>(x) }
sage: list(Odds.iterator_range(stop=6))
[1, -1, 3, -3, 5, -5]

sage: R = IntegerModRing(8)
sage: R_primes = ConditionSet(R, is_prime); R_primes
{ x ∈ Ring of integers modulo 8 : <function is_prime at 0x...>(x) }
sage: R_primes.is_finite()
True
sage: list(R_primes)
[2, 6]
>>> from sage.all import *
>>> Odds = ConditionSet(ZZ, is_odd); Odds
{ x ∈ Integer Ring : <function is_odd at 0x...>(x) }
>>> list(Odds.iterator_range(stop=Integer(6)))
[1, -1, 3, -3, 5, -5]

>>> R = IntegerModRing(Integer(8))
>>> R_primes = ConditionSet(R, is_prime); R_primes
{ x ∈ Ring of integers modulo 8 : <function is_prime at 0x...>(x) }
>>> R_primes.is_finite()
True
>>> list(R_primes)
[2, 6]

Using ConditionSet without predicates provides a way of attaching variable names to a set:

sage: Z3 = ConditionSet(ZZ^3, vars=['x', 'y', 'z']); Z3                         # needs sage.modules
{ (x, y, z) ∈ Ambient free module of rank 3
               over the principal ideal domain Integer Ring }
sage: Z3.variable_names()                                                       # needs sage.modules
('x', 'y', 'z')
sage: Z3.arguments()                                                            # needs sage.modules sage.symbolic
(x, y, z)

sage: Q4.<a, b, c, d> = ConditionSet(QQ^4); Q4                                  # needs sage.modules sage.symbolic
{ (a, b, c, d) ∈ Vector space of dimension 4 over Rational Field }
sage: Q4.variable_names()                                                       # needs sage.modules sage.symbolic
('a', 'b', 'c', 'd')
sage: Q4.arguments()                                                            # needs sage.modules sage.symbolic
(a, b, c, d)
>>> from sage.all import *
>>> Z3 = ConditionSet(ZZ**Integer(3), vars=['x', 'y', 'z']); Z3                         # needs sage.modules
{ (x, y, z) ∈ Ambient free module of rank 3
               over the principal ideal domain Integer Ring }
>>> Z3.variable_names()                                                       # needs sage.modules
('x', 'y', 'z')
>>> Z3.arguments()                                                            # needs sage.modules sage.symbolic
(x, y, z)

>>> Q4 = ConditionSet(QQ**Integer(4), names=('a', 'b', 'c', 'd',)); (a, b, c, d,) = Q4._first_ngens(4); Q4                                  # needs sage.modules sage.symbolic
{ (a, b, c, d) ∈ Vector space of dimension 4 over Rational Field }
>>> Q4.variable_names()                                                       # needs sage.modules sage.symbolic
('a', 'b', 'c', 'd')
>>> Q4.arguments()                                                            # needs sage.modules sage.symbolic
(a, b, c, d)
ambient()[source]#

Return the universe of self.

EXAMPLES:

sage: Evens = ConditionSet(ZZ, is_even); Evens
{ x ∈ Integer Ring : <function is_even at 0x...>(x) }
sage: Evens.ambient()
Integer Ring
>>> from sage.all import *
>>> Evens = ConditionSet(ZZ, is_even); Evens
{ x ∈ Integer Ring : <function is_even at 0x...>(x) }
>>> Evens.ambient()
Integer Ring
arguments()[source]#

Return the variables of self as elements of the symbolic ring.

EXAMPLES:

sage: Odds = ConditionSet(ZZ, is_odd); Odds
{ x ∈ Integer Ring : <function is_odd at 0x...>(x) }
sage: args = Odds.arguments(); args                                         # needs sage.symbolic
(x,)
sage: args[0].parent()                                                      # needs sage.symbolic
Symbolic Ring
>>> from sage.all import *
>>> Odds = ConditionSet(ZZ, is_odd); Odds
{ x ∈ Integer Ring : <function is_odd at 0x...>(x) }
>>> args = Odds.arguments(); args                                         # needs sage.symbolic
(x,)
>>> args[Integer(0)].parent()                                                      # needs sage.symbolic
Symbolic Ring
intersection(X)[source]#

Return the intersection of self and X.

EXAMPLES:

sage: # needs sage.modules sage.symbolic
sage: in_small_oblong(x, y) = x^2 + 3 * y^2 <= 42
sage: SmallOblongUniverse = ConditionSet(QQ^2, in_small_oblong)
sage: SmallOblongUniverse
{ (x, y) ∈ Vector space of dimension 2
                    over Rational Field : x^2 + 3*y^2 <= 42 }
sage: parity_check(x, y) = abs(sin(pi/2*(x + y))) < 1/1000
sage: EvenUniverse = ConditionSet(ZZ^2, parity_check); EvenUniverse
{ (x, y) ∈ Ambient free module of rank 2 over the principal ideal
           domain Integer Ring : abs(sin(1/2*pi*x + 1/2*pi*y)) < (1/1000) }
sage: SmallOblongUniverse & EvenUniverse
{ (x, y) ∈ Free module of degree 2 and rank 2 over Integer Ring
Echelon basis matrix:
[1 0]
[0 1] : x^2 + 3*y^2 <= 42, abs(sin(1/2*pi*x + 1/2*pi*y)) < (1/1000) }
>>> from sage.all import *
>>> # needs sage.modules sage.symbolic
>>> __tmp__=var("x,y"); in_small_oblong = symbolic_expression(x**Integer(2) + Integer(3) * y**Integer(2) <= Integer(42)).function(x,y)
>>> SmallOblongUniverse = ConditionSet(QQ**Integer(2), in_small_oblong)
>>> SmallOblongUniverse
{ (x, y) ∈ Vector space of dimension 2
                    over Rational Field : x^2 + 3*y^2 <= 42 }
>>> __tmp__=var("x,y"); parity_check = symbolic_expression(abs(sin(pi/Integer(2)*(x + y))) < Integer(1)/Integer(1000)).function(x,y)
>>> EvenUniverse = ConditionSet(ZZ**Integer(2), parity_check); EvenUniverse
{ (x, y) ∈ Ambient free module of rank 2 over the principal ideal
           domain Integer Ring : abs(sin(1/2*pi*x + 1/2*pi*y)) < (1/1000) }
>>> SmallOblongUniverse & EvenUniverse
{ (x, y) ∈ Free module of degree 2 and rank 2 over Integer Ring
Echelon basis matrix:
[1 0]
[0 1] : x^2 + 3*y^2 <= 42, abs(sin(1/2*pi*x + 1/2*pi*y)) < (1/1000) }

Combining two ConditionSet``s with different formal variables works correctly. The formal variables of the intersection are taken from ``self:

sage: # needs sage.modules sage.symbolic
sage: SmallMirrorUniverse = ConditionSet(QQ^2, in_small_oblong,
....:                                    vars=(y, x))
sage: SmallMirrorUniverse
{ (y, x) ∈ Vector space of dimension 2
                   over Rational Field : 3*x^2 + y^2 <= 42 }
sage: SmallOblongUniverse & SmallMirrorUniverse
{ (x, y) ∈ Vector space of dimension 2
                   over Rational Field : x^2 + 3*y^2 <= 42 }
sage: SmallMirrorUniverse & SmallOblongUniverse
{ (y, x) ∈ Vector space of dimension 2
                   over Rational Field : 3*x^2 + y^2 <= 42 }
>>> from sage.all import *
>>> # needs sage.modules sage.symbolic
>>> SmallMirrorUniverse = ConditionSet(QQ**Integer(2), in_small_oblong,
...                                    vars=(y, x))
>>> SmallMirrorUniverse
{ (y, x) ∈ Vector space of dimension 2
                   over Rational Field : 3*x^2 + y^2 <= 42 }
>>> SmallOblongUniverse & SmallMirrorUniverse
{ (x, y) ∈ Vector space of dimension 2
                   over Rational Field : x^2 + 3*y^2 <= 42 }
>>> SmallMirrorUniverse & SmallOblongUniverse
{ (y, x) ∈ Vector space of dimension 2
                   over Rational Field : 3*x^2 + y^2 <= 42 }