Esempio n. 1
0
def order(f, a, b, n):
    x = sympy.symbols('x')

    f_ab = sympy.Subs(f, (x), (b)).n()-\
           sympy.Subs(f, (x), (a)).n()
    df = f.diff(x)
    f_1n = integrate(df, a, b, n)
    f_2n = integrate(df, a, b, 2 * n)

    return math.log((f_ab - f_1n) / (f_ab - f_2n), 2)
Esempio n. 2
0
def test_J():
    """This test verifies that dJ/dt = 2*H."""

    qp = phase_space_coordinates()
    qp_ = qp.reshape(-1).tolist()

    x, y, z = qp[0, :]
    p_x, p_y, p_z = qp[1, :]

    P_x_ = P_x__(*qp_)
    P_y_ = P_y__(*qp_)
    mu_ = mu__(x, y, z)
    r_squared_ = r_squared__(x, y)

    H_qp = H__(*qp_)
    X_H = vorpy.symplectic.symplectic_gradient_of(H_qp, qp)

    J_qp = J__(*qp_)
    # Because X_H gives the vector field defining the time derivative of a solution to the dynamics,
    # it follows that X_H applied to J is equal to dJ/dt (where J(t) is J(qp(t)), where qp(t) is a
    # solution to Hamilton's equations).
    X_H__J = vorpy.manifold.directional_derivative(X_H, J_qp, qp)
    #print(f'test_J; X_H__J = {X_H__J}')
    #print(f'test_J; 2*H = {sp.expand(2*H_qp)}')
    actual_value = X_H__J - sp.expand(2 * H_qp)
    #print(f'test_J; X_H__J - 2*H = {actual_value}')

    # Annoyingly, this doesn't simplify to 0 automatically, so some manual manipulation has to be done.

    # Manipulate the expression to ensure the P_x and P_y terms cancel
    actual_value = sp.collect(actual_value, [P_x_, P_y_])
    #print(f'test_J; after collect P_x, P_y: X_H__J - 2*H = {actual_value}')
    actual_value = sp.Subs(
        actual_value, [P_x_, P_y_],
        [P_x_._expanded(), P_y_._expanded()]).doit()
    #print(f'test_J; after subs P_x, P_y: X_H__J - 2*H = {actual_value}')

    # Manipulate the expression to ensure the mu terms cancel
    actual_value = sp.factor_terms(actual_value, clear=True, fraction=True)
    #print(f'test_J; after factor_terms: X_H__J - 2*H = {actual_value}')
    actual_value = sp.collect(actual_value, [r_squared_])
    #print(f'test_J; after collect r_squared_: X_H__J - 2*H = {actual_value}')
    actual_value = sp.Subs(actual_value, [r_squared_._expanded()],
                           [r_squared_]).doit()
    #print(f'test_J; after subs r_squared: X_H__J - 2*H = {actual_value}')
    actual_value = sp.Subs(actual_value, [mu_._expanded()], [mu_]).doit()
    #print(f'test_J; after subs mu: X_H__J - 2*H = {actual_value}')

    if actual_value != 0:
        raise ValueError(
            f'Expected X_H__J - 2*H == 0, but actual value was {actual_value}')

    print('test_J passed')
Esempio n. 3
0
def substitute_all(sp_object, pairs):
    """
    Performs multiple substitutions in an expression
    :param expr: a sympy matrix or expression
    :param pairs: a list of pairs (a,b) where each a_i is to be substituted with b_i
    :return: the substituted expression
    """
    if not isinstance(pairs, dict):
        dict_pairs = dict(pairs)
    else:
        dict_pairs = pairs

    # we recurse if the object was a matrix so we apply substitution to all elements
    if isinstance(sp_object, sympy.Matrix):
        return sp_object.applyfunc(lambda x: substitute_all(x, dict_pairs))

    try:
        expr = sp_object.xreplace(dict_pairs)

    # in sympy 0.7.2, this would not work, so we do it manually
    except Exception:
        expr = sp_object
        for (a, b) in dict_pairs.items():
            expr = sympy.Subs(expr, a, b)
        expr = expr.doit()
    return expr
