I am currently working on a tiny matrix library in C++ to help myself learn more about them. So far, I have implemented basic functions such as addition, subtraction, multiplication, the determinant, cofactor, adjoint and inverse, and was recently looking into $\exp\left(M\right)$, $\ln\left(M\right)$, $\ln\left(M\right)$, $\ln\left(M\right)$, and other such functions.
I am currently only working on functions which are equal to their taylor series ($\exp\left(M\right)$, $\sin\left(M\right)$, $\cos\left(M\right)$, ...).
To implement the functions, one method would be to use the taylor series directly
$$\exp(M) = I + M + \frac{1}{2!}M + \frac{1}{3!}M + \frac{1}{4!}M + \frac{1}{5!}M\dots$$
Mathematically, this would work just fine so tried it, and it was bad, to say the least. Matrix multiplication is a very slow algorithm, and the series does not converge very fast.
My Progress
After looking into it, I learned about diagonalization of matrices. Then, I learned that exponentiation of diagonal matrices is just that element to the power, and I thought I could use this to my advantage.
Let our function be $f(M)$ and let its taylor series be
$$f(M) = a_0I + a_1M + \frac{a_2}{2!}M + \frac{a_3}{3!}M + \frac{a_4}{4!}M + \frac{a_5}{5!}M\dots$$ We can write $f(M)$ compactly as $$f(M) = \sum_{i=0}^\infty{\frac{a_i}{i!}M^i}$$
If $M$ is of the form $M = P^{-1}QP$, then
$$ f(M) = \sum_{i=0}^\infty{\frac{a_i}{i!}M^i} \\~\\ f(M) = \sum_{i=0}^\infty{\frac{a_i}{i!}{P^{-1}}^iQ^iP^i} \\~\\ f(M) = P^{-1}\left(\sum_{i=0}^\infty{\frac{a_i}{i!}Q^i}\right)P \\~\\ f(M) = P^{-1}\left[\sum_{i=0}^\infty{\frac{a_i}{i!}q^i}\right]P \\~\\ f(M) = P^{-1}\left[f(q)\right]P $$ If $q$ denotes the diagonal elements.
I quickly ran into a problem, not every matrix can be diagonalized. I discovered the Jordan Normal Form, and a similar result can be proved for the Jordan Normal Form by exponentiating blocks instead of diagonal elements.
With the JNF, I ran into the problem that matrices can have complex eigenvalues, and I haven't been able to find any solution to it.
My Problem
I want to be able to take any matrix $M$ and write it in the form $$M = P^{-1}QP$$ where
- P is an invertible matrix
- Q is a matrix such that Q is a diagonal block matrix.
- Q has as few blocks larger than $1\times1$ as possible.
- Q has only real elements (Otherwise the JNF would have sufficed)