Esempio n. 1
0
def run_eigsy(A, verbose=False):
    if verbose:
        print("original matrix:\n", str(A))

    D, Q = mp.eigsy(A)
    B = Q * mp.diag(D) * Q.transpose()
    C = A - B
    E = Q * Q.transpose() - mp.eye(A.rows)

    if verbose:
        print("eigenvalues:\n", D)
        print("eigenvectors:\n", Q)

    NC = mp.mnorm(C)
    NE = mp.mnorm(E)

    if verbose:
        print("difference:", NC, "\n", C, "\n")
        print("difference:", NE, "\n", E, "\n")

    eps = mp.exp(0.8 * mp.log(mp.eps))

    assert NC < eps
    assert NE < eps

    return NC
Esempio n. 2
0
def run_eigsy(A, verbose = False):
    if verbose:
        print("original matrix:\n", str(A))

    D, Q = mp.eigsy(A)
    B = Q * mp.diag(D) * Q.transpose()
    C = A - B
    E = Q * Q.transpose() - mp.eye(A.rows)

    if verbose:
        print("eigenvalues:\n", D)
        print("eigenvectors:\n", Q)

    NC = mp.mnorm(C)
    NE = mp.mnorm(E)

    if verbose:
        print("difference:", NC, "\n", C, "\n")
        print("difference:", NE, "\n", E, "\n")

    eps = mp.exp( 0.8 * mp.log(mp.eps))

    assert NC < eps
    assert NE < eps

    return NC
Esempio n. 3
0
def _compute_roots(w, x, use_mp):
    # Cf.:
    # Knockaert, L. (2008). A simple and accurate algorithm for barycentric
    # rational interpolation. IEEE Signal processing letters, 15, 154-157.
    if use_mp:
        from mpmath import mp
        if use_mp is True:
            use_mp = 100
        mp.dps = use_mp

        ak = mp.matrix(w)
        ak /= sum(ak)
        bk = mp.matrix(x)

        M = mp.diag(bk)
        for i in range(M.rows):
            for j in range(M.cols):
                M[i, j] -= ak[i] * bk[j]
        lam = mp.eig(M, left=False, right=False)
        lam = np.array(lam, dtype=complex)
    else:
        # the same procedure in standard double precision
        ak = w / w.sum()
        M = np.diag(x) - np.outer(ak, x)
        lam = scipy.linalg.eigvals(M)

    # remove one simple root
    lam = np.delete(lam, np.argmin(abs(lam)))
    return np.real_if_close(lam)
Esempio n. 4
0
def _compute_roots(w, x, use_mp):
    # Cf.:
    # Knockaert, L. (2008). A simple and accurate algorithm for barycentric
    # rational interpolation. IEEE Signal processing letters, 15, 154-157.
    #
    # This version requires solving only a standard eigenvalue problem, but
    # has troubles when the polynomial has leading 0 coefficients.
    if _is_mp_array(w) or _is_mp_array(x):
        use_mp = True

    if use_mp:
        assert mpmath, 'mpmath package is not installed'
        ak = mp.matrix(w)
        ak /= sum(ak)
        bk = mp.matrix(x)

        M = mp.diag(bk)
        for i in range(M.rows):
            for j in range(M.cols):
                M[i, j] -= ak[i] * bk[j]
        lam = np.array(mp.eig(M, left=False, right=False))
        # remove one simple root
        lam = np.delete(lam, np.argmin(abs(lam)))
        return lam
    else:
        # the same procedure in standard double precision
        ak = w / w.sum()
        M = np.diag(x) - np.outer(ak, x)
        lam = scipy.linalg.eigvals(M)
        # remove one simple root
        lam = np.delete(lam, np.argmin(abs(lam)))
        return np.real_if_close(lam)
Esempio n. 5
0
def takagi_fact(a):
    A = mp.matrix(a)
    sing_vs, Q = symmetric_svd(A)
    phase_mat = mp.diag(
        [mp.exp(-1j * mp.arg(sing_v) / 2.0) for sing_v in sing_vs])

    vs = [mp.fabs(sing_v) for sing_v in sing_vs]
    Qp = Q * phase_mat

    return vs, Qp
Esempio n. 6
0
    def apply(self, m, evaluation):
        'SingularValueDecomposition[m_]'

        if not any(leaf.is_inexact() for row in m.leaves for leaf in row.leaves):
            # symbolic argument (not implemented)
            evaluation.message('SingularValueDecomposition', 'nosymb')

        matrix = to_mpmath_matrix(m)
        U, S, V = mp.svd(matrix)
        S = mp.diag(S)
        U_list = Expression('List', *U.tolist())
        S_list = Expression('List', *S.tolist())
        V_list = Expression('List', *V.tolist())
        return Expression('List', *[U_list, S_list, V_list])
Esempio n. 7
0
    def apply(self, m, evaluation):
        'SingularValueDecomposition[m_?MatrixQ]'

        if not any(leaf.is_inexact() for row in m.leaves for leaf in row.leaves):
            # symbolic argument (not implemented)
            evaluation.message('SingularValueDecomposition', 'nosymb')

        matrix = to_mpmath_matrix(m)
        U, S, V = mp.svd(matrix)
        S = mp.diag(S)
        U_list = Expression('List', *U.tolist())
        S_list = Expression('List', *S.tolist())
        V_list = Expression('List', *V.tolist())
        return Expression('List', *[U_list, S_list, V_list])
Esempio n. 8
0
    def apply(self, m, evaluation):
        "SingularValueDecomposition[m_]"

        matrix = to_mpmath_matrix(m)
        if matrix is None:
            return evaluation.message("SingularValueDecomposition", "matrix",
                                      m, 1)

        if not any(leaf.is_inexact() for row in m.leaves
                   for leaf in row.leaves):
            # symbolic argument (not implemented)
            evaluation.message("SingularValueDecomposition", "nosymb")

        U, S, V = mp.svd(matrix)
        S = mp.diag(S)
        U_list = Expression("List", *U.tolist())
        S_list = Expression("List", *S.tolist())
        V_list = Expression("List", *V.tolist())
        return Expression("List", *[U_list, S_list, V_list])
Esempio n. 9
0
def conjugator_into_SL2R(SL2C_matrices):
    """
    Returns a matrix C in SL(2, C) so that C^-1 * M * C is
    (essentially) in SL(2, R) for all the input matrices M.
    """
    ans, sig, form = preserves_hermitian_form(SL2C_matrices)
    if ans is None:
        raise ValueError('No invariant hermitian form found')
    if sig == 'definite':
        raise ValueError('Conjugate into SU(2), not SL(2, R)')
    if sig == 'both':
        raise ValueError('This degnerate case not implemented')
    assert sig == 'indefinite'
    J = sage_matrix_to_mpmath(form)
    eigs, U = mp.eighe(J)
    C = U * mp.diag([1 / mp.sqrt(abs(e)) for e in eigs])
    sq_two = mp.sqrt(2)
    sq_two_i = mp.mpc(imag=sq_two)
    S = mp.matrix([[1 / sq_two_i, 1 / sq_two_i], [-1 / sq_two, 1 / sq_two]])
    C = C * S.H
    C = (1 / mp.sqrt(mp.det(C))) * C
    return mpmath_matrix_to_sage(C)
Esempio n. 10
0
def nearly_diagonal(A):
    assert A.rows == A.cols == 2
    a = A[0, 0]
    return mp.norm(A - mp.diag([a, a])) < 1000 * mp.eps