Matrix to the power n equals a matrix, solve it for n How to solve the equation $$\mathbf{A}^n=\mathbf{B}$$ for $n$, where $\mathbf{A}$ and $\mathbf{B}$ are given real square matrices? (Formally something like the logarithm would be needed, but instead of the usual interpretation of matrix logarithm, here the base is a matrix.)
 A: This I'm afraid isn't always numerically solvable, especially in the presence of round-off errors.
E.g. consider the case where $A$ is a $2\times2$ rotation matrix for an angle $\theta$. Then $B$ is also a rotation matrix for an angle $\phi$. The problem then reduces to solving $n\theta=\phi+2m\pi$ for integers $n$ and $m$. Numerically, we can perhaps find a sequence $\{(n_k,m_k)\}_{k\in\mathbb N}$ such that $|n_k\theta-\phi-2m_k\pi|$ is arbitrarily small, but the problem is that the sequence $\{(n_k,m_k)\}_{k\in\mathbb N}$ itself doesn't converge, and so we don't know which $n_k$ is close to the true value of $n$.
However, if $A$ possesses a nonzero eigenvalue of modulus $\ne1$, we may obtain $n$ from $\frac{\log|\lambda|^n}{\log|\lambda|}$. That is, if $A=PJP^{-1}$ where $J$ is the Jordan form of $A$ and $\lambda_i$ is the $i$-th diagonal entry of $J$, then $n=\frac{\log|(P^{-1}BP)_{ii}|}{\log|\lambda_i|}$ when $|\lambda_i|\ne0,1$.
If $J$ has a nilpotent Jordan block and the corresponding diagonal sub-block in $P^{-1}BP$ is nonzero, we may also read off $n$ directly from the position of the super-diagonal of that diagonal sub-block.
