コード例 #1
0
ファイル: ask.py プロジェクト: fgrosshans/sympy
def eval_predicate(predicate, expr, assumptions=True):
    """
    Evaluate predicate(expr) under the given assumptions.

    This uses only direct resolution methods, not logical inference.
    """
    res, _res = None, None
    mro = inspect.getmro(type(expr))
    for handler in predicate.handlers:
        cls = get_class(handler)
        for subclass in mro:
            try:
                eval = getattr(cls, subclass.__name__)
            except AttributeError:
                continue
            res = eval(expr, assumptions)
            if _res is None:
                _res = res
            elif res is None:
                # since first resolutor was conclusive, we keep that value
                res = _res
            else:
                # only check consistency if both resolutors have concluded
                if _res != res:
                    raise ValueError('incompatible resolutors')
            break
    return res
コード例 #2
0
ファイル: assume.py プロジェクト: twobitlogic/sympy
    def eval(self, expr, assumptions=True):
        """
        Evaluate self(expr) under the given assumptions.

        This uses only direct resolution methods, not logical inference.
        """
        res, _res = None, None
        mro = inspect.getmro(type(expr))
        for handler in self.handlers:
            cls = get_class(handler)
            for subclass in mro:
                try:
                    eval = getattr(cls, subclass.__name__)
                except AttributeError:
                    continue
                res = eval(expr, assumptions)
                if _res is None:
                    _res = res
                elif res is None:
                    # since first resolutor was conclusive, we keep that value
                    res = _res
                else:
                    # only check consistency if both resolutors have concluded
                    if _res != res:
                        raise ValueError('incompatible resolutors')
                break
        return res
コード例 #3
0
ファイル: assume.py プロジェクト: dagidagi1/Matrix
 def eval(self, args, assumptions=True):
     # Support for deprecated design
     # When old design is removed, this will always return None
     SymPyDeprecationWarning(
         feature="Evaluating UndefinedPredicate",
         useinstead="multipledispatch handler of Predicate",
         issue=20873,
         deprecated_since_version="1.8").warn()
     expr, = args
     res, _res = None, None
     mro = inspect.getmro(type(expr))
     for handler in self.handlers:
         cls = get_class(handler)
         for subclass in mro:
             eval_ = getattr(cls, subclass.__name__, None)
             if eval_ is None:
                 continue
             res = eval_(expr, assumptions)
             # Do not stop if value returned is None
             # Try to check for higher classes
             if res is None:
                 continue
             if _res is None:
                 _res = res
             elif res is None:
                 # since first resolutor was conclusive, we keep that value
                 res = _res
             else:
                 # only check consistency if both resolutors have concluded
                 if _res != res:
                     raise ValueError('incompatible resolutors')
             break
     return res
コード例 #4
0
ファイル: assume.py プロジェクト: sudhanshura-i/sympy
 def eval(self, args, assumptions=True):
     # Support for deprecated design
     # When old design is removed, this will always return None
     expr, = args
     res, _res = None, None
     mro = inspect.getmro(type(expr))
     for handler in self.handlers:
         cls = get_class(handler)
         for subclass in mro:
             eval_ = getattr(cls, subclass.__name__, None)
             if eval_ is None:
                 continue
             res = eval_(expr, assumptions)
             # Do not stop if value returned is None
             # Try to check for higher classes
             if res is None:
                 continue
             if _res is None:
                 _res = res
             elif res is None:
                 # since first resolutor was conclusive, we keep that value
                 res = _res
             else:
                 # only check consistency if both resolutors have concluded
                 if _res != res:
                     raise ValueError('incompatible resolutors')
             break
     return res
