Algorithm to isolate a variable in an arithmetic inequality

139 Views Asked by At

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.

1

There are 1 best solutions below

1
On

I don't think you can do that algebraically.

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.