解决同情中的理性不平等

时间:2018-01-15 15:44:00

标签: python sympy

我很新见,但想解决以下问题:

我有多种形式的不等式约束

A < f(x) < B
C < g(x) < D
...

其中A,B,C,D只是数字。 f和g是有理函数。

例如,我可以使用以下方法:

solve_rational_inequalities([[
   ((Poly(x-10000.00), Poly(1, x)), '>'), 
   ((Poly(x-100000.00), Poly(1, x)), '<'), 
   ((Poly((x/130000.00)-0.00), Poly(1, x)), '>'), 
   ((Poly((x/130000.00)-0.19), Poly(1, x)), '<')]])
Interval.open(10000, 24700)

这里,A = 10.000,B = 100.000,C = 0,D = 0.19,f(x)= x并且g(x)= x / 130.000。所以这很有效。

现在,对于另一种情况,我有函数f(x)= 10100.00 / x。

如果我只是应用上面的配方,我会得到:

solve_rational_inequalities([[
    ((Poly((10100.00/x)-0.00), Poly(1, x)), '>'), 
    ((Poly((10100.00/x)-0.19), Poly(1, x)), '<')]])                     
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/oliverdemetz/Library/Python/2.7/lib/python/site-packages/sympy/solvers/inequalities.py", line 162, in solve_rational_inequalities
    numer_intervals = solve_poly_inequality(numer*denom, rel)
  File "/Users/oliverdemetz/Library/Python/2.7/lib/python/site-packages/sympy/solvers/inequalities.py", line 55, in solve_poly_inequality
    reals, intervals = poly.real_roots(multiple=False), []
  File "/Users/oliverdemetz/Library/Python/2.7/lib/python/site-packages/sympy/polys/polytools.py", line 3454, in real_roots
    reals = sympy.polys.rootoftools.CRootOf.real_roots(f, radicals=radicals)
  File "/Users/oliverdemetz/Library/Python/2.7/lib/python/site-packages/sympy/polys/rootoftools.py", line 196, in real_roots
    return cls._get_roots("_real_roots", poly, radicals)
  File "/Users/oliverdemetz/Library/Python/2.7/lib/python/site-packages/sympy/polys/rootoftools.py", line 565, in _get_roots
    raise PolynomialError("only univariate polynomials are allowed")
sympy.polys.polyerrors.PolynomialError: only univariate polynomials are allowed

2 个答案:

答案 0 :(得分:1)

我只是用多项式弄乱了有理函数。我在寻找的是solve_poly_inequalities

solve_poly_inequalities(((
    (Poly((10100.00/x)-0.00),'>'),
    (Poly((10100.00/x)-0.19),'<')
)))
BTW:有人可以告诉我(作为初学者)为什么他们使用((...))作为列表,尽管手册将方括号[...]解释为列表语法???

无论如何,我终于使用solveset命令更轻松地解决了我的不平等问题的实际问题,请参阅this other SO post

答案 1 :(得分:1)

solve_rational_inequality()语法中第一个多项式(poly)是分子,第二个多项式是分母,所以(1 / x)在此语法中为poly(1,x), poly(x)

solve_rational_inequalities([[((poly(-10100.00,x), poly(x)), '>'),((poly(-0.19*x+10100.00), poly(x)), '<')]])

(-oo, 0)