def test_functional_diffgeom_ch3():
    x0, y0 = symbols('x0, y0', extended_real=True)
    x, y, t = symbols('x, y, t', extended_real=True)
    f = Function('f')
    b1 = Function('b1')
    b2 = Function('b2')
    p_r = R2_r.point([x0, y0])

    s_field = f(R2.x, R2.y)
    v_field = b1(R2.x)*R2.e_x + b2(R2.y)*R2.e_y
    assert v_field.rcall(s_field).rcall(p_r).doit() == b1(
        x0)*Derivative(f(x0, y0), x0) + b2(y0)*Derivative(f(x0, y0), y0)

    assert R2.e_x(R2.r**2).rcall(p_r) == 2*x0
    v = R2.e_x + 2*R2.e_y
    s = R2.r**2 + 3*R2.x
    assert v.rcall(s).rcall(p_r).doit() == 2*x0 + 4*y0 + 3

    circ = -R2.y*R2.e_x + R2.x*R2.e_y
    series = intcurve_series(circ, t, R2_r.point([1, 0]), coeffs=True)
    series_x, series_y = zip(*series)
    assert all(term == cos(t).taylor_term(i, t)
               for i, term in enumerate(series_x))
    assert all(term == sin(t).taylor_term(i, t)
               for i, term in enumerate(series_y))
示例#2
0
def test_correct_arguments():
    pytest.raises(ValueError, lambda: R2.e_x(R2.e_x))
    pytest.raises(ValueError, lambda: R2.e_x(R2.dx))

    pytest.raises(ValueError, lambda: Commutator(R2.e_x, R2.x))
    pytest.raises(ValueError, lambda: Commutator(R2.dx, R2.e_x))

    pytest.raises(ValueError, lambda: Differential(Differential(R2.e_x)))

    pytest.raises(ValueError, lambda: R2.dx(R2.x))

    pytest.raises(ValueError, lambda: TensorProduct(R2.e_x, R2.dx))

    pytest.raises(ValueError, lambda: LieDerivative(R2.dx, R2.dx))
    pytest.raises(ValueError, lambda: LieDerivative(R2.x, R2.dx))

    pytest.raises(ValueError, lambda: CovarDerivativeOp(R2.dx, []))
    pytest.raises(ValueError, lambda: CovarDerivativeOp(R2.x, []))

    a = Symbol('a')
    pytest.raises(ValueError, lambda: intcurve_series(R2.dx, a, R2_r.point([1, 2])))
    pytest.raises(ValueError, lambda: intcurve_series(R2.x, a, R2_r.point([1, 2])))

    pytest.raises(ValueError, lambda: intcurve_diffequ(R2.dx, a, R2_r.point([1, 2])))
    pytest.raises(ValueError, lambda: intcurve_diffequ(R2.x, a, R2_r.point([1, 2])))

    pytest.raises(ValueError, lambda: contravariant_order(R2.e_x + R2.dx))
    pytest.raises(ValueError, lambda: contravariant_order(R2.dx**2))
    pytest.raises(ValueError, lambda: covariant_order(R2.e_x + R2.dx))

    pytest.raises(ValueError, lambda: contravariant_order(R2.e_x*R2.e_y))
    pytest.raises(ValueError, lambda: covariant_order(R2.dx*R2.dy))

    assert covariant_order(Integer(0), True) == -1
    assert contravariant_order(Integer(0), True) == -1
