Exemplo n.º 1
0
def compute_quadratic_function_biggest_stable_learning_rate(
        f, threshold, *func_vars):
    """Computes the biggest stable (steepest descent) learning rate for a quadratic function.

    Args:
        f (function): quadratic function.
        threshold (float): maximum distance from the best learning rate.
        *func_vars (sympy.Symbol): a list of function variables. the derivative of the function
            f will be computed with respect to these parameters.

    Returns:
        alpha (float): the biggest stable learning rate.

    Examples:
        >>> def f1(x1, x2): return x1 ** 2 + 25 * x2 ** 2
        >>> x1, x2 = symbols('x1, x2')
        >>> compute_quadratic_function_biggest_stable_learning_rate(f1, 0.001, x1, x2)
        0.039
    """
    gradient = derive_by_array(f(*func_vars), func_vars)
    hessian = derive_by_array(gradient, func_vars).tomatrix()
    S = mp.svd(hessian, compute_uv=False)
    alpha = 2. / float(max(S)) - threshold

    return alpha
Exemplo n.º 2
0
def _mp_svd(A, full_matrices=True):
    """Convenience wrapper for mpmath high-precision SVD."""
    assert mpmath, 'mpmath package is not installed'
    AA = mp.matrix(A.tolist())
    U, Sigma, VT = mp.svd(AA, full_matrices=full_matrices)
    return np.array(U.tolist()), np.array(Sigma.tolist()).ravel(), np.array(
        VT.tolist())
Exemplo n.º 3
0
def preserves_hermitian_form(SL2C_matrices):
    """
    >>> CC = ComplexField(100)
    >>> A = matrix(CC, [[1, 1], [1, 2]]);
    >>> B = matrix(CC, [[0, 1], [-1, 0]])
    >>> C = matrix(CC, [[CC('I'),0], [0, -CC('I')]])
    >>> ans, sig, form = preserves_hermitian_form([A, B])
    >>> ans
    True
    >>> sig
    'indefinite'
    >>> form.change_ring(ComplexField(10))
    [  0.00 -1.0*I]
    [ 1.0*I   0.00]
    >>> preserves_hermitian_form([A, B, C])
    (False, None, None)
    >>> ans, sig, form = preserves_hermitian_form([B, C])
    >>> sig
    'definite'
    """
    M = block_matrix(len(SL2C_matrices), 1, [
        left_mult_by_adjoint(A) - right_mult_by_inverse(A)
        for A in SL2C_matrices
    ])

    CC = M.base_ring()
    mp.prec = CC.prec()
    RR = RealField(CC.prec())
    epsilon = RR(2)**(-int(0.8 * mp.prec))
    U, S, V = mp.svd(sage_matrix_to_mpmath(M))
    S = list(mp.chop(S, epsilon))
    if mp.zero not in S:
        return False, None, None
    elif S.count(mp.zero) > 1:
        for i, A in enumerate(SL2C_matrices):
            for B in SL2C_matrices[i + 1:]:
                assert (A * B - B * A).norm() < epsilon

        sig = 'indefinite' if any(abs(A.trace()) > 2
                                  for A in SL2C_matrices) else 'both'
        return True, sig, None
    else:
        in_kernel = list(mp.chop(V.H.column(S.index(mp.zero))))
        J = mp.matrix([in_kernel[:2], in_kernel[2:]])
        iJ = mp.mpc(imag=1) * J
        J1, J2 = J + J.H, iJ + iJ.H
        J = J1 if mp.norm(J1) >= mp.norm(J2) else J2
        J = (1 / mp.sqrt(abs(mp.det(J)))) * J
        J = mpmath_matrix_to_sage(J)
        assert all((A.conjugate_transpose() * J * A - J).norm() < epsilon
                   for A in SL2C_matrices)
        sig = 'definite' if J.det() > 0 else 'indefinite'
        return True, sig, J
Exemplo n.º 4
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])
Exemplo n.º 5
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])
Exemplo n.º 6
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])
def svd_func(_matrix):
    U, S, V = mp.svd(_matrix)
    smallest_eig_values = (S[len(S)-1], S[len(S)-2])
    ss_raw = V[len(V)-1, :]
    ss = ss_raw / fsum(ss_raw)
    return ss.T, smallest_eig_values