问题
I'm writing a program to compute an exact differential for my physics laboratory. I know that I can set real domain or positive (from sympy import *):
x, y, z = symbol('x y z', positive = True)
My problem is to specify domain for example n>1. Is it possible?
In my output I'm getting an expresion like |n^2-1| and with setting this domain n>1 I would accept output like n^2-1 (without absolute value "||")
回答1:
For assumptions on symbols, you can use positive or negative:
p = Symbol('p', positive=True)
But this can only define p>0 (or p<0 if you use negative=True).
For more complex expression refinement, try refine(expression, assumption):
In [1]: n = Symbol('n')
In [2]: refine(Abs(n-1), Q.positive(n-1))
Out[2]: n - 1
In [3]: refine(Abs(n-1))
Out[3]: │n - 1│
That is, you create the assumption Q.positive(n-1), that is n > 1, and pass it to refine.
There is currently work in progress to port this assumption style to other algorithms, but support is still incomplete (simplify appears not to recognize this kind of assumption).
It is expected that support of Q.statement( ... ) will be extended in future versions of SymPy, as there is currently a lot of work in progress on this.
来源:https://stackoverflow.com/questions/33216750/sympy-define-domain-of-variable