Esempio n. 1
0
def mysqrtm(A):
    # Schur decomposition and cast to complex array
    T, Z = lm.schur(A)
    T, Z = lm.rsf2csf(T, Z)
    n, n = T.shape

    # Inner loop of sqrtm algorithm -> call C code
    R = np.zeros((n, n), dtype=T.dtype)
    stat = sqrtm_loop(R, T, n)

    R, Z = lm.all_mat(R, Z)
    X = (Z * R * Z.H)

    return X.A
Esempio n. 2
0
def mysqrtm(A):
    # Schur decomposition and cast to complex array
    T, Z = lm.schur(A)
    T, Z = lm.rsf2csf(T,Z)
    n,n = T.shape

    # Inner loop of sqrtm algorithm -> call C code
    R = np.zeros((n,n), dtype=T.dtype)
    stat = sqrtm_loop(R, T, n)

    R, Z = lm.all_mat(R,Z)
    X = (Z * R * Z.H)

    return X.A
def sqrtm(A, disp=True):
    """
    Symmetric Matrix square root.

    modified version of the scipy.linalg sqrtm function for performance:
    (i) introduced a dot product [based on https://groups.google.com/forum/#!topic/scipy-user/iNzZzkHjlgA]
    (ii) avoid rsf2csf as the input is expected to be symmetric

    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)

    Returns
    -------
    sgrtm : (N, N) ndarray
        Value of the sign function at `A`

    errest : float
        (if disp == False)

        Frobenius norm of the estimated error, ||err||_F / ||A||_F

    Notes
    -----
    Uses algorithm by Nicholas J. Higham

    """
    A = np.asarray(A)
    if len(A.shape)!=2:
        raise ValueError("Non-matrix input to matrix function.")
    T, Z = la.schur(A)
    # if the matrix is real and symmetric can skip the complex part
    if not (np.allclose(A,A.T,rtol=0,atol=1e-8) and np.all(A.imag==0)):
        T, Z = la.rsf2csf(T,Z)
    n,n = T.shape

    R = np.zeros((n,n),T.dtype.char)
    for j in xrange(n):
        R[j,j] = np.sqrt(T[j,j])
        for i in xrange(j-1,-1,-1):
            #s = 0
            #for k in range(i+1,j):
            #    s = s + R[i,k]*R[k,j]
            s = R[i,(i+1):j].dot(R[(i+1):j,j])
            R[i,j] = (T[i,j] - s)/(R[i,i] + R[j,j])

    R, Z = la.all_mat(R,Z)
    X = (Z * R * Z.H)

    if disp:
        nzeig = np.any(np.diag(T)==0)
        if nzeig:
            print("Matrix is singular and may not have a square root.")
        return X.A
    else:
        arg2 = la.norm(X*X - A,'fro')**2 / la.norm(A,'fro')
        return X.A, arg2
Esempio n. 4
0
def sqrtm(A, disp=True):
    """
    Symmetric Matrix square root.

    modified version of the scipy.linalg sqrtm function for performance:
    (i) introduced a dot product [based on https://groups.google.com/forum/#!topic/scipy-user/iNzZzkHjlgA]
    (ii) avoid rsf2csf as the input is expected to be symmetric

    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)

    Returns
    -------
    sgrtm : (N, N) ndarray
        Value of the sign function at `A`

    errest : float
        (if disp == False)

        Frobenius norm of the estimated error, ||err||_F / ||A||_F

    Notes
    -----
    Uses algorithm by Nicholas J. Higham

    """
    A = np.asarray(A)
    if len(A.shape) != 2:
        raise ValueError("Non-matrix input to matrix function.")
    T, Z = la.schur(A)
    # if the matrix is real and symmetric can skip the complex part
    if not (np.allclose(A, A.T, rtol=0, atol=1e-8) and np.all(A.imag == 0)):
        T, Z = la.rsf2csf(T, Z)
    n, n = T.shape

    R = np.zeros((n, n), T.dtype.char)
    for j in xrange(n):
        R[j, j] = np.sqrt(T[j, j])
        for i in xrange(j - 1, -1, -1):
            #s = 0
            #for k in range(i+1,j):
            #    s = s + R[i,k]*R[k,j]
            s = R[i, (i + 1):j].dot(R[(i + 1):j, j])
            R[i, j] = (T[i, j] - s) / (R[i, i] + R[j, j])

    R, Z = la.all_mat(R, Z)
    X = (Z * R * Z.H)

    if disp:
        nzeig = np.any(np.diag(T) == 0)
        if nzeig:
            print("Matrix is singular and may not have a square root.")
        return X.A
    else:
        arg2 = la.norm(X * X - A, 'fro')**2 / la.norm(A, 'fro')
        return X.A, arg2
from scipy import linalg
import sympy
from sympy import matrices
#p=sympy.symbols('p')
#g=sympy.symbols('g')
p=1
g=1
hh=linalg.all_mat([[2*p-g, -g, -g], [-g, 4*p-g, -g], [-g,-g,6*p-g]])[0]
eigval=linalg.eigh(hh)
#print(linalg.det(h))
#eigval=hh.eigenvals()
#eigvec=hh.eigenvects()
#print(linalg.eigvals(hh))
print(hh)
print(eigval)
#print(eigvec)