示例#3
0
def test_functional_diffgeom_ch2():
    x0, y0, r0, theta0 = symbols('x0, y0, r0, theta0', extended_real=True)
    x, y = symbols('x, y', extended_real=True)
    f = Function('f')

    assert (R2_p.point_to_coords(R2_r.point([x0, y0])) == Matrix(
        [sqrt(x0**2 + y0**2), atan2(y0, x0)]))
    assert (R2_r.point_to_coords(R2_p.point([r0, theta0])) == Matrix(
        [r0 * cos(theta0), r0 * sin(theta0)]))

    assert R2_p.jacobian(R2_r, [r0, theta0]) == Matrix(
        [[cos(theta0), -r0 * sin(theta0)], [sin(theta0), r0 * cos(theta0)]])

    field = f(R2.x, R2.y)
    p1_in_rect = R2_r.point([x0, y0])
    p1_in_polar = R2_p.point([sqrt(x0**2 + y0**2), atan2(y0, x0)])
    assert field.rcall(p1_in_rect) == f(x0, y0)
    assert field.rcall(p1_in_polar) == f(x0, y0)

    p_r = R2_r.point([x0, y0])
    p_p = R2_p.point([r0, theta0])
    assert R2.x(p_r) == x0
    assert R2.x(p_p) == r0 * cos(theta0)
    assert R2.r(p_p) == r0
    assert R2.r(p_r) == sqrt(x0**2 + y0**2)
    assert R2.theta(p_r) == atan2(y0, x0)

    h = R2.x * R2.r**2 + R2.y**3
    assert h.rcall(p_r) == x0 * (x0**2 + y0**2) + y0**3
    assert h.rcall(p_p) == r0**3 * sin(theta0)**3 + r0**3 * cos(theta0)
示例#4
0
def test_correct_arguments():
    pytest.raises(ValueError, lambda: R2.e_x(R2.e_x))
    pytest.raises(ValueError, lambda: R2.e_x(R2.dx))

    pytest.raises(ValueError, lambda: Commutator(R2.e_x, R2.x))
    pytest.raises(ValueError, lambda: Commutator(R2.dx, R2.e_x))

    pytest.raises(ValueError, lambda: Differential(Differential(R2.e_x)))

    pytest.raises(ValueError, lambda: R2.dx(R2.x))

    pytest.raises(ValueError, lambda: TensorProduct(R2.e_x, R2.dx))

    pytest.raises(ValueError, lambda: LieDerivative(R2.dx, R2.dx))
    pytest.raises(ValueError, lambda: LieDerivative(R2.x, R2.dx))

    pytest.raises(ValueError, lambda: CovarDerivativeOp(R2.dx, []))
    pytest.raises(ValueError, lambda: CovarDerivativeOp(R2.x, []))

    a = Symbol('a')
    pytest.raises(ValueError, lambda: intcurve_series(R2.dx, a, R2_r.point([1, 2])))
    pytest.raises(ValueError, lambda: intcurve_series(R2.x, a, R2_r.point([1, 2])))

    pytest.raises(ValueError, lambda: intcurve_diffequ(R2.dx, a, R2_r.point([1, 2])))
    pytest.raises(ValueError, lambda: intcurve_diffequ(R2.x, a, R2_r.point([1, 2])))

    pytest.raises(ValueError, lambda: contravariant_order(R2.e_x + R2.dx))
    pytest.raises(ValueError, lambda: contravariant_order(R2.dx**2))
    pytest.raises(ValueError, lambda: covariant_order(R2.e_x + R2.dx))

    pytest.raises(ValueError, lambda: contravariant_order(R2.e_x*R2.e_y))
    pytest.raises(ValueError, lambda: covariant_order(R2.dx*R2.dy))

    assert covariant_order(Integer(0), True) == -1
    assert contravariant_order(Integer(0), True) == -1
示例#5
0
def test_functional_diffgeom_ch3():
    x0, y0 = symbols('x0, y0', extended_real=True)
    x, y, t = symbols('x, y, t', extended_real=True)
    f = Function('f')
    b1 = Function('b1')
    b2 = Function('b2')
    p_r = R2_r.point([x0, y0])

    s_field = f(R2.x, R2.y)
    v_field = b1(R2.x) * R2.e_x + b2(R2.y) * R2.e_y
    assert v_field.rcall(s_field).rcall(p_r).doit() == b1(x0) * Derivative(
        f(x0, y0), x0) + b2(y0) * Derivative(f(x0, y0), y0)

    assert R2.e_x(R2.r**2).rcall(p_r) == 2 * x0
    v = R2.e_x + 2 * R2.e_y
    s = R2.r**2 + 3 * R2.x
    assert v.rcall(s).rcall(p_r).doit() == 2 * x0 + 4 * y0 + 3

    circ = -R2.y * R2.e_x + R2.x * R2.e_y
    series = intcurve_series(circ, t, R2_r.point([1, 0]), coeffs=True)
    series_x, series_y = zip(*series)
    assert all(term == cos(t).taylor_term(i, t)
               for i, term in enumerate(series_x))
    assert all(term == sin(t).taylor_term(i, t)
               for i, term in enumerate(series_y))
