假设SymPy的符号在零和一之间? [英] Assuming a symbol is between zero and one with SymPy?

查看:45
本文介绍了假设SymPy的符号在零和一之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SymPy 在 Python 中对非常大的表达式进行符号操作.我处理的大多数符号表示小于或等于 1 的非负实数.

我如何告诉 SymPy 这些假设?我发现我可以在创建符号时执行以下操作.

import sympy as symx = sym.symbols('x', real=True, nonnegative=True)

但我不知道如何强加一个的上限.

解决方案

不幸的是,当前实现的假设系统没有提供施加这种界限的方法.出于某些目的,引入隐含边界的代数结构可能是合理的:例如,

t = sym.symbols('t', nonnegative=True)x = t/(1+t)

现在 SymPy 知道 x 在 0 和 1 之间:

<预><代码>>>>×<1真的>>>x >= 0真的

这是否有帮助取决于这种替换您正在使用的表达式的自然程度.另一个选项是 x = sym.exp(-t)

I am doing symbolic manipulation of very large expressions in Python using SymPy. Most of the symbols that I am manipulating represent nonnegative, real numbers, less than or equal to one.

How can I tell SymPy these assumptions? I have figured out that I can do the following when creating the symbol.

import sympy as sym

x = sym.symbols('x', real=True, nonnegative=True)

but I don't see how to impose the upper bound of one.

解决方案

Unfortunately, the currently implemented system of assumptions does not provide a way to impose such a bound. For some purposes, it may be reasonable to introduce algebraic structure that implies the bound: for example,

t = sym.symbols('t', nonnegative=True)
x = t/(1+t)

Now SymPy knows that x is between 0 and 1:

>>> x < 1
True
>>> x >= 0
True

Whether this is helpful depends on how natural this substitution for the expressions you are working with. Another option is x = sym.exp(-t)

这篇关于假设SymPy的符号在零和一之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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