python - How to rearrange sympy expressions containing a relational operator -


i have expressions containing relational operator, symbols, , constants. i'd rearrange expressions (to extent possible) constant terms on 1 side of relational operator, , remaining terms on other side. example, i'd rearrange:

x - 5 > y - z

to:

x - y + z > 5

is there existing sympy method doing this? if not, should start in extending sympy?

somewhat surprisingly, couldn't find way "out of box". can use method in this question make 1 variable sole subject of left hand side (lhs) of inequality can't seem make constant term subject.

so, wrote own version , reproduce below. i've tested on example given , couple of other examples. tries make right hand side (rhs) consist of either 0 or constant terms based on optional parameter. there may corner cases fails - use / modify caution.

code:

import sympy 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)         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)  # test code demo function below     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]  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('====') 

output:

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 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -