Proving that the Average Product function attains max value at the point where the Marginal Product is equal to the Average Product. I am basically trying to prove the following excerpt from my Microeconomics textbook:

When Average Product is maximum, Marginal Product is equal to the Average Product.

The Total Product function $TP(x)$ gives the output produced by using $x$ units of variable input. The Average Product function $AP(x)$ is just $\frac{TP(x)}{x}$ and is defined only when $x > 0$.
Here's my attempt at a formal proof:
Let $AP(x)$ be maximum when $x = r$ where $r$ is a constant greater than $0$. Then,
$$AP(r)\ge AP(x)\forall x>0$$
We have to prove that at $x=r$, Marginal Product is equal to the Average Product. That is,
$$MP(r)=AP(r)$$
Proof:
$$AP(r)\ge AP(x)\forall x>0$$
$$\implies \frac{TP(r)}{r}\ge \frac{TP(x)}{x}\forall x>0$$
$x>0$ and $r>0 \implies xr > 0$. So multiplying both sides of the above inequality by $xr$ won't reverse the inequality.
$$\implies xr \frac{TP(r)}{r}\ge xr\frac{TP(x)}{x}\forall x>0$$
$$\implies xTP(r)\ge rTP(x)\forall x>0$$
Multiply both sides by $-1$.
$$\implies -xTP(r)\le -rTP(x)\forall x>0$$
Add $rTP(r)$ to both sides.
$$\implies -xTP(r)+rTP(r)\le -rTP(x)+rTP(r)\forall x>0$$
$$\implies (-x+r)TP(r)\le r\bigr(-TP(x)+TP(r)\bigr)\forall x>0$$
$$\implies (r-x)TP(r)\le r\bigr(TP(r)-TP(x)\bigr)\forall x>0$$
Divide both sides by $r$. Again, the inequality will not be flipped since $r>0$.
$$\implies (r-x)\frac{TP(r)}{r}\le TP(r)-TP(x)\forall x>0$$
$AP(r)= \frac{TP(r)}{r}$ so
$$\implies (r-x)AP(r)\le TP(r)-TP(x)\forall x>0$$
If $0<x<r$ then $r-x>0$. Dividing both sides by $r-x$ won't change the sign of the inequality.
$$\implies AP(r)\le \frac{TP(r)-TP(x)}{r-x} ; 0<x<r$$
If $x>r$ then $r-x<0$. Since $r-x$ is a negative number, dividing both sides by it will change the sign of the inequality.
$$\implies AP(r)\ge \frac{TP(r)-TP(x)}{r-x} ; x>r$$
If $x=r$ then both sides of the inequality become $0$. We can't divide by $x-r$ because that would make the expression undefined. I am not sure how to handle this case and was hoping someone would let me know what to do here.
Anyway, my textbook has two formulae for calculating the value Marginal product at some input. They are
$$MP(x)=TP(x)-TP(x-1)$$
and
$$MP(x) = \frac{\Delta TP(x)}{\Delta x}$$
Using the second formula we get:
$$MP(r)=\frac{TP(r)-TP(x)}{r-x}$$
Substituting this in the above two inequalities, we get:
$$\implies AP(r)\le MP(r) ; 0<x<r$$
$$AP(r)\ge MP(r) ; x>r$$
Now, we know that $r$ is a constant. That means $AP(r)$ and $MP(r)$ are also constants! Irrespective of whether $x$ is greater than or less than $r$, $AP(r)$ and $MP(r)$ will remain constants. In both of the above two cases, either $AP(r)$ is less than $MP(r)$ OR $AP(r)$ is equal to $MP(r)$ OR $AP(r)$ is greater than $MP(r)$.
There is only one case where both of the two inequalities are true and this is when $AP(r)$ is equal to $MP(r)$.
$$\therefore MP(r) = AP(r)$$
Is this proof correct?
Edit:
The book I am using asserts that $MP$ is not a strictly decreasing function. Therefore, $MP'$ is not always negative.


How about the following: $AP(x)=TP(x)/x$ To find the max, take the derivative and set it to zero: $$\frac{d AP}{d x}=\frac{x MP(x) -TP(x)}{x^2}=0.$$
or $$MP(x)=\frac{TP}{x}=AP(x).$$
Checking the second order term to make sure it's a maximum,
$$\frac{x^3 MP' -2x^2 MP}{x^4}$$ which is negative because marginal product is decreasing.
