Compound Inequalities
A compound inequality joins two inequalities with "and" or "or". An "and" inequality such as describes the numbers between two bounds, an interval; an "or" inequality such as or describes numbers outside a range. This lesson solves both kinds, writes the answers as intervals, and maps them onto the range checks and boolean logic in code.
"And": the overlap
" and " holds only where both are true, the intersection of the two sets. It is usually written as one chain:
To solve a chained inequality, do the same operation to all three parts:
With a negative coefficient, flip both signs, then rewrite the chain so the smaller number is on the left:
Note: A chain only makes sense when . "" describes no numbers at all, and an "and" inequality can have an empty answer.
"Or": the union
" or " holds where at least one is true, the union. It cannot be written as a single chain; use two intervals joined by :
If the two pieces overlap, an "or" can cover every number: or is true for all real .
| Type | Logic | Set operation | Typical shape |
|---|---|---|---|
| and | both | intersection | one bounded interval |
| or | at least one | union | two rays pointing away |
In SymPy, solve each part with solveset and combine the sets: & is the intersection ("and") and | the union ("or"):
from sympy import symbols, solveset, S
x = symbols("x")
def sol(ineq):
return solveset(ineq, x, S.Reals)
print(sol(-7 <= 3*x + 2) & sol(3*x + 2 < 11))
print(sol(1 < 5 - 2*x) & sol(5 - 2*x <= 9))
print(sol(2*x + 3 < 1) | sol(x - 4 >= 2))
print(sol(x < 3) & sol(x > 6)) # an empty "and"
print(sol(x < 5) | sol(x > 2)) # an "or" that covers everythingInterval.Ropen(-3, 3)
Interval.Ropen(-2, 2)
Union(Interval.open(-oo, -1), Interval(6, oo))
EmptySet
Interval(-oo, oo)
Interval.Ropen(-3, 3) means "right-open", , and Interval(-oo, oo) is the whole number line.
In software: range checks
Python is one of the few languages that lets you write a chain exactly as in algebra: 0 <= x < width means 0 <= x and x < width. In most C-family languages you must write x >= 0 && x < width; the chain 0 <= x < width in C compares the boolean result of 0 <= x (0 or 1) with width, a classic bug.
The half-open interval is the programmer's favourite: array indices run from 0 up to but not including the length, range(0, n) and slices a[i:j] are half-open, and adjacent half-open ranges , tile without overlap or gap.
def in_bounds(x, y, width, height):
return 0 <= x < width and 0 <= y < height
print(in_bounds(0, 0, 640, 480), in_bounds(639, 479, 640, 480))
print(in_bounds(640, 10, 640, 480), in_bounds(-1, 10, 640, 480))
def grade(score): # half-open bands tile the range [0, 100]
if 90 <= score: return "A"
if 75 <= score < 90: return "B"
if 50 <= score < 75: return "C"
return "F"
print([grade(s) for s in [49.9, 50, 74.99, 75, 90]])True True
False False
['F', 'C', 'C', 'B', 'A']
"Or" conditions describe what is outside a range: if temp < 2 or temp > 8: alarm() for a fridge that must stay in . By De Morgan's law, "" is exactly "", which is how you turn an "is valid" check into an "is invalid" check.
Exercises
Exercise 1: A chain
Solve and give the interval.
Answer
, that is
Solution
Exercise 2: Negative coefficient
Solve .
Answer
, that is
Solution
Exercise 3: An "or"
Solve or .
Answer
or , that is
Solution
from sympy import symbols, solveset, S
x = symbols("x")
print(solveset(3*x - 2 < -8, x, S.Reals) | solveset(2*x + 1 > 9, x, S.Reals))Union(Interval.open(-oo, -2), Interval.open(4, oo))
Exercise 4: Invert a check
A sensor reading is valid when . Write invalid(r) using "or", without not, and test it on 9.9, 10, 90, 90.1.
Answer
r < 10 or r > 90; results True, False, False, True.
Solution
By De Morgan, is .
def invalid(r):
return r < 10 or r > 90
print([invalid(r) for r in [9.9, 10, 90, 90.1]])[True, False, False, True]
Test yourself
Which interval is the solution of ?
What is the solution of and ?
- No numbers at all
- Every real number
Why are indices in Python described by the half-open interval ?
- Because 0 is excluded
- Valid indices are 0 up to but not including
- Because is negative
- Because indices can be fractions