2

Let ${\bf A}$ be a symmetric matrix, and I would like to find $\sqrt{\bf A}$ of a $3\times 3$ matrix. I am interested in finding the $\sqrt{\rm A}$ which is equivalent to the "sqrtm" built-in function in python (which is based on the Schur method: LINK).

I found this formulation, $$ \sqrt{\bf A} = \frac{{\bf A} + \sqrt{\operatorname{det}{\bf A}}\ {\tt I}} {\sqrt{\,{\operatorname{tr}\left(\bf A\right) + 2 \sqrt{\,{\operatorname{det}\left(\bf A\right)}\,}}}} $$ in one of the previous posts LINK. This formulation gives the exact results as the sqrtm in python. However, it is valid for ONLY a $2\times2$ matrix.

My question: is there a similar formulation that can be valid for a $3\times3$ matrix?

Many thanks in advance!

Felix Marin
  • 94,079
  • Does $A$ have real entries or complex? If real, are you looking only for $\sqrt{A}$ that have real entries, or could they have complex entries? (I am not familiar with what sqrtm returns.) – 2'5 9'2 Nov 27 '22 at 23:38
  • $A$ has only real entries, and I am looking for only the real entries of $\sqrt{A}$. I just checked out the values returned by sqrtm; it returns real entries, in my case, as well. – I. Mohamed Nov 28 '22 at 02:48
  • In the equation you posted for a 2x2 square root, if $A$ has a negative determinant, then that formula returns something with non-real entries. For example if $A=\begin{bmatrix}1&0\0&-1\end{bmatrix}$, then you get $\frac{1}{\sqrt{2i}}\begin{bmatrix}1+i&0\0&-1+i\end{bmatrix}=\begin{bmatrix}1&0\0&i\end{bmatrix}$. – 2'5 9'2 Nov 28 '22 at 05:42
  • Another thing about that formula is that some care is needed about which $\sqrt{\phantom{x}}$ to use. If $A=-I$, then naively, the numerator (and denominator) of that expression is $0$ so the formula fails. But if we take $\sqrt{\det(-I)}=\sqrt{1}=-1$ (instead of $\sqrt{1}=1$) then it all works out. – 2'5 9'2 Nov 28 '22 at 06:11
  • I agree with you. In my case, all entries are positive and real. Thus, I did not record any failures. – I. Mohamed Nov 28 '22 at 16:31

3 Answers3

1

Your link to the sqrtm method has a link to that function's source.

The algorithm does a Schuur decomposition of $A$, finding $Z$ and $T$ such that $A=ZTZ^*$ with $Z$ unitary and $T$ is upper triangular.

Then it tries to find $\sqrt{T}$ using _sqrtm_triu. This method is for finding the square root of an upper triangular. Once it find that (if it succeeds) it returns $\sqrt{A}$ as $Z\sqrt{T}Z^*$.

So now the question is about how _sqrtm_triu works. Consider that if you know $T$ is upper triangular, then (as long as you allow non-real entries) it is easy to find the diagonal entries of an upper triangular $\sqrt{T}$: they have to be square roots of the diagonal entries of $T$. After these are established, you can solve for the entries of the first off-diagonal. And repeat. I can't think of a nice way to write this in a succinct formula at present.

2'5 9'2
  • 56,991
  • Thanks, @2'5 9'2 for your help; it is really appreciated. I got your point, I have tried what you recommended, but I faced many issues with the implementation on the GPU. For instance, this code LINK gives the same results as sqrtm; however, after converting it to cublas to be compatible with GPU, it does not work. This is the reason behind looking for a formulation like the one I provided in the post, which will be easier to be implemented and will reduce the computational burden (which is a very good factor in our work). – I. Mohamed Nov 28 '22 at 16:44
  • 1
    Hi everyone, many thanks again for your help. I have implemented this formulation Link, and it works perfectly as the one in python. It requires only the calculation of the eigenvalues. – I. Mohamed Dec 12 '22 at 14:44
0

