def test_locatenew_point(): """ Tests Point class, and locate_new method in CoordSysCartesian. """ A = CoordSysCartesian('A') assert isinstance(A.origin, Point) v = a * A.i + b * A.j + c * A.k C = A.locate_new('C', v) assert C.origin.position_wrt(A) == \ C.position_wrt(A) == \ C.origin.position_wrt(A.origin) == v assert A.origin.position_wrt(C) == \ A.position_wrt(C) == \ A.origin.position_wrt(C.origin) == -v assert A.origin.express_coordinates(C) == (-a, -b, -c) p = A.origin.locate_new('p', -v) assert p.express_coordinates(A) == (-a, -b, -c) assert p.position_wrt(C.origin) == p.position_wrt(C) == \ -2 * v p1 = p.locate_new('p1', 2 * v) assert p1.position_wrt(C.origin) == Vector.zero assert p1.express_coordinates(C) == (0, 0, 0) p2 = p.locate_new('p2', A.i) assert p1.position_wrt(p2) == 2 * v - A.i assert p2.express_coordinates(C) == (-2 * a + 1, -2 * b, -2 * c)
def test_locatenew_point(): """ Tests Point class, and locate_new method in CoordSysCartesian. """ A = CoordSysCartesian('A') assert isinstance(A.origin, Point) v = a*A.i + b*A.j + c*A.k C = A.locate_new('C', v) assert C.origin.position_wrt(A) == \ C.position_wrt(A) == \ C.origin.position_wrt(A.origin) == v assert A.origin.position_wrt(C) == \ A.position_wrt(C) == \ A.origin.position_wrt(C.origin) == -v assert A.origin.express_coordinates(C) == (-a, -b, -c) p = A.origin.locate_new('p', -v) assert p.express_coordinates(A) == (-a, -b, -c) assert p.position_wrt(C.origin) == p.position_wrt(C) == \ -2 * v p1 = p.locate_new('p1', 2*v) assert p1.position_wrt(C.origin) == Vector.zero assert p1.express_coordinates(C) == (0, 0, 0) p2 = p.locate_new('p2', A.i) assert p1.position_wrt(p2) == 2*v - A.i assert p2.express_coordinates(C) == (-2*a + 1, -2*b, -2*c)
def test_vector(): """ Tests the effects of orientation of coordinate systems on basic vector operations. """ N = CoordSysCartesian("N") A = N.orient_new_axis("A", q1, N.k) B = A.orient_new_axis("B", q2, A.i) C = B.orient_new_axis("C", q3, B.j) # Test to_matrix v1 = a * N.i + b * N.j + c * N.k assert v1.to_matrix(A) == Matrix([[a * cos(q1) + b * sin(q1)], [-a * sin(q1) + b * cos(q1)], [c]]) # Test dot assert N.i.dot(A.i) == cos(q1) assert N.i.dot(A.j) == -sin(q1) assert N.i.dot(A.k) == 0 assert N.j.dot(A.i) == sin(q1) assert N.j.dot(A.j) == cos(q1) assert N.j.dot(A.k) == 0 assert N.k.dot(A.i) == 0 assert N.k.dot(A.j) == 0 assert N.k.dot(A.k) == 1 assert N.i.dot(A.i + A.j) == -sin(q1) + cos(q1) == (A.i + A.j).dot(N.i) assert A.i.dot(C.i) == cos(q3) assert A.i.dot(C.j) == 0 assert A.i.dot(C.k) == sin(q3) assert A.j.dot(C.i) == sin(q2) * sin(q3) assert A.j.dot(C.j) == cos(q2) assert A.j.dot(C.k) == -sin(q2) * cos(q3) assert A.k.dot(C.i) == -cos(q2) * sin(q3) assert A.k.dot(C.j) == sin(q2) assert A.k.dot(C.k) == cos(q2) * cos(q3) # Test cross assert N.i.cross(A.i) == sin(q1) * A.k assert N.i.cross(A.j) == cos(q1) * A.k assert N.i.cross(A.k) == -sin(q1) * A.i - cos(q1) * A.j assert N.j.cross(A.i) == -cos(q1) * A.k assert N.j.cross(A.j) == sin(q1) * A.k assert N.j.cross(A.k) == cos(q1) * A.i - sin(q1) * A.j assert N.k.cross(A.i) == A.j assert N.k.cross(A.j) == -A.i assert N.k.cross(A.k) == Vector.zero assert N.i.cross(A.i) == sin(q1) * A.k assert N.i.cross(A.j) == cos(q1) * A.k assert N.i.cross(A.i + A.j) == sin(q1) * A.k + cos(q1) * A.k assert (A.i + A.j).cross(N.i) == (-sin(q1) - cos(q1)) * N.k assert A.i.cross(C.i) == sin(q3) * C.j assert A.i.cross(C.j) == -sin(q3) * C.i + cos(q3) * C.k assert A.i.cross(C.k) == -cos(q3) * C.j assert C.i.cross(A.i) == (-sin(q3) * cos(q2)) * A.j + (-sin(q2) * sin(q3)) * A.k assert C.j.cross(A.i) == (sin(q2)) * A.j + (-cos(q2)) * A.k assert express(C.k.cross(A.i), C).trigsimp() == cos(q3) * C.j
def test_coordsyscartesian_equivalence(): A1 = CoordSysCartesian('A') assert A1 == A B = CoordSysCartesian('B') assert A != B assert A.locate_new('C1', A.i) == A.locate_new('C2', A.i) assert A.orient_new_axis('C1', a, A.i) == \ A.orient_new_axis('C2', a, A.i)
def test_coordsyscartesian_equivalence(): A = CoordSysCartesian("A") A1 = CoordSysCartesian("A") assert A1 == A B = CoordSysCartesian("B") assert A != B assert A.locate_new("C1", A.i) == A.locate_new("C2", A.i) assert A.orient_new_axis("C1", a, A.i) == A.orient_new_axis("C2", a, A.i)
def test_coordsyscartesian_equivalence(): A = CoordSysCartesian('A') A1 = CoordSysCartesian('A') assert A1 == A B = CoordSysCartesian('B') assert A != B assert A.locate_new('C1', A.i) == A.locate_new('C2', A.i) assert A.orient_new_axis('C1', a, A.i) == \ A.orient_new_axis('C2', a, A.i)
def test_orient_new_methods(): N = CoordSysCartesian('N') D = N.orient_new('D', 'Axis', [q4, N.j]) E = N.orient_new('E', 'Space', [q1, q2, q3], '123') F = N.orient_new('F', 'Quaternion', [q1, q2, q3, q4]) G = N.orient_new('G', 'Body', [q1, q2, q3], '123') assert D == N.orient_new_axis('D', q4, N.j) assert E == N.orient_new_space('E', q1, q2, q3, '123') assert F == N.orient_new_quaternion('F', q1, q2, q3, q4) assert G == N.orient_new_body('G', q1, q2, q3, '123')
def test_differential_operators_curvilinear_system(): A = CoordSysCartesian('A') A._set_lame_coefficient_mapping('spherical') B = CoordSysCartesian('B') B._set_lame_coefficient_mapping('cylindrical') # Test for spherical coordinate system and gradient assert gradient(3 * A.x + 4 * A.y) == 3 * A.i + 4 / A.x * A.j assert gradient( 3 * A.x * A.z + 4 * A.y) == 3 * A.z * A.i + 4 / A.x * A.j + (3 / sin(A.y)) * A.k assert gradient(0 * A.x + 0 * A.y + 0 * A.z) == Vector.zero assert gradient( A.x * A.y * A.z) == A.y * A.z * A.i + A.z * A.j + (A.y / sin(A.y)) * A.k # Test for spherical coordinate system and divergence assert divergence(A.x * A.i + A.y * A.j + A.z * A.k) == \ (sin(A.y)*A.x + cos(A.y)*A.x*A.y)/(sin(A.y)*A.x**2) + 3 + 1/(sin(A.y)*A.x) assert divergence(3*A.x*A.z*A.i + A.y*A.j + A.x*A.y*A.z*A.k) == \ (sin(A.y)*A.x + cos(A.y)*A.x*A.y)/(sin(A.y)*A.x**2) + 9*A.z + A.y/sin(A.y) assert divergence(Vector.zero) == 0 assert divergence(0 * A.i + 0 * A.j + 0 * A.k) == 0 # Test for cylindrical coordinate system and divergence assert divergence(B.x * B.i + B.y * B.j + B.z * B.k) == 2 + 1 / B.y assert divergence(B.x * B.j + B.z * B.k) == 1 # Test for spherical coordinate system and divergence assert curl(A.x*A.i + A.y*A.j + A.z*A.k) == \ (cos(A.y)*A.z/(sin(A.y)*A.x))*A.i + (-A.z/A.x)*A.j + A.y/A.x*A.k assert curl(A.x * A.j + A.z * A.k) == (cos(A.y) * A.z / (sin(A.y) * A.x)) * A.i + (-A.z / A.x) * A.j + 2 * A.k
def test_orienters(): A = CoordSysCartesian('A') axis_orienter = AxisOrienter(a, A.k) body_orienter = BodyOrienter(a, b, c, '123') space_orienter = SpaceOrienter(a, b, c, '123') q_orienter = QuaternionOrienter(q1, q2, q3, q4) assert axis_orienter.rotation_matrix(A) == Matrix([ [ cos(a), sin(a), 0], [-sin(a), cos(a), 0], [ 0, 0, 1]]) assert body_orienter.rotation_matrix() == Matrix([ [ cos(b)*cos(c), sin(a)*sin(b)*cos(c) + sin(c)*cos(a), sin(a)*sin(c) - sin(b)*cos(a)*cos(c)], [-sin(c)*cos(b), -sin(a)*sin(b)*sin(c) + cos(a)*cos(c), sin(a)*cos(c) + sin(b)*sin(c)*cos(a)], [ sin(b), -sin(a)*cos(b), cos(a)*cos(b)]]) assert space_orienter.rotation_matrix() == Matrix([ [cos(b)*cos(c), sin(c)*cos(b), -sin(b)], [sin(a)*sin(b)*cos(c) - sin(c)*cos(a), sin(a)*sin(b)*sin(c) + cos(a)*cos(c), sin(a)*cos(b)], [sin(a)*sin(c) + sin(b)*cos(a)*cos(c), -sin(a)*cos(c) + sin(b)*sin(c)*cos(a), cos(a)*cos(b)]]) assert q_orienter.rotation_matrix() == Matrix([ [q1**2 + q2**2 - q3**2 - q4**2, 2*q1*q4 + 2*q2*q3, -2*q1*q3 + 2*q2*q4], [-2*q1*q4 + 2*q2*q3, q1**2 - q2**2 + q3**2 - q4**2, 2*q1*q2 + 2*q3*q4], [2*q1*q3 + 2*q2*q4, -2*q1*q2 + 2*q3*q4, q1**2 - q2**2 - q3**2 + q4**2]])
def test_orthogonalize(): C = CoordSysCartesian('C') a, b = symbols('a b', integer=True) i, j, k = C.base_vectors() v1 = i + 2*j v2 = 2*i + 3*j v3 = 3*i + 5*j v4 = 3*i + j v5 = 2*i + 2*j v6 = a*i + b*j v7 = 4*a*i + 4*b*j assert orthogonalize(v1, v2) == [C.i + 2*C.j, 2*C.i/5 + -C.j/5] # from wikipedia assert orthogonalize(v4, v5, orthonormal=True) == \ [(3*sqrt(10))*C.i/10 + (sqrt(10))*C.j/10, (-sqrt(10))*C.i/10 + (3*sqrt(10))*C.j/10] raises(ValueError, lambda: orthogonalize(v1, v2, v3)) raises(ValueError, lambda: orthogonalize(v6, v7))
def test_orthogonalize(): C = CoordSysCartesian('C') a, b = symbols('a b', integer=True); i, j, k = C.base_vectors() v1 = i + 2*j v2 = 2*i + 3*j v3 = 3*i + 5*j v4 = 3*i + j v5 = 2*i + 2*j v6 = a*i + b*j v7 = 4*a*i + 4*b*j assert orthogonalize(v1, v2) == [C.i + 2*C.j, 2*C.i/5 + -C.j/5] # from wikipedia assert orthogonalize(v4, v5, orthonormal=True) == \ [(3*sqrt(10))*C.i/10 + (sqrt(10))*C.j/10, (-sqrt(10))*C.i/10 + (3*sqrt(10))*C.j/10] raises(ValueError, lambda: orthogonalize(v1, v2, v3)) raises(ValueError, lambda: orthogonalize(v6, v7))
def test_func_args(): A = CoordSysCartesian('A') assert A.x.func(*A.x.args) == A.x expr = 3 * A.x + 4 * A.y assert expr.func(*expr.args) == expr assert A.i.func(*A.i.args) == A.i v = A.x * A.i + A.y * A.j + A.z * A.k assert v.func(*v.args) == v assert A.origin.func(*A.origin.args) == A.origin
def test_orient_new_methods(): N = CoordSysCartesian('N') orienter1 = AxisOrienter(q4, N.j) orienter2 = SpaceOrienter(q1, q2, q3, '123') orienter3 = QuaternionOrienter(q1, q2, q3, q4) orienter4 = BodyOrienter(q1, q2, q3, '123') D = N.orient_new('D', (orienter1, )) E = N.orient_new('E', (orienter2, )) F = N.orient_new('F', (orienter3, )) G = N.orient_new('G', (orienter4, )) assert D == N.orient_new_axis('D', q4, N.j) assert E == N.orient_new_space('E', q1, q2, q3, '123') assert F == N.orient_new_quaternion('F', q1, q2, q3, q4) assert G == N.orient_new_body('G', q1, q2, q3, '123')
def test_rotation_matrix(): N = CoordSysCartesian('N') A = N.orient_new_axis('A', q1, N.k) B = A.orient_new_axis('B', q2, A.i) C = B.orient_new_axis('C', q3, B.j) D = N.orient_new_axis('D', q4, N.j) E = N.orient_new_space('E', q1, q2, q3, '123') F = N.orient_new_quaternion('F', q1, q2, q3, q4) G = N.orient_new_body('G', q1, q2, q3, '123') assert N.rotation_matrix(C) == Matrix([ [- sin(q1) * sin(q2) * sin(q3) + cos(q1) * cos(q3), - sin(q1) * cos(q2), sin(q1) * sin(q2) * cos(q3) + sin(q3) * cos(q1)], \ [sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), \ cos(q1) * cos(q2), sin(q1) * sin(q3) - sin(q2) * cos(q1) * \ cos(q3)], [- sin(q3) * cos(q2), sin(q2), cos(q2) * cos(q3)]]) test_mat = D.rotation_matrix(C) - Matrix( [[cos(q1) * cos(q3) * cos(q4) - sin(q3) * (- sin(q4) * cos(q2) + sin(q1) * sin(q2) * cos(q4)), - sin(q2) * sin(q4) - sin(q1) * cos(q2) * cos(q4), sin(q3) * cos(q1) * cos(q4) + cos(q3) * \ (- sin(q4) * cos(q2) + sin(q1) * sin(q2) * cos(q4))], \ [sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), cos(q1) * \ cos(q2), sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3)], \ [sin(q4) * cos(q1) * cos(q3) - sin(q3) * (cos(q2) * cos(q4) + \ sin(q1) * sin(q2) * \ sin(q4)), sin(q2) * cos(q4) - sin(q1) * sin(q4) * cos(q2), sin(q3) * \ sin(q4) * cos(q1) + cos(q3) * (cos(q2) * cos(q4) + \ sin(q1) * sin(q2) * sin(q4))]]) assert test_mat.expand() == zeros(3, 3) assert E.rotation_matrix(N) == Matrix( [[cos(q2)*cos(q3), sin(q3)*cos(q2), -sin(q2)], [sin(q1)*sin(q2)*cos(q3) - sin(q3)*cos(q1), \ sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3), sin(q1)*cos(q2)], \ [sin(q1)*sin(q3) + sin(q2)*cos(q1)*cos(q3), - \ sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1), cos(q1)*cos(q2)]]) assert F.rotation_matrix(N) == Matrix([[ q1**2 + q2**2 - q3**2 - q4**2, 2*q1*q4 + 2*q2*q3, -2*q1*q3 + 2*q2*q4],[ -2*q1*q4 + 2*q2*q3, q1**2 - q2**2 + q3**2 - q4**2, 2*q1*q2 + 2*q3*q4], [2*q1*q3 + 2*q2*q4, -2*q1*q2 + 2*q3*q4, q1**2 - q2**2 - q3**2 + q4**2]]) assert G.rotation_matrix(N) == Matrix([[ cos(q2)*cos(q3), sin(q1)*sin(q2)*cos(q3) + sin(q3)*cos(q1), sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3)], [ -sin(q3)*cos(q2), -sin(q1)*sin(q2)*sin(q3) + cos(q1)*cos(q3), sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1)],[ sin(q2), -sin(q1)*cos(q2), cos(q1)*cos(q2)]])
def test_orient_new_methods(): N = CoordSysCartesian("N") orienter1 = AxisOrienter(q4, N.j) orienter2 = SpaceOrienter(q1, q2, q3, "123") orienter3 = QuaternionOrienter(q1, q2, q3, q4) orienter4 = BodyOrienter(q1, q2, q3, "123") D = N.orient_new("D", (orienter1,)) E = N.orient_new("E", (orienter2,)) F = N.orient_new("F", (orienter3,)) G = N.orient_new("G", (orienter4,)) assert D == N.orient_new_axis("D", q4, N.j) assert E == N.orient_new_space("E", q1, q2, q3, "123") assert F == N.orient_new_quaternion("F", q1, q2, q3, q4) assert G == N.orient_new_body("G", q1, q2, q3, "123")
from sympy.vector.vector import Vector from sympy.vector.coordsysrect import CoordSysCartesian from sympy.vector.functions import express, matrix_to_vector from sympy import symbols, S, sin, cos, ImmutableMatrix as Matrix N = CoordSysCartesian('N') q1, q2, q3, q4, q5 = symbols('q1 q2 q3 q4 q5') A = N.orient_new_axis('A', q1, N.k) B = A.orient_new_axis('B', q2, A.i) C = B.orient_new_axis('C', q3, B.j) def test_express(): assert express(Vector.zero, N) == Vector.zero assert express(S(0), N) == S(0) assert express(A.i, C) == cos(q3) * C.i + sin(q3) * C.k assert express(A.j, C) == sin(q2)*sin(q3)*C.i + cos(q2)*C.j - \ sin(q2)*cos(q3)*C.k assert express(A.k, C) == -sin(q3)*cos(q2)*C.i + sin(q2)*C.j + \ cos(q2)*cos(q3)*C.k assert express(A.i, N) == cos(q1) * N.i + sin(q1) * N.j assert express(A.j, N) == -sin(q1) * N.i + cos(q1) * N.j assert express(A.k, N) == N.k assert express(A.i, A) == A.i assert express(A.j, A) == A.j assert express(A.k, A) == A.k assert express(A.i, B) == B.i assert express(A.j, B) == cos(q2) * B.j - sin(q2) * B.k assert express(A.k, B) == sin(q2) * B.j + cos(q2) * B.k assert express(A.i, C) == cos(q3) * C.i + sin(q3) * C.k assert express(A.j, C) == sin(q2)*sin(q3)*C.i + cos(q2)*C.j - \
from sympy.core.function import Derivative from sympy.vector.vector import Vector from sympy.vector.coordsysrect import CoordSysCartesian from sympy.simplify import simplify from sympy.core.symbol import symbols from sympy.core import S from sympy import sin, cos from sympy.vector.functions import (curl, divergence, gradient, is_conservative, is_solenoidal, scalar_potential, scalar_potential_difference) from sympy.utilities.pytest import raises C = CoordSysCartesian('C') i, j, k = C.base_vectors() x, y, z = C.base_scalars() delop = C.delop a, b, c, q = symbols('a b c q') def test_del_operator(): #Tests for curl assert (delop ^ Vector.zero == (Derivative(0, C.y) - Derivative(0, C.z)) * C.i + (-Derivative(0, C.x) + Derivative(0, C.z)) * C.j + (Derivative(0, C.x) - Derivative(0, C.y)) * C.k) assert ((delop ^ Vector.zero).doit() == Vector.zero == curl( Vector.zero, C)) assert delop.cross(Vector.zero) == delop ^ Vector.zero assert (delop ^ i).doit() == Vector.zero
def test_coordinate_vars(): """ Tests the coordinate variables functionality with respect to reorientation of coordinate systems. """ A = CoordSysCartesian("A") assert BaseScalar("Ax", 0, A, " ", " ") == A.x assert BaseScalar("Ay", 1, A, " ", " ") == A.y assert BaseScalar("Az", 2, A, " ", " ") == A.z assert BaseScalar("Ax", 0, A, " ", " ").__hash__() == A.x.__hash__() assert isinstance(A.x, BaseScalar) and isinstance(A.y, BaseScalar) and isinstance(A.z, BaseScalar) assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z} assert A.x.system == A B = A.orient_new_axis("B", q, A.k) assert B.scalar_map(A) == {B.z: A.z, B.y: -A.x * sin(q) + A.y * cos(q), B.x: A.x * cos(q) + A.y * sin(q)} assert A.scalar_map(B) == {A.x: B.x * cos(q) - B.y * sin(q), A.y: B.x * sin(q) + B.y * cos(q), A.z: B.z} assert express(B.x, A, variables=True) == A.x * cos(q) + A.y * sin(q) assert express(B.y, A, variables=True) == -A.x * sin(q) + A.y * cos(q) assert express(B.z, A, variables=True) == A.z assert express(B.x * B.y * B.z, A, variables=True) == A.z * (-A.x * sin(q) + A.y * cos(q)) * ( A.x * cos(q) + A.y * sin(q) ) assert ( express(B.x * B.i + B.y * B.j + B.z * B.k, A) == (B.x * cos(q) - B.y * sin(q)) * A.i + (B.x * sin(q) + B.y * cos(q)) * A.j + B.z * A.k ) assert simplify(express(B.x * B.i + B.y * B.j + B.z * B.k, A, variables=True)) == A.x * A.i + A.y * A.j + A.z * A.k assert ( express(A.x * A.i + A.y * A.j + A.z * A.k, B) == (A.x * cos(q) + A.y * sin(q)) * B.i + (-A.x * sin(q) + A.y * cos(q)) * B.j + A.z * B.k ) assert simplify(express(A.x * A.i + A.y * A.j + A.z * A.k, B, variables=True)) == B.x * B.i + B.y * B.j + B.z * B.k N = B.orient_new_axis("N", -q, B.k) assert N.scalar_map(A) == {N.x: A.x, N.z: A.z, N.y: A.y} C = A.orient_new_axis("C", q, A.i + A.j + A.k) mapping = A.scalar_map(C) assert ( mapping[A.x] == 2 * C.x * cos(q) / 3 + C.x / 3 - 2 * C.y * sin(q + pi / 6) / 3 + C.y / 3 - 2 * C.z * cos(q + pi / 3) / 3 + C.z / 3 ) assert ( mapping[A.y] == -2 * C.x * cos(q + pi / 3) / 3 + C.x / 3 + 2 * C.y * cos(q) / 3 + C.y / 3 - 2 * C.z * sin(q + pi / 6) / 3 + C.z / 3 ) assert ( mapping[A.z] == -2 * C.x * sin(q + pi / 6) / 3 + C.x / 3 - 2 * C.y * cos(q + pi / 3) / 3 + C.y / 3 + 2 * C.z * cos(q) / 3 + C.z / 3 ) D = A.locate_new("D", a * A.i + b * A.j + c * A.k) assert D.scalar_map(A) == {D.z: A.z - c, D.x: A.x - a, D.y: A.y - b} E = A.orient_new_axis("E", a, A.k, a * A.i + b * A.j + c * A.k) assert A.scalar_map(E) == {A.z: E.z + c, A.x: E.x * cos(a) - E.y * sin(a) + a, A.y: E.x * sin(a) + E.y * cos(a) + b} assert E.scalar_map(A) == { E.x: (A.x - a) * cos(a) + (A.y - b) * sin(a), E.y: (-A.x + a) * sin(a) + (A.y - b) * cos(a), E.z: A.z - c, } F = A.locate_new("F", Vector.zero) assert A.scalar_map(F) == {A.z: F.z, A.x: F.x, A.y: F.y}
def test_transformation_equations(): from sympy import symbols x, y, z = symbols('x y z') a = CoordSysCartesian('a') # Str a._connect_to_standard_cartesian('spherical') assert a._transformation_equations() == (a.x * sin(a.y) * cos(a.z), a.x * sin(a.y) * sin(a.z), a.x * cos(a.y)) assert a.lame_coefficients() == (1, a.x, a.x * sin(a.y)) a._connect_to_standard_cartesian('cylindrical') assert a._transformation_equations() == (a.x * cos(a.y), a.x * sin(a.y), a.z) assert a.lame_coefficients() == (1, a.y, 1) a._connect_to_standard_cartesian('cartesian') assert a._transformation_equations() == (a.x, a.y, a.z) assert a.lame_coefficients() == (1, 1, 1) # Variables and expressions a._connect_to_standard_cartesian(((x, y, z), (x, y, z))) assert a._transformation_equations() == (a.x, a.y, a.z) assert a.lame_coefficients() == (1, 1, 1) a._connect_to_standard_cartesian( ((x, y, z), ((x * cos(y), x * sin(y), z)))) assert a._transformation_equations() == (a.x * cos(a.y), a.x * sin(a.y), a.z) assert simplify(a.lame_coefficients()) == (1, sqrt(a.x**2), 1) a._connect_to_standard_cartesian( ((x, y, z), (x * sin(y) * cos(z), x * sin(y) * sin(z), x * cos(y)))) assert a._transformation_equations() == (a.x * sin(a.y) * cos(a.z), a.x * sin(a.y) * sin(a.z), a.x * cos(a.y)) assert simplify(a.lame_coefficients()) == (1, sqrt(a.x**2), sqrt(sin(a.y)**2 * a.x**2)) # Equations a._connect_to_standard_cartesian( (a.x * sin(a.y) * cos(a.z), a.x * sin(a.y) * sin(a.z), a.x * cos(a.y))) assert a._transformation_equations() == (a.x * sin(a.y) * cos(a.z), a.x * sin(a.y) * sin(a.z), a.x * cos(a.y)) assert simplify(a.lame_coefficients()) == (1, sqrt(a.x**2), sqrt(sin(a.y)**2 * a.x**2)) a._connect_to_standard_cartesian((a.x, a.y, a.z)) assert a._transformation_equations() == (a.x, a.y, a.z) assert simplify(a.lame_coefficients()) == (1, 1, 1) a._connect_to_standard_cartesian((a.x * cos(a.y), a.x * sin(a.y), a.z)) assert a._transformation_equations() == (a.x * cos(a.y), a.x * sin(a.y), a.z) assert simplify(a.lame_coefficients()) == (1, sqrt(a.x**2), 1)
from sympy.vector.vector import Vector from sympy.vector.coordsysrect import CoordSysCartesian from sympy.vector.functions import express, matrix_to_vector from sympy import symbols, S, sin, cos, ImmutableMatrix as Matrix N = CoordSysCartesian('N') q1, q2, q3, q4, q5 = symbols('q1 q2 q3 q4 q5') A = N.orient_new('A', 'Axis', [q1, N.k]) B = A.orient_new('B', 'Axis', [q2, A.i]) C = B.orient_new('C', 'Axis', [q3, B.j]) def test_express(): assert express(Vector.zero, N) == Vector.zero assert express(S(0), N) == S(0) assert express(A.i, C) == cos(q3) * C.i + sin(q3) * C.k assert express(A.j, C) == sin(q2)*sin(q3)*C.i + cos(q2)*C.j - \ sin(q2)*cos(q3)*C.k assert express(A.k, C) == -sin(q3)*cos(q2)*C.i + sin(q2)*C.j + \ cos(q2)*cos(q3)*C.k assert express(A.i, N) == cos(q1) * N.i + sin(q1) * N.j assert express(A.j, N) == -sin(q1) * N.i + cos(q1) * N.j assert express(A.k, N) == N.k assert express(A.i, A) == A.i assert express(A.j, A) == A.j assert express(A.k, A) == A.k assert express(A.i, B) == B.i assert express(A.j, B) == cos(q2) * B.j - sin(q2) * B.k assert express(A.k, B) == sin(q2) * B.j + cos(q2) * B.k assert express(A.i, C) == cos(q3) * C.i + sin(q3) * C.k assert express(A.j, C) == sin(q2)*sin(q3)*C.i + cos(q2)*C.j - \
def test_coordinate_vars(): """ Tests the coordinate variables functionality with respect to reorientation of coordinate systems. """ A = CoordSysCartesian('A') assert BaseScalar('Ax', 0, A, ' ', ' ') == A.x assert BaseScalar('Ay', 1, A, ' ', ' ') == A.y assert BaseScalar('Az', 2, A, ' ', ' ') == A.z assert BaseScalar('Ax', 0, A, ' ', ' ').__hash__() == A.x.__hash__() assert isinstance(A.x, BaseScalar) and \ isinstance(A.y, BaseScalar) and \ isinstance(A.z, BaseScalar) assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z} assert A.x.system == A B = A.orient_new_axis('B', q, A.k) assert B.scalar_map(A) == { B.z: A.z, B.y: -A.x * sin(q) + A.y * cos(q), B.x: A.x * cos(q) + A.y * sin(q) } assert A.scalar_map(B) == { A.x: B.x * cos(q) - B.y * sin(q), A.y: B.x * sin(q) + B.y * cos(q), A.z: B.z } assert express(B.x, A, variables=True) == A.x * cos(q) + A.y * sin(q) assert express(B.y, A, variables=True) == -A.x * sin(q) + A.y * cos(q) assert express(B.z, A, variables=True) == A.z assert express(B.x*B.y*B.z, A, variables=True) == \ A.z*(-A.x*sin(q) + A.y*cos(q))*(A.x*cos(q) + A.y*sin(q)) assert express(B.x*B.i + B.y*B.j + B.z*B.k, A) == \ (B.x*cos(q) - B.y*sin(q))*A.i + (B.x*sin(q) + \ B.y*cos(q))*A.j + B.z*A.k assert simplify(express(B.x*B.i + B.y*B.j + B.z*B.k, A, \ variables=True)) == \ A.x*A.i + A.y*A.j + A.z*A.k assert express(A.x*A.i + A.y*A.j + A.z*A.k, B) == \ (A.x*cos(q) + A.y*sin(q))*B.i + \ (-A.x*sin(q) + A.y*cos(q))*B.j + A.z*B.k assert simplify(express(A.x*A.i + A.y*A.j + A.z*A.k, B, \ variables=True)) == \ B.x*B.i + B.y*B.j + B.z*B.k N = B.orient_new_axis('N', -q, B.k) assert N.scalar_map(A) == \ {N.x: A.x, N.z: A.z, N.y: A.y} C = A.orient_new_axis('C', q, A.i + A.j + A.k) mapping = A.scalar_map(C) assert mapping[A.x] == 2*C.x*cos(q)/3 + C.x/3 - \ 2*C.y*sin(q + pi/6)/3 + C.y/3 - 2*C.z*cos(q + pi/3)/3 + C.z/3 assert mapping[A.y] == -2*C.x*cos(q + pi/3)/3 + \ C.x/3 + 2*C.y*cos(q)/3 + C.y/3 - 2*C.z*sin(q + pi/6)/3 + C.z/3 assert mapping[A.z] == -2*C.x*sin(q + pi/6)/3 + C.x/3 - \ 2*C.y*cos(q + pi/3)/3 + C.y/3 + 2*C.z*cos(q)/3 + C.z/3 D = A.locate_new('D', a * A.i + b * A.j + c * A.k) assert D.scalar_map(A) == {D.z: A.z - c, D.x: A.x - a, D.y: A.y - b} E = A.orient_new_axis('E', a, A.k, a * A.i + b * A.j + c * A.k) assert A.scalar_map(E) == { A.z: E.z + c, A.x: E.x * cos(a) - E.y * sin(a) + a, A.y: E.x * sin(a) + E.y * cos(a) + b } assert E.scalar_map(A) == { E.x: (A.x - a) * cos(a) + (A.y - b) * sin(a), E.y: (-A.x + a) * sin(a) + (A.y - b) * cos(a), E.z: A.z - c } F = A.locate_new('F', Vector.zero) assert A.scalar_map(F) == {A.z: F.z, A.x: F.x, A.y: F.y}
from sympy.vector.coordsysrect import CoordSysCartesian from sympy.vector.scalar import BaseScalar from sympy import Symbol, sin, cos, pi, ImmutableMatrix as Matrix, \ symbols, simplify, sqrt, zeros from sympy.vector.functions import express from sympy.vector.point import Point from sympy.vector.vector import Vector from sympy.vector.orienters import (AxisOrienter, BodyOrienter, SpaceOrienter, QuaternionOrienter) A = CoordSysCartesian('A') a, b, c, q = symbols('a b c q') q1, q2, q3, q4 = symbols('q1 q2 q3 q4') def test_coordsyscartesian_equivalence(): A1 = CoordSysCartesian('A') assert A1 == A B = CoordSysCartesian('B') assert A != B assert A.locate_new('C1', A.i) == A.locate_new('C2', A.i) assert A.orient_new_axis('C1', a, A.i) == \ A.orient_new_axis('C2', a, A.i) def test_orienters(): axis_orienter = AxisOrienter(a, A.k) body_orienter = BodyOrienter(a, b, c, '123') space_orienter = SpaceOrienter(a, b, c, '123') q_orienter = QuaternionOrienter(q1, q2, q3, q4) assert axis_orienter.rotation_matrix(A) == Matrix([[cos(a),
from sympy.vector.vector import Vector from sympy.vector.coordsysrect import CoordSysCartesian from sympy.simplify import simplify from sympy.core.symbol import symbols from sympy.core import S from sympy import sin, cos C = CoordSysCartesian('C') i, j, k = C.base_vectors() x, y, z = C.base_scalars() delop = C.delop a, b, c = symbols('a b c') def test_del_operator(): #Tests for curl assert delop ^ Vector.zero == Vector.zero assert delop.cross(Vector.zero) == Vector.zero assert delop ^ i == Vector.zero assert delop.cross(2 * y**2 * j) == Vector.zero v = x * y * z * (i + j + k) assert delop ^ v == \ (-x*y + x*z)*i + (x*y - y*z)*j + (-x*z + y*z)*k assert delop ^ v == delop.cross(v) assert delop.cross(2 * x**2 * j) == 4 * x * k #Tests for divergence assert delop & Vector.zero == S(0) assert delop.dot(Vector.zero) == S(0) assert delop & i == S(0)
from sympy.vector.vector import Vector from sympy.vector.coordsysrect import CoordSysCartesian from sympy.vector.functions import express, matrix_to_vector, orthogonalize from sympy import symbols, S, sqrt, sin, cos, ImmutableMatrix as Matrix from sympy.utilities.pytest import raises N = CoordSysCartesian('N') q1, q2, q3, q4, q5 = symbols('q1 q2 q3 q4 q5') A = N.orient_new_axis('A', q1, N.k) B = A.orient_new_axis('B', q2, A.i) C = B.orient_new_axis('C', q3, B.j) def test_express(): assert express(Vector.zero, N) == Vector.zero assert express(S(0), N) == S(0) assert express(A.i, C) == cos(q3)*C.i + sin(q3)*C.k assert express(A.j, C) == sin(q2)*sin(q3)*C.i + cos(q2)*C.j - \ sin(q2)*cos(q3)*C.k assert express(A.k, C) == -sin(q3)*cos(q2)*C.i + sin(q2)*C.j + \ cos(q2)*cos(q3)*C.k assert express(A.i, N) == cos(q1)*N.i + sin(q1)*N.j assert express(A.j, N) == -sin(q1)*N.i + cos(q1)*N.j assert express(A.k, N) == N.k assert express(A.i, A) == A.i assert express(A.j, A) == A.j assert express(A.k, A) == A.k assert express(A.i, B) == B.i assert express(A.j, B) == cos(q2)*B.j - sin(q2)*B.k assert express(A.k, B) == sin(q2)*B.j + cos(q2)*B.k assert express(A.i, C) == cos(q3)*C.i + sin(q3)*C.k
def test_coordinate_vars(): """ Tests the coordinate variables functionality with respect to reorientation of coordinate systems. """ A = CoordSysCartesian('A') # Note that the name given on the lhs is different from A.x._name assert BaseScalar('A.x', 0, A, 'A_x', r'\mathbf{{x}_{A}}') == A.x assert BaseScalar('A.y', 1, A, 'A_y', r'\mathbf{{y}_{A}}') == A.y assert BaseScalar('A.z', 2, A, 'A_z', r'\mathbf{{z}_{A}}') == A.z assert BaseScalar('A.x', 0, A, 'A_x', r'\mathbf{{x}_{A}}').__hash__() == A.x.__hash__() assert isinstance(A.x, BaseScalar) and \ isinstance(A.y, BaseScalar) and \ isinstance(A.z, BaseScalar) assert A.x*A.y == A.y*A.x assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z} assert A.x.system == A assert A.x.diff(A.x) == 1 B = A.orient_new_axis('B', q, A.k) assert B.scalar_map(A) == {B.z: A.z, B.y: -A.x*sin(q) + A.y*cos(q), B.x: A.x*cos(q) + A.y*sin(q)} assert A.scalar_map(B) == {A.x: B.x*cos(q) - B.y*sin(q), A.y: B.x*sin(q) + B.y*cos(q), A.z: B.z} assert express(B.x, A, variables=True) == A.x*cos(q) + A.y*sin(q) assert express(B.y, A, variables=True) == -A.x*sin(q) + A.y*cos(q) assert express(B.z, A, variables=True) == A.z assert expand(express(B.x*B.y*B.z, A, variables=True)) == \ expand(A.z*(-A.x*sin(q) + A.y*cos(q))*(A.x*cos(q) + A.y*sin(q))) assert express(B.x*B.i + B.y*B.j + B.z*B.k, A) == \ (B.x*cos(q) - B.y*sin(q))*A.i + (B.x*sin(q) + \ B.y*cos(q))*A.j + B.z*A.k assert simplify(express(B.x*B.i + B.y*B.j + B.z*B.k, A, \ variables=True)) == \ A.x*A.i + A.y*A.j + A.z*A.k assert express(A.x*A.i + A.y*A.j + A.z*A.k, B) == \ (A.x*cos(q) + A.y*sin(q))*B.i + \ (-A.x*sin(q) + A.y*cos(q))*B.j + A.z*B.k assert simplify(express(A.x*A.i + A.y*A.j + A.z*A.k, B, \ variables=True)) == \ B.x*B.i + B.y*B.j + B.z*B.k N = B.orient_new_axis('N', -q, B.k) assert N.scalar_map(A) == \ {N.x: A.x, N.z: A.z, N.y: A.y} C = A.orient_new_axis('C', q, A.i + A.j + A.k) mapping = A.scalar_map(C) assert mapping[A.x] == (C.x*(2*cos(q) + 1)/3 + C.y*(-2*sin(q + pi/6) + 1)/3 + C.z*(-2*cos(q + pi/3) + 1)/3) assert mapping[A.y] == (C.x*(-2*cos(q + pi/3) + 1)/3 + C.y*(2*cos(q) + 1)/3 + C.z*(-2*sin(q + pi/6) + 1)/3) assert mapping[A.z] == (C.x*(-2*sin(q + pi/6) + 1)/3 + C.y*(-2*cos(q + pi/3) + 1)/3 + C.z*(2*cos(q) + 1)/3) D = A.locate_new('D', a*A.i + b*A.j + c*A.k) assert D.scalar_map(A) == {D.z: A.z - c, D.x: A.x - a, D.y: A.y - b} E = A.orient_new_axis('E', a, A.k, a*A.i + b*A.j + c*A.k) assert A.scalar_map(E) == {A.z: E.z + c, A.x: E.x*cos(a) - E.y*sin(a) + a, A.y: E.x*sin(a) + E.y*cos(a) + b} assert E.scalar_map(A) == {E.x: (A.x - a)*cos(a) + (A.y - b)*sin(a), E.y: (-A.x + a)*sin(a) + (A.y - b)*cos(a), E.z: A.z - c} F = A.locate_new('F', Vector.zero) assert A.scalar_map(F) == {A.z: F.z, A.x: F.x, A.y: F.y}
def test_coordsyscartesian_equivalence(): A = CoordSysCartesian('A') A1 = CoordSysCartesian('A') assert A1 == A B = CoordSysCartesian('B') assert A != B
def test_vector(): """ Tests the effects of orientation of coordinate systems on basic vector operations. """ N = CoordSysCartesian('N') A = N.orient_new_axis('A', q1, N.k) B = A.orient_new_axis('B', q2, A.i) C = B.orient_new_axis('C', q3, B.j) #Test to_matrix v1 = a * N.i + b * N.j + c * N.k assert v1.to_matrix(A) == Matrix([[a * cos(q1) + b * sin(q1)], [-a * sin(q1) + b * cos(q1)], [c]]) #Test dot assert N.i.dot(A.i) == cos(q1) assert N.i.dot(A.j) == -sin(q1) assert N.i.dot(A.k) == 0 assert N.j.dot(A.i) == sin(q1) assert N.j.dot(A.j) == cos(q1) assert N.j.dot(A.k) == 0 assert N.k.dot(A.i) == 0 assert N.k.dot(A.j) == 0 assert N.k.dot(A.k) == 1 assert N.i.dot(A.i + A.j) == -sin(q1) + cos(q1) == \ (A.i + A.j).dot(N.i) assert A.i.dot(C.i) == cos(q3) assert A.i.dot(C.j) == 0 assert A.i.dot(C.k) == sin(q3) assert A.j.dot(C.i) == sin(q2) * sin(q3) assert A.j.dot(C.j) == cos(q2) assert A.j.dot(C.k) == -sin(q2) * cos(q3) assert A.k.dot(C.i) == -cos(q2) * sin(q3) assert A.k.dot(C.j) == sin(q2) assert A.k.dot(C.k) == cos(q2) * cos(q3) #Test cross assert N.i.cross(A.i) == sin(q1) * A.k assert N.i.cross(A.j) == cos(q1) * A.k assert N.i.cross(A.k) == -sin(q1) * A.i - cos(q1) * A.j assert N.j.cross(A.i) == -cos(q1) * A.k assert N.j.cross(A.j) == sin(q1) * A.k assert N.j.cross(A.k) == cos(q1) * A.i - sin(q1) * A.j assert N.k.cross(A.i) == A.j assert N.k.cross(A.j) == -A.i assert N.k.cross(A.k) == Vector.zero assert N.i.cross(A.i) == sin(q1) * A.k assert N.i.cross(A.j) == cos(q1) * A.k assert N.i.cross(A.i + A.j) == sin(q1) * A.k + cos(q1) * A.k assert (A.i + A.j).cross(N.i) == (-sin(q1) - cos(q1)) * N.k assert A.i.cross(C.i) == sin(q3) * C.j assert A.i.cross(C.j) == -sin(q3) * C.i + cos(q3) * C.k assert A.i.cross(C.k) == -cos(q3) * C.j assert C.i.cross(A.i) == (-sin(q3)*cos(q2))*A.j + \ (-sin(q2)*sin(q3))*A.k assert C.j.cross(A.i) == (sin(q2)) * A.j + (-cos(q2)) * A.k assert express(C.k.cross(A.i), C).trigsimp() == cos(q3) * C.j
def test_coordsys3d(): with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=SymPyDeprecationWarning) assert CoordSysCartesian("C") == CoordSys3D("C")
def test_coordinate_vars(): """ Tests the coordinate variables functionality with respect to reorientation of coordinate systems. """ A = CoordSysCartesian('A') # Note that the name given on the lhs is different from A.x._name assert BaseScalar('A.x', 0, A, 'A_x', r'\mathbf{{x}_{A}}') == A.x assert BaseScalar('A.y', 1, A, 'A_y', r'\mathbf{{y}_{A}}') == A.y assert BaseScalar('A.z', 2, A, 'A_z', r'\mathbf{{z}_{A}}') == A.z assert BaseScalar('A.x', 0, A, 'A_x', r'\mathbf{{x}_{A}}').__hash__() == A.x.__hash__() assert isinstance(A.x, BaseScalar) and \ isinstance(A.y, BaseScalar) and \ isinstance(A.z, BaseScalar) assert A.x * A.y == A.y * A.x assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z} assert A.x.system == A assert A.x.diff(A.x) == 1 B = A.orient_new_axis('B', q, A.k) assert B.scalar_map(A) == { B.z: A.z, B.y: -A.x * sin(q) + A.y * cos(q), B.x: A.x * cos(q) + A.y * sin(q) } assert A.scalar_map(B) == { A.x: B.x * cos(q) - B.y * sin(q), A.y: B.x * sin(q) + B.y * cos(q), A.z: B.z } assert express(B.x, A, variables=True) == A.x * cos(q) + A.y * sin(q) assert express(B.y, A, variables=True) == -A.x * sin(q) + A.y * cos(q) assert express(B.z, A, variables=True) == A.z assert expand(express(B.x*B.y*B.z, A, variables=True)) == \ expand(A.z*(-A.x*sin(q) + A.y*cos(q))*(A.x*cos(q) + A.y*sin(q))) assert express(B.x*B.i + B.y*B.j + B.z*B.k, A) == \ (B.x*cos(q) - B.y*sin(q))*A.i + (B.x*sin(q) + \ B.y*cos(q))*A.j + B.z*A.k assert simplify(express(B.x*B.i + B.y*B.j + B.z*B.k, A, \ variables=True)) == \ A.x*A.i + A.y*A.j + A.z*A.k assert express(A.x*A.i + A.y*A.j + A.z*A.k, B) == \ (A.x*cos(q) + A.y*sin(q))*B.i + \ (-A.x*sin(q) + A.y*cos(q))*B.j + A.z*B.k assert simplify(express(A.x*A.i + A.y*A.j + A.z*A.k, B, \ variables=True)) == \ B.x*B.i + B.y*B.j + B.z*B.k N = B.orient_new_axis('N', -q, B.k) assert N.scalar_map(A) == \ {N.x: A.x, N.z: A.z, N.y: A.y} C = A.orient_new_axis('C', q, A.i + A.j + A.k) mapping = A.scalar_map(C) assert mapping[A.x].equals(C.x * (2 * cos(q) + 1) / 3 + C.y * (-2 * sin(q + pi / 6) + 1) / 3 + C.z * (-2 * cos(q + pi / 3) + 1) / 3) assert mapping[A.y].equals(C.x * (-2 * cos(q + pi / 3) + 1) / 3 + C.y * (2 * cos(q) + 1) / 3 + C.z * (-2 * sin(q + pi / 6) + 1) / 3) assert mapping[A.z].equals(C.x * (-2 * sin(q + pi / 6) + 1) / 3 + C.y * (-2 * cos(q + pi / 3) + 1) / 3 + C.z * (2 * cos(q) + 1) / 3) D = A.locate_new('D', a * A.i + b * A.j + c * A.k) assert D.scalar_map(A) == {D.z: A.z - c, D.x: A.x - a, D.y: A.y - b} E = A.orient_new_axis('E', a, A.k, a * A.i + b * A.j + c * A.k) assert A.scalar_map(E) == { A.z: E.z + c, A.x: E.x * cos(a) - E.y * sin(a) + a, A.y: E.x * sin(a) + E.y * cos(a) + b } assert E.scalar_map(A) == { E.x: (A.x - a) * cos(a) + (A.y - b) * sin(a), E.y: (-A.x + a) * sin(a) + (A.y - b) * cos(a), E.z: A.z - c } F = A.locate_new('F', Vector.zero) assert A.scalar_map(F) == {A.z: F.z, A.x: F.x, A.y: F.y}
from sympy.simplify import simplify, trigsimp from sympy import pi, sqrt, symbols, ImmutableMatrix as Matrix, sin, cos from sympy.vector.vector import Vector, BaseVector, VectorAdd, VectorMul, VectorZero from sympy.vector.coordsysrect import CoordSysCartesian C = CoordSysCartesian("C") i, j, k = C.base_vectors() a, b, c = symbols("a b c") def test_vector_sympy(): """ Test whether the Vector framework confirms to the hashing and equality testing properties of SymPy. """ i1 = BaseVector("i1", 0, C) assert i1 == i assert i1.__hash__() == i.__hash__() v1 = 3 * j assert v1 == j * 3 assert v1.components == {j: 3} v2 = 3 * i + 4 * j + 5 * k v3 = 2 * i + 4 * j + i + 4 * k + k assert v3 == v2 assert v3.__hash__() == v2.__hash__() def test_vector(): assert isinstance(i, BaseVector) assert i != j
def test_evalf(): A = CoordSysCartesian('A') v = 3 * A.i + 4 * A.j + a * A.k assert v.n() == v.evalf() assert v.evalf(subs={a: 1}) == v.subs(a, 1).evalf()
from sympy.vector.vector import Vector from sympy.vector.coordsysrect import CoordSysCartesian from sympy.simplify import simplify from sympy.core.symbol import symbols from sympy.core import S from sympy import sin, cos C = CoordSysCartesian('C') i, j, k = C.base_vectors() x, y, z = C.base_scalars() delop = C.delop a, b, c = symbols('a b c') def test_del_operator(): #Tests for curl assert delop ^ Vector.zero == Vector.zero assert delop.cross(Vector.zero) == Vector.zero assert delop ^ i == Vector.zero assert delop.cross(2*y**2*j) == Vector.zero v = x*y*z * (i + j + k) assert delop ^ v == \ (-x*y + x*z)*i + (x*y - y*z)*j + (-x*z + y*z)*k assert delop ^ v == delop.cross(v) assert delop.cross(2*x**2*j) == 4*x*k #Tests for divergence assert delop & Vector.zero == S(0) assert delop.dot(Vector.zero) == S(0) assert delop & i == S(0) assert delop & x**2*i == 2*x
from sympy.simplify import simplify, trigsimp from sympy import pi, sqrt, symbols, ImmutableMatrix as Matrix, \ sin, cos, Function, Integral, Derivative, diff, integrate from sympy.vector.vector import Vector, BaseVector, VectorAdd, \ VectorMul, VectorZero from sympy.vector.coordsysrect import CoordSysCartesian C = CoordSysCartesian('C') i, j, k = C.base_vectors() a, b, c = symbols('a b c') def test_vector_sympy(): """ Test whether the Vector framework confirms to the hashing and equality testing properties of SymPy. """ v1 = 3 * j assert v1 == j * 3 assert v1.components == {j: 3} v2 = 3 * i + 4 * j + 5 * k v3 = 2 * i + 4 * j + i + 4 * k + k assert v3 == v2 assert v3.__hash__() == v2.__hash__() def test_vector(): assert isinstance(i, BaseVector) assert i != j assert j != k
def test_coordinate_vars(): """ Tests the coordinate variables functionality with respect to reorientation of coordinate systems. """ A = CoordSysCartesian('A') assert BaseScalar('Ax', 0, A, ' ', ' ') == A.x assert BaseScalar('Ay', 1, A, ' ', ' ') == A.y assert BaseScalar('Az', 2, A, ' ', ' ') == A.z assert BaseScalar('Ax', 0, A, ' ', ' ').__hash__() == A.x.__hash__() assert isinstance(A.x, BaseScalar) and \ isinstance(A.y, BaseScalar) and \ isinstance(A.z, BaseScalar) assert A.scalar_map(A) == {A.x: A.x, A.y: A.y, A.z: A.z} assert A.x.system == A B = A.orient_new_axis('B', q, A.k) assert B.scalar_map(A) == {B.z: A.z, B.y: -A.x*sin(q) + A.y*cos(q), B.x: A.x*cos(q) + A.y*sin(q)} assert A.scalar_map(B) == {A.x: B.x*cos(q) - B.y*sin(q), A.y: B.x*sin(q) + B.y*cos(q), A.z: B.z} assert express(B.x, A, variables=True) == A.x*cos(q) + A.y*sin(q) assert express(B.y, A, variables=True) == -A.x*sin(q) + A.y*cos(q) assert express(B.z, A, variables=True) == A.z assert express(B.x*B.y*B.z, A, variables=True) == \ A.z*(-A.x*sin(q) + A.y*cos(q))*(A.x*cos(q) + A.y*sin(q)) assert express(B.x*B.i + B.y*B.j + B.z*B.k, A) == \ (B.x*cos(q) - B.y*sin(q))*A.i + (B.x*sin(q) + \ B.y*cos(q))*A.j + B.z*A.k assert simplify(express(B.x*B.i + B.y*B.j + B.z*B.k, A, \ variables=True)) == \ A.x*A.i + A.y*A.j + A.z*A.k assert express(A.x*A.i + A.y*A.j + A.z*A.k, B) == \ (A.x*cos(q) + A.y*sin(q))*B.i + \ (-A.x*sin(q) + A.y*cos(q))*B.j + A.z*B.k assert simplify(express(A.x*A.i + A.y*A.j + A.z*A.k, B, \ variables=True)) == \ B.x*B.i + B.y*B.j + B.z*B.k N = B.orient_new_axis('N', -q, B.k) assert N.scalar_map(A) == \ {N.x: A.x, N.z: A.z, N.y: A.y} C = A.orient_new_axis('C', q, A.i + A.j + A.k) mapping = A.scalar_map(C) assert mapping[A.x] == 2*C.x*cos(q)/3 + C.x/3 - \ 2*C.y*sin(q + pi/6)/3 + C.y/3 - 2*C.z*cos(q + pi/3)/3 + C.z/3 assert mapping[A.y] == -2*C.x*cos(q + pi/3)/3 + \ C.x/3 + 2*C.y*cos(q)/3 + C.y/3 - 2*C.z*sin(q + pi/6)/3 + C.z/3 assert mapping[A.z] == -2*C.x*sin(q + pi/6)/3 + C.x/3 - \ 2*C.y*cos(q + pi/3)/3 + C.y/3 + 2*C.z*cos(q)/3 + C.z/3 D = A.locate_new('D', a*A.i + b*A.j + c*A.k) assert D.scalar_map(A) == {D.z: A.z - c, D.x: A.x - a, D.y: A.y - b} E = A.orient_new_axis('E', a, A.k, a*A.i + b*A.j + c*A.k) assert A.scalar_map(E) == {A.z: E.z + c, A.x: E.x*cos(a) - E.y*sin(a) + a, A.y: E.x*sin(a) + E.y*cos(a) + b} assert E.scalar_map(A) == {E.x: (A.x - a)*cos(a) + (A.y - b)*sin(a), E.y: (-A.x + a)*sin(a) + (A.y - b)*cos(a), E.z: A.z - c} F = A.locate_new('F', Vector.zero) assert A.scalar_map(F) == {A.z: F.z, A.x: F.x, A.y: F.y}
from sympy.core.function import Derivative from sympy.vector.vector import Vector from sympy.vector.coordsysrect import CoordSysCartesian from sympy.simplify import simplify from sympy.core.symbol import symbols from sympy.core import S from sympy import sin, cos from sympy.vector.functions import (curl, divergence, gradient, is_conservative, is_solenoidal, scalar_potential, scalar_potential_difference) from sympy.utilities.pytest import raises C = CoordSysCartesian('C') i, j, k = C.base_vectors() x, y, z = C.base_scalars() delop = C.delop a, b, c, q = symbols('a b c q') def test_del_operator(): #Tests for curl assert (delop ^ Vector.zero == (Derivative(0, C.y) - Derivative(0, C.z))*C.i + (-Derivative(0, C.x) + Derivative(0, C.z))*C.j + (Derivative(0, C.x) - Derivative(0, C.y))*C.k) assert ((delop ^ Vector.zero).doit() == Vector.zero == curl(Vector.zero, C)) assert delop.cross(Vector.zero) == delop ^ Vector.zero assert (delop ^ i).doit() == Vector.zero assert delop.cross(2*y**2*j, doit = True) == Vector.zero
def test_rotation_matrix(): N = CoordSysCartesian("N") A = N.orient_new_axis("A", q1, N.k) B = A.orient_new_axis("B", q2, A.i) C = B.orient_new_axis("C", q3, B.j) D = N.orient_new_axis("D", q4, N.j) E = N.orient_new_space("E", q1, q2, q3, "123") F = N.orient_new_quaternion("F", q1, q2, q3, q4) G = N.orient_new_body("G", q1, q2, q3, "123") assert N.rotation_matrix(C) == Matrix( [ [ -sin(q1) * sin(q2) * sin(q3) + cos(q1) * cos(q3), -sin(q1) * cos(q2), sin(q1) * sin(q2) * cos(q3) + sin(q3) * cos(q1), ], [ sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), cos(q1) * cos(q2), sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3), ], [-sin(q3) * cos(q2), sin(q2), cos(q2) * cos(q3)], ] ) test_mat = D.rotation_matrix(C) - Matrix( [ [ cos(q1) * cos(q3) * cos(q4) - sin(q3) * (-sin(q4) * cos(q2) + sin(q1) * sin(q2) * cos(q4)), -sin(q2) * sin(q4) - sin(q1) * cos(q2) * cos(q4), sin(q3) * cos(q1) * cos(q4) + cos(q3) * (-sin(q4) * cos(q2) + sin(q1) * sin(q2) * cos(q4)), ], [ sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), cos(q1) * cos(q2), sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3), ], [ sin(q4) * cos(q1) * cos(q3) - sin(q3) * (cos(q2) * cos(q4) + sin(q1) * sin(q2) * sin(q4)), sin(q2) * cos(q4) - sin(q1) * sin(q4) * cos(q2), sin(q3) * sin(q4) * cos(q1) + cos(q3) * (cos(q2) * cos(q4) + sin(q1) * sin(q2) * sin(q4)), ], ] ) assert test_mat.expand() == zeros(3, 3) assert E.rotation_matrix(N) == Matrix( [ [cos(q2) * cos(q3), sin(q3) * cos(q2), -sin(q2)], [ sin(q1) * sin(q2) * cos(q3) - sin(q3) * cos(q1), sin(q1) * sin(q2) * sin(q3) + cos(q1) * cos(q3), sin(q1) * cos(q2), ], [ sin(q1) * sin(q3) + sin(q2) * cos(q1) * cos(q3), -sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), cos(q1) * cos(q2), ], ] ) assert F.rotation_matrix(N) == Matrix( [ [q1 ** 2 + q2 ** 2 - q3 ** 2 - q4 ** 2, 2 * q1 * q4 + 2 * q2 * q3, -2 * q1 * q3 + 2 * q2 * q4], [-2 * q1 * q4 + 2 * q2 * q3, q1 ** 2 - q2 ** 2 + q3 ** 2 - q4 ** 2, 2 * q1 * q2 + 2 * q3 * q4], [2 * q1 * q3 + 2 * q2 * q4, -2 * q1 * q2 + 2 * q3 * q4, q1 ** 2 - q2 ** 2 - q3 ** 2 + q4 ** 2], ] ) assert G.rotation_matrix(N) == Matrix( [ [ cos(q2) * cos(q3), sin(q1) * sin(q2) * cos(q3) + sin(q3) * cos(q1), sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3), ], [ -sin(q3) * cos(q2), -sin(q1) * sin(q2) * sin(q3) + cos(q1) * cos(q3), sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1), ], [sin(q2), -sin(q1) * cos(q2), cos(q1) * cos(q2)], ] )
def test_coordsys3d(): with warns_deprecated_sympy(): assert CoordSysCartesian("C") == CoordSys3D("C")
def test_lame_coefficients(): a = CoordSysCartesian('a') a._set_lame_coefficient_mapping('spherical') assert a.lame_coefficients() == (1, a.x, sin(a.y) * a.x) a = CoordSysCartesian('a') assert a.lame_coefficients() == (1, 1, 1) a = CoordSysCartesian('a') a._set_lame_coefficient_mapping('cartesian') assert a.lame_coefficients() == (1, 1, 1) a = CoordSysCartesian('a') a._set_lame_coefficient_mapping('cylindrical') assert a.lame_coefficients() == (1, a.y, 1)