相对于其他变量在Sympy中设置变量的假设 [英] Setting Assumptions on Variables in Sympy Relative to Other Variables

查看:297
本文介绍了相对于其他变量在Sympy中设置变量的假设的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道python中的sympy可以对变量设置假设,例如x为正,负,实,复数等.我想知道sympy是否可以对相对于其他变量的变量设置假设.例如,如果我有变量x和y,可以将sympy设置为在解决方案中假设x> y.或者,或者,如果我有两个变量a和B,我可以设置sympy来假设a + 2B< 2B< 2B< B; 1个?这些假设可能会有助于sympy简化解决方案solve()和特征向量的复杂解决方案.

I know that sympy in python can set assumptions on variables, such as x is positive, negative, real, complex, etc. I was wondering if sympy can set assumptions on variables relative to other variables. For example, if I have variables x and y, can I set sympy to assume that x > y in its solutions. Or, alternatively, if I have two variables, a and B, can I set sympy to assume that a + 2B < 1? These sorts of assumptions would possibly help sympy simplify complicated solutions to solve() and eigenvectors.

我四处张望,没有找到与在sympy中设置这类假设有关的信息.

I've looked all over and haven't found information pertaining to setting these kinds of assumptions in sympy.

我问是因为我试图找到特定矩阵的特征向量

I ask because I'm attempting to find the eigenvectors of a particular matrix

a,b = symbols('a,b', nonnegative=False)
M = Matrix([ [1-a-2*b, a, b, b], 
             [a, 1-a-2*b, b, b],
             [b, b, 1-a-2*b, a],
             [b, b, a, 1-a-2*b] ])

Sympy正确找到特征值

Sympy finds the eigenvalues correctly

M.eigenvals()

我已经通过MATLAB和WolframAlpha确认的

都给出了相同的结果.但是,特征向量是一团糟

which I've confirmed via MATLAB and WolframAlpha, which all give the same results. However, the eigenvectors are a mess

M.eigenvects()

MATLAB和WolframAlpha都返回特征向量[1,1,1,1] [-1,-1,1,1] [0,0,-1,1] [-1,1,0,0] ,这是正确的特征向量.我什至没有尝试简化sympy的结果,因为它们非常长且复杂.我怀疑这与对变量的假设有关,例如指定a + 2b< 1,但我不确定.

MATLAB and WolframAlpha both return eigenvectors of [1,1,1,1] [-1,-1,1,1] [0,0,-1,1] [-1,1,0,0], which are the correct eigenvectors. I haven't even tried to simplify sympy's results because they're incredibly long and complex. I suspect it has to do with assumptions on the variables, like specifying that a+2b < 1, but I'm not sure.

推荐答案

我想知道是否将其发布为评论,但它太长了:

I was wondering whether to post this as a comment but it is too long:

简短的回答:不是可用的方式.

Short answer: not in a usable way.

SymPy的假设系统目前有点混乱(版本0.7.2,最新日期为2013年5月).由于潜在的GSoC项目,今年夏天它可能会变得更好,但这还不确定.

The assumption system of SymPy is kind of a mess right now (version 0.7.2, latest as of May 2013). There is a possibility that it will get better this summer due to a prospective GSoC project, but this is not certain yet.

SymPy中实际上有两个假设系统.旧的将假设添加到符号本身(因此会导致表达式树的重建问题)并在构造函数中调用(例如,Symbol(..., positive=True)),而新的则是基于全局变量的.全局假设和针对本地假设的上下文管理器(with assume(...):).

There are actually two assumption systems within SymPy. The old one, which adds the assumptions to the Symbols themselves (hence causes problems with rebuilding of expression trees) and is called in the constructor (e.g. Symbol(..., positive=True)), and there is the new one, which is based around global variables for the global assumptions and context managers (with assume(...):) for local ones.

SymPy中的许多功能确实会检查旧的假设(例如Abs将检查是否设置了关键字参数positive),但仍然可能会遗漏.新的假设系统可能会更强大,但目前几乎未使用(最近的子模块中除外).

Many of the functions within SymPy do check the old assumptions (for instance Abs will check whether the keyword argument positive was set), but there still can be misses. The new assumption system can be more powerful but is almost unused at the moment (except in very recent submodules).

在旧的假设系统中,您无法实现所需要的.在新版本中,它是可能的,但可能尚未实现,并且未在SymPy的任何部分中使用.

In the old assumption system what you want is not possible. In the new one it is possible, but probably not implemented yet and not used in any parts of SymPy.

因此,您有两个选择:帮助我们使用假设系统或帮助我们使用矩阵模块.双方都可以使用更多的爱.

So you have two options: help us with the assumption systems or help us with the matrix module. Both can use some more love.

这篇关于相对于其他变量在Sympy中设置变量的假设的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