# Algorithm to isolate a variable in an arithmetic inequality

Simple Question

I'm looking for a algorithm to isolate a single variable in an inequality.

Context:

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).

For example:

(X + 1) > (Y + 1)

Detailed Question:

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.

Problem:

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.

• On a practical level, this doesn't seem like a math question (and the tags are all inappropriate, except maybe "algorithms"). Perhaps you should try StackOverflow, of which you have an account. – Lee David Chung Lin Apr 9 '19 at 1:14
• I guess the math question is this: Is there a way around the "flip the inequality when multiplying or dividing by a negative number" rule? Something that will work when the sign of the elements in unknown. – sgware Apr 9 '19 at 1:16

How would you handle $$x < 2x ,$$ which is true when $$x > 0$$ and false otherwise?
Perhaps this is OK for you since it has an $$x$$ alone on the left.