# Compute the square root of a complex matrix

Hello,

I can't find how to compute the square root of a complex matrix... I tried m.sqrt(), sqrt(m) (display only symbolic sqrt), sqrt(m).n()... None of them work. What is the regular way to compute the root of a complex matrix? (you can assume the matrix is self-adjoint if needed).

Thanks!

def test():
phi = matrix(CC, [[1/sqrt(2)],[i]])
m = phi * phi.C.T
print("Matrix: {}".format(m))
s = sqrt(m).n() # Does not work: AttributeError: 'ComplexField_class_with_category' object has no attribute 'complex_field'
#s = m^(1/2) # Does not work either, NotImplementedError: non-integral exponents not supported
# s = m.sqrt() # Does not work either, no attribute sqrt
print("Sqrt: {}".format(s))
print("s*s: {}".format(s*s))
if s*s == m:
print("Ok")
else:
print("Nope :(")


EDIT: For now I'm using the numpy backend with manual diagonalisation, but I'd like to know if there is a better way to proceed...

 phi = matrix(CDF, [[1/sqrt(2)],[i]])
D, P = m.eigenmatrix_right()
s = P*diagonal_matrix([sqrt(x) for x in D.diagonal()])*P^-1

edit retag close merge delete

Sort by ยป oldest newest most voted

One way would be to use the "symbolic n-th power of a matrix".

Define the matrix m with coefficients in QQbar, the field of algebraic numbers:

sage: phi = matrix(QQbar, [[1/sqrt(2)],[i]])
sage: m = phi * phi.C.T
sage: m
[                   1/2 -0.7071067811865475?*I]
[ 0.7071067811865475?*I                      1]


Define a symbolic variable in Sage's symbolic ring:

sage: n = SR.var('n')


Take the n-th power of m:

sage: mn = m^n
sage: mn
[           1/3*3^n/2^n -1/3*I*sqrt(2)*3^n/2^n]
[ 1/3*I*sqrt(2)*3^n/2^n            2/3*3^n/2^n]


Substitute 1/2 to the exponent n:

sage: msqrt = mn.subs({n: 1/2})
sage: msqrt
[1/6*sqrt(3)*sqrt(2)      -1/3*I*sqrt(3)]
[      1/3*I*sqrt(3) 1/3*sqrt(3)*sqrt(2)]


The result has coefficients in Sage's symbolic ring.

To convert it to another ring, use change_ring:

sage: msqrt.change_ring(QQbar)
[   0.4082482904638630? -0.5773502691896258?*I]
[ 0.5773502691896258?*I    0.8164965809277260?]


Check that squaring the square root gives back the original matrix:

sage: msqrt^2
[           1/2 -1/2*I*sqrt(2)]
[ 1/2*I*sqrt(2)              1]

more