I'm not sure about a formulation for all $3\times 3$ matrices, but we can pretty easily find the square root of diagonalizable matrices. Suppose $$A = \begin{bmatrix} \\ v_1 & v_2 & v_3 \\ \\\end{bmatrix} \begin{bmatrix} \lambda_1 & 0 & 0\\ 0 & \lambda_2 & 0\\ 0 & 0 & \lambda_3 \end{bmatrix} \begin{bmatrix} \\ v_1 & v_2 & v_3 \\ \\\end{bmatrix}^{-1}$$ For eigenvalues $\lambda_i$ and eigenvectors $v_i$. Then $$\sqrt{A}= \begin{bmatrix} \\ v_1 & v_2 & v_3 \\ \\\end{bmatrix} \begin{bmatrix} \sqrt{\lambda_1} & 0 & 0\\ 0 & \sqrt{\lambda_2} & 0\\ 0 & 0 & \sqrt{\lambda_3} \end{bmatrix} \begin{bmatrix} \\ v_1 & v_2 & v_3 \\ \\\end{bmatrix}^{-1}$$ since $$\sqrt{A}^2 = \begin{bmatrix} \\ v_1 & v_2 & v_3 \\ \\\end{bmatrix} \begin{bmatrix} \sqrt{\lambda_1} & 0 & 0\\ 0 & \sqrt{\lambda_2} & 0\\ 0 & 0 & \sqrt{\lambda_3} \end{bmatrix} \begin{bmatrix} \sqrt{\lambda_1} & 0 & 0\\ 0 & \sqrt{\lambda_2} & 0\\ 0 & 0 & \sqrt{\lambda_3} \end{bmatrix} \begin{bmatrix} \\ v_1 & v_2 & v_3 \\ \\\end{bmatrix}^{-1} = A.$$

  • Thanks, @Pel34 for your help; it is really appreciated. In fact, I have seen the formulation you posted. Yours requires the calculation of the eigenvalues and eigenvectors. The issue for me is that I need a simple form to be implemented on the GPU. This is the reason behind looking for a formulation like the one I provided in the post. – I. Mohamed Nov 28 '22 at 02:58
0

I found myself in the same situation, had to find a real symmetric square root $B$ of a real 3x3 symmetric positive definite matrix $A$, and that, on a small board with basically no memory, javascript, and very few libraries.

Note that if the matrix has negative eigenvalues, then there is at least no symmetric and real square root.

First, find the eigen values, say $\lambda_1$, $\lambda_2$, $\lambda_3$. Since the characteristic polynomial is of degree $3$, we can find the roots numerically, using either roots or trigonometric functions, see the wikipedia page for the cubic equation. So, set $\mu_i = \sqrt{\lambda_i}$, and define an interpolating polynomial $$ q(t) = \frac{ (t-\lambda_2)(t-\lambda_3)} { (\lambda_1-\lambda_2)(\lambda_1-\lambda_3) } \mu_1 + \frac{ (t-\lambda_3)(t-\lambda_1)} { (\lambda_1-\lambda_2)(\lambda_1-\lambda_3) } \mu_2 + \frac{ (t-\lambda_1)(t-\lambda_2)} { (\lambda_1-\lambda_2)(\lambda_1-\lambda_3) } \mu_3 $$ and set $$ B = q(A). $$ We now note that if $v$ is an eigenvector for $A$ with eigenvalue $\lambda_i$, then $$ Bv = q(A)v = q(\lambda_i)v = \mu_iv $$ and so $v$ is an eigenvector for $B$ with eigenvalue $\mu_i$. It follows from this, since $B$ is diaonalizable, and so $\mathbb{R}^3$ is generated by eigenvectors, that $B^2 = A$.

Note 1:

In this proof, you can really replace $\mu_i$ with any number, and get a new matrix using the polynomial $q$. In particular, you can set $\mu_i = \lambda^{a/b}$ to get a rational power of the matrix $A$.

Note 2:

In the formula for $q(t)$, we divide by differences of eigenvalues. As a result, the $\lambda_i$ must be distict. This method must be modified (simplified really) if there are only two or one eigenvalues. However, if you are implementing this using floating numbers, and you end up with eigenvalues that are very close, then you end up dividing and multiplying with very small numbers, which can be a problem with floating point arithmetic, I am not an expert on this, but be careful :)

Note 3:

This method works for matrices of any size, even nonsymmetric ones, as long as they are diagonalizable. The only problem is finding the roots of the characteristic polynomial, but there should exist simple numerical methods for this (again, not an expert on that topic).

Note 4:

This result can be generalized to basically any matrix, nonsymmetric, nondiagonalizable, etc. This is described in one of the answers here:

Find the square root of a matrix