""" Matrix square root for general matrices and for upper triangular matrices.
This module exists to avoid cyclic imports.
"""
# Local imports
""" Matrix square root of an upper triangular matrix.
This is a helper function for `sqrtm` and `logm`.
Parameters ---------- T : (N, N) array_like upper triangular Matrix whose square root to evaluate blocksize : int, optional If the blocksize is not degenerate with respect to the size of the input array, then use a blocked algorithm. (Default: 64)
Returns ------- sqrtm : (N, N) ndarray Value of the sqrt function at `T`
References ---------- .. [1] Edvin Deadman, Nicholas J. Higham, Rui Ralha (2013) "Blocked Schur Algorithms for Computing the Matrix Square Root, Lecture Notes in Computer Science, 7782. pp. 171-182.
""" T_diag = T_diag.astype(complex)
# Compute the number of blocks to use; use at least one block.
# Compute the smaller of the two sizes of blocks that # we will actually use, and compute the number of large blocks. raise Exception('internal inconsistency')
# Define the index range covered by each block.
# Within-block interactions. elif denom == 0 and num == 0: R[i, j] = 0 else: raise SqrtmError('failed to find the matrix square root')
# Between-block interactions. jstart:jstop])
# Invoke LAPACK. # For more details, see the solve_sylvester implemention # and the fortran dtrsyl and ztrsyl docs. else: x, scale, info = ztrsyl(Rii, Rjj, S)
# Return the matrix square root.
""" Matrix square root.
Parameters ---------- A : (N, N) array_like Matrix whose square root to evaluate disp : bool, optional Print warning if error in the result is estimated large instead of returning estimated error. (Default: True) blocksize : integer, optional If the blocksize is not degenerate with respect to the size of the input array, then use a blocked algorithm. (Default: 64)
Returns ------- sqrtm : (N, N) ndarray Value of the sqrt function at `A`
errest : float (if disp == False)
Frobenius norm of the estimated error, ||err||_F / ||A||_F
References ---------- .. [1] Edvin Deadman, Nicholas J. Higham, Rui Ralha (2013) "Blocked Schur Algorithms for Computing the Matrix Square Root, Lecture Notes in Computer Science, 7782. pp. 171-182.
Examples -------- >>> from scipy.linalg import sqrtm >>> a = np.array([[1.0, 3.0], [1.0, 4.0]]) >>> r = sqrtm(a) >>> r array([[ 0.75592895, 1.13389342], [ 0.37796447, 1.88982237]]) >>> r.dot(r) array([[ 1., 3.], [ 1., 4.]])
""" raise ValueError("Non-matrix input to matrix function.") raise ValueError("The blocksize should be at least 1.") T, Z = rsf2csf(T, Z) else: T, Z = schur(A, output='complex') except SqrtmError: failflag = True X = np.empty_like(A) X.fill(np.nan)
print("Failed to find a square root.") else: try: arg2 = norm(X.dot(X) - A, 'fro')**2 / norm(A, 'fro') except ValueError: # NaNs in matrix arg2 = np.inf
return X, arg2 |