Example #1
0
def test_pde_separate_mul():
    x, y, z, t = symbols("x,y,z,t")
    c = Symbol("C", extended_real=True)
    Phi = Function('Phi')
    F, R, T, X, Y, Z, u = map(Function, 'FRTXYZu')
    r, theta, z = symbols('r,theta,z')

    # Something simple :)
    eq = Eq(D(F(x, y, z), x) + D(F(x, y, z), y) + D(F(x, y, z), z))

    # Duplicate arguments in functions
    pytest.raises(
        ValueError,
        lambda: pde_separate_mul(eq, F(x, y, z), [X(x), u(z, z)]))
    # Wrong number of arguments
    pytest.raises(ValueError,
                  lambda: pde_separate_mul(eq, F(x, y, z), [X(x), Y(y)]))
    # Wrong variables: [x, y] -> [x, z]
    pytest.raises(
        ValueError,
        lambda: pde_separate_mul(eq, F(x, y, z), [X(t), Y(x, y)]))

    assert pde_separate_mul(eq, F(x, y, z), [Y(y), u(x, z)]) == \
        [D(Y(y), y)/Y(y), -D(u(x, z), x)/u(x, z) - D(u(x, z), z)/u(x, z)]
    assert pde_separate_mul(eq, F(x, y, z), [X(x), Y(y), Z(z)]) == \
        [D(X(x), x)/X(x), -D(Z(z), z)/Z(z) - D(Y(y), y)/Y(y)]

    # wave equation
    wave = Eq(D(u(x, t), t, t), c**2 * D(u(x, t), x, x))
    res = pde_separate_mul(wave, u(x, t), [X(x), T(t)])
    assert res == [D(X(x), x, x) / X(x), D(T(t), t, t) / (c**2 * T(t))]

    # Laplace equation in cylindrical coords
    eq = Eq(1 / r * D(Phi(r, theta, z), r) + D(Phi(r, theta, z), r, 2) +
            1 / r**2 * D(Phi(r, theta, z), theta, 2) +
            D(Phi(r, theta, z), z, 2))
    # Separate z
    res = pde_separate_mul(eq, Phi(r, theta, z), [Z(z), u(theta, r)])
    assert res == [
        D(Z(z), z, z) / Z(z),
        -D(u(theta, r), r, r) / u(theta, r) - D(u(theta, r), r) /
        (r * u(theta, r)) - D(u(theta, r), theta, theta) / (r**2 * u(theta, r))
    ]
    # Lets use the result to create a new equation...
    eq = Eq(res[1], c)
    # ...and separate theta...
    res = pde_separate_mul(eq, u(theta, r), [T(theta), R(r)])
    assert res == [
        D(T(theta), theta, theta) / T(theta),
        -r * D(R(r), r) / R(r) - r**2 * D(R(r), r, r) / R(r) - c * r**2
    ]
    # ...or r...
    res = pde_separate_mul(eq, u(theta, r), [R(r), T(theta)])
    assert res == [
        r * D(R(r), r) / R(r) + r**2 * D(R(r), r, r) / R(r) + c * r**2,
        -D(T(theta), theta, theta) / T(theta)
    ]
