问题描述
我想找到下面两个常量cons1
和cons2
的区间我写了以下代码
I want to find the interval of following two constants cons1
and cons2
I wrote the follwing code
from sympy import Poly
from sympy import Abs
from sympy.solvers.inequalities import solve_rational_inequalities
from sympy.abc import x
cons1=2*((x+2)**2)-Abs(x)-1
cons2=exp(2*x+1)-2.5
solve_rational_inequalities([[((Poly(cons1), Poly(1, x)), '<='), (Poly(cons2), Poly(1, x)), '<=')]])
但是因为我得到了腹肌:
but because of Abs I am getting:
PolynomialError: only univariate polynomials are allowed
推荐答案
这是设计使然.2*((x+2)**2) - Abs(x) - 1
不是 x 的多项式.exp(2*x+1) - 2.5
也不是.solve_rational_inequalities
的方法不适用于此类函数.
This is by design. 2*((x+2)**2) - Abs(x) - 1
is not a polynomial in x. Neither is exp(2*x+1) - 2.5
. The methods of solve_rational_inequalities
do not apply to such functions.
如果你试图通过将这些表达式包装在 Poly 中来欺骗数学,所发生的一切就是 SymPy 会使它们成为关于 x
和 Abs(x)
(或 exp(2*x+1)
等).这是一种多项式,但不是关于 x 的多项式,而这正是 solve_rational_inequalities
所要求的.因此错误.
If you try to cheat mathematics by wrapping those expressions in Poly, all that happens is that SymPy will make them polynomials with respect to x
and Abs(x)
(or exp(2*x+1)
, etc). That's a kind of a polynomial, but not a polynomial with respect to x, which is what solve_rational_inequalities
requires. Hence the error.
底线:SymPy 目前没有解决一般不等式系统的算法.有一个有理不等式的算法以及不等式求解器 页面,包括solve_univariate_inequality
(允许通用函数,但仅适用于一个变量).
Bottom line: SymPy does not currently have an algorithm for solving systems of general inequalities. There is an algorithm for rational inequalities and some other things listed on inequality solvers page, including solve_univariate_inequality
(which allows general functions, but for one variable only).
这篇关于“PolynomialError:只允许单变量多项式";在求解不等式系统时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!