fa.bianp.net

Query module - finally in trunk

Category: General, sympy, Tecnologí­a

The query module is finally in the main SymPy repository. I made substantial changes since last post, most of them at the user interface level (thanks to Vinzent and Mateusz for many insightful comments). Main function is ask(), which replaces the old expression.is_* syntax. You can ask many things. For example, you can ask if a given expression is integer, prime or real: [cc lang="python"] >>> ask(2, Q.integer) True >>> ask(x**2, Q.integer) None >>> ask(x**2, Q.integer, Assume(x, Q.integer)) True >>> ask(sqrt(2)*x, Q.integer, Assume(x, Q.integer)) >>> ask(I*x, Q.real, Assume(x, Q.imaginary)) True >>> ask(x*y, Q.prime, Assume(x, Q.prime) & Assume(y, Q.prime)) False [/cc] as you see, it returns True when it is sure that the expression is an integer, None if it does not know, and False if it is certainly not an integer. The second argument, which we will call 'key' specifies what we want to ask about. For example, Q.integer ask wether expression is an integer, Q.negative wether it's a negative, etc. For a complete list of all the keys available, see doc/src/modules/queries.txt in sympy codebase. It also accepts an optional third argument where you can specify assumptions that symbols in expr satisfy. That can be any kind of boolean expression involving assumptions, for example: Assume(x, Q.positive) & Assume(x, Q.integer) or Assume(x, Q.positive) | Assume(x, Q.negative), or even NAnd(Assume(x, Q.positive), Assume(x, Q.integer)