# Logarithm of a matrix, with a matrix as the base: Find $x$ in $\mathbf{A}^x = \mathbf{B}$

I want to solve $\mathbf{A}^x = \mathbf{B}$ where $\mathbf{A}$ and $\mathbf{B}$ are both $n$-by-$n$ matrices and $x$ is real. I see that in general there may be no solutions, or multiple solutions.

I was trying to find the period of a discrete-time linear dynamical system, but now I'm interested in the equation itself.

By "solve" I mean, given the two matrices, how can I find $x$ using the linear algebra primitives that MATLAB has, for example. Or maybe there's a name for this equation.

edit maybe less generally $\mathbf{A}^x = \lambda \mathbf{I}$

• You have to be rather lucky to find an $x$ for "random" $A$ and $B$. Just think about the degrees of freedom... Jul 7, 2012 at 17:18
• What kind of values do $A$ and $B$ take? Do you know them exactly or only to finite precision? Jul 7, 2012 at 18:54

It is not clear what you mean by $A^x$ for $x$ irrational or even for $x$ not an integer. This is an issue already when $n = 1$; for example, what do you mean by $(-1)^{1/2}$? ( Which square root of $-1$?) What do you mean by $(-1)^{\pi}$?

One fix in the case $n = 1$ is to restrict to the case that $A, B$ are positive reals. The corresponding restriction for matrices is to restrict to the case that $A, B$ are positive-definite Hermitian matrices. In that case the spectral theorem guarantees that $A$ has an orthonormal basis of eigenvectors $e_1, ... e_n$ with positive real eigenvalues $\lambda_1, ... \lambda_n$, so you can define $A^x$ by requiring that $$A^x e_i = \lambda_i^x e_i$$

for all $i$. A necessary condition for the existence of a solution to $A^x = B$ is that $B$ also has eigenvectors $e_1, ... e_n$ (since this is true of $A^x$), and a necessary and sufficient condition is that its eigenvalues $\mu_1, ... \mu_n$ with respect to those eigenvectors satisfies $$\lambda_i^x = \mu_i$$

for all $i$.

• This was long ago! Sorry. Yes, probably in my case, the right answer would have been to diagonalize both matrices (or perhaps use the SVD). If the the bases were the same, then there is a chance that the eigenvalues of $A$, after exponentiating by some integer, would equal the eigenvalues of $B$. It looks like what I was trying to do was find the period of the discrete-time linear time-invariant system. And so the approach would be to find $A=V^{-1}\Lambda V$ and the smallest integer $n$ such that $\Lambda^n = I$
– Gus
Apr 23, 2015 at 14:38

Let's start with a random matrix $$A$$:

A = round(10*rand(2))
A =
2   6
7   9


The following matrix $$B$$ is the cube of $$A$$:

B=A^3
B =
554    870
1015   1569


How would you find the exponent with which we raised $$A$$? In this case it was 3. Let's see if we can find it in a systematic say.

We start by verifying that $$B$$ is indeed a power of $$A$$, by verifying that the eigenvectors are the same:

[AE AD]=eig(A);AE
AE =

-0.8406  -0.4834
0.5416  -0.8754

[BE BD]=eig(B);BE
BE =

-0.8406  -0.4834
0.5416  -0.8754


We now just have to find the exponent $$x$$ such that the eigenvalues $$\lambda_{A,i}$$ of $$A$$ and $$\lambda_{B,i}$$ of $$B$$ are related by:

$$\tag{1} \left(\lambda_{A,i}\right)^x = \lambda_{B,i}.$$

Unfortunately in this case we have:

$$\tag{2} \left(\lambda_{A,1}\right)^3 = \lambda_{B,1} < 0,$$

so it might not be the best idea to try to use logarithms immediately to obtain the exponent 3 (we would like to try to avoid calculating the log of a negative number!), but by applying the absolute value first and using the change of base formula we get:

diag(log(abs(BD))./log(abs(AD)))
ans =
3.0000
3.0000


This tells us that the solution to $$A^x = B$$ in this case is $$x=3$$.

In general, for matrices $$A$$ and $$B$$ with shared eigenvector matrices $$U$$, we can define $$\log_A(B)$$ as:

$$\tag{3} UDU^{-1}$$

where as long as no eigenvalues were 0, we have:

$$D = \begin{pmatrix} \frac{\ln|\lambda_{B,1}|}{\ln|\lambda_{A,1}|} & 0& 0 & \cdots & 0\\ 0 & \frac{\ln|\lambda_{B,2}|}{\ln|\lambda_{A,2}|} & 0& \cdots & 0\\ 0 & 0 & \ddots & \cdots & 0\\ \vdots & \vdots & \ddots & \cdots & 0\\ 0 & 0 & 0 & 0 & \frac{\ln|\lambda_{B,n}|}{\ln|\lambda_{A,n}|}\\ \end{pmatrix}$$

If $$B$$ is truly a power of $$A$$, then this power will be the value on each diagonal of the matrix $$\log_A(B)$$. The following sequence of commands will get you there (notice that the diagonals in the final answer are both 3):

A = round(10*rand(2));B=A^3;
X =
3.0000e+00   1.2304e-13
1.2301e-13   3.0000e+00


If you want to bring the off-diagonals closer to 0 (they'll never be exactly 0 if you're using floating-point arithmetic), you can multiply by $$U^{-1}$$ on the left and $$U$$ on the right:

inv(AE)*AE*diag(diag(log(abs(BD))./log(abs(AD))))*inv(AE)*AE
ans =
3.0000e+00   2.8477e-16
2.4057e-17   3.0000e+00


But the amazing thing about what we just did, is that if you then calculate $$A^X = e^{\ln(A)X}$$, you actually get back $$B$$! Perhaps even more amazingly, this even works when $$A$$ has a negative eigenvalue and no absolute value functions are included in the definition for calculating the matrix $$X$$ (though the direct interpretation as an exponent is lost).

There's also other ways to define functions of matrices, for example

• by Sylvester's formula
• by Taylor expansion (in this case expand $$\log_A(B)$$ around $$A=C$$ as long as $$C$$ has non-negative eigenvalues and is not a matrix of ones?),
• by a Laurent series when a Taylor series doesn't work,