Пример #1
0
def test_issue_15889():
    eq = exp(f(x).diff(x)) - f(x)**2
    sol = Eq(NonElementaryIntegral(1 / log(y**2), (y, f(x))), C1 + x)
    assert sol.dummy_eq(dsolve(eq))
    assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x)**2 - f(x)**3
    sol = Eq(f(x), 4 / (C1**2 - 2 * C1 * x + x**2))
    assert sol == dsolve(eq)
    assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x)**2 - f(x)
    sol = Eq(f(x), C1**2 / 4 - C1 * x / 2 + x**2 / 4)
    assert sol == dsolve(eq)
    assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x)**2 - f(x)**2
    sol = [Eq(f(x), C1 * exp(x)), Eq(f(x), C1 * exp(-x))]
    assert sol == dsolve(eq)
    assert checkodesol(eq, sol) == 2 * [(True, 0)]

    eq = f(x).diff(x)**2 - f(x)**3
    sol = Eq(f(x), 4 / (C1**2 - 2 * C1 * x + x**2))
    assert sol == dsolve(eq)
    assert checkodesol(eq, sol) == (True, 0)
Пример #2
0
def test_2nd_power_series_regular():
    C1, C2 = symbols("C1 C2")
    eq = x**2 * (f(x).diff(x, 2)) - 3 * x * (f(x).diff(x)) + (4 * x + 4) * f(x)
    sol = Eq(f(x),
             C1 * x**2 * (-16 * x**3 / 9 + 4 * x**2 - 4 * x + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_regular') == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = 4 * x**2 * (f(x).diff(
        x, 2)) - 8 * x**2 * (f(x).diff(x)) + (4 * x**2 + 1) * f(x)
    sol = Eq(
        f(x),
        C1 * sqrt(x) * (x**4 / 24 + x**3 / 6 + x**2 / 2 + x + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_regular') == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = x**2 * (f(x).diff(x, 2)) - x**2 * (f(x).diff(x)) + (x**2 - 2) * f(x)
    sol = Eq(
        f(x),
        C1 *
        (-x**6 / 720 - 3 * x**5 / 80 - x**4 / 8 + x**2 / 2 + x / 2 + 1) / x +
        C2 * x**2 * (-x**3 / 60 + x**2 / 20 + x / 2 + 1) + O(x**6))
    assert dsolve(eq) == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = x**2 * (f(x).diff(
        x, 2)) + x * (f(x).diff(x)) + (x**2 - Rational(1, 4)) * f(x)
    sol = Eq(
        f(x),
        C1 * (x**4 / 24 - x**2 / 2 + 1) / sqrt(x) + C2 * sqrt(x) *
        (x**4 / 120 - x**2 / 6 + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_regular') == sol
    assert checkodesol(eq, sol) == (True, 0)
Пример #3
0
def test_issue_4825():
    raises(ValueError, lambda: dsolve(f(x, y).diff(x) - y * f(x, y), f(x)))
    assert classify_ode(f(x, y).diff(x) - y*f(x, y), f(x), dict=True) == \
        {'order': 0, 'default': None, 'ordered_hints': ()}
    # See also issue 3793, test Z13.
    raises(ValueError, lambda: dsolve(f(x).diff(x), f(y)))
    assert classify_ode(f(x).diff(x), f(y), dict=True) == \
        {'order': 0, 'default': None, 'ordered_hints': ()}
Пример #4
0
def test_dsolve_all_hint():
    eq = f(x).diff(x)
    output = dsolve(eq, hint='all')

    # Match the Dummy variables:
    sol1 = output['separable_Integral']
    _y = sol1.lhs.args[1][0]
    sol1 = output['1st_homogeneous_coeff_subs_dep_div_indep_Integral']
    _u1 = sol1.rhs.args[1].args[1][0]

    expected = {
        'Bernoulli_Integral':
        Eq(f(x), C1 + Integral(0, x)),
        '1st_homogeneous_coeff_best':
        Eq(f(x), C1),
        'Bernoulli':
        Eq(f(x), C1),
        'nth_algebraic':
        Eq(f(x), C1),
        'nth_linear_euler_eq_homogeneous':
        Eq(f(x), C1),
        'nth_linear_constant_coeff_homogeneous':
        Eq(f(x), C1),
        'separable':
        Eq(f(x), C1),
        '1st_homogeneous_coeff_subs_indep_div_dep':
        Eq(f(x), C1),
        'nth_algebraic_Integral':
        Eq(f(x), C1),
        '1st_linear':
        Eq(f(x), C1),
        '1st_linear_Integral':
        Eq(f(x), C1 + Integral(0, x)),
        'lie_group':
        Eq(f(x), C1),
        '1st_homogeneous_coeff_subs_dep_div_indep':
        Eq(f(x), C1),
        '1st_homogeneous_coeff_subs_dep_div_indep_Integral':
        Eq(log(x), C1 + Integral(-1 / _u1, (_u1, f(x) / x))),
        '1st_power_series':
        Eq(f(x), C1),
        'separable_Integral':
        Eq(Integral(1, (_y, f(x))), C1 + Integral(0, x)),
        '1st_homogeneous_coeff_subs_indep_div_dep_Integral':
        Eq(f(x), C1),
        'best':
        Eq(f(x), C1),
        'best_hint':
        'nth_algebraic',
        'default':
        'nth_algebraic',
        'order':
        1
    }
    assert output == expected

    assert dsolve(eq, hint='best') == Eq(f(x), C1)
Пример #5
0
def test_linear_3eq_order1_type4_slow():
    x, y, z = symbols('x, y, z', cls=Function)
    t = Symbol('t')

    f = t**3 + log(t)
    g = t**2 + sin(t)
    eq1 = (Eq(diff(x(t), t), (4 * f + g) * x(t) - f * y(t) - 2 * f * z(t)),
           Eq(diff(y(t), t), 2 * f * x(t) + (f + g) * y(t) - 2 * f * z(t)),
           Eq(diff(z(t), t), 5 * f * x(t) + f * y(t) + (-3 * f + g) * z(t)))
    dsolve(eq1)
Пример #6
0
def test_dsolve_remove_redundant_solutions():

    eq = (f(x) - 2) * f(x).diff(x)
    sol = Eq(f(x), C1)
    assert dsolve(eq) == sol

    eq = (f(x) - sin(x)) * (f(x).diff(x, 2))
    sol = {Eq(f(x), C1 + C2 * x), Eq(f(x), sin(x))}
    assert set(dsolve(eq)) == sol

    eq = (f(x)**2 - 2 * f(x) + 1) * f(x).diff(x, 3)
    sol = Eq(f(x), C1 + C2 * x + C3 * x**2)
    assert dsolve(eq) == sol
Пример #7
0
def test_Bernoulli():
    # Type: Bernoulli, f'(x) + p(x)*f(x) == q(x)*f(x)**n
    eq = Eq(x*f(x).diff(x) + f(x) - f(x)**2, 0)
    sol = dsolve(eq, f(x), hint='Bernoulli')
    assert sol == Eq(f(x), 1/(C1*x + 1))
    assert checkodesol(eq, sol, order=1, solve_for_func=False)[0]

    eq = f(x).diff(x) - y*f(x)
    sol = dsolve(eq, hint='Bernoulli')
    assert sol == Eq(f(x), C1*exp(x*y))
    assert checkodesol(eq, sol)[0]

    eq = f(x)*f(x).diff(x) - 1
    sol = dsolve(eq,hint='Bernoulli')
    assert sol == [Eq(f(x), -sqrt(C1 + 2*x)), Eq(f(x), sqrt(C1 + 2*x))]
    assert checkodesol(eq, sol) == [(True, 0), (True, 0)]
Пример #8
0
def test_user_infinitesimals():
    x = Symbol("x")  # assuming x is real generates an error
    eq = x * (f(x).diff(x)) + 1 - f(x)**2
    sol = Eq(f(x), (C1 + x**2) / (C1 - x**2))
    infinitesimals = {'xi': sqrt(f(x) - 1) / sqrt(f(x) + 1), 'eta': 0}
    assert dsolve(eq, hint='lie_group', **infinitesimals) == sol
    assert checkodesol(eq, sol) == (True, 0)
Пример #9
0
def test_series():
    C1 = Symbol("C1")
    eq = f(x).diff(x) - f(x)
    sol = Eq(f(x), C1 + C1*x + C1*x**2/2 + C1*x**3/6 + C1*x**4/24 +
            C1*x**5/120 + O(x**6))
    assert dsolve(eq, hint='1st_power_series') == sol
    assert checkodesol(eq, sol, order=1)[0]

    eq = f(x).diff(x) - x*f(x)
    sol = Eq(f(x), C1*x**4/8 + C1*x**2/2 + C1 + O(x**6))
    assert dsolve(eq, hint='1st_power_series') == sol
    assert checkodesol(eq, sol, order=1)[0]

    eq = f(x).diff(x) - sin(x*f(x))
    sol = Eq(f(x), (x - 2)**2*(1+ sin(4))*cos(4) + (x - 2)*sin(4) + 2 + O(x**3))
    assert dsolve(eq, hint='1st_power_series', ics={f(2): 2}, n=3) == sol
Пример #10
0
def test_linear_3eq_order1_type4_skip():
    if ON_TRAVIS:
        skip("Too slow for travis.")

    x, y, z = symbols('x, y, z', cls=Function)
    t = Symbol('t')

    f = t**3 + log(t)
    g = t**2 + sin(t)
    eq1 = (Eq(diff(x(t), t), (4 * f + g) * x(t) - f * y(t) - 2 * f * z(t)),
           Eq(diff(y(t), t), 2 * f * x(t) + (f + g) * y(t) - 2 * f * z(t)),
           Eq(diff(z(t), t), 5 * f * x(t) + f * y(t) + (-3 * f + g) * z(t)))

    # sol1 = [Eq(x(t), (C1*exp(-2*Integral(t**3 + log(t), t)) + C2*(sqrt(3)*sin(sqrt(3)*Integral(t**3 + log(t), t))/6 \
    #         + cos(sqrt(3)*Integral(t**3 + log(t), t))/2) + C3*(-sin(sqrt(3)*Integral(t**3 + log(t), t))/2 \
    #         + sqrt(3)*cos(sqrt(3)*Integral(t**3 + log(t), t))/6))*exp(Integral(-t**2 - sin(t), t))),
    #         Eq(y(t), (C2*(sqrt(3)*sin(sqrt(3)*Integral(t**3 + log(t), t))/6 + cos(sqrt(3)* \
    #         Integral(t**3 + log(t), t))/2) + C3*(-sin(sqrt(3)*Integral(t**3 + log(t), t))/2 \
    #         + sqrt(3)*cos(sqrt(3)*Integral(t**3 + log(t), t))/6))*exp(Integral(-t**2 - sin(t), t))),
    #         Eq(z(t), (C1*exp(-2*Integral(t**3 + log(t), t)) + C2*cos(sqrt(3)*Integral(t**3 + log(t), t)) - \
    #         C3*sin(sqrt(3)*Integral(t**3 + log(t), t)))*exp(Integral(-t**2 - sin(t), t)))]

    dsolve_sol = dsolve(eq1)
    # dsolve_sol = [eq.subs(C3, -C3) for eq in dsolve_sol]
    # assert all(simplify(s1.rhs - ds1.rhs) == 0 for s1, ds1 in zip(sol1, dsolve_sol))
    assert checksysodesol(eq1, dsolve_sol) == (True, [0, 0, 0])
Пример #11
0
def func(i):
    print("")
    print("kamke number ",i)
    # kamke ODEs start at 1 but a list start at 0 
    print("kamke ode = ",kamke1_1[i])
    kamkesol = dsolve(kamke1_1[i],y)
    print("kamkesol = ",kamkesol)
    return(kamkesol)
Пример #12
0
def test_eval(kamkenumber):
    print("kamkenumber = ",kamkenumber)
    odesol = dsolve(kamke1_1[kamkenumber],y)
    print("odesol = ",odesol)
    #expected = solution_kamke1[kamkenumber]
    #print("odeexpected = ",expected)
    
    #assert odesol == expected
    assert checkodesol(kamke1_1[kamkenumber],odesol)[0]
Пример #13
0
def test_issue_13060():
    A, B = symbols("A B", cls=Function)
    t = Symbol("t")
    eq = [
        Eq(Derivative(A(t), t),
           A(t) * B(t)),
        Eq(Derivative(B(t), t),
           A(t) * B(t))
    ]
    sol = dsolve(eq)
    assert checkodesol(eq, sol) == (True, [0, 0])
Пример #14
0
def test_2nd_power_series_ordinary():
    C1, C2 = symbols("C1 C2")

    eq = f(x).diff(x, 2) - x * f(x)
    assert classify_ode(eq) == ('2nd_linear_airy', '2nd_power_series_ordinary')
    sol = Eq(f(x), C2 * (x**3 / 6 + 1) + C1 * x * (x**3 / 12 + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_ordinary') == sol
    assert checkodesol(eq, sol) == (True, 0)

    sol = Eq(
        f(x),
        C2 * ((x + 2)**4 / 6 + (x + 2)**3 / 6 - (x + 2)**2 + 1) + C1 *
        (x + (x + 2)**4 / 12 - (x + 2)**3 / 3 + S(2)) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_ordinary', x0=-2) == sol
    # FIXME: Solution should be O((x+2)**6)
    # assert checkodesol(eq, sol) == (True, 0)

    sol = Eq(f(x), C2 * x + C1 + O(x**2))
    assert dsolve(eq, hint='2nd_power_series_ordinary', n=2) == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = (1 + x**2) * (f(x).diff(x, 2)) + 2 * x * (f(x).diff(x)) - 2 * f(x)
    assert classify_ode(eq) == ('2nd_hypergeometric',
                                '2nd_hypergeometric_Integral',
                                '2nd_power_series_ordinary')

    sol = Eq(f(x), C2 * (-x**4 / 3 + x**2 + 1) + C1 * x + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_ordinary') == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x, 2) + x * (f(x).diff(x)) + f(x)
    assert classify_ode(eq) == ('2nd_power_series_ordinary', )
    sol = Eq(
        f(x),
        C2 * (x**4 / 8 - x**2 / 2 + 1) + C1 * x * (-x**2 / 3 + 1) + O(x**6))
    assert dsolve(eq) == sol
    # FIXME: checkodesol fails for this solution...
    # assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x, 2) + f(x).diff(x) - x * f(x)
    assert classify_ode(eq) == ('2nd_power_series_ordinary', )
    sol = Eq(
        f(x),
        C2 * (-x**4 / 24 + x**3 / 6 + 1) + C1 * x *
        (x**3 / 24 + x**2 / 6 - x / 2 + 1) + O(x**6))
    assert dsolve(eq) == sol
    # FIXME: checkodesol fails for this solution...
    # assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x, 2) + x * f(x)
    assert classify_ode(eq) == ('2nd_linear_airy', '2nd_power_series_ordinary')
    sol = Eq(
        f(x),
        C2 * (x**6 / 180 - x**3 / 6 + 1) + C1 * x * (-x**3 / 12 + 1) + O(x**7))
    assert dsolve(eq, hint='2nd_power_series_ordinary', n=7) == sol
    assert checkodesol(eq, sol) == (True, 0)
Пример #15
0
def test_issue_22604():
    x1, x2 = symbols('x1, x2', cls = Function)
    t, k1, k2, m1, m2 = symbols('t k1 k2 m1 m2', real = True)
    k1, k2, m1, m2 = 1, 1, 1, 1
    eq1 = Eq(m1*diff(x1(t), t, 2) + k1*x1(t) - k2*(x2(t) - x1(t)), 0)
    eq2 = Eq(m2*diff(x2(t), t, 2) + k2*(x2(t) - x1(t)), 0)
    eqs = [eq1, eq2]
    [x1sol, x2sol] = dsolve(eqs, [x1(t), x2(t)], ics = {x1(0):0, x1(t).diff().subs(t,0):0, \
                                                        x2(0):1, x2(t).diff().subs(t,0):0})
    assert x1sol == Eq(x1(t), sqrt(3 - sqrt(5))*(sqrt(10) + 5*sqrt(2))*cos(sqrt(2)*t*sqrt(3 - sqrt(5))/2)/20 + \
                       (-5*sqrt(2) + sqrt(10))*sqrt(sqrt(5) + 3)*cos(sqrt(2)*t*sqrt(sqrt(5) + 3)/2)/20)
    assert x2sol == Eq(x2(t), (sqrt(5) + 5)*cos(sqrt(2)*t*sqrt(3 - sqrt(5))/2)/10 + (5 - sqrt(5))*cos(sqrt(2)*t*sqrt(sqrt(5) + 3)/2)/10)
Пример #16
0
def test_nth_algebraic_redundant_solutions():
    # This one has a redundant solution that should be removed
    eqn = f(x) * f(x).diff(x)
    soln = Eq(f(x), C1)
    assert checkodesol(eqn, soln, order=1, solve_for_func=False)[0]
    assert soln == dsolve(eqn, f(x), hint='nth_algebraic')
    assert soln == dsolve(eqn, f(x))

    # This has two integral solutions and no algebraic solutions
    eqn = (diff(f(x)) - x) * (diff(f(x)) + x)
    sol = [Eq(f(x), C1 - x**2 / 2), Eq(f(x), C1 + x**2 / 2)]
    assert all(c[0]
               for c in checkodesol(eqn, sol, order=1, solve_for_func=False))
    assert set(sol) == set(dsolve(eqn, f(x), hint='nth_algebraic'))
    assert set(sol) == set(dsolve(eqn, f(x)))

    eqn = f(x) + f(x) * f(x).diff(x)
    solns = [Eq(f(x), 0), Eq(f(x), C1 - x)]
    assert all(c[0]
               for c in checkodesol(eqn, solns, order=1, solve_for_func=False))
    assert set(solns) == set(dsolve(eqn, f(x)))

    solns = [Eq(f(x), exp(x)), Eq(f(x), C1 * exp(C2 * x))]
    solns_final = _remove_redundant_solutions(eqn, solns, 2, x)
    assert solns_final == [Eq(f(x), C1 * exp(C2 * x))]

    # This one needs a substitution f' = g.
    eqn = -exp(x) + (x * Derivative(f(x), (x, 2)) + Derivative(f(x), x)) / x
    sol = Eq(f(x), C1 + C2 * log(x) + exp(x) - Ei(x))
    assert checkodesol(eqn, sol, order=2, solve_for_func=False)[0]
    assert sol == dsolve(eqn, f(x))
Пример #17
0
def test_issue_5770():
    k = Symbol("k", real=True)
    t = Symbol('t')
    w = Function('w')
    sol = dsolve(w(t).diff(t, 6) - k**6 * w(t), w(t))
    assert len([s for s in sol.free_symbols if s.name.startswith('C')]) == 6
    assert constantsimp((C1*cos(x) + C2*cos(x))*exp(x), {C1, C2}) == \
        C1*cos(x)*exp(x)
    assert constantsimp(C1*cos(x) + C2*cos(x) + C3*sin(x), {C1, C2, C3}) == \
        C1*cos(x) + C3*sin(x)
    assert constantsimp(exp(C1 + x), {C1}) == C1 * exp(x)
    assert constantsimp(x + C1 + y, {C1, y}) == C1 + x
    assert constantsimp(x + C1 + Integral(x, (x, 1, 2)), {C1}) == C1 + x
Пример #18
0
    def calc(self):
        f = Function('f')

        de = diff(self.L, Derivative(f(t), t))
        de = diff(de, t)
        dw = diff(self.L, f(t))
        eq = de - dw
        eq = eq.subs({cos(f(t)): 1 - (((f(t))**2) / 2), sin(f(t)): f(t)})
        print(eq)
        eq = simplify(eq)
        print(eq)
        solution = dsolve(eq, f(t))
        print(solution)
Пример #19
0
    def calc(self):  # The function that calculates the expression of f(t)

        f = Function('f')
        t = symbols('t')

        #The lagrangian
        self.L = ode.kinetic(self) - ode.potential(self)
        print('the lagrangian is : ')
        display(self.L)

        ##The different placebos for the Lagrange equation##
        de = diff(self.L, Derivative(f(t), t))
        de = diff(de, t)
        dw = diff(self.L, f(t))

        da = 0.5 * self.alpha * (self.v**2)  #Dissipation function
        da = da.subs({sin(f(t)): f(t)})

        da = da.subs({cos(f(t)): 1 - (f(t))**2 / 2})

        da = diff(da, Derivative(f(t), t))
        eq = de - dw + da  #LAgrange Equation
        ##############----------##############

        ##Solving the equation##
        print('The equation is : ')
        eq = simplify(eq)
        display(eq)
        solution = dsolve(eq, f(t))
        print('The solution is : ')
        display(solution)
        #######----------#######

        #Initial conditions:
        cnd0 = Eq(solution.subs({t: 0}), self.x0)
        cnd1 = Eq(solution.diff(t).subs({t: 0}), self.xd0)
        display(cnd0)

        #Solve for C1 and C2:
        C1, C2 = symbols('C1 C2')  #Temporary symbols
        C1C2_sl = linsolve([cnd0, cnd1], (C1, C2))

        #Substitute into the Solution of the equation
        solution2 = simplify(solution.subs(C1C2_sl))
        if 'C1' in str(solution2):
            solution3 = solution2.subs({C1: 1})
        if 'C2' in str(solution2):
            solution3 = solution2.subs({C2: 2})
        print('The solution is :')
        display(solution3)  #The Solution
        return solution3
Пример #20
0
def _test_all_examples_for_one_hint(our_hint, all_examples=[], runxfail=None):
    if all_examples == []:
        all_examples = _get_all_examples()
    match_list, unsolve_list, exception_list = [], [], []
    for ode_example in all_examples:
        eq = ode_example['eq']
        expected_sol = ode_example['sol']
        example = ode_example['example_name']
        xfail = our_hint in ode_example['XFAIL']
        xpass = True
        if runxfail and not xfail:
            continue
        if our_hint in classify_ode(eq):
            match_list.append(example)
            try:
                dsolve_sol = dsolve(eq, hint=our_hint)
                expected_checkodesol = [(True, 0)
                                        for i in range(len(expected_sol))]
                if len(expected_sol) == 1:
                    expected_checkodesol = (True, 0)

                if checkodesol(eq, dsolve_sol) != expected_checkodesol:
                    unsolve_list.append(example)
                    message = dsol_incorrect_msg.format(hint=our_hint,
                                                        eq=eq,
                                                        sol=expected_sol,
                                                        dsolve_sol=dsolve_sol)
                    if runxfail is not None:
                        raise AssertionError(message)
            except Exception as e:
                exception_list.append(example)
                if runxfail is not None:
                    print(
                        exception_msg.format(e=str(e),
                                             hint=our_hint,
                                             example=example,
                                             eq=eq))
                    traceback.print_exc()
                xpass = False
        if xpass and xfail:
            print(example, "is now passing for the hint", our_hint)
    if runxfail is None:
        match_count = len(match_list)
        solved = len(match_list) - len(unsolve_list) - len(exception_list)
        msg = check_hint_msg.format(hint=our_hint,
                                    matched=match_count,
                                    solve=solved,
                                    unsolve=unsolve_list,
                                    exceptions=exception_list)
        print(msg)
Пример #21
0
def reduction_of_order(eq):

    try:

        return dsolve(eq)

    except NotImplementedError:

        atom_x = list(eq.atoms(Symbol))[0]
        atom_f = list(eq.atoms(Function))[0]

        if ode_order(eq, atom_f) > 1:
            g = Function('g')(atom_x)
            eq = eq.subs(atom_f.diff(atom_x), g)
            if not (eq.has(atom_f)):
                return dsolve(
                    reduction_of_order(eq).subs(g, atom_f.diff(atom_x)))
            else:
                raise NotImplementedError
        else:
            raise NotImplementedError

    else:
        raise NotImplementedError
Пример #22
0
def test_issue_22523():
    N, s = symbols('N s')
    rho = Function('rho')
    # intentionally use 4.0 to confirm issue with nfloat
    # works here
    eqn = 4.0*N*sqrt(N - 1)*rho(s) + (4*s**2*(N - 1) + (N - 2*s*(N - 1))**2
        )*Derivative(rho(s), (s, 2))
    match = classify_ode(eqn, dict=True, hint='all')
    assert match['2nd_power_series_ordinary']['terms'] == 5
    C1, C2 = symbols('C1,C2')
    sol = dsolve(eqn, hint='2nd_power_series_ordinary')
    # there is no r(2.0) in this result
    assert filldedent(sol) == filldedent(str('''
        Eq(rho(s), C2*(1 - 4.0*s**4*sqrt(N - 1.0)/N + 0.666666666666667*s**4/N
        - 2.66666666666667*s**3*sqrt(N - 1.0)/N - 2.0*s**2*sqrt(N - 1.0)/N +
        9.33333333333333*s**4*sqrt(N - 1.0)/N**2 - 0.666666666666667*s**4/N**2
        + 2.66666666666667*s**3*sqrt(N - 1.0)/N**2 -
        5.33333333333333*s**4*sqrt(N - 1.0)/N**3) + C1*s*(1.0 -
        1.33333333333333*s**3*sqrt(N - 1.0)/N - 0.666666666666667*s**2*sqrt(N
        - 1.0)/N + 1.33333333333333*s**3*sqrt(N - 1.0)/N**2) + O(s**6))'''))
Пример #23
0
def test_2nd_power_series_regular():
    C1, C2, a = symbols("C1 C2 a")
    eq = x**2 * (f(x).diff(x, 2)) - 3 * x * (f(x).diff(x)) + (4 * x + 4) * f(x)
    sol = Eq(f(x),
             C1 * x**2 * (-16 * x**3 / 9 + 4 * x**2 - 4 * x + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_regular') == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = 4 * x**2 * (f(x).diff(
        x, 2)) - 8 * x**2 * (f(x).diff(x)) + (4 * x**2 + 1) * f(x)
    sol = Eq(
        f(x),
        C1 * sqrt(x) * (x**4 / 24 + x**3 / 6 + x**2 / 2 + x + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_regular') == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = x**2 * (f(x).diff(x, 2)) - x**2 * (f(x).diff(x)) + (x**2 - 2) * f(x)
    sol = Eq(
        f(x),
        C1 *
        (-x**6 / 720 - 3 * x**5 / 80 - x**4 / 8 + x**2 / 2 + x / 2 + 1) / x +
        C2 * x**2 * (-x**3 / 60 + x**2 / 20 + x / 2 + 1) + O(x**6))
    assert dsolve(eq) == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = x**2 * (f(x).diff(
        x, 2)) + x * (f(x).diff(x)) + (x**2 - Rational(1, 4)) * f(x)
    sol = Eq(
        f(x),
        C1 * (x**4 / 24 - x**2 / 2 + 1) / sqrt(x) + C2 * sqrt(x) *
        (x**4 / 120 - x**2 / 6 + 1) + O(x**6))
    assert dsolve(eq, hint='2nd_power_series_regular') == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = x * f(x).diff(x, 2) + f(x).diff(x) - a * x * f(x)
    sol = Eq(f(x), C1 * (a**2 * x**4 / 64 + a * x**2 / 4 + 1) + O(x**6))
    assert dsolve(eq, f(x), hint="2nd_power_series_regular") == sol
    assert checkodesol(eq, sol) == (True, 0)

    eq = f(x).diff(x, 2) + ((1 - x) / x) * f(x).diff(x) + (a / x) * f(x)
    sol = Eq(f(x), C1*(-a*x**5*(a - 4)*(a - 3)*(a - 2)*(a - 1)/14400 + \
        a*x**4*(a - 3)*(a - 2)*(a - 1)/576 - a*x**3*(a - 2)*(a - 1)/36 + \
        a*x**2*(a - 1)/4 - a*x + 1) + O(x**6))
    assert dsolve(eq, f(x), hint="2nd_power_series_regular") == sol
    assert checkodesol(eq, sol) == (True, 0)
Пример #24
0
def _ode_solver_test(ode_examples):
    our_hint = ode_examples['hint']
    for example in ode_examples['examples']:
        eq = ode_examples['examples'][example]['eq']
        sol = ode_examples['examples'][example]['sol']
        if our_hint not in classify_ode(eq):
            message = hint_message.format(example=example,
                                          eq=eq,
                                          our_hint=our_hint)
            raise AssertionError(message)

        dsolve_sol = dsolve(eq, hint=our_hint)
        if dsolve_sol not in sol:
            message = expected_sol_message.format(example=example,
                                                  eq=eq,
                                                  sol=sol,
                                                  dsolve_sol=dsolve_sol)
            raise AssertionError(message)

        expected_checkodesol = [(True, 0) for i in range(len(sol))]
        if checkodesol(eq, sol) != expected_checkodesol:
            message = checkodesol.format(example=example, eq=eq)
            raise AssertionError(message)
Пример #25
0
def test_dsolve_ics():
    # Maybe this should just use one of the solutions instead of raising...
    with raises(NotImplementedError):
        dsolve(f(x).diff(x) - sqrt(f(x)), ics={f(1): 1})
Пример #26
0
def pde_1st_linear_variable_coeff(eq, func, order, match, solvefun):
    r"""
    Solves a first order linear partial differential equation
    with variable coefficients. The general form of this partial differential equation is

    .. math:: a(x, y) \frac{df(x, y)}{dx} + a(x, y) \frac{df(x, y)}{dy}
                + c(x, y) f(x, y) - G(x, y)

    where `a(x, y)`, `b(x, y)`, `c(x, y)` and `G(x, y)` are arbitrary functions
    in `x` and `y`. This PDE is converted into an ODE by making the following transformation.

    1] `\xi` as `x`

    2] `\eta` as the constant in the solution to the differential equation
    `\frac{dy}{dx} = -\frac{b}{a}`

    Making the following substitutions reduces it to the linear ODE

    .. math:: a(\xi, \eta)\frac{du}{d\xi} + c(\xi, \eta)u - d(\xi, \eta) = 0

    which can be solved using dsolve.

    The general form of this PDE is::

        >>> from sympy.solvers.pde import pdsolve
        >>> from sympy.abc import x, y
        >>> from sympy import Function, pprint
        >>> a, b, c, G, f= [Function(i) for i in ['a', 'b', 'c', 'G', 'f']]
        >>> u = f(x,y)
        >>> ux = u.diff(x)
        >>> uy = u.diff(y)
        >>> genform = a(x, y)*u + b(x, y)*ux + c(x, y)*uy - G(x,y)
        >>> pprint(genform)
                                             d                     d
        -G(x, y) + a(x, y)*f(x, y) + b(x, y)*--(f(x, y)) + c(x, y)*--(f(x, y))
                                             dx                    dy

    Examples
    ========

    >>> from sympy.solvers.pde import pdsolve
    >>> from sympy import Function, diff, pprint, exp
    >>> from sympy.abc import x,y
    >>> f = Function('f')
    >>> eq =  x*(u.diff(x)) - y*(u.diff(y)) + y**2*u - y**2
    >>> pdsolve(eq)
    f(x, y) == F(x*y)*exp(y**2/2) + 1

    References
    ==========

    - Viktor Grigoryan, "Partial Differential Equations"
      Math 124A - Fall 2010, pp.7

    """
    from sympy.integrals.integrals import integrate
    from sympy.solvers.ode import dsolve

    xi, eta = symbols("xi eta")
    f = func.func
    x = func.args[0]
    y = func.args[1]
    b = match[match['b']]
    c = match[match['c']]
    d = match[match['d']]
    e = -match[match['e']]


    if not d:
         # To deal with cases like b*ux = e or c*uy = e
         if not (b and c):
            if c:
                try:
                    tsol = integrate(e/c, y)
                except NotImplementedError:
                    raise NotImplementedError("Unable to find a solution"
                        " due to inability of integrate")
                else:
                    return Eq(f(x,y), solvefun(x) + tsol)
            if b:
                try:
                    tsol = integrate(e/b, x)
                except NotImplementedError:
                    raise NotImplementedError("Unable to find a solution"
                        " due to inability of integrate")
                else:
                    return Eq(f(x,y), solvefun(y) + tsol)

    if not c:
        # To deal with cases when c is 0, a simpler method is used.
        # The PDE reduces to b*(u.diff(x)) + d*u = e, which is a linear ODE in x
        plode = f(x).diff(x)*b + d*f(x) - e
        sol = dsolve(plode, f(x))
        syms = sol.free_symbols - plode.free_symbols - set([x, y])
        rhs = _simplify_variable_coeff(sol.rhs, syms, solvefun, y)
        return Eq(f(x, y), rhs)

    if not b:
        # To deal with cases when b is 0, a simpler method is used.
        # The PDE reduces to c*(u.diff(y)) + d*u = e, which is a linear ODE in y
        plode = f(y).diff(y)*c + d*f(y) - e
        sol = dsolve(plode, f(y))
        syms = sol.free_symbols - plode.free_symbols - set([x, y])
        rhs = _simplify_variable_coeff(sol.rhs, syms, solvefun, x)
        return Eq(f(x, y), rhs)

    dummy = Function('d')
    h = (c/b).subs(y, dummy(x))
    sol = dsolve(dummy(x).diff(x) - h, dummy(x))
    if isinstance(sol, list):
        sol = sol[0]
    solsym = sol.free_symbols - h.free_symbols - set([x, y])
    if len(solsym) == 1:
        solsym = solsym.pop()
        etat = (solve(sol, solsym)[0]).subs(dummy(x), y)
        ysub = solve(eta - etat, y)[0]
        deq = (b*(f(x).diff(x)) + d*f(x) - e).subs(y, ysub)
        final = (dsolve(deq, f(x), hint='1st_linear')).rhs
        if isinstance(final, list):
            final = final[0]
        finsyms = final.free_symbols - deq.free_symbols - set([x, y])
        rhs = _simplify_variable_coeff(final, finsyms, solvefun, etat)
        return Eq(f(x, y), rhs)

    else:
        raise NotImplementedError("Cannot solve the partial differential equation due"
            " to inability of constantsimp")
Пример #27
0
def test_issue_5095():
    f = Function('f')
    raises(ValueError, lambda: dsolve(f(x).diff(x)**2, f(x), 'fdsjf'))
Пример #28
0
def test_dsolve_options():
    eq = x * f(x).diff(x) + f(x)
    a = dsolve(eq, hint='all')
    b = dsolve(eq, hint='all', simplify=False)
    c = dsolve(eq, hint='all_Integral')
    keys = [
        '1st_exact', '1st_exact_Integral', '1st_homogeneous_coeff_best',
        '1st_homogeneous_coeff_subs_dep_div_indep',
        '1st_homogeneous_coeff_subs_dep_div_indep_Integral',
        '1st_homogeneous_coeff_subs_indep_div_dep',
        '1st_homogeneous_coeff_subs_indep_div_dep_Integral', '1st_linear',
        '1st_linear_Integral', 'Bernoulli', 'Bernoulli_Integral',
        'almost_linear', 'almost_linear_Integral', 'best', 'best_hint',
        'default', 'lie_group', 'nth_linear_euler_eq_homogeneous', 'order',
        'separable', 'separable_Integral'
    ]
    Integral_keys = [
        '1st_exact_Integral',
        '1st_homogeneous_coeff_subs_dep_div_indep_Integral',
        '1st_homogeneous_coeff_subs_indep_div_dep_Integral',
        '1st_linear_Integral', 'Bernoulli_Integral', 'almost_linear_Integral',
        'best', 'best_hint', 'default', 'nth_linear_euler_eq_homogeneous',
        'order', 'separable_Integral'
    ]
    assert sorted(a.keys()) == keys
    assert a['order'] == ode_order(eq, f(x))
    assert a['best'] == Eq(f(x), C1 / x)
    assert dsolve(eq, hint='best') == Eq(f(x), C1 / x)
    assert a['default'] == 'separable'
    assert a['best_hint'] == 'separable'
    assert not a['1st_exact'].has(Integral)
    assert not a['separable'].has(Integral)
    assert not a['1st_homogeneous_coeff_best'].has(Integral)
    assert not a['1st_homogeneous_coeff_subs_dep_div_indep'].has(Integral)
    assert not a['1st_homogeneous_coeff_subs_indep_div_dep'].has(Integral)
    assert not a['1st_linear'].has(Integral)
    assert a['1st_linear_Integral'].has(Integral)
    assert a['1st_exact_Integral'].has(Integral)
    assert a['1st_homogeneous_coeff_subs_dep_div_indep_Integral'].has(Integral)
    assert a['1st_homogeneous_coeff_subs_indep_div_dep_Integral'].has(Integral)
    assert a['separable_Integral'].has(Integral)
    assert sorted(b.keys()) == keys
    assert b['order'] == ode_order(eq, f(x))
    assert b['best'] == Eq(f(x), C1 / x)
    assert dsolve(eq, hint='best', simplify=False) == Eq(f(x), C1 / x)
    assert b['default'] == 'separable'
    assert b['best_hint'] == '1st_linear'
    assert a['separable'] != b['separable']
    assert a['1st_homogeneous_coeff_subs_dep_div_indep'] != \
        b['1st_homogeneous_coeff_subs_dep_div_indep']
    assert a['1st_homogeneous_coeff_subs_indep_div_dep'] != \
        b['1st_homogeneous_coeff_subs_indep_div_dep']
    assert not b['1st_exact'].has(Integral)
    assert not b['separable'].has(Integral)
    assert not b['1st_homogeneous_coeff_best'].has(Integral)
    assert not b['1st_homogeneous_coeff_subs_dep_div_indep'].has(Integral)
    assert not b['1st_homogeneous_coeff_subs_indep_div_dep'].has(Integral)
    assert not b['1st_linear'].has(Integral)
    assert b['1st_linear_Integral'].has(Integral)
    assert b['1st_exact_Integral'].has(Integral)
    assert b['1st_homogeneous_coeff_subs_dep_div_indep_Integral'].has(Integral)
    assert b['1st_homogeneous_coeff_subs_indep_div_dep_Integral'].has(Integral)
    assert b['separable_Integral'].has(Integral)
    assert sorted(c.keys()) == Integral_keys
    raises(ValueError, lambda: dsolve(eq, hint='notarealhint'))
    raises(ValueError, lambda: dsolve(eq, hint='Liouville'))
    assert dsolve(f(x).diff(x) - 1/f(x)**2, hint='all')['best'] == \
        dsolve(f(x).diff(x) - 1/f(x)**2, hint='best')
    assert dsolve(f(x) + f(x).diff(x) + sin(x).diff(x) + 1, f(x),
                  hint="1st_linear_Integral") == \
        Eq(f(x), (C1 + Integral((-sin(x).diff(x) - 1)*
                exp(Integral(1, x)), x))*exp(-Integral(1, x)))
Пример #29
0
def test_solve_ics():
    # Basic tests that things work from dsolve.
    assert dsolve(f(x).diff(x) - 1/f(x), f(x), ics={f(1): 2}) == \
        Eq(f(x), sqrt(2 * x + 2))
    assert dsolve(f(x).diff(x) - f(x), f(x), ics={f(0): 1}) == Eq(f(x), exp(x))
    assert dsolve(f(x).diff(x) - f(x), f(x), ics={f(x).diff(x).subs(x, 0):
                                                  1}) == Eq(f(x), exp(x))
    assert dsolve(f(x).diff(x, x) + f(x),
                  f(x),
                  ics={
                      f(0): 1,
                      f(x).diff(x).subs(x, 0): 1
                  }) == Eq(f(x),
                           sin(x) + cos(x))
    assert dsolve([f(x).diff(x) - f(x) + g(x),
                   g(x).diff(x) - g(x) - f(x)], [f(x), g(x)],
                  ics={
                      f(0): 1,
                      g(0): 0
                  }) == [Eq(f(x),
                            exp(x) * cos(x)),
                         Eq(g(x),
                            exp(x) * sin(x))]

    # Test cases where dsolve returns two solutions.
    eq = (x**2 * f(x)**2 - x).diff(x)
    assert dsolve(eq, f(x), ics={f(1): 0}) == [
                                     Eq(f(x), -sqrt(x - 1) / x),
                                     Eq(f(x),
                                        sqrt(x - 1) / x)
                                 ]
    assert dsolve(eq, f(x), ics={f(x).diff(x).subs(x, 1): 0}) == [
                                     Eq(f(x), -sqrt(x - S.Half) / x),
                                     Eq(f(x),
                                        sqrt(x - S.Half) / x)
                                 ]

    eq = cos(f(x)) - (x * sin(f(x)) - f(x)**2) * f(x).diff(x)
    assert dsolve(eq, f(x), ics={f(0): 1}, hint='1st_exact',
                  simplify=False) == Eq(x * cos(f(x)) + f(x)**3 / 3,
                                        Rational(1, 3))
    assert dsolve(eq, f(x), ics={f(0): 1}, hint='1st_exact',
                  simplify=True) == Eq(x * cos(f(x)) + f(x)**3 / 3,
                                       Rational(1, 3))

    assert solve_ics([Eq(f(x), C1 * exp(x))], [f(x)], [C1], {f(0): 1}) == {
                                                                 C1: 1
                                                             }
    assert solve_ics([Eq(f(x),
                         C1 * sin(x) + C2 * cos(x))], [f(x)], [C1, C2], {
                             f(0): 1,
                             f(pi / 2): 1
                         }) == {
                             C1: 1,
                             C2: 1
                         }

    assert solve_ics([Eq(f(x),
                         C1 * sin(x) + C2 * cos(x))], [f(x)], [C1, C2], {
                             f(0): 1,
                             f(x).diff(x).subs(x, 0): 1
                         }) == {
                             C1: 1,
                             C2: 1
                         }

    assert solve_ics([Eq(f(x), C1*sin(x) + C2*cos(x))], [f(x)], [C1, C2], {f(0): 1}) == \
        {C2: 1}

    # Some more complicated tests Refer to PR #16098

    assert set(dsolve(f(x).diff(x)*(f(x).diff(x, 2)-x), ics={f(0):0, f(x).diff(x).subs(x, 1):0})) == \
        {Eq(f(x), 0), Eq(f(x), x ** 3 / 6 - x / 2)}
    assert set(dsolve(f(x).diff(x)*(f(x).diff(x, 2)-x), ics={f(0):0})) == \
        {Eq(f(x), 0), Eq(f(x), C2*x + x**3/6)}

    K, r, f0 = symbols('K r f0')
    sol = Eq(
        f(x),
        K * f0 * exp(r * x) / ((-K + f0) * (f0 * exp(r * x) / (-K + f0) - 1)))
    assert (dsolve(Eq(f(x).diff(x),
                      r * f(x) * (1 - f(x) / K)),
                   f(x),
                   ics={f(0): f0})) == sol

    #Order dependent issues Refer to PR #16098
    assert set(dsolve(f(x).diff(x)*(f(x).diff(x, 2)-x), ics={f(x).diff(x).subs(x,0):0, f(0):0})) == \
        {Eq(f(x), 0), Eq(f(x), x ** 3 / 6)}
    assert set(dsolve(f(x).diff(x)*(f(x).diff(x, 2)-x), ics={f(0):0, f(x).diff(x).subs(x,0):0})) == \
        {Eq(f(x), 0), Eq(f(x), x ** 3 / 6)}

    # XXX: Ought to be ValueError
    raises(
        ValueError,
        lambda: solve_ics([Eq(f(x),
                              C1 * sin(x) + C2 * cos(x))], [f(x)], [C1, C2], {
                                  f(0): 1,
                                  f(pi): 1
                              }))

    # Degenerate case. f'(0) is identically 0.
    raises(
        ValueError, lambda: solve_ics([Eq(f(x), sqrt(C1 - x**2))], [f(x)],
                                      [C1], {f(x).diff(x).subs(x, 0): 0}))

    EI, q, L = symbols('EI q L')

    # eq = Eq(EI*diff(f(x), x, 4), q)
    sols = [
        Eq(f(x), C1 + C2 * x + C3 * x**2 + C4 * x**3 + q * x**4 / (24 * EI))
    ]
    funcs = [f(x)]
    constants = [C1, C2, C3, C4]
    # Test both cases, Derivative (the default from f(x).diff(x).subs(x, L)),
    # and Subs
    ics1 = {
        f(0): 0,
        f(x).diff(x).subs(x, 0): 0,
        f(L).diff(L, 2): 0,
        f(L).diff(L, 3): 0
    }
    ics2 = {
        f(0): 0,
        f(x).diff(x).subs(x, 0): 0,
        Subs(f(x).diff(x, 2), x, L): 0,
        Subs(f(x).diff(x, 3), x, L): 0
    }

    solved_constants1 = solve_ics(sols, funcs, constants, ics1)
    solved_constants2 = solve_ics(sols, funcs, constants, ics2)
    assert solved_constants1 == solved_constants2 == {
        C1: 0,
        C2: 0,
        C3: L**2 * q / (4 * EI),
        C4: -L * q / (6 * EI)
    }
Пример #30
0
def test_issue_16146():
    raises(ValueError, lambda: dsolve([f(x).diff(
        x), g(x).diff(x)], [f(x), g(x), h(x)]))
    raises(ValueError, lambda: dsolve([f(x).diff(x), g(x).diff(x)], [f(x)]))