I'm looking for a algorithm to isolate a single variable in an inequality.
Consider the following grammar for writing inequalities:
EXP -> (SIDE INEQ SIDE) INEQ -> = | != | > | >= | < | <= SIDE -> LIT | (SIDE OP SIDE) OP -> + | - | * | / LIT -> [letter] | [number]
In other words, an expression is always made up of a left hand side and a right hand side with one of six possible relationships between them (equal, not equal, greater than, greater than or equal to, less than, less than or equal to).
A side can be a constant or a variable by itself, or it can be an arithmetic expression.
An arithmetic expression is always made up of a left hand side and a right hand side with one of four possible arithmetic operators between them (add, subtract, multiply, or divide).
(X + 1) > (Y + 1)
I'm looking for an algorithm which can isolate a variable (in other words, modify the expression such that the variable appears by itself on the left hand side).
I don't mind if the variable also appears on the right side, so long as it definitely appears by itself on the left.
I can do this if I only allow the equal and not equal relationships.
I can also do this if I only allow addition and subtraction.
The gist is that you just reverse the arithmetic operation until the variable is by itself.
So if you want to isolate X in this expression:
(X + 1) > (Y + 1)
You can just convert it to:
X > ((Y + 1) - 1)
But I can't do it when allowing all 6 relationships and all 4 arithmetic operators.
The problem arises when multiplying or dividing by a negative number. When multiplying or dividing by a negative, you have to switch the direction of the inequality relationships, and I don't always know the sign of the thing I'm about the multiply or divide by.