Esempio n. 4
0
def test_J_restricted_conservation():
    """This test verifies that J is conserved along the flow of H if restricted to the H = 0 submanifold."""

    qp = phase_space_coordinates()
    H_qp = H(qp)
    X_H = vorpy.symplectic.symplectic_gradient_of(H_qp, qp)

    J_qp = J(qp)
    X_H__J = vorpy.manifold.directional_derivative(X_H, J_qp, qp)

    p_z = qp[1, 2]

    # Solve for p_z in H_qp == 0; there are two sheets to this solution.
    p_z_solution_v = sp.solve(H_qp, p_z)
    assert len(
        p_z_solution_v
    ) == 2, f'Expected 2 solutions for p_z in H == 0, but instead got {len(p_z_solution_v)}'
    #print('There are {0} solutions for the equation: {1} = 0'.format(len(p_z_solution_v), H_qp))
    #for i,p_z_solution in enumerate(p_z_solution_v):
    #print('    solution {0}: p_z = {1}'.format(i, p_z_solution))

    for solution_index, p_z_solution in enumerate(p_z_solution_v):
        # We have to copy X_H__J or it will only be a view into X_H__J and will modify the original.
        # The [tuple()] access is to obtain the scalar value out of the
        # sympy.tensor.array.dense_ndim_array.ImmutableDenseNDimArray object that doit() returns.
        X_H__J__restricted = sp.Subs(np.copy(X_H__J), p_z,
                                     p_z_solution).doit()[tuple()].simplify()
        #print(f'solution_index = {solution_index}, X_H__J__restricted = {X_H__J__restricted}')
        if X_H__J__restricted != 0:
            raise ValueError(
                f'Expected X_H__J__restricted == 0 for solution_index = {solution_index}, but actual value was {X_H__J__restricted}'
            )

    print('test_J_restricted_conservation passed')
Esempio n. 5
0
def sim_wp(x,t):
    x = np.zeros(4)
    f = np.array([1,2,3,4], dtype=object)
    for i in range(4):
        f[i] = np.sum((p_simple_ident_zen.T*better_basis)[i])
    f = sp.Subs(f, ["x1","x2","x3","x4"], [x[0], x[1], x[2], x[3]])
    f= f.doit()
    return f
Esempio n. 6
0
def test_conv11():
    x = sympy.Symbol("x")
    y = sympy.Symbol("y")
    x1 = Symbol("x")
    y1 = Symbol("y")
    e1 = sympy.Subs(sympy.Derivative(sympy.Function("f")(x, y), x), [x, y],
                    [y, y])

    e2 = Subs(Derivative(function_symbol("f", x1, y1), [x1]), [x1, y1],
              [y1, y1])
    e3 = Subs(Derivative(function_symbol("f", x1, y1), [x1]), [y1, x1],
              [x1, y1])

    assert sympify(e1) == e2
    assert sympify(e1) != e3

    assert e2._sympy_() == e1
    assert e3._sympy_() != e1
Esempio n. 7
0
def diff_scalar_using_chain_rule(f_expr, g_expr, h_expr, ord, dNf_dgN_subs,
                                 dNg_dhN_subs):

    dNf_dgN_subs = dNf_dgN_subs[0:ord + 1]
    dNg_dhN_subs = dNg_dhN_subs[0:ord + 1]

    dNf_dgN_expr = sympy.Matrix.zeros(ord + 1, 1)
    dNf_dgN_expr[0] = f_expr
    dNf_dgN_expr[1] = f_expr.diff(g_expr)
    for i in range(2, ord + 1):
        dNf_dgN_expr[i] = dNf_dgN_expr[i - 1].diff(g_expr) / 2

    dNg_dhN_expr = sympy.Matrix.zeros(ord + 1, 1)
    dNg_dhN_expr[0] = g_expr
    dNg_dhN_expr[1] = g_expr.diff(h_expr)
    for i in range(2, ord + 1):
        dNg_dhN_expr[i] = dNg_dhN_expr[i - 1].diff(h_expr)

    dNf_dhN_expr = sympy.Matrix.zeros(ord + 1, 1)
    dNf_dhN_expr[0] = f_expr
    dNf_dhN_expr[1] = f_expr.diff(h_expr)
    for i in range(2, ord + 1):
        dNf_dhN_expr[i] = dNf_dhN_expr[i - 1].diff(h_expr)

    f_wild_expr = sympy.Wild("p_wild", real=True)
    x_wild_expr = sympy.Wild("x_wild", real=True)
    dNf_dgN_not_yet_evaluated_expr = sympy.Matrix.zeros(ord + 1, 1)
    for i in range(1, ord + 1):
        derivative_args = [f_wild_expr] + [x_wild_expr] * i
        dNf_dgN_not_yet_evaluated_expr[i] = sympy.Subs(
            sympy.Derivative(*derivative_args), x_wild_expr, g_expr)

    dNf_dhN_in_terms_of_subs_expr = sympy.Matrix.zeros(ord + 1, 1)
    dNf_dhN_in_terms_of_subs_expr[0] = dNf_dgN_subs[0]
    for i in range(1, ord + 1):
        dNf_dhN_in_terms_of_subs_expr[i] = dNf_dhN_expr[i]
        dNf_dhN_in_terms_of_subs_expr[i] = dNf_dhN_in_terms_of_subs_expr[
            i].subs(zip(dNg_dhN_expr[::-1], dNg_dhN_subs[::-1])[:-1],
                    simultaneous=True)
        for j in range(1, ord + 1):
            dNf_dhN_in_terms_of_subs_expr[i] = dNf_dhN_in_terms_of_subs_expr[
                i].replace(dNf_dgN_not_yet_evaluated_expr[j], dNf_dgN_subs[j])

    return dNf_dhN_in_terms_of_subs_expr