Here is some code for calculating a Takagi factorization. It uses the eigenvalue decomposition of a Hermitian matrix. It is not intended to be efficient, fault tolerant, numerically stable, nor guaranteed correct for all possible matrices. An algorithm designed for this factorization is preferable, particularly if large matrices need to be factored. Even so, if you just need to factor some matrices and get on with your life, then using mathematical tricks such as this can be useful.
import numpy as np
import scipy.linalg as la
def takagi(A) :
"""Extremely simple and inefficient Takagi factorization of a
symmetric, complex matrix A. Here we take this to mean A = U D U^T
where D is a real, diagonal matrix and U is a unitary matrix. There
is no guarantee that it will always work. """
# Construct a Hermitian matrix.
H = np.dot(A.T.conj(),A)
# Calculate the eigenvalue decomposition of the Hermitian matrix.
# The diagonal matrix in the Takagi factorization is the square
# root of the eigenvalues of this matrix.
(lam, u) = la.eigh(H)
# The "almost" Takagi factorization. There is a conjugate here
# so the final form is as given in the doc string.
T = np.dot(u.T, np.dot(A,u)).conj()
# T is diagonal but not real. That is easy to fix by a
# simple transformation which removes the complex phases
# from the resulting diagonal matrix.
c = np.diag(np.exp(-1j*np.angle(np.diag(T))/2))
U = np.dot(u,c)
# Now A = np.dot(U, np.dot(np.diag(np.sqrt(lam)),U.T))
return (np.sqrt(lam), U)
To understand the algorithm it is convenient to write out each step and see how it leads to the desired factorization. The code can then be made more efficient if need be.