# Functions and their inverses¶

See Section 1.5 Solving equations for the intro.

In the notebook below we’ll use look at examples of equations involving these functions and use apply the inverse function to solve the equations.

## Notebooks setup¶

# import sympy
from sympy import *
from sympy.abc import *

# import and apply mathcube7's hack for
# auto-broadcasting operations to Eq.lhs and Eq.rhs
from customizer import *


## Example 1¶

Solve the equation $$x + 2 = 7$$.

eqn1 = Eq(x + 2, 7)
eqn1

$\displaystyle x + 2 = 7$

The left hand side describes the function $$f(x)=x+2$$ (add two to the input).

We want to apply the undo opration, i.e., the inverse function $$f^{-1}$$, which we can lookup in the right hand side of the table. The opposite operation of “add 2 to the input” is “subtract 2 from the input.” The inverse function of $$f(x)=x+2$$ is $$f^{-1}=x-2$$.

So if we want to simplify eqn1 we want to subtract 2 from both sides of the equation

eqn1 - 2

$\displaystyle x = 5$

The equation is now solved. The value of $$x$$ that satisfies the equation $$x + 2 = 7$$ is $$x=5$$.

# Implementation note: applying -2 to an equation object is convenience-customization
# that under the hood creates a new equation where -2 is applied to both the left-hand-side (lhs)
# and the right hand side (rhs) of the equation:
Eq(eqn1.lhs - 2, eqn1.rhs - 2)

$\displaystyle x = 5$

In all examples below, we’ll apply the inferse function on the equation as a whole, but remember that this is shortcut for describing “apply inverse functoin to both sides of the equation.”

## Example 2¶

Solve the equation $$2x = 7$$.

eqn2 = Eq(2*x, 7)
eqn2

$\displaystyle 2 x = 7$

The inferse operation of multiply by $$2$$ is divide by $$2$$

eqn2 / 2

$\displaystyle x = \frac{7}{2}$

## Example 3¶

Solve the equation $$-x = 7$$.

The negative of the unknown $$x$$ is equal to $$7$$, so $$x = -7$$. We don’t really need to use the whole apply-the-inverse procedure here, but let’s still go through the motions:

eqn3 = Eq(-x, 7)
eqn3

$\displaystyle - x = 7$

The inverse function of $$f(x)=-x$$ (multiply by -1) is $$f^{-1}=-x$$ (multiply by -1).

eqn3 * -1

$\displaystyle x = -7$

Geometrically $$-x$$ is the point on the number line which is the reflection of $$x$$ to the opposite side of the axis.

Doing the reflection twice, (-1)*(-1), puts the point back to the original $$x$$. Algebraically speaking, this is the rule negative-times-a-negative-is-positive (-1)*(-1)*x = x.

## Example 4¶

Solve the equation $$x^2 = 7$$.

eqn4 = Eq(x**2, 7)
eqn4

$\displaystyle x^{2} = 7$
sqrt(eqn4)

$\displaystyle \sqrt{x^{2}} = \sqrt{7}$

The combination of squaring a number then taking the square root is equivalent to the absolute value function, which erases the sign of the number. The solutions to the equation are numbers whose absolute value is $$\sqrt{7}$$.

The equation has two solutions: $$x=\sqrt{7}$$ and $$x=-\sqrt{7}$$.

eqn4.subs({x:sqrt(7)})

$\displaystyle \text{True}$
eqn4.subs({x:-sqrt(7)})

$\displaystyle \text{True}$

## Example 5¶

Solve the equation $$2^x = 7$$.

x = Symbol('x', real=True)  # explicitly specify x is real and not complex
eqn5 = Eq(2**x, 7)
eqn5

$\displaystyle 2^{x} = 7$
log(eqn5, 2)

$\displaystyle \frac{\log{\left(2^{x} \right)}}{\log{\left(2 \right)}} = \frac{\log{\left(7 \right)}}{\log{\left(2 \right)}}$
# need to call simplify whcich will recognize that ln(2^x)/ln(2) = x, when x is a real number
simplify(log(eqn5, 2))

$\displaystyle x = \frac{\log{\left(7 \right)}}{\log{\left(2 \right)}}$

The solution is $$x = \log_2(7) = \frac{\ln(7)}{\ln(2)}$$.

To see a numerical approximation of the solution as a float, we can use the evalf method:

simplify(log(eqn5, 2)).rhs.evalf()

$\displaystyle 2.8073549220576$
# compute the float approximation of log_2(7)
log(7, 2).evalf()

$\displaystyle 2.8073549220576$

## Example 6¶

Solve the equation $$3x + 5 = 7$$.

eqn6 = Eq(3*x + 5, 7)
eqn6

$\displaystyle 3 x + 5 = 7$
# subtract 5 on both sides:
eqn6 - 5

$\displaystyle 3 x = 2$
# subtract 5 on both sides, then multiply by 1/3
(eqn6 - 5) * 1/3

$\displaystyle x = \frac{2}{3}$