コード例 #5
0
 def eval(self, args, assumptions=True):
     # Support for deprecated design
     # When old design is removed, this will always return None
     sympy_deprecation_warning(
         """
         The AskHandler system is deprecated. Evaluating UndefinedPredicate
         objects should be replaced with the multipledispatch handler of
         Predicate.
         """,
         deprecated_since_version="1.8",
         active_deprecations_target='deprecated-askhandler',
         stacklevel=5,
     )
     expr, = args
     res, _res = None, None
     mro = inspect.getmro(type(expr))
     for handler in self.handlers:
         cls = get_class(handler)
         for subclass in mro:
             eval_ = getattr(cls, subclass.__name__, None)
             if eval_ is None:
                 continue
             res = eval_(expr, assumptions)
             # Do not stop if value returned is None
             # Try to check for higher classes
             if res is None:
                 continue
             if _res is None:
                 _res = res
             else:
                 # only check consistency if both resolutors have concluded
                 if _res != res:
                     raise ValueError('incompatible resolutors')
             break
     return res
コード例 #6
0
ファイル: assume.py プロジェクト: asmeurer/sympy
    def eval(self, expr, assumptions=True):
        """
        Evaluate self(expr) under the given assumptions.

        This uses only direct resolution methods, not logical inference.
        """
        res, _res = None, None
        mro = inspect.getmro(type(expr))
        for handler in self.handlers:
            cls = get_class(handler)
            for subclass in mro:
                eval_ = getattr(cls, subclass.__name__, None)
                if eval_ is None:
                    continue
                res = eval_(expr, assumptions)
                # Do not stop if value returned is None
                # Try to check for higher classes
                if res is None:
                    continue
                if _res is None:
                    _res = res
                elif res is None:
                    # since first resolutor was conclusive, we keep that value
                    res = _res
                else:
                    # only check consistency if both resolutors have concluded
                    if _res != res:
                        raise ValueError('incompatible resolutors')
                break
        return res
コード例 #7
0
ファイル: test_source.py プロジェクト: 101man/sympy
def test_get_class():
    _basic = get_class('sympy.core.basic.Basic')
    assert _basic.__name__ == 'Basic'
コード例 #8
0
ファイル: ask.py プロジェクト: tovrstra/sympy
def ask(expr, key, assumptions=True):
    """
    Method for inferring properties about objects.

    **Syntax**

        * ask(expression, key)

        * ask(expression, key, assumptions)

            where expression is any SymPy expression

    **Examples**
        >>> from sympy import ask, Q, Assume, pi
        >>> from sympy.abc import x, y
        >>> ask(pi, Q.rational)
        False
        >>> ask(x*y, Q.even, Assume(x, Q.even) & Assume(y, Q.integer))
        True
        >>> ask(x*y, Q.prime, Assume(x, Q.integer) &  Assume(y, Q.integer))
        False

    **Remarks**
        Relations in assumptions are not implemented (yet), so the following
        will not give a meaningful result.
        >> ask(x, positive=True, Assume(x>0))
        It is however a work in progress and should be available before
        the official release

    """
    expr = sympify(expr)
    assumptions = And(assumptions, And(*global_assumptions))

    # direct resolution method, no logic
    resolutors = []
    for handler in handlers_dict[key]:
        resolutors.append( get_class(handler) )
    res, _res = None, None
    mro = inspect.getmro(type(expr))
    for handler in resolutors:
        for subclass in mro:
            if hasattr(handler, subclass.__name__):
                res = getattr(handler, subclass.__name__)(expr, assumptions)
                if _res is None: _res = res
                elif res is None:
                    # since first resolutor was conclusive, we keep that value
                    res = _res
                else:
                    # only check consistency if both resolutors have concluded
                    if _res != res: raise ValueError, 'incompatible resolutors'
                break
    if res is not None:
        return res

    if assumptions is True: return

    # use logic inference
    if not expr.is_Atom: return
    clauses = copy.deepcopy(known_facts_compiled)

    assumptions = conjuncts(to_cnf(assumptions))
    # add assumptions to the knowledge base
    for assump in assumptions:
        conj = eliminate_assume(assump, symbol=expr)
        if conj:
            out = []
            for sym in conjuncts(to_cnf(conj)):
                lit, pos = literal_symbol(sym), type(sym) is not Not
                if pos:
                    out.extend([known_facts_keys.index(str(l))+1 for l in disjuncts(lit)])
                else:
                    out.extend([-(known_facts_keys.index(str(l))+1) for l in disjuncts(lit)])
            clauses.append(out)

    n = len(known_facts_keys)
    clauses.append([known_facts_keys.index(key)+1])
    if not dpll_int_repr(clauses, range(1, n+1), {}):
        return False
    clauses[-1][0] = -clauses[-1][0]
    if not dpll_int_repr(clauses, range(1, n+1), {}):
        # if the negation is satisfiable, it is entailed
        return True
    del clauses
コード例 #9
0
def test_get_class():
    _basic = get_class('sympy.core.basic.Basic')
    assert _basic.__name__ == 'Basic'
コード例 #10
0
ファイル: test_source.py プロジェクト: msgoff/sympy
def test_get_class():
    _basic = get_class("sympy.core.basic.Basic")
    assert _basic.__name__ == "Basic"
コード例 #11
0
ファイル: ask.py プロジェクト: unix0000/sympy-polys
def ask(expr, key, assumptions=True):
    """
    Method for inferring properties about objects.

    **Syntax**

        * ask(expression, key)

        * ask(expression, key, assumptions)

            where expression is any SymPy expression

    **Examples**
        >>> from sympy import ask, Q, Assume, pi
        >>> from sympy.abc import x, y
        >>> ask(pi, Q.rational)
        False
        >>> ask(x*y, Q.even, Assume(x, Q.even) & Assume(y, Q.integer))
        True
        >>> ask(x*y, Q.prime, Assume(x, Q.integer) &  Assume(y, Q.integer))
        False

    **Remarks**
        Relations in assumptions are not implemented (yet), so the following
        will not give a meaningful result.
        >> ask(x, positive=True, Assume(x>0))
        It is however a work in progress and should be available before
        the official release

    """
    expr = sympify(expr)
    assumptions = And(assumptions, And(*global_assumptions))

    # direct resolution method, no logic
    resolutors = []
    for handler in handlers_dict[key]:
        resolutors.append(get_class(handler))
    res, _res = None, None
    mro = inspect.getmro(type(expr))
    for handler in resolutors:
        for subclass in mro:
            if hasattr(handler, subclass.__name__):
                res = getattr(handler, subclass.__name__)(expr, assumptions)
                if _res is None:
                    _res = res
                elif res is None:
                    # since first resolutor was conclusive, we keep that value
                    res = _res
                else:
                    # only check consistency if both resolutors have concluded
                    if _res != res:
                        raise ValueError('incompatible resolutors')
                break
    if res is not None:
        return res

    if assumptions is True:
        return

    # use logic inference
    if not expr.is_Atom:
        return
    clauses = copy.deepcopy(known_facts_compiled)

    assumptions = conjuncts(to_cnf(assumptions))
    # add assumptions to the knowledge base
    for assump in assumptions:
        conj = eliminate_assume(assump, symbol=expr)
        if conj:
            out = set()
            for sym in conjuncts(to_cnf(conj)):
                lit, pos = literal_symbol(sym), type(sym) is not Not
                if pos:
                    out.update([
                        known_facts_keys.index(str(l)) + 1
                        for l in disjuncts(lit)
                    ])
                else:
                    out.update([
                        -(known_facts_keys.index(str(l)) + 1)
                        for l in disjuncts(lit)
                    ])
            clauses.append(out)

    n = len(known_facts_keys)
    clauses.append(set([known_facts_keys.index(key) + 1]))
    if not dpll_int_repr(clauses, set(range(1, n + 1)), {}):
        return False
    clauses[-1] = set([-(known_facts_keys.index(key) + 1)])
    if not dpll_int_repr(clauses, set(range(1, n + 1)), {}):
        # if the negation is satisfiable, it is entailed
        return True
    del clauses