如何在 Sympy 中添加限制假设? [英] How to add assumptions in limit in Sympy?
问题描述
我想在极限中添加一些假设.
假设 0
I want to add some assumptions in limit.
Suppose 0<x<1
, then $$limit_{n \to \infty} x^n = 0$$
from sympy import *
x = var('x, n')
limit(x**n, n, oo)
但是我得到一个错误NotImplementedError:结果取决于符号的符号(log(x))
.
有什么办法可以解决这个问题吗?
Is there some way in sympy to handle this?
推荐答案
EDIT:正如评论中所指出的,下面的解决方案失败了,并且出现了与之前相同的 NotImplementedError
问题(截至 2019 年 11 月)说答案取决于 sign(log(x)).看来这个符号问题不能用假设
解决,只能用Symbol
的positive
参数解决.所以解决这个问题的一种方法是描述 0 <×<1 作为 exp(-y) for y > 0:
EDIT: As pointed out in the comments the solution below fails with the same NotImplementedError
as in the question (as of November 2019) saying that the answer depends on sign(log(x)). It seems that this sign problem can not be solved with assuming
but only with the positive
parameter of Symbol
. So one way around the issue is to describe 0 < x < 1 as exp(-y) for y > 0:
from sympy import *
y = Symbol("y", positive=True)
n = Symbol("n")
print(limit(exp(-y)**n, n, oo)) # outputs 0
<小时>
假设你可以说:
To assume something you can say:
from sympy.assumptions import assuming, Q
with assuming(...):
见这里:http://docs.sympy.org/latest/模块/假设/assume.html
就你而言:
from sympy import *
from sympy.assumptions import assuming, Q
x, n = symbols("x n")
with assuming(Q.is_true(0 < x), Q.is_true(x <1)):
print(limit(x**n, n, oo))
这篇关于如何在 Sympy 中添加限制假设?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!