# Oh! Matryoshka!

Given polynomial $f(x) = a_0 + a_1 x+a_2 x^2 + ... + a_{n-1}x^{n-1}+a_n x^n$, we wish to evaluate integral $\int \frac{f(x)}{(x-a)^p}\;dx, \quad p \in N^+\quad\quad\quad(1)$

When $p = 1$, $\int \frac{f(x)}{x-a} \;dx= \int \frac{f(x)-f(a)+f(a)}{x-a}\;dx$ $= \int \frac{f(x)-f(a)}{x-a}\;dx + \int \frac{f(a)}{x-a}\;dx$ $=\int \frac{f(x)-f(a)}{x-a}\;dx + f(a)\cdot \log(x-a)$.

Since $f(x) = a_0 + a_1x + a_2x^2 + ... + a_{n-1}x^{n-1} + a_n x^n$

and $f(a) = a_0 + a_1 a + a_2 a^2 + ... + a_{n-1}a^{n-1} + a_n a^n$

It follows that $f(x)-f(a) = a_1(x-a) + a_2(x^2-a^2) + ... + a_{n-1}(x^{n-1}-a^{n-1}) + a_n (x^n-a^n)$.

That is $f(x)-f(a) = \sum\limits_{k=1}^{n}a_k(x^k-a^k)$

By the fact (see “Every dog has its day“) that $x^k-a^k =(x-a)\sum\limits_{i=1}^{k}x^{k-i}a^{i-1}$,

we have $f(x)-f(a) = \sum\limits_{k=1}^{n}a_k(x-a)\sum\limits_{i=1}^{k}x^{k-i}a^{i-1}=(x-a)\sum\limits_{k=1}^{n}(a_k\sum\limits_{i=1}^{k}x^{k-i}a^{i-1})$

or, $\frac{f(x)-f(a)}{x-a}= \sum\limits_{k=1}^{n}(a_k\sum\limits_{i=1}^{k}x^{k-i}a^{i-1})\quad\quad\quad(2)$

Hence, $\int\frac{f(x)}{x-a}\;dx = \int \sum\limits_{k=1}^{n}(a_k \sum\limits_{i=1}^{k}x^{k-i}a^{i-1})\;dx + f(a)\log(x-a)$ $=\sum\limits_{k=1}^{n}(a_k \sum\limits_{i=1}^{k}\int x^{k-i}a^{i-1}\; dx)+ f(a)\log(x-a)$

i.e., $\int \frac{f(x)}{x-a} = \sum\limits_{k=1}^{n}(a_k\sum\limits_{i=1}^{k}\frac{x^{k-i+1}}{k-i+1}a^{i-1})+ f(a)\log(x-a)$

Let us now consider the case when $p>1$: $\int \frac{f(x)}{(x-a)^p}\; dx$ $=\int \frac{f(x)-f(a)+f(a)}{(x-a)^p}\;dx$ $=\int \frac{f(x)-f(a)}{(x-a)^p} + \frac{f(a)}{(x-a)^p}\;dx$ $=\int \frac{f(x)-f(a)}{(x-a)}\cdot\frac{1}{(x-a)^{p-1}} + \frac{f(a)}{(x-a)^p}\;dx$ $= \int \frac{f(x)-f(a)}{x-a}\cdot\frac{1}{(x-a)^{p-1}}\;dx + \int\frac{f(a)}{(x-a)^p}\; dx$ $\overset{(2)}{=}\int \frac{g(x)}{(x-a)^{p-1}}\;dx + \frac{f(a)(x-a)^{1-p}}{1-p}$

where $g(x) = \frac{f(x)-f(a)}{x-a}=\sum\limits_{k=1}^{n}(a_k\sum\limits_{i=1}^{k}x^{k-i}a^{i-1})$, a polynomial of order $n-1$.

What emerges from the two cases of $p$ is a recursive algorithm for evaluating (1):

Given polynomial $f(x) = \sum\limits_{k=0}^{n} a_k x^k$, $\int \frac{f(x)}{(x-a)^p} \;dx, \; p \in N^+= \begin{cases}p=1: \sum\limits_{k=1}^{n}(a_k\sum\limits_{i=1}^{k}\frac{x^{k-i+1}}{k-i+1}a^{i-1})+ f(a)\log(x-a) \\p>1: \int \frac{g(x)}{(x-a)^{p-1}}\;dx + \frac{f(a)(x-a)^{1-p}}{1-p}, \\ \quad\quad\quad g(x) = \frac{f(x)-f(a)}{x-a}=\sum\limits_{k=1}^{n}(a_k\sum\limits_{i=1}^{k}x^{k-i}a^{i-1}). \end{cases}$

Exercise-1 Optimize the above recursive algorithm (hint: examine how it handles the case when $f(x)=0$)

# Integration of Trigonometric Expressions

