, " ". , (LHS) , .
, . . (RHS) . , - / .
:
import sympy
from sympy.core.relational import Relational
mult_by_minus_one_map = {
None: '==',
'==': '==',
'eq': '==',
'!=': '!=',
'<>': '!=',
'ne': '!=',
'>=': '<=',
'ge': '<=',
'<=': '>=',
'le': '>=',
'>': '<',
'gt': '<',
'<': '>',
'lt': '>',
}
def move_inequality_constants(ineq, zero_on_right=False):
l = ineq.lhs
r = ineq.rhs
op = ineq.rel_op
all_on_left = l - r
if zero_on_right:
return Relational(all_on_left, sympy.sympify(0), op)
else:
coeff_dict = all_on_left.as_coefficients_dict()
var_types = coeff_dict.keys()
new_rhs = sympy.sympify(0)
for s in var_types:
if s == 1:
all_on_left = all_on_left - coeff_dict[s]
new_rhs = new_rhs - coeff_dict[s]
if new_rhs < 0:
all_on_left = all_on_left * -1
new_rhs = new_rhs * -1
op = mult_by_minus_one_map[op]
return Relational(all_on_left,new_rhs,op)
from sympy.abc import x,y,z
test_ineqs = [ x - 5 > y - z,
x**2 + x - 5 > y + x**2 - z,
x + 5 > y - z,
x**3 + y**2 >= x + 5*y - z - 15]
for k in test_ineqs:
print('Re-arranging '+ str(k))
kn = move_inequality_constants(k)
print('Gives '+str(kn))
print('Or equivalently ' + str(move_inequality_constants(k, True)))
print('====')
:
Re-arranging x - 5 > y - z
Gives x - y + z > 5
Or equivalently x - y + z - 5 > 0
====
Re-arranging x**2 + x - 5 > x**2 + y - z
Gives x - y + z > 5
Or equivalently x - y + z - 5 > 0
====
Re-arranging x + 5 > y - z
Gives -x + y - z < 5
Or equivalently x - y + z + 5 > 0
====
Re-arranging x**3 + y**2 >= x + 5*y - z - 15
Gives -x**3 + x - y**2 + 5*y - z <= 15
Or equivalently x**3 - x + y**2 - 5*y + z + 15 >= 0