Dr J H Klopper
Department of Biostatistics and Bioinformatics Milken Institute School of Public Health George Washington University
This chapter of Algebra for Health Data Science by Dr JH Klopper is licensed under Attribution-NonCommercial-NoDerivatives 4.0 International
We start by importing the functions and package classes that we will use in this chapter and set the notebook to print mathematical notation.
from sympy import init_printing, symbols, Eq, solve, Rational
from sympy import Abs, reduce_inequalities, reduce_abs_inequality, Poly, solve_poly_inequality, solve_rational_inequalities
init_printing()
We use the %config
magic to create high-resultion plots.
%config InlineBackend.figure_format = 'retina'
We also import the spb
package to extend the plotting functionalities of sympy
.
import spb
The equal symbol $=$ allows us to consider two versions of the same entity. We have a left-hand and a right-hand side of the equal sign. The left-hand side is equal to the right-hand side. We can also say that the left-hand side is identical to the right-hand side. The equal sign is a relational operator.
We can also consider inequalities, where the relationship between the left-hand and the right-hand sides are not equal. In this chapter we consider inequalities. We also consider the useful notion of absolute values.
In more advanced mathematical studies, we consider conditional statements. Such statements evaluate to either true or false. Some simple statements include conditional arguments. We could write the statmenet $3=3$. The condition for this statement uses the equality symbols. The statement returns a value of true since the left-hand side and the right-hand side are equal to each other. In Python code, we cannot use the =
symbol, since this is used for assignment. We use the ==
symbol to test for equality.
# Is 3 equal tot 3?
3 == 3
True
The code returns a value of True
indicating that the statement is true.
The table below shows the symbols used for conditional statements, with example statements that all evaluate to true.
Symbol | Meaning | Example |
---|---|---|
$=$ | equal to | $3=3$ |
$\neq$ | not equal to | $3\neq 4$ |
$<$ | less than | $3<4$ |
$\leq$ | less than or equal to | $3\leq 4$ |
$>$ | greater than | $4>3$ |
$\geq$ | greater than or equal to | $4\geq 3$ |
The symbols $\le , \leq, \ge$ and $\geq$ are called inequality symbols. We can use these symbols to write inequalities. For example, we can write $x<3$ or $x\geq 3$. The first inequality states that $x$ is less than $3$. The second inequality states that $x$ is greater than or equal to $3$.
These inequalities allow use to consider intervals of real number. Intervals are sets of contiguous values. For example, the interval $[1,3]$ is the set of all values between $1$ and $3$, including $1$ and $3$. The interval $(1,3)$ is the set of all values between $1$ and $3$, excluding $1$ and $3$. In bother cases, the values inside of the brackets and parentheses indicate the minimum and the maximum value in the set.
The brackets and the parentheses notations are commonly used in mathematics. The square brackets $[ \, ]$ indicate that the minimum and maximum values are included in the interval. The round brackets $( \, )$ indicate that the value is excluded from the interval. The interval $[1,3]$ is called a closed interval and the interval $(1,3)$ is called an open interval.
For any value $x$ in the closed interval $[a,b]$ where $a,b \in \mathbb{R}$ and $a<b$ we can also use the notation $a \leq x \leq b$. For any value $x$ in the open interval $(a,b)$ where $a,b \in \mathbb{R}$ and $a<b$, we can use the notation $a < x < b$.
We also define semi-open or semi-closed intervals. The interval $[ a , b )$ is left-closed and right-open and can be written as $a \leq x < b$. The interval $(a,b]$ is left-open and right closed and can be written as $a < x \leq b>$.
Positive infinity and negative infinity are not numbers and cannot be the minimum or maximum values in an interval. We can never have that $[ -\infty, a ]$ or $[3 , \infty]$. Instead we would have $ \left( - \infty , \infty \right)$.
When we perform operations on equalities, we require that the left- and the right-hand sides stay equal. In Equation 9.3.1 we start with the equality $3=3$ and then add $4$ to both sides, maintaining the equality.
The same argument applies to operations on inequalities. These operation must maintain the inequality. For $a,b,c,d \in \mathbb{R} - \{ 0 \}$ (the real numbers without $0$), we can write the following inequalities.
If $a<b$ then $a + c < b + c$
If $a<b$ then $a - c < b - c$
If $a>b$ then $a + c > b + c$
If $a>b$ then $a - c > b - c$
If $a<b$ and $c>0$ then $ac<bc$
If $a<b$ and $c<0$ then $ac>bc$ (the sign flips)
If $a>b$ and $c>0$ then $ac>bc$
If $a>b$ and $c<0$ then $ac<bc$ (the sign flips)
If $a<b$ and $c>0$ then $\frac{a}{c} < \frac{b}{c}$
If $a<b$ and $c<0$ then $\frac{a}{c} > \frac{b}{c}$ (the sign flips)
If $a>b$ and $c>0$ then $\frac{a}{c} > \frac{b}{c}$
If $a>b$ and $c<0$ then $\frac{a}{c} < \frac{b}{c}$ (the sign flips)
If $\frac{a}{c} < \frac{b}{d}$ then $\frac{c}{a} > \frac{d}{b}$ (the sign flips)
Problem 9.3.1 Consider the inequality $-3 < 4$. Show that it is true using code and the show that multiplying both sides by $3$ maintains the inequality.
We can use simply code to determine if $-3$ is less than $4$.
-3 < 4
True
The statement is true indeed.
We multiply both sides by $3$ and evaluate the inequality.
-3 * 3 < 4 * 3
True
The inquality is still true.
Homework 9.3.1 Consider the inequality $-3 < 4$. Show that the inequality holds if we subtract $4$ from both sides.
Problem 9.3.2 Consider the inequality $-3 < 4$. We have shown that it is true using code. Now show that multiplying both sides by $-3$ reverses the inequality.
Now we mutiply both sides by $-3$ and flip the inequality.
-3 * (-3) > 4 * (-3)
True
By flipping the inequality, we have changed the direction of the inequality and the statement remains true.
Homework 9.3.2 Consider the inequality $-3 < 4$. Show that the inequality holds if we multiply both sides by $9$.
Problem 9.3.3 Show that the inequality in Equation 9.3.2 holds.
We use the Rational
function below and pass the numerator and denominator as arguments.
Rational(1, 3) < Rational(1, 2)
A reciprocal is the inverse of a number. The reciprocal of $3$ is $\frac{1}{3}$.
Below, we use the reciprocals of a thrid and a half and swap inequality symbol.
Rational(3, 1) > Rational(2, 1)
We see that Equation 9.3.2 is indeed true.
Homework 9.3.3 Show that the inequality in Equation 9.3.3 holds.
Solving inequalities is the process of solving for the unknown variable in an equation. The rules are very similar to solving any algebraic equation with a single unknown. We do have to remember the rules for operations on inequalities.
Problem 9.4.1 Solve the inequality $3x - 4 < 5$.
We see the steps of the solution in Equation 9.4.1.
The sympy
package can be used to solve ineqialities. We create the mathematical variable $x$ and assign it to the computer variable x
using the symbols
function.
# Create the mathematical symbol x using the symbols function and assign it to the variable x
x = symbols('x')
We use the reduce_inequalities
function and pass the inequality as first argument. The second argument x
refers to the variable that we want to solve for. The reduce_inequalities
function returns two intervals and the logical connective $\land$. We use the simplifty
method to simplify the result. Try the code withouth the simplify
method to see the difference.
# Solve the inequality for x and simplify the solution
reduce_inequalities(3 * x - 4 < 5, x).simplify()
The solve_poly_inequality
can also be used and returns an interval where the inequality holds. The argument of this function contains the Poly
function. The latter contains the left-hand side of the inequality after algebraic manipulation, that is, $3x-4<5$ is manipulated to be $3x-4-5<0$ and we pass $3x-4-5$ as argument to the Poly
function. The second argument is the variable that we want to solve for. The second argument of the solve_poly_inequality
function is the sign of the inequality. The solve_poly_inequality
function returns a single interval.
# Solve for the interval that satisfies the inequality
solve_poly_inequality(Poly(3 * x - 4 - 5, x), '<')
We can also write the solution as the open interval $\left( - \infty , 3 \right)$ which is simply $x<3$.
The subs
method allows us to use an equation or an inequality and then substitute a value for the unknow. Below, we use the inequality in Problem 9.4.1 and subsitute $x=2$, which is on the open interval $\left( - \infty , 3 \right)$.
# Use the subs method to determine if 2 satisfies the inequality
(3 * x - 4 < 5).subs(x, 2)
The result is true and the number $2$, as representative of the interval that is our solution, does indeed satisfy the inequality.
Using a value outside of the interval such as $x = 3$ does not satisfy the inequality. We use the subs
method to substitute $x=3$.
# Use the subs method to determine if 3 satisfies the inequality
(3 * x - 4 < 5).subs(x, 3)
The result indeed shows that a represenattive value outside of the solution interval does not satisfy the inequality.
Homework 9.4.1 Find the interval where the inequality $x + 5 \ge 3$ .
Problem 9.4.2 Solve the inequality $-2 (x - 4) < 5$.
We solve the inequality in Equation 9.4.2 using the rules of the operations on inequalities.
We solve the inequality using the reduce_inequalities
function.
reduce_inequalities(-2 * (x - 4) < 5, x).simplify()
Despite using the simplify
method, the reduce_inequalities
function still returns the solution with a logical connective $\land$, which means and that reads $x$ is larger than three-halves and less than infinity. The solution is the open interval $\left( \frac{3}{2} , \infty \right)$. A result that is returned by the solve_poly_inequality
function.
solve_poly_inequality(Poly(-2 * (x - 4) - 5, x), '<')
We use the subs
method to substitute $x=2$, as representative value in the interval solution, into the inequality.
(-2 * (x - 4) < 5).subs(x, 2)
A representative value in the interval solution satisfies the inequality.
We can also solve quadratic equations.
Problem 9.4.3 Solve the inequality $x^2 - 4x > -3$.
Through simple algebra, we recognize the quadratic equation (second order polynomial) $x^2 - 4x + 3 > 0$. We can factorize this equation to $(x - 3)(x - 1) > 0$.
For the product of two arbitrary values $a,b \in \mathbb{R}$ to be positive (larger than $0$), we must have that both $a$ and $b$ are positive or that both $a$ abd $b$ are negative. We show the solution in Equation 9.4.3.
The reduce_inequalities
function confirms the result.
reduce_inequalities(x**2 - 4 * x > -3, x).simplify()
The logical connective $\vee$ means or. We have the intervals $\left( 3 , \infty \right)$ or $\left( - \infty , 1 \right)$.
The solve_poly_inequality
function returns the same result.
solve_poly_inequality(Poly(x**2 - 4 * x + 3, x), '>')
Below, we use the subs
method to substitute $x = 4$ and $x = 0$, one represenattive value from each of the two intervals.
(x**2 - 4 * x > -3).subs(x, 4)
(x**2 - 4 * x > -3).subs(x, 0)
A value such as $x = 2$ which is not in either of the two intervals does not satisfy the inequality.
(x**2 - 4 * x > -3).subs(x, 2)
Homework 9.4.2 Solve the inequality $x^2 - x -6 > 0$.
Problem 9.4.4 Solve the inequality in Equation 9.4.4.
The right-hand side of the inequality is non-positive. That means that the left-hand side can only be $0$ or negative. The left-hand side of the equation has a polynomial in the numerator and in the denominator and left-hand side can only equal $0$ when the numerator is $0$. Furthemore, the left hand side can only be negative when the numerator and denominator have opposite signs. Each of these cases have to be explored. We show the solution in Equation 9.4.5.
Combining the three cases $x = \pm 1 \land -1 < x < 1$ and $x < -3$ we have the solution $(x < -3) \lor (-1 \le x \le 1)$.
We confirm the result with the reduce_inequalities
function.
reduce_inequalities((x**2 - 1)/(x+3) <= 0, x).simplify()
Choosing the representative values $x=-4$ and $x=0$ from the solution interval, we see that the inequality is true.
((x**2 - 1)/(x+3) <= 0).subs(x, -4)
((x**2 - 1)/(x+3) <= 0).subs(x, 0)
Homework 9.4.3 Solve the inequality in Equation 9.4.6.
Instead of solving for a single variable in an equation (or then finding the interval(s) where the inequality holds), we can also sconsider inequalities in functions.
Problem 9.5.1 Consider the function $f(x) \ge x^2$. We want to use the Cartesian plane to show where $y \ge x^{2}$. Figure 9.5.1 shows the graph of $y \ge x^{2}$. To do this, we create the mathemical symbol $y$ using the symbols
function and assign it to the variable y
. We use the spb
package to plot the function.
# Create the mathematical symbol y using the symbols function and assign it to the variable y
y = symbols('y')
# Plot the function y >= x^2 using the spb.plot_inequality function
spb.graphics(
spb.implicit_2d(
y >= x**2,
(x, -3, 3),
(y, -1, 3),
title='Figure 9.5.1'
)
);
In the shaded area we have that $y \ge x^{2}$.
In Equation 9.6.1 we show the definition of the absolute value of a real number $x$.
To illustrate the definition, we consider an example where $x=3$ and then $x=-3$. Since $3>0$ we have that $\left| 3 \right| = 3$ and since $-3<0$ we have that $\left| -3 \right| = -(-3) = 3$. The absolute value of a real number is always positive or zero. The absolute value of a real number is the distance of the real number from $0$ on the number line.
Problem 9.6.1 Solve the equation $\left| x - 3 \right| = 5$.
We create two separate equation using the definition of the absolute value in Equation 9.6.1. We solve each equation separately, shown in Equation 9.6.2.
We use the solve
function to confirm the results. In this case, we have to solve both equation separately. We use the Eq
function to define each case and assign it to the variable case_1
and case_2
.
# Use the Eq function to create the equation x - 3 = 5 and assign it to the variable case_1
case_1 = Eq(x - 3, 5)
case_1
# Solve the equation for x
solve(case_1, x)
# Use the Eq function to create the equation -(x - 3) = 5 and assign it to the variable case_2
case_2 = Eq(-(x - 3), 5)
case_2
# Solve the equation for x
solve(case_2, x)
We see that the two solutions are indeed $x = 8$ or $x = -2$. The subs
method is used below to substitute $x=8$ and $x=-2$ into the two cases of the original equation.
case_1.subs(x, 8) # The case that x is equal to or greater than 0
case_2.subs(x, -2) # The case where x is less than 0
Homework 9.6.1 Solve the equation $\lvert x^{3} \rvert = 27$. Hint: There are many solutions (inlcuding complex numbers).
Absolute values can also be used in inequalities. We use the same definition of the absolute value in Equation 9.6.1.
Problem 9.6.2 Solve the inequality $\left| x - 3 \right| < 5$.
We see the solution in Equation 9.6.3.
The final solution is the open interval $-2 < x < 8$.
The use the reduce_inequalities
function to solve an inequality that contains an absolute value expression. The Bas
function is used to express an absolute value. To the the reduce_inequalities
function, we have to do some algebraic manipulation to get the absolute value expression on the left-hand side of the inequality. In the case of this problem it would be $\left| x - 3 \right| - 5$. Note that this expression is passed as first argument. The second argument is a string denoting less than or more than. The third argument is the variable that we want to solve for.
# Solve the inequality for x
reduce_abs_inequality(Abs(x - 3) - 5, '<', x).simplify()
Below we use three representative values, $x=-1$, $x=0$, and $x=7$, for the solution interval.
# Determine of -1 satisfies the inequality
(Abs(x - 3) < 5).subs(x, -1)
# Determine of 0 satisfies the inequality
(Abs(x - 3) < 5).subs(x, 0)
# Determine of 7 satisfies the inequality
(Abs(x - 3) < 5).subs(x, 7)
The inequality is true for all three values.
Homework 9.6.2 Solve the inequality $\left| x^{3} + 1 \right| \ge 5$.