We will introduce an algorithm for obtaining indefinite integrals such as $\int \frac{(1+\sin(x))}{\sin(x)(1+\cos(x))}\;dx$

or, in general, integral of the form $\int R(\sin(x), \cos(x))\;dx\quad\quad\quad(1)$

where $R$ is any rational function $R(p, q)$, with $p=\sin(x), q=\cos(x)$.

Let $t = \tan(\frac{x}{2})\quad\quad(2)$

Solving (2) for $x$, we have $x = 2\cdot\arctan(t)\quad\quad\quad(3)$

which provides $\frac{dx}{dt} = \frac{2}{1+t^2}\quad\quad\quad(4)$

and, $\sin(x) =2\sin(\frac{x}{2})\cos(\frac{x}{2})\overset{\cos^(\frac{x}{2})+\sin^2(\frac{x}{2})=1}{=}\frac{2\sin(\frac{x}{2})\cos(\frac{x}{2})}{\cos^2(\frac{x}{2})+\sin^2(\frac{x}{2})}=\frac{2\frac{\sin(\frac{x}{2})}{\cos(\frac{x}{2})}}{1+\frac{\sin^2(\frac{x}{2})}{\cos^2(\frac{x}{2})}}=\frac{2\tan(\frac{x}{2})}{1+\tan^2(\frac{x}{2})}$

yields $\sin(x) = \frac{2 t}{1+t^2}\quad\quad\quad(5)$

Similarly, $\cos(x) = \cos^2(\frac{x}{2})-\sin^2(\frac{x}{2})=\frac{\cos^2(\frac{x}{2})-\sin^2(\frac{x}{2})}{\cos^2(\frac{x}{2})+\sin^2(\frac{x}{2})}=\frac{1+\frac{\sin^2(\frac{x}{2})}{\cos^2(\frac{x}{2})}}{1+\frac{\sin^2(\frac{x}{2})}{\cos^2(\frac{x}{2})}}=\frac{1-\tan^2(\frac{x}{2})}{1+\tan^2(\frac{x}{2})}$

gives $\cos(x)=\frac{1-t^2}{1+t^2}\quad\quad\quad(6)$

We also have (see “Finding Indefinite Integrals” ) $\int f(x)\;dx \overset{x=\phi(t)}{=} \int f(\phi(t))\cdot\frac{d\phi(t)}{dt}\;dt$.

Hence $\int R(\cos(x), \sin(x))\;dx \overset{(2), (4), (5), (6)}{=} \int R(\frac{1-t^2}{1+t^2}, \frac{2t}{1+t^2})\cdot\frac{2}{1+t^2}\;dt$,

and (1) is reduced to an integral of rational functions in $t$.

Example-1 Evaluate $\int \csc(x)\;dx$.

Solution: $\csc(x) = \frac{1}{\sin(x)}\implies \int \csc(x)\;dx = \int \frac{1}{\sin(x)}\;dx$ $= \int \frac{1}{\frac{2t}{1+t^2}}\cdot\frac{2}{1+t^2}\;dt=\int\frac{1}{t}\;dt = \log(t) = \log(\tan(\frac{x}{2}))$.

Example-2 Evaluate $\int \sec(x)\;dx$.

Solution: $\sec(x) = \frac{1}{\cos(x)}\implies \int \sec(x)\; dx =\int \frac{1}{\cos(x)}\;dx$ $= \int \frac{1}{\frac{1-t^2}{1+t^2}}\cdot \frac{2}{1+t^2}\; dt=\int \frac{2}{1-t^2}\;dt=\int \frac{2}{(1+t)(1-t)}\;dt=\int \frac{1}{1+t} + \frac{1}{1-t}\;dt$ $=\int \frac{1}{1+t}\;dt - \int \frac{-1}{1-t}\;dt$ $=\log(1+t) -\log(1-t) =\log\frac{1+t}{1-t}=\log(\frac{1+\tan(\frac{x}{2})}{1-\tan(\frac{x}{2})})$.

According to CAS (see Fig. 1),

Fig. 1

However, the two results are equivalent as a CAS-aided verification (see Fig. 2) confirms their difference is a constant (see Corollary 2 in “Sprint to FTC“).

Fig. 2

Exercise-1 According to CAS,

Show that it is equivalent to the result obtained in Example-1

Exercise-2 Try $\int \frac{1}{\sin(x)+1}\;dx$ $\int \frac{1}{\sin(x)+\cos(x)}\;dx$ $\int \frac{1}{(2+\cos(x))\sin(x)}\;dx$ $\int \frac{1}{5+4\sin(x)}\;dx$ $\int \frac{1}{2\sin(x)-\cos(x)+5}\;dx$

and of course, $\int \frac{1+\sin(x)}{\sin(x)(1+\cos(x))}\;dx$