def test_functional_diffgeom_ch2():
    x0, y0, r0, theta0 = symbols('x0, y0, r0, theta0', extended_real=True)
    x, y = symbols('x, y', extended_real=True)
    f = Function('f')

    assert (R2_p.point_to_coords(R2_r.point([x0, y0])) ==
            Matrix([sqrt(x0**2 + y0**2), atan2(y0, x0)]))
    assert (R2_r.point_to_coords(R2_p.point([r0, theta0])) ==
            Matrix([r0*cos(theta0), r0*sin(theta0)]))

    assert R2_p.jacobian(R2_r, [r0, theta0]) == Matrix(
        [[cos(theta0), -r0*sin(theta0)], [sin(theta0), r0*cos(theta0)]])

    field = f(R2.x, R2.y)
    p1_in_rect = R2_r.point([x0, y0])
    p1_in_polar = R2_p.point([sqrt(x0**2 + y0**2), atan2(y0, x0)])
    assert field.rcall(p1_in_rect) == f(x0, y0)
    assert field.rcall(p1_in_polar) == f(x0, y0)

    p_r = R2_r.point([x0, y0])
    p_p = R2_p.point([r0, theta0])
    assert R2.x(p_r) == x0
    assert R2.x(p_p) == r0*cos(theta0)
    assert R2.r(p_p) == r0
    assert R2.r(p_r) == sqrt(x0**2 + y0**2)
    assert R2.theta(p_r) == atan2(y0, x0)

    h = R2.x*R2.r**2 + R2.y**3
    assert h.rcall(p_r) == x0*(x0**2 + y0**2) + y0**3
    assert h.rcall(p_p) == r0**3*sin(theta0)**3 + r0**3*cos(theta0)
示例#7
0
def test_simplify():
    x, y = R2_r.coord_functions()
    dx, dy = R2_r.base_oneforms()
    ex, ey = R2_r.base_vectors()
    assert simplify(x) == x
    assert simplify(x * y) == x * y
    assert simplify(dx * dy) == dx * dy
    assert simplify(ex * ey) == ex * ey
    assert ((1 - x) * dx) / (1 - x)**2 == dx / (1 - x)
示例#8
0
def test_simplify():
    x, y = R2_r.coord_functions()
    dx, dy = R2_r.base_oneforms()
    ex, ey = R2_r.base_vectors()
    assert simplify(x) == x
    assert simplify(x*y) == x*y
    assert simplify(dx*dy) == dx*dy
    assert simplify(ex*ey) == ex*ey
    assert ((1-x)*dx)/(1-x)**2 == dx/(1-x)
示例#9
0
def test_intcurve_diffequ():
    start_point = R2_r.point([1, 0])
    vector_field = -R2.y*R2.e_x + R2.x*R2.e_y
    equations, init_cond = intcurve_diffequ(vector_field, t, start_point)
    assert sstr(equations) == '[f_1(t) + Derivative(f_0(t), t), -f_0(t) + Derivative(f_1(t), t)]'
    assert sstr(init_cond) == '[f_0(0) - 1, f_1(0)]'
    equations, init_cond = intcurve_diffequ(vector_field, t, start_point, R2_p)
    assert sstr(
        equations) == '[Derivative(f_0(t), t), Derivative(f_1(t), t) - 1]'
    assert sstr(init_cond) == '[f_0(0) - 1, f_1(0)]'

    start_point = R2_r.point([x, y])
    vector_field = R2_r.e_x
    assert intcurve_series(vector_field, t, start_point,
                           n=3) == Matrix([[t + x], [y]])
示例#10
0
def test_R2():
    x0, y0, r0, theta0 = symbols('x0, y0, r0, theta0', extended_real=True)
    point_r = R2_r.point([x0, y0])
    point_p = R2_p.point([r0, theta0])

    # r**2 = x**2 + y**2
    assert (R2.r**2 - R2.x**2 - R2.y**2).rcall(point_r) == 0
    assert trigsimp( (R2.r**2 - R2.x**2 - R2.y**2).rcall(point_p) ) == 0
    assert trigsimp(R2.e_r(R2.x**2 + R2.y**2).rcall(point_p).doit()) == 2*r0

    # polar->rect->polar == Id
    a, b = symbols('a b', positive=True)
    m = Matrix([[a], [b]])
    # TODO assert m == R2_r.coord_tuple_transform_to(R2_p, R2_p.coord_tuple_transform_to(R2_r, [a, b])).applyfunc(simplify)
    assert m == R2_p.coord_tuple_transform_to(
        R2_r, R2_r.coord_tuple_transform_to(R2_p, m)).applyfunc(simplify)