Example #2
0
def test_pde_separate_mul():
    x, y, z, t = symbols('x,y,z,t')
    c = Symbol('C', extended_real=True)
    Phi = Function('Phi')
    F, R, T, X, Y, Z, u = map(Function, 'FRTXYZu')
    r, theta, z = symbols('r,theta,z')

    # Something simple :)
    eq = Eq(diff(F(x, y, z), x) + diff(F(x, y, z), y) + diff(F(x, y, z), z), 0)

    # Duplicate arguments in functions
    pytest.raises(
        ValueError,
        lambda: pde_separate_mul(eq, F(x, y, z), [X(x), u(z, z)]))
    # Wrong number of arguments
    pytest.raises(ValueError,
                  lambda: pde_separate_mul(eq, F(x, y, z), [X(x), Y(y)]))
    # Wrong variables: [x, y] -> [x, z]
    pytest.raises(
        ValueError,
        lambda: pde_separate_mul(eq, F(x, y, z), [X(t), Y(x, y)]))

    assert pde_separate_mul(eq, F(x, y, z), [Y(y), u(x, z)]) == \
        [diff(Y(y), y)/Y(y), -diff(u(x, z), x)/u(x, z) - diff(u(x, z), z)/u(x, z)]
    assert pde_separate_mul(eq, F(x, y, z), [X(x), Y(y), Z(z)]) == \
        [diff(X(x), x)/X(x), -diff(Z(z), z)/Z(z) - diff(Y(y), y)/Y(y)]

    # wave equation
    wave = Eq(diff(u(x, t), t, t), c**2 * diff(u(x, t), x, x))
    res = pde_separate_mul(wave, u(x, t), [X(x), T(t)])
    assert res == [diff(X(x), x, x) / X(x), diff(T(t), t, t) / (c**2 * T(t))]

    # Laplace equation in cylindrical coords
    eq = Eq(
        1 / r * diff(Phi(r, theta, z), r) + diff(Phi(r, theta, z), r, 2) +
        1 / r**2 * diff(Phi(r, theta, z), theta, 2) +
        diff(Phi(r, theta, z), z, 2), 0)
    # Separate z
    res = pde_separate_mul(eq, Phi(r, theta, z), [Z(z), u(theta, r)])
    assert res == [
        diff(Z(z), z, z) / Z(z), -diff(u(theta, r), r, r) / u(theta, r) -
        diff(u(theta, r), r) / (r * u(theta, r)) -
        diff(u(theta, r), theta, theta) / (r**2 * u(theta, r))
    ]
    # Lets use the result to create a new equation...
    eq = Eq(res[1], c)
    # ...and separate theta...
    res = pde_separate_mul(eq, u(theta, r), [T(theta), R(r)])
    assert res == [
        diff(T(theta), theta, theta) / T(theta),
        -r * diff(R(r), r) / R(r) - r**2 * diff(R(r), r, r) / R(r) - c * r**2
    ]
    # ...or r...
    res = pde_separate_mul(eq, u(theta, r), [R(r), T(theta)])
    assert res == [
        r * diff(R(r), r) / R(r) + r**2 * diff(R(r), r, r) / R(r) + c * r**2,
        -diff(T(theta), theta, theta) / T(theta)
    ]

    # Laplace eq in spherical coordinates
    r, phi, theta, C1 = symbols('r,phi,theta,C1')
    Xi = Function('Xi')
    R, Phi, Theta, u = map(Function, ['R', 'Phi', 'Theta', 'u'])
    eq = Eq(
        diff(Xi(r, phi, theta), r, 2) + 2 / r * diff(Xi(r, phi, theta), r) +
        1 / (r**2 * sin(phi)**2) * diff(Xi(r, phi, theta), theta, 2) +
        cos(phi) / (r**2 * sin(phi)) * diff(Xi(r, phi, theta), phi) +
        1 / r**2 * diff(Xi(r, phi, theta), phi, 2), 0)
    res_theta = pde_separate(eq, Xi(r, phi, theta), [Theta(theta), u(r, phi)])
    eq_left = Eq(res_theta[1], -C1)
    res_theta = pde_separate(eq_left, u(r, phi), [Phi(phi), R(r)])
    assert (res_theta == [
        C1 / sin(phi)**2 - diff(Phi(phi), phi, phi) / Phi(phi) -
        diff(Phi(phi), phi) / (Phi(phi) * tan(phi)),
        r**2 * diff(R(r), r, r) / R(r) + 2 * r * diff(R(r), r) / R(r)
    ])

    # coverage tests
    assert pde_separate_mul(Eq(u(x, t).diff(x),
                               u(x, t).diff(x, t)), u(x, t),
                            [X(x), T(t)]) == [-1, -T(t) / diff(T(t), t)]
    assert pde_separate(Eq((x + t) * u(x, t).diff(x),
                           u(x, t).diff(t)),
                        u(x, t), [X(x), T(t)],
                        strategy='mul') is None
    assert pde_separate(Eq(u(x, t).diff(x),
                           u(x, t).diff(t) + t),
                        u(x, t), [X(x), T(t)],
                        strategy='mul') is None
    assert pde_separate(Eq(u(x, t).diff(x), exp(u(x, t).diff(t))),
                        u(x, t), [X(x), T(t)],
                        strategy='mul') is None
