function S = sign_schurparlett(M) % Computes the matrix sign function with the Schur-Parlett method n = size(M, 1); [Q, U] = schur(M, 'complex'); % overwrite Q, U with their reordered version [Q, U] = ordschur(Q, U, "lhp"); % count the number of eigenvalues in the LHP p = sum(real(diag(U)) < 0); A = U(1:p, 1:p); B = U(p+1:n, p+1:n); C = U(1:p, p+1:n); % Matlab function to solve a Sylvester equation % Note that Matlab's syntax has different signs Z = lyap(A, -B, 2*C); S = zeros(n, n); S(1:p, 1:p) = -eye(p); S(1:p, p+1:end) = Z; S(p+1:end, p+1:end) = eye(n-p); S = Q*S*Q'; % If M is real, S=sign(M) is supposed to be real, % but the computed one will have a tiny nonzero imaginary part, % due to complex arithmetic in the Schur form. % We remove it if that is the case. if isreal(M) S = real(S); end