示例#11
0
def test_R2():
    x0, y0, r0, theta0 = symbols('x0, y0, r0, theta0', extended_real=True)
    point_r = R2_r.point([x0, y0])
    point_p = R2_p.point([r0, theta0])

    # r**2 = x**2 + y**2
    assert (R2.r**2 - R2.x**2 - R2.y**2).rcall(point_r) == 0
    assert trigsimp((R2.r**2 - R2.x**2 - R2.y**2).rcall(point_p)) == 0
    assert trigsimp(R2.e_r(R2.x**2 + R2.y**2).rcall(point_p).doit()) == 2 * r0

    # polar->rect->polar == Id
    a, b = symbols('a b', positive=True)
    m = Matrix([[a], [b]])
    # TODO assert m == R2_r.coord_tuple_transform_to(R2_p, R2_p.coord_tuple_transform_to(R2_r, [a, b])).applyfunc(simplify)
    assert m == R2_p.coord_tuple_transform_to(
        R2_r, R2_r.coord_tuple_transform_to(R2_p, m)).applyfunc(simplify)
示例#12
0
def test_intcurve_diffequ():
    start_point = R2_r.point([1, 0])
    vector_field = -R2.y * R2.e_x + R2.x * R2.e_y
    equations, init_cond = intcurve_diffequ(vector_field, t, start_point)
    assert sstr(
        equations
    ) == '[f_1(t) + Derivative(f_0(t), t), -f_0(t) + Derivative(f_1(t), t)]'
    assert sstr(init_cond) == '[f_0(0) - 1, f_1(0)]'
    equations, init_cond = intcurve_diffequ(vector_field, t, start_point, R2_p)
    assert sstr(
        equations) == '[Derivative(f_0(t), t), Derivative(f_1(t), t) - 1]'
    assert sstr(init_cond) == '[f_0(0) - 1, f_1(0)]'

    start_point = R2_r.point([x, y])
    vector_field = R2_r.e_x
    assert intcurve_series(vector_field, t, start_point,
                           n=3) == Matrix([[t + x], [y]])
示例#13
0
def test_intcurve_diffequ():
    t = symbols('t')
    start_point = R2_r.point([1, 0])
    vector_field = -R2.y*R2.e_x + R2.x*R2.e_y
    equations, init_cond = intcurve_diffequ(vector_field, t, start_point)
    assert sstr(equations) == '[f_1(t) + Derivative(f_0(t), t), -f_0(t) + Derivative(f_1(t), t)]'
    assert sstr(init_cond) == '[f_0(0) - 1, f_1(0)]'
    equations, init_cond = intcurve_diffequ(vector_field, t, start_point, R2_p)
    assert sstr(
        equations) == '[Derivative(f_0(t), t), Derivative(f_1(t), t) - 1]'
    assert sstr(init_cond) == '[f_0(0) - 1, f_1(0)]'
示例#14
0
def test_functional_diffgeom_ch4():
    x0, y0, theta0 = symbols('x0, y0, theta0', extended_real=True)
    x, y, r, theta = symbols('x, y, r, theta', extended_real=True)
    r0 = symbols('r0', positive=True)
    f = Function('f')
    b1 = Function('b1')
    b2 = Function('b2')
    p_r = R2_r.point([x0, y0])
    p_p = R2_p.point([r0, theta0])

    f_field = b1(R2.x, R2.y) * R2.dx + b2(R2.x, R2.y) * R2.dy
    assert f_field.rcall(R2.e_x).rcall(p_r) == b1(x0, y0)
    assert f_field.rcall(R2.e_y).rcall(p_r) == b2(x0, y0)

    s_field_r = f(R2.x, R2.y)
    df = Differential(s_field_r)
    assert df(R2.e_x).rcall(p_r).doit() == Derivative(f(x0, y0), x0)
    assert df(R2.e_y).rcall(p_r).doit() == Derivative(f(x0, y0), y0)

    s_field_p = f(R2.r, R2.theta)
    df = Differential(s_field_p)
    assert trigsimp(df(R2.e_x).rcall(p_p).doit()) == (
        cos(theta0) * Derivative(f(r0, theta0), r0) -
        sin(theta0) * Derivative(f(r0, theta0), theta0) / r0)
    assert trigsimp(df(R2.e_y).rcall(p_p).doit()) == (
        sin(theta0) * Derivative(f(r0, theta0), r0) +
        cos(theta0) * Derivative(f(r0, theta0), theta0) / r0)

    assert R2.dx(R2.e_x).rcall(p_r) == 1
    assert R2.dx(R2.e_x) == 1
    assert R2.dx(R2.e_y).rcall(p_r) == 0
    assert R2.dx(R2.e_y) == 0

    circ = -R2.y * R2.e_x + R2.x * R2.e_y
    assert R2.dx(circ).rcall(p_r).doit() == -y0
    assert R2.dy(circ).rcall(p_r) == x0
    assert R2.dr(circ).rcall(p_r) == 0
    assert simplify(R2.dtheta(circ).rcall(p_r)) == 1

    assert (circ - R2.e_theta).rcall(s_field_r).rcall(p_r) == 0
def test_functional_diffgeom_ch4():
    x0, y0, theta0 = symbols('x0, y0, theta0', extended_real=True)
    x, y, r, theta = symbols('x, y, r, theta', extended_real=True)
    r0 = symbols('r0', positive=True)
    f = Function('f')
    b1 = Function('b1')
    b2 = Function('b2')
    p_r = R2_r.point([x0, y0])
    p_p = R2_p.point([r0, theta0])

    f_field = b1(R2.x, R2.y)*R2.dx + b2(R2.x, R2.y)*R2.dy
    assert f_field.rcall(R2.e_x).rcall(p_r) == b1(x0, y0)
    assert f_field.rcall(R2.e_y).rcall(p_r) == b2(x0, y0)

    s_field_r = f(R2.x, R2.y)
    df = Differential(s_field_r)
    assert df(R2.e_x).rcall(p_r).doit() == Derivative(f(x0, y0), x0)
    assert df(R2.e_y).rcall(p_r).doit() == Derivative(f(x0, y0), y0)

    s_field_p = f(R2.r, R2.theta)
    df = Differential(s_field_p)
    assert trigsimp(df(R2.e_x).rcall(p_p).doit()) == (
        cos(theta0)*Derivative(f(r0, theta0), r0) -
        sin(theta0)*Derivative(f(r0, theta0), theta0)/r0)
    assert trigsimp(df(R2.e_y).rcall(p_p).doit()) == (
        sin(theta0)*Derivative(f(r0, theta0), r0) +
        cos(theta0)*Derivative(f(r0, theta0), theta0)/r0)

    assert R2.dx(R2.e_x).rcall(p_r) == 1
    assert R2.dx(R2.e_x) == 1
    assert R2.dx(R2.e_y).rcall(p_r) == 0
    assert R2.dx(R2.e_y) == 0

    circ = -R2.y*R2.e_x + R2.x*R2.e_y
    assert R2.dx(circ).rcall(p_r).doit() == -y0
    assert R2.dy(circ).rcall(p_r) == x0
    assert R2.dr(circ).rcall(p_r) == 0
    assert simplify(R2.dtheta(circ).rcall(p_r)) == 1

    assert (circ - R2.e_theta).rcall(s_field_r).rcall(p_r) == 0
示例#16
0
def test_point():
    p = R2_r.point([x, y])
    # TODO assert p.free_symbols() == {x, y}
    assert p.coords(R2_r) == p.coords() == Matrix([x, y])
    assert p.coords(R2_p) == Matrix([sqrt(x**2 + y**2), atan2(y, x)])
示例#17
0
def test_point():
    p = R2_r.point([x, y])
    # TODO assert p.free_symbols() == {x, y}
    assert p.coords(R2_r) == p.coords() == Matrix([x, y])
    assert p.coords(R2_p) == Matrix([sqrt(x**2 + y**2), atan2(y, x)])