Example #3
0
def test_pde_separate_mul():
    x, y, z, t = symbols("x,y,z,t")
    c = Symbol("C", extended_real=True)
    Phi = Function('Phi')
    F, R, T, X, Y, Z, u = map(Function, 'FRTXYZu')
    r, theta, z = symbols('r,theta,z')

    # Something simple :)
    eq = Eq(diff(F(x, y, z), x) + diff(F(x, y, z), y) + diff(F(x, y, z), z))

    # Duplicate arguments in functions
    pytest.raises(ValueError, lambda: pde_separate_mul(eq, F(x, y, z), [X(x), u(z, z)]))
    # Wrong number of arguments
    pytest.raises(ValueError, lambda: pde_separate_mul(eq, F(x, y, z), [X(x), Y(y)]))
    # Wrong variables: [x, y] -> [x, z]
    pytest.raises(ValueError, lambda: pde_separate_mul(eq, F(x, y, z), [X(t), Y(x, y)]))

    assert pde_separate_mul(eq, F(x, y, z), [Y(y), u(x, z)]) == \
        [diff(Y(y), y)/Y(y), -diff(u(x, z), x)/u(x, z) - diff(u(x, z), z)/u(x, z)]
    assert pde_separate_mul(eq, F(x, y, z), [X(x), Y(y), Z(z)]) == \
        [diff(X(x), x)/X(x), -diff(Z(z), z)/Z(z) - diff(Y(y), y)/Y(y)]

    # wave equation
    wave = Eq(diff(u(x, t), t, t), c**2*diff(u(x, t), x, x))
    res = pde_separate_mul(wave, u(x, t), [X(x), T(t)])
    assert res == [diff(X(x), x, x)/X(x), diff(T(t), t, t)/(c**2*T(t))]

    # Laplace equation in cylindrical coords
    eq = Eq(1/r * diff(Phi(r, theta, z), r) + diff(Phi(r, theta, z), r, 2) +
            1/r**2 * diff(Phi(r, theta, z), theta, 2) + diff(Phi(r, theta, z), z, 2))
    # Separate z
    res = pde_separate_mul(eq, Phi(r, theta, z), [Z(z), u(theta, r)])
    assert res == [diff(Z(z), z, z)/Z(z),
                   -diff(u(theta, r), r, r)/u(theta, r) -
                   diff(u(theta, r), r)/(r*u(theta, r)) -
                   diff(u(theta, r), theta, theta)/(r**2*u(theta, r))]
    # Lets use the result to create a new equation...
    eq = Eq(res[1], c)
    # ...and separate theta...
    res = pde_separate_mul(eq, u(theta, r), [T(theta), R(r)])
    assert res == [diff(T(theta), theta, theta)/T(theta),
                   -r*diff(R(r), r)/R(r) - r**2*diff(R(r), r, r)/R(r) - c*r**2]
    # ...or r...
    res = pde_separate_mul(eq, u(theta, r), [R(r), T(theta)])
    assert res == [r*diff(R(r), r)/R(r) + r**2*diff(R(r), r, r)/R(r) + c*r**2,
                   -diff(T(theta), theta, theta)/T(theta)]

    # Laplace eq in spherical coordinates
    r, phi, theta, C1 = symbols("r,phi,theta,C1")
    Xi = Function('Xi')
    R, Phi, Theta, u = map(Function, ['R', 'Phi', 'Theta', 'u'])
    eq = Eq(diff(Xi(r, phi, theta), r, 2) + 2/r * diff(Xi(r, phi, theta), r) +
            1/(r**2 * sin(phi)**2) * diff(Xi(r, phi, theta), theta, 2) +
            cos(phi)/(r**2 * sin(phi)) * diff(Xi(r, phi, theta), phi) +
            1/r**2 * diff(Xi(r, phi, theta), phi, 2))
    res_theta = pde_separate(eq, Xi(r, phi, theta), [Theta(theta), u(r, phi)])
    eq_left = Eq(res_theta[1], -C1)
    res_theta = pde_separate(eq_left, u(r, phi), [Phi(phi), R(r)])
    assert (res_theta == [C1/sin(phi)**2 - diff(Phi(phi), phi, phi)/Phi(phi) -
                          diff(Phi(phi), phi)/(Phi(phi)*tan(phi)),
                          r**2*diff(R(r), r, r)/R(r) + 2*r*diff(R(r), r)/R(r)])

    # coverage tests
    assert pde_separate_mul(Eq(u(x, t).diff(x), u(x, t).diff(x, t)), u(x, t),
                            [X(x), T(t)]) == [-1, -T(t)/diff(T(t), t)]
    assert pde_separate(Eq((x + t)*u(x, t).diff(x), u(x, t).diff(t)),
                        u(x, t), [X(x), T(t)], strategy='mul') is None
    assert pde_separate(Eq(u(x, t).diff(x), u(x, t).diff(t) + t), u(x, t),
                        [X(x), T(t)], strategy='mul') is None
    assert pde_separate(Eq(u(x, t).diff(x), exp(u(x, t).diff(t))), u(x, t),
                        [X(x), T(t)], strategy='mul') is None