Пример #1
0
 def is_equivalent(self, other):
     """
     >>> from sympy import *
     >>> from symplus.strplus import init_mprinting
     >>> init_mprinting()
     >>> x, y = symbols('x y')
     >>> AbstractSet(x, x-y>1).is_equivalent(AbstractSet(x, x-y>1))
     True
     >>> AbstractSet(x, x<1).is_equivalent(AbstractSet(x, x-1<0))
     A. x st (x - 1 < 0) <=> (x < 1)
     >>> AbstractSet(x, abs(x)>1).is_equivalent(Interval(1,2))
     A. x st (x =< 2) /\ (x >= 1) <=> (|x| > 1)
     """
     if isinstance(other, AbstractSet):
         vars1 = self.variables
         vars2 = other.variables
         if not type_match(vars1, vars2):
             return false
         vars12 = rename_variables_in(vars1, free_symbols(self) | free_symbols(other))
         return Forall(
             vars12,
             Equivalent(self.expr.xreplace(dict(zip(vars1, vars12))), other.expr.xreplace(dict(zip(vars2, vars12)))),
         )
     else:
         other_ = as_abstract(other)
         if isinstance(other_, AbstractSet):
             return self.is_equivalent(other_)
         else:
             raise ValueError("Unknown argument '%s'" % other)
Пример #2
0
    def _complement(self, other):
        """complement two set builder

        >>> from sympy import *
        >>> from symplus.strplus import init_mprinting
        >>> init_mprinting()
        >>> x, y = symbols('x y')
        >>> AbstractSet(y, abs(y)<=3)._complement(AbstractSet(x, abs(x)>1))
        {y | (|y| > 1) /\ (|y| > 3)}
        >>> AbstractSet((x,y), abs(x-y)>1)._complement(AbstractSet(x, abs(x)>1))
        {x | |x| > 1}
        >>> AbstractSet(y, y<=x)._complement(AbstractSet(x, abs(x)>1))
        {y | (y > x) /\ (|y| > 1)}
        >>> x2 = symbols('x2', positive=True)
        >>> AbstractSet(x, abs(x)>1)._complement(AbstractSet(x2, x2<=y))
        {x | (x =< y) /\ (|x| =< 1)}
        >>> m, n = MatrixSymbol('m', 2, 2), MatrixSymbol('n', 2, 2)
        >>> AbstractSet(n, Eq(det(n),0))._complement(AbstractSet(m, Eq(m[0,0]+m[1,1],0)))
        {n | (0 =/= ||n||) /\ (0 == n[0, 0] + n[1, 1])}
        >>> AbstractSet(x, abs(x)>1)._complement(AbstractSet(m, Eq(m[0,0]+m[1,1],0)))
        {m | 0 == m[0, 0] + m[1, 1]}
        >>> AbstractSet(x, abs(x)>1)._complement(Interval(1,2))
        """
        if isinstance(other, AbstractSet):
            vars1 = self.variables
            vars2 = other.variables
            if not type_match(vars1, vars2):
                return other

            vars12 = rename_variables_in(vars1, free_symbols(self) | free_symbols(other))
            expr12 = And(
                other.expr.xreplace(dict(zip(vars2, vars12))), Not(self.expr.xreplace(dict(zip(vars1, vars12))))
            )
            return AbstractSet(vars12, expr12)
Пример #3
0
    def _union(self, other):
        """union two set builder

        >>> from sympy import *
        >>> from symplus.strplus import init_mprinting
        >>> init_mprinting()
        >>> x, y = symbols('x y')
        >>> AbstractSet(x, abs(x)>1)._union(AbstractSet(y, abs(y)<3))
        {x | (|x| < 3) \/ (|x| > 1)}
        >>> AbstractSet(x, abs(x)>1)._union(AbstractSet((x,y), abs(x-y)>1))
        >>> AbstractSet(x, abs(x)>1)._union(AbstractSet(y, y<x))
        {x_ | (x_ < x) \/ (|x_| > 1)}
        >>> x2 = symbols('x2', positive=True)
        >>> AbstractSet(x, abs(x)>1)._union(AbstractSet(x2, x2<y))
        {x | (x < y) \/ (|x| > 1)}
        >>> m, n = MatrixSymbol('m', 2, 2), MatrixSymbol('n', 2, 2)
        >>> AbstractSet(m, Eq(m[0,0]+m[1,1],0))._union(AbstractSet(n, Eq(det(n),0)))
        {m | (0 == m[0, 0] + m[1, 1]) \/ (0 == ||m||)}
        >>> AbstractSet(m, Eq(m[0,0]+m[1,1],0))._union(AbstractSet(x, abs(x)>1))
        >>> AbstractSet(x, abs(x)>1)._union(Interval(1,2))
        """
        if isinstance(other, AbstractSet):
            vars1 = self.variables
            vars2 = other.variables
            if not type_match(vars1, vars2):
                return None

            vars12 = rename_variables_in(vars1, free_symbols(self) | free_symbols(other))
            expr12 = Or(self.expr.xreplace(dict(zip(vars1, vars12))), other.expr.xreplace(dict(zip(vars2, vars12))))
            return AbstractSet(vars12, expr12)
Пример #4
0
 def _contains(self, other):
     """
     >>> from sympy import *
     >>> from symplus.strplus import init_mprinting
     >>> init_mprinting()
     >>> x, y, z = symbols('x y z')
     >>> AbstractSet(x, abs(x)>1)._contains(2)
     True
     >>> AbstractSet((x,y), abs(x-y)>1)._contains((3,1))
     True
     >>> AbstractSet((x,y), abs(x-y)>1)._contains((x+3,x+1))
     True
     >>> AbstractSet(x, x>y)._contains(z)
     z > y
     >>> AbstractSet(x, x>y)._contains(Matrix(2,2,[1,2,3,4]))
     False
     """
     var = self.variables
     val = pack_if_not(other)
     if not type_match(var, val):
         return false
     return self.expr.xreplace(dict(zip(var, val)))