Subsets of a Universe Defined by Predicates

class sage.sets.condition_set.ConditionSet(universe, names, category, *predicates)

Bases: sage.structure.parent.Set_generic, sage.sets.set.Set_base, sage.sets.set.Set_boolean_operators, sage.sets.set.Set_add_sub_operators, sage.structure.unique_representation.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')
y
sage: SmallOdds = ConditionSet(ZZ, is_odd, abs(y) <= 11, vars=[y]); SmallOdds
{ y ∈ Integer Ring : abs(y) <= 11, <function is_odd at 0x...>(y) }

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
False

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
{ (x, y, z) ∈ P : sqrt(x^2 + y^2 + z^2) < 1.20000000000000 }
sage: vector([1, 0, 0]) in P_inter_B_again
True
sage: vector([1, 1, 1]) in P_inter_B_again
False

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

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

sage: Q4.<a, b, c, d> = ConditionSet(QQ^4); Q4
{ (a, b, c, d) ∈ Vector space of dimension 4 over Rational Field }
sage: Q4.variable_names()
('a', 'b', 'c', 'd')
sage: Q4.arguments()
(a, b, c, d)
ambient()

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
arguments()

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
(x,)
sage: args[0].parent()
Symbolic Ring
intersection(X)

Return the intersection of self and X.

EXAMPLES:

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) }

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

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 }