-- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy-patches" group. To post to this group, send email to sympy-patches@googlegroups.com To unsubscribe from this group, send email to sympy-patches+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sympy-patches?hl=en -~----------~----~----~----~------~----~------~--~---
From e1ee640264c93650d74607cf57e0aa4749baebe3 Mon Sep 17 00:00:00 2001 From: Vinzent Steinberg <vinzent.steinb...@gmail.com> Date: Thu, 6 Aug 2009 18:00:14 +0200 Subject: [PATCH] Use a set for global assumptions instead of functions This way the namespace is less cluttered, it's slightly easier to use and more flexible. Some pedantic tweaks to docstrings. --- sympy/assumptions/__init__.py | 4 +- sympy/assumptions/assume.py | 91 +++++++----------------- sympy/assumptions/tests/test_assumptions_2.py | 24 +++---- sympy/queries/__init__.py | 9 ++- sympy/queries/tests/test_query.py | 7 +- 5 files changed, 48 insertions(+), 87 deletions(-) diff --git a/sympy/assumptions/__init__.py b/sympy/assumptions/__init__.py index bdd47c2..bdd3be0 100644 --- a/sympy/assumptions/__init__.py +++ b/sympy/assumptions/__init__.py @@ -1,2 +1,2 @@ -from assume import Assume, register_global_assumptions, list_global_assumptions, \ - remove_global_assumptions, clean_global_assumptions +from assume import Assume, global_assumptions + diff --git a/sympy/assumptions/assume.py b/sympy/assumptions/assume.py index 2463108..47d8f1f 100644 --- a/sympy/assumptions/assume.py +++ b/sympy/assumptions/assume.py @@ -2,74 +2,36 @@ from sympy.core import Basic, Symbol from sympy.core.relational import Relational -__global_assumptions = [] +class AssumptionsContext(set): + """Set representing assumptions. -def register_global_assumptions(*assump): - """Register an assumption as global + This is used to represent global assumptions, but you can also use this + class to create your own local assumptions contexts. It is basically a thin + wrapper to Python's set, so see its documentation for advanced usage. Examples: >>> from sympy import * - >>> list_global_assumptions() - [] + >>> global_assumptions + set([]) >>> x = Symbol('x') - >>> register_global_assumptions(Assume(x, Q.real)) - >>> list_global_assumptions() - [Assume(x, 'real', True)] - >>> clean_global_assumptions() + >>> global_assumptions.add(Assume(x, Q.real)) + >>> global_assumptions + set([Assume(x, 'real', True)]) + >>> global_assumptions.remove(Assume(x, Q.real)) + >>> global_assumptions + set([]) + >>> global_assumptions.clear() """ - __global_assumptions.extend(assump) + def add(self, *assumptions): + """Add an assumption.""" + for a in assumptions: + assert isinstance(a, Assume), 'can only store instances of Assume' + super(AssumptionsContext, self).add(a) -def list_global_assumptions(): - """List all global assumptions - - Examples: - >>> from sympy import * - >>> x = Symbol('x') - >>> list_global_assumptions() - [] - >>> register_global_assumptions(Assume(x>0)) - >>> list_global_assumptions() - [Assume(0 < x, 'relational', True)] - >>> clean_global_assumptions() - """ - return __global_assumptions[:] # make a copy - -def remove_global_assumptions(*assump): - """Remove a global assumption. If argument is not - a global assumption, it will raise ValueError - - Examples: - >>> from sympy import * - >>> x = Symbol('x') - >>> register_global_assumptions(Assume(x, Q.even)) - >>> list_global_assumptions() - [Assume(x, 'even', True)] - >>> remove_global_assumptions(Assume(x, Q.even)) - >>> list_global_assumptions() - [] - """ - for assumption in assump: - __global_assumptions.remove(assumption) - -def clean_global_assumptions(): - """ - Remove all global assumptions - - Examples: - >>> from sympy import * - >>> x = Symbol('x') - >>> register_global_assumptions(Assume(x, Q.integer)) - >>> list_global_assumptions() - [Assume(x, 'integer', True)] - >>> clean_global_assumptions() - >>> list_global_assumptions() - [] - """ - global __global_assumptions - __global_assumptions = [] +global_assumptions = AssumptionsContext() class Assume(Basic): - """New-style assumptions + """New-style assumptions. >>> from sympy import * >>> x = Symbol('x') @@ -88,7 +50,7 @@ def __init__(self, expr, key='relational', value=True): @property def expr(self): """ - Returns the expression used by this assumption + Return the expression used by this assumption. Examples: >>> from sympy import * @@ -102,7 +64,7 @@ def expr(self): @property def key(self): """ - Returns the key used by this assumption. + Return the key used by this assumption. It is a string, e.g. 'integer', 'rational', etc. Examples: @@ -117,7 +79,7 @@ def key(self): @property def value(self): """ - Returns the value stored by this assumptions. + Return the value stored by this assumptions. It's a boolean. True means that the assumption holds always, and False means the assumption does not hold @@ -141,8 +103,9 @@ def __eq__(self, other): def eliminate_assume(expr, symbol=None): """ - Will convert an expression with assumptions to an equivalent with all assumptions - replaced by symbols + Convert an expression with assumptions to an equivalent with all assumptions + replaced by symbols. + Assume(x, integer=True) --> integer Assume(x, integer=False) --> ~integer diff --git a/sympy/assumptions/tests/test_assumptions_2.py b/sympy/assumptions/tests/test_assumptions_2.py index 4c630bc..254b12e 100644 --- a/sympy/assumptions/tests/test_assumptions_2.py +++ b/sympy/assumptions/tests/test_assumptions_2.py @@ -1,7 +1,6 @@ """rename this to test_assumptions.py when the old assumptions system is deleted""" from sympy.core import symbols -from sympy.assumptions import Assume, register_global_assumptions, \ - list_global_assumptions, remove_global_assumptions, clean_global_assumptions +from sympy.assumptions import Assume, global_assumptions from sympy.assumptions.assume import eliminate_assume from sympy.printing import pretty @@ -44,15 +43,14 @@ def test_eliminate_assumptions(): def test_global(): """Test for global assumptions""" x, y = symbols('x y') - register_global_assumptions(Assume(x>0)) - assert Assume(x>0) in list_global_assumptions() - remove_global_assumptions(Assume(x>0)) - assert (Assume(x>0) in list_global_assumptions()) == False - + global_assumptions.add(Assume(x>0)) + assert Assume(x>0) in global_assumptions + global_assumptions.remove(Assume(x>0)) + assert not Assume(x>0) in global_assumptions # same with multiple of assumptions - register_global_assumptions(Assume(x>0), Assume(y>0)) - assert Assume(x>0) in list_global_assumptions() - assert Assume(y>0) in list_global_assumptions() - clean_global_assumptions() - assert (Assume(x>0) in list_global_assumptions()) == False - assert (Assume(y>0) in list_global_assumptions()) == False + global_assumptions.add(Assume(x>0), Assume(y>0)) + assert Assume(x>0) in global_assumptions + assert Assume(y>0) in global_assumptions + global_assumptions.clear() + assert not Assume(x>0) in global_assumptions + assert not Assume(y>0) in global_assumptions diff --git a/sympy/queries/__init__.py b/sympy/queries/__init__.py index 85a3a03..d8146fd 100644 --- a/sympy/queries/__init__.py +++ b/sympy/queries/__init__.py @@ -1,7 +1,7 @@ import inspect from sympy.core import Symbol, sympify from sympy.utilities.source import get_class -from sympy.assumptions import list_global_assumptions +from sympy.assumptions import global_assumptions from sympy.assumptions.assume import eliminate_assume from sympy.logic.boolalg import to_cnf, conjuncts, \ compile_rule, Equivalent, And @@ -27,6 +27,7 @@ class Q: real = 'real' odd = 'odd' +# TODO: maybe this should be moved to another file? def ask(expr, key, assumptions=[]): """ Method for inferring properties about objects. @@ -58,10 +59,10 @@ def ask(expr, key, assumptions=[]): """ expr = sympify(expr) - global_assump = list_global_assumptions() if assumptions: - assumptions = And(assumptions, And(*global_assump)) - elif global_assump: assumptions = And(*global_assump) + assumptions = And(assumptions, And(*global_assumptions)) + elif global_assumptions: + assumptions = And(*global_assumptions) if not isinstance(assumptions, (list, tuple)): assumptions = conjuncts(to_cnf(assumptions)) diff --git a/sympy/queries/tests/test_query.py b/sympy/queries/tests/test_query.py index 8c6bf9b..e96ce7e 100644 --- a/sympy/queries/tests/test_query.py +++ b/sympy/queries/tests/test_query.py @@ -1,7 +1,6 @@ from sympy.core import Symbol, symbols, S, Rational, Integer from sympy.functions import exp, log, sin, cos, sign, re, im, sqrt -from sympy.assumptions import Assume, register_global_assumptions, \ - clean_global_assumptions +from sympy.assumptions import Assume, global_assumptions from sympy.queries import Q, ask, register_handler, remove_handler from sympy.queries.handlers import AskHandler from sympy.utilities.pytest import raises, XFAIL @@ -907,9 +906,9 @@ def test_global(): """Test ask with global assumptions""" x = symbols('x') assert ask(x, Q.integer) == None - register_global_assumptions(Assume(x, Q.integer)) + global_assumptions.add(Assume(x, Q.integer)) assert ask(x, Q.integer) == True - clean_global_assumptions() + global_assumptions.clear() assert ask(x, Q.integer) == None def test_incompatible_resolutors(): -- 1.6.4