本文介绍了SymPy -- 定义变量域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序来计算物理实验室的精确微分.我知道我可以设置真实域或正域(from sympy import *):

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)

我的问题是指定域,例如 n>1.是否可以?在我的输出中,我得到了一个像 |n^2-1| 这样的表达式,并且通过设置这个域 n>1 我会接受像 n^2 这样的输出-1(没有绝对值||")

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 "||")

推荐答案

对于符号的假设,您可以使用 positivenegative:

For assumptions on symbols, you can use positive or negative:

p = Symbol('p', positive=True)

但这只能定义 p>0(或者 p 如果你使用 negative=True).

But this can only define p>0 (or p<0 if you use negative=True).

对于更复杂的表达式细化,请尝试refine(表达式,假设):

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│

也就是说,您创建假设 Q.positive(n-1),即 n > 1,并将其传递给 refine.

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).

预计 Q.statement( ... ) 的支持将在 SymPy 的未来版本中得到扩展,因为目前有很多工作正在进行中.

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.

这篇关于SymPy -- 定义变量域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 12:14