Refine module
This commit introduced a new module in sympy: the refine module.
The purpose of this module is to simplify expressions when they are bound to assumptions. For example, if you know that x>0, then you can simplify abs(x) to x. This code was traditionally embedded into the core, but now this will be part of an external module (sympy.refine) upon which the core has no dependencies.
In a not very original move, I named the main function in this module refine(). It’s syntax is very straightforward: first argument is an expression and second argument are assumptions. Some examples (from isympy):
In [1]: refine(1+abs(x), Assume(x, Q.positive))
Out[1]: 1 + x
In [2]: refine(exp(I*x*pi), Assume(x, Q.odd))
Out[2]: -1
In [3]: refine(exp(I*x*pi), Assume(x, Q.even))
Out[3]: 1
Right now the module lacks some rules, but the design (very similar to the query module) will make adding these rules an easy task.
August 17th, 2009 20:25
It looks good, though now it needs expansion. For example, in the example you give:
>>> refine(exp(I*x*pi), Assume(x, Q.integer))
exp(pi*I*x)
This should return (-1)**x.
Will the new assumptions system let us implement assumptions like “f(x) is continuous” or “diff(f(x, y), x) == diff(g(x, y), y)”?
August 18th, 2009 02:42
Aaron, sure, that would be possible (although it is currently not implemented). f(x) is continuous would look like:
>>> Assume(f(x), Q.continuous)
and diff(f(x, y), x) == diff(g(x, y), y):
>>> Assume(diff(f(x, y), x) == diff(g(x, y), y))
You can see some hints on how to extend the assumption system looking at the code of sympy/queries/__init__.py and the last two examples of sympy/queries/tests/test_query.py, the first one at least should be straightforward to implement.
August 19th, 2009 04:16
That’s great. Clearly, this is better than the old system. Of course, the real work would be modifying refine to do things with those assumptions. For example, the following should return 0:
>>> refine(f(x, y).diff(x, y) – f(x, y).diff(y, x), Assume(f(x, y), Q.continuous))
And of course, once we have an interval class proper, you would assumedly be able to assign that assumption (or any assumption really) over an iterval. There could be real power in that, especially if it can gather some of those things automatically, like knowing that sin(x)/x has a discontinuity at 0.
You could probably make a whole new GSoC project out of just adding in assumptions next year