def test_mixed_coordinates(): # gradient a = CoordSys3D('a') b = CoordSys3D('b') c = CoordSys3D('c') assert gradient(a.x*b.y) == b.y*a.i + a.x*b.j assert gradient(3*cos(q)*a.x*b.x+a.y*(a.x+((cos(q)+b.x)))) ==\ (a.y + 3*b.x*cos(q))*a.i + (a.x + b.x + cos(q))*a.j + (3*a.x*cos(q) + a.y)*b.i # Some tests need further work: # assert gradient(a.x*(cos(a.x+b.x))) == (cos(a.x + b.x))*a.i + a.x*Gradient(cos(a.x + b.x)) # assert gradient(cos(a.x + b.x)*cos(a.x + b.z)) == Gradient(cos(a.x + b.x)*cos(a.x + b.z)) assert gradient(a.x**b.y) == Gradient(a.x**b.y) # assert gradient(cos(a.x+b.y)*a.z) == None assert gradient(cos(a.x*b.y)) == Gradient(cos(a.x*b.y)) assert gradient(3*cos(q)*a.x*b.x*a.z*a.y+ b.y*b.z + cos(a.x+a.y)*b.z) == \ (3*a.y*a.z*b.x*cos(q) - b.z*sin(a.x + a.y))*a.i + \ (3*a.x*a.z*b.x*cos(q) - b.z*sin(a.x + a.y))*a.j + (3*a.x*a.y*b.x*cos(q))*a.k + \ (3*a.x*a.y*a.z*cos(q))*b.i + b.z*b.j + (b.y + cos(a.x + a.y))*b.k # divergence assert divergence(a.i*a.x+a.j*a.y+a.z*a.k + b.i*b.x+b.j*b.y+b.z*b.k + c.i*c.x+c.j*c.y+c.z*c.k) == S(9) # assert divergence(3*a.i*a.x*cos(a.x+b.z) + a.j*b.x*c.z) == None assert divergence(3*a.i*a.x*a.z + b.j*b.x*c.z + 3*a.j*a.z*a.y) == \ 6*a.z + Dot(b.j, c.z*b.i + b.x*c.k) assert divergence(3*cos(q)*a.x*b.x*b.i*c.x) == \ Dot(b.i, (3*b.x*c.x*cos(q))*a.i + (3*a.x*c.x*cos(q))*b.i + (3*a.x*b.x*cos(q))*c.i) assert divergence(a.x*b.x*c.x*Cross(a.x*a.i, a.y*b.j)) ==\ a.x*b.x*c.x*Divergence(Cross(a.x*a.i, a.y*b.j)) + \ Dot(Cross(a.x*a.i, a.y*b.j), b.x*c.x*a.i + a.x*c.x*b.i + a.x*b.x*c.i) assert divergence(a.x*b.x*c.x*(a.x*a.i + b.x*b.i)) ==\ Dot(a.i, 2*a.x*b.x*c.x*a.i + a.x**2*c.x*b.i + a.x**2*b.x*c.i) + \ Dot(b.i, b.x**2*c.x*a.i + 2*a.x*b.x*c.x*b.i + a.x*b.x**2*c.i)
def is_solenoidal(field): """ Checks if a field is solenoidal. Paramaters ========== field : Vector The field to check for solenoidal property Examples ======== >>> from sympy.vector import CoordSys3D >>> from sympy.vector import is_solenoidal >>> R = CoordSys3D('R') >>> is_solenoidal(R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k) True >>> is_solenoidal(R.y * R.j) False """ # Field is solenoidal irrespective of system # Take the first coordinate system in the result of the # separate method in Vector if not isinstance(field, Vector): raise TypeError("field should be a Vector") if field == Vector.zero: return True return divergence(field).simplify() == S(0)
def laplacian(expr): """ Return the laplacian of the given field computed in terms of the base scalars of the given coordinate system. Parameters ========== expr : SymPy Expr or Vector expr denotes a scalar or vector field. Examples ======== >>> from sympy.vector import CoordSys3D, laplacian >>> R = CoordSys3D('R') >>> f = R.x**2*R.y**5*R.z >>> laplacian(f) 20*R.x**2*R.y**3*R.z + 2*R.y**5*R.z >>> f = R.x**2*R.i + R.y**3*R.j + R.z**4*R.k >>> laplacian(f) 2*R.i + 6*R.y*R.j + 12*R.z**2*R.k """ delop = Del() if expr.is_Vector: return (gradient(divergence(expr)) - curl(curl(expr))).doit() return delop.dot(delop(expr)).doit()
def is_solenoidal(field): """ Checks if a field is solenoidal. Parameters ========== field : Vector The field to check for solenoidal property Examples ======== >>> from sympy.vector import CoordSys3D >>> from sympy.vector import is_solenoidal >>> R = CoordSys3D('R') >>> is_solenoidal(R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k) True >>> is_solenoidal(R.y * R.j) False """ # Field is solenoidal irrespective of system # Take the first coordinate system in the result of the # separate method in Vector if not isinstance(field, Vector): raise TypeError("field should be a Vector") if field == Vector.zero: return True return divergence(field).simplify() is S.Zero
def dot(self, vect, doit=False): """ Represents the dot product between this operator and a given vector - equal to the divergence of the vector field. Parameters ========== vect : Vector The vector whose divergence is to be calculated. doit : bool If True, the result is returned after calling .doit() on each component. Else, the returned expression contains Derivative instances Examples ======== >>> from sympy.vector import CoordSys3D, Del >>> delop = Del() >>> C = CoordSys3D('C') >>> delop.dot(C.x*C.i) Derivative(C.x, C.x) >>> v = C.x*C.y*C.z * (C.i + C.j + C.k) >>> (delop & v).doit() C.x*C.y + C.x*C.z + C.y*C.z """ return divergence(vect, doit=doit)
def test_differential_operators_curvilinear_system(): A = CoordSys3D( "A", transformation="spherical", variable_names=["r", "theta", "phi"] ) B = CoordSys3D( "B", transformation="cylindrical", variable_names=["r", "theta", "z"] ) # Test for spherical coordinate system and gradient assert gradient(3 * A.r + 4 * A.theta) == 3 * A.i + 4 / A.r * A.j assert ( gradient(3 * A.r * A.phi + 4 * A.theta) == 3 * A.phi * A.i + 4 / A.r * A.j + (3 / sin(A.theta)) * A.k ) assert gradient(0 * A.r + 0 * A.theta + 0 * A.phi) == Vector.zero assert ( gradient(A.r * A.theta * A.phi) == A.theta * A.phi * A.i + A.phi * A.j + (A.theta / sin(A.theta)) * A.k ) # Test for spherical coordinate system and divergence assert divergence(A.r * A.i + A.theta * A.j + A.phi * A.k) == ( sin(A.theta) * A.r + cos(A.theta) * A.r * A.theta ) / (sin(A.theta) * A.r ** 2) + 3 + 1 / (sin(A.theta) * A.r) assert divergence( 3 * A.r * A.phi * A.i + A.theta * A.j + A.r * A.theta * A.phi * A.k ) == (sin(A.theta) * A.r + cos(A.theta) * A.r * A.theta) / ( sin(A.theta) * A.r ** 2 ) + 9 * A.phi + A.theta / sin( A.theta ) assert divergence(Vector.zero) == 0 assert divergence(0 * A.i + 0 * A.j + 0 * A.k) == 0 # Test for spherical coordinate system and curl assert ( curl(A.r * A.i + A.theta * A.j + A.phi * A.k) == (cos(A.theta) * A.phi / (sin(A.theta) * A.r)) * A.i + (-A.phi / A.r) * A.j + A.theta / A.r * A.k ) assert ( curl(A.r * A.j + A.phi * A.k) == (cos(A.theta) * A.phi / (sin(A.theta) * A.r)) * A.i + (-A.phi / A.r) * A.j + 2 * A.k ) # Test for cylindrical coordinate system and gradient assert gradient(0 * B.r + 0 * B.theta + 0 * B.z) == Vector.zero assert ( gradient(B.r * B.theta * B.z) == B.theta * B.z * B.i + B.z * B.j + B.r * B.theta * B.k ) assert gradient(3 * B.r) == 3 * B.i assert gradient(2 * B.theta) == 2 / B.r * B.j assert gradient(4 * B.z) == 4 * B.k # Test for cylindrical coordinate system and divergence assert divergence(B.r * B.i + B.theta * B.j + B.z * B.k) == 3 + 1 / B.r assert divergence(B.r * B.j + B.z * B.k) == 1 # Test for cylindrical coordinate system and curl assert curl(B.r * B.j + B.z * B.k) == 2 * B.k assert curl(3 * B.i + 2 / B.r * B.j + 4 * B.k) == Vector.zero
def test_differential_operators_curvilinear_system(): A = CoordSys3D('A', transformation="spherical", variable_names=["r", "theta", "phi"]) B = CoordSys3D('B', transformation='cylindrical', variable_names=["r", "theta", "z"]) # Test for spherical coordinate system and gradient assert gradient(3*A.r + 4*A.theta) == 3*A.i + 4/A.r*A.j assert gradient(3*A.r*A.phi + 4*A.theta) == 3*A.phi*A.i + 4/A.r*A.j + (3/sin(A.theta))*A.k assert gradient(0*A.r + 0*A.theta+0*A.phi) == Vector.zero assert gradient(A.r*A.theta*A.phi) == A.theta*A.phi*A.i + A.phi*A.j + (A.theta/sin(A.theta))*A.k # Test for spherical coordinate system and divergence assert divergence(A.r * A.i + A.theta * A.j + A.phi * A.k) == \ (sin(A.theta)*A.r + cos(A.theta)*A.r*A.theta)/(sin(A.theta)*A.r**2) + 3 + 1/(sin(A.theta)*A.r) assert divergence(3*A.r*A.phi*A.i + A.theta*A.j + A.r*A.theta*A.phi*A.k) == \ (sin(A.theta)*A.r + cos(A.theta)*A.r*A.theta)/(sin(A.theta)*A.r**2) + 9*A.phi + A.theta/sin(A.theta) assert divergence(Vector.zero) == 0 assert divergence(0*A.i + 0*A.j + 0*A.k) == 0 # Test for spherical coordinate system and curl assert curl(A.r*A.i + A.theta*A.j + A.phi*A.k) == \ (cos(A.theta)*A.phi/(sin(A.theta)*A.r))*A.i + (-A.phi/A.r)*A.j + A.theta/A.r*A.k assert curl(A.r*A.j + A.phi*A.k) == (cos(A.theta)*A.phi/(sin(A.theta)*A.r))*A.i + (-A.phi/A.r)*A.j + 2*A.k # Test for cylindrical coordinate system and gradient assert gradient(0*B.r + 0*B.theta+0*B.z) == Vector.zero assert gradient(B.r*B.theta*B.z) == B.theta*B.z*B.i + B.z*B.j + B.r*B.theta*B.k assert gradient(3*B.r) == 3*B.i assert gradient(2*B.theta) == 2/B.r * B.j assert gradient(4*B.z) == 4*B.k # Test for cylindrical coordinate system and divergence assert divergence(B.r*B.i + B.theta*B.j + B.z*B.k) == 3 + 1/B.r assert divergence(B.r*B.j + B.z*B.k) == 1 # Test for cylindrical coordinate system and curl assert curl(B.r*B.j + B.z*B.k) == 2*B.k assert curl(3*B.i + 2/B.r*B.j + 4*B.k) == Vector.zero
def test_differential_operators_curvilinear_system(): A = CoordSys3D('A') A._set_lame_coefficient_mapping('spherical') B = CoordSys3D('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_mixed_coordinates(): # gradient a = CoordSys3D('a') b = CoordSys3D('b') c = CoordSys3D('c') assert gradient(a.x*b.y) == b.y*a.i + a.x*b.j assert gradient(3*cos(q)*a.x*b.x+a.y*(a.x+((cos(q)+b.x)))) ==\ (a.y + 3*b.x*cos(q))*a.i + (a.x + b.x + cos(q))*a.j + (3*a.x*cos(q) + a.y)*b.i # Some tests need further work: # assert gradient(a.x*(cos(a.x+b.x))) == (cos(a.x + b.x))*a.i + a.x*Gradient(cos(a.x + b.x)) # assert gradient(cos(a.x + b.x)*cos(a.x + b.z)) == Gradient(cos(a.x + b.x)*cos(a.x + b.z)) assert gradient(a.x**b.y) == Gradient(a.x**b.y) # assert gradient(cos(a.x+b.y)*a.z) == None assert gradient(cos(a.x*b.y)) == Gradient(cos(a.x*b.y)) assert gradient(3*cos(q)*a.x*b.x*a.z*a.y+ b.y*b.z + cos(a.x+a.y)*b.z) == \ (3*a.y*a.z*b.x*cos(q) - b.z*sin(a.x + a.y))*a.i + \ (3*a.x*a.z*b.x*cos(q) - b.z*sin(a.x + a.y))*a.j + (3*a.x*a.y*b.x*cos(q))*a.k + \ (3*a.x*a.y*a.z*cos(q))*b.i + b.z*b.j + (b.y + cos(a.x + a.y))*b.k # divergence assert divergence(a.i*a.x+a.j*a.y+a.z*a.k + b.i*b.x+b.j*b.y+b.z*b.k + c.i*c.x+c.j*c.y+c.z*c.k) == S(9) # assert divergence(3*a.i*a.x*cos(a.x+b.z) + a.j*b.x*c.z) == None assert divergence(3*a.i*a.x*a.z + b.j*b.x*c.z + 3*a.j*a.z*a.y) == \ 6*a.z + b.x*Dot(b.j, c.k) assert divergence(3*cos(q)*a.x*b.x*b.i*c.x) == \ 3*a.x*b.x*cos(q)*Dot(b.i, c.i) + 3*a.x*c.x*cos(q) + 3*b.x*c.x*cos(q)*Dot(b.i, a.i) assert divergence(a.x*b.x*c.x*Cross(a.x*a.i, a.y*b.j)) ==\ a.x*b.x*c.x*Divergence(Cross(a.x*a.i, a.y*b.j)) + \ b.x*c.x*Dot(Cross(a.x*a.i, a.y*b.j), a.i) + \ a.x*c.x*Dot(Cross(a.x*a.i, a.y*b.j), b.i) + \ a.x*b.x*Dot(Cross(a.x*a.i, a.y*b.j), c.i) assert divergence(a.x*b.x*c.x*(a.x*a.i + b.x*b.i)) == \ 4*a.x*b.x*c.x +\ a.x**2*c.x*Dot(a.i, b.i) +\ a.x**2*b.x*Dot(a.i, c.i) +\ b.x**2*c.x*Dot(b.i, a.i) +\ a.x*b.x**2*Dot(b.i, c.i)
def test_differential_operators_curvilinear_system(): A = CoordSys3D('A') A._set_lame_coefficient_mapping('spherical') B = CoordSys3D('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_mixed_coordinates(): # gradient a = CoordSys3D("a") b = CoordSys3D("b") c = CoordSys3D("c") assert gradient(a.x * b.y) == b.y * a.i + a.x * b.j assert ( gradient(3 * cos(q) * a.x * b.x + a.y * (a.x + ((cos(q) + b.x)))) == (a.y + 3 * b.x * cos(q)) * a.i + (a.x + b.x + cos(q)) * a.j + (3 * a.x * cos(q) + a.y) * b.i ) # Some tests need further work: # assert gradient(a.x*(cos(a.x+b.x))) == (cos(a.x + b.x))*a.i + a.x*Gradient(cos(a.x + b.x)) # assert gradient(cos(a.x + b.x)*cos(a.x + b.z)) == Gradient(cos(a.x + b.x)*cos(a.x + b.z)) assert gradient(a.x ** b.y) == Gradient(a.x ** b.y) # assert gradient(cos(a.x+b.y)*a.z) == None assert gradient(cos(a.x * b.y)) == Gradient(cos(a.x * b.y)) assert ( gradient(3 * cos(q) * a.x * b.x * a.z * a.y + b.y * b.z + cos(a.x + a.y) * b.z) == (3 * a.y * a.z * b.x * cos(q) - b.z * sin(a.x + a.y)) * a.i + (3 * a.x * a.z * b.x * cos(q) - b.z * sin(a.x + a.y)) * a.j + (3 * a.x * a.y * b.x * cos(q)) * a.k + (3 * a.x * a.y * a.z * cos(q)) * b.i + b.z * b.j + (b.y + cos(a.x + a.y)) * b.k ) # divergence assert divergence( a.i * a.x + a.j * a.y + a.z * a.k + b.i * b.x + b.j * b.y + b.z * b.k + c.i * c.x + c.j * c.y + c.z * c.k ) == S(9) # assert divergence(3*a.i*a.x*cos(a.x+b.z) + a.j*b.x*c.z) == None assert divergence( 3 * a.i * a.x * a.z + b.j * b.x * c.z + 3 * a.j * a.z * a.y ) == 6 * a.z + b.x * Dot(b.j, c.k) assert divergence(3 * cos(q) * a.x * b.x * b.i * c.x) == 3 * a.x * b.x * cos( q ) * Dot(b.i, c.i) + 3 * a.x * c.x * cos(q) + 3 * b.x * c.x * cos(q) * Dot(b.i, a.i) assert divergence( a.x * b.x * c.x * Cross(a.x * a.i, a.y * b.j) ) == a.x * b.x * c.x * Divergence(Cross(a.x * a.i, a.y * b.j)) + b.x * c.x * Dot( Cross(a.x * a.i, a.y * b.j), a.i ) + a.x * c.x * Dot( Cross(a.x * a.i, a.y * b.j), b.i ) + a.x * b.x * Dot( Cross(a.x * a.i, a.y * b.j), c.i ) assert divergence( a.x * b.x * c.x * (a.x * a.i + b.x * b.i) ) == 4 * a.x * b.x * c.x + a.x ** 2 * c.x * Dot(a.i, b.i) + a.x ** 2 * b.x * Dot( a.i, c.i ) + b.x ** 2 * c.x * Dot( b.i, a.i ) + a.x * b.x ** 2 * Dot( b.i, c.i )
def test_del_operator(): # Tests for curl assert delop ^ Vector.zero == Vector.zero assert (delop ^ Vector.zero).doit() == Vector.zero == curl(Vector.zero) 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 assert delop.cross(2 * y ** 2 * j) == delop ^ 2 * y ** 2 * j v = x * y * z * (i + j + k) assert ( (delop ^ v).doit() == (-x * y + x * z) * i + (x * y - y * z) * j + (-x * z + y * z) * k == curl(v) ) assert delop ^ v == delop.cross(v) assert ( delop.cross(2 * x ** 2 * j) == (Derivative(0, C.y) - Derivative(2 * C.x ** 2, C.z)) * C.i + (-Derivative(0, C.x) + Derivative(0, C.z)) * C.j + (-Derivative(0, C.y) + Derivative(2 * C.x ** 2, C.x)) * C.k ) assert delop.cross(2 * x ** 2 * j, doit=True) == 4 * x * k == curl(2 * x ** 2 * j) # Tests for divergence assert delop & Vector.zero is S.Zero == divergence(Vector.zero) assert (delop & Vector.zero).doit() is S.Zero assert delop.dot(Vector.zero) == delop & Vector.zero assert (delop & i).doit() is S.Zero assert (delop & x ** 2 * i).doit() == 2 * x == divergence(x ** 2 * i) assert delop.dot(v, doit=True) == x * y + y * z + z * x == divergence(v) assert delop & v == delop.dot(v) assert delop.dot(1 / (x * y * z) * (i + j + k), doit=True) == -1 / ( x * y * z ** 2 ) - 1 / (x * y ** 2 * z) - 1 / (x ** 2 * y * z) v = x * i + y * j + z * k assert delop & v == Derivative(C.x, C.x) + Derivative(C.y, C.y) + Derivative( C.z, C.z ) assert delop.dot(v, doit=True) == 3 == divergence(v) assert delop & v == delop.dot(v) assert simplify((delop & v).doit()) == 3 # Tests for gradient assert delop.gradient(0, doit=True) == Vector.zero == gradient(0) assert delop.gradient(0) == delop(0) assert (delop(S.Zero)).doit() == Vector.zero assert ( delop(x) == (Derivative(C.x, C.x)) * C.i + (Derivative(C.x, C.y)) * C.j + (Derivative(C.x, C.z)) * C.k ) assert (delop(x)).doit() == i == gradient(x) assert ( delop(x * y * z) == (Derivative(C.x * C.y * C.z, C.x)) * C.i + (Derivative(C.x * C.y * C.z, C.y)) * C.j + (Derivative(C.x * C.y * C.z, C.z)) * C.k ) assert ( delop.gradient(x * y * z, doit=True) == y * z * i + z * x * j + x * y * k == gradient(x * y * z) ) assert delop(x * y * z) == delop.gradient(x * y * z) assert (delop(2 * x ** 2)).doit() == 4 * x * i assert (delop(a * sin(y) / x)).doit() == -a * sin(y) / x ** 2 * i + a * cos( y ) / x * j # Tests for directional derivative assert (Vector.zero & delop)(a) is S.Zero assert ((Vector.zero & delop)(a)).doit() is S.Zero assert ((v & delop)(Vector.zero)).doit() == Vector.zero assert ((v & delop)(S.Zero)).doit() is S.Zero assert ((i & delop)(x)).doit() == 1 assert ((j & delop)(y)).doit() == 1 assert ((k & delop)(z)).doit() == 1 assert ((i & delop)(x * y * z)).doit() == y * z assert ((v & delop)(x)).doit() == x assert ((v & delop)(x * y * z)).doit() == 3 * x * y * z assert (v & delop)(x + y + z) == C.x + C.y + C.z assert ((v & delop)(x + y + z)).doit() == x + y + z assert ((v & delop)(v)).doit() == v assert ((i & delop)(v)).doit() == i assert ((j & delop)(v)).doit() == j assert ((k & delop)(v)).doit() == k assert ((v & delop)(Vector.zero)).doit() == Vector.zero # Tests for laplacian on scalar fields assert laplacian(x * y * z) is S.Zero assert laplacian(x ** 2) == S(2) assert ( laplacian(x ** 2 * y ** 2 * z ** 2) == 2 * y ** 2 * z ** 2 + 2 * x ** 2 * z ** 2 + 2 * x ** 2 * y ** 2 ) A = CoordSys3D( "A", transformation="spherical", variable_names=["r", "theta", "phi"] ) B = CoordSys3D( "B", transformation="cylindrical", variable_names=["r", "theta", "z"] ) assert laplacian(A.r + A.theta + A.phi) == 2 / A.r + cos(A.theta) / ( A.r ** 2 * sin(A.theta) ) assert laplacian(B.r + B.theta + B.z) == 1 / B.r # Tests for laplacian on vector fields assert laplacian(x * y * z * (i + j + k)) == Vector.zero assert ( laplacian(x * y ** 2 * z * (i + j + k)) == 2 * x * z * i + 2 * x * z * j + 2 * x * z * k )
def test_del_operator(): # Tests for curl assert delop ^ Vector.zero == Vector.zero assert ((delop ^ Vector.zero).doit() == Vector.zero == curl(Vector.zero)) 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 assert delop.cross(2*y**2*j) == delop ^ 2*y**2*j v = x*y*z * (i + j + k) assert ((delop ^ v).doit() == (-x*y + x*z)*i + (x*y - y*z)*j + (-x*z + y*z)*k == curl(v)) assert delop ^ v == delop.cross(v) assert (delop.cross(2*x**2*j) == (Derivative(0, C.y) - Derivative(2*C.x**2, C.z))*C.i + (-Derivative(0, C.x) + Derivative(0, C.z))*C.j + (-Derivative(0, C.y) + Derivative(2*C.x**2, C.x))*C.k) assert (delop.cross(2*x**2*j, doit=True) == 4*x*k == curl(2*x**2*j)) #Tests for divergence assert delop & Vector.zero == S(0) == divergence(Vector.zero) assert (delop & Vector.zero).doit() == S(0) assert delop.dot(Vector.zero) == delop & Vector.zero assert (delop & i).doit() == S(0) assert (delop & x**2*i).doit() == 2*x == divergence(x**2*i) assert (delop.dot(v, doit=True) == x*y + y*z + z*x == divergence(v)) assert delop & v == delop.dot(v) assert delop.dot(1/(x*y*z) * (i + j + k), doit=True) == \ - 1 / (x*y*z**2) - 1 / (x*y**2*z) - 1 / (x**2*y*z) v = x*i + y*j + z*k assert (delop & v == Derivative(C.x, C.x) + Derivative(C.y, C.y) + Derivative(C.z, C.z)) assert delop.dot(v, doit=True) == 3 == divergence(v) assert delop & v == delop.dot(v) assert simplify((delop & v).doit()) == 3 #Tests for gradient assert (delop.gradient(0, doit=True) == Vector.zero == gradient(0)) assert delop.gradient(0) == delop(0) assert (delop(S(0))).doit() == Vector.zero assert (delop(x) == (Derivative(C.x, C.x))*C.i + (Derivative(C.x, C.y))*C.j + (Derivative(C.x, C.z))*C.k) assert (delop(x)).doit() == i == gradient(x) assert (delop(x*y*z) == (Derivative(C.x*C.y*C.z, C.x))*C.i + (Derivative(C.x*C.y*C.z, C.y))*C.j + (Derivative(C.x*C.y*C.z, C.z))*C.k) assert (delop.gradient(x*y*z, doit=True) == y*z*i + z*x*j + x*y*k == gradient(x*y*z)) assert delop(x*y*z) == delop.gradient(x*y*z) assert (delop(2*x**2)).doit() == 4*x*i assert ((delop(a*sin(y) / x)).doit() == -a*sin(y)/x**2 * i + a*cos(y)/x * j) #Tests for directional derivative assert (Vector.zero & delop)(a) == S(0) assert ((Vector.zero & delop)(a)).doit() == S(0) assert ((v & delop)(Vector.zero)).doit() == Vector.zero assert ((v & delop)(S(0))).doit() == S(0) assert ((i & delop)(x)).doit() == 1 assert ((j & delop)(y)).doit() == 1 assert ((k & delop)(z)).doit() == 1 assert ((i & delop)(x*y*z)).doit() == y*z assert ((v & delop)(x)).doit() == x assert ((v & delop)(x*y*z)).doit() == 3*x*y*z assert (v & delop)(x + y + z) == C.x + C.y + C.z assert ((v & delop)(x + y + z)).doit() == x + y + z assert ((v & delop)(v)).doit() == v assert ((i & delop)(v)).doit() == i assert ((j & delop)(v)).doit() == j assert ((k & delop)(v)).doit() == k assert ((v & delop)(Vector.zero)).doit() == Vector.zero # Tests for laplacian on scalar fields assert laplacian(x*y*z) == S.Zero assert laplacian(x**2) == S(2) assert laplacian(x**2*y**2*z**2) == \ 2*y**2*z**2 + 2*x**2*z**2 + 2*x**2*y**2 # Tests for laplacian on vector fields assert laplacian(x*y*z*(i + j + k)) == Vector.zero assert laplacian(x*y**2*z*(i + j + k)) == \ 2*x*z*i + 2*x*z*j + 2*x*z*k
def test_del_operator(): # Tests for curl assert delop ^ Vector.zero == Vector.zero assert ((delop ^ Vector.zero).doit() == Vector.zero == curl(Vector.zero)) 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 assert delop.cross(2*y**2*j) == delop ^ 2*y**2*j v = x*y*z * (i + j + k) assert ((delop ^ v).doit() == (-x*y + x*z)*i + (x*y - y*z)*j + (-x*z + y*z)*k == curl(v)) assert delop ^ v == delop.cross(v) assert (delop.cross(2*x**2*j) == (Derivative(0, C.y) - Derivative(2*C.x**2, C.z))*C.i + (-Derivative(0, C.x) + Derivative(0, C.z))*C.j + (-Derivative(0, C.y) + Derivative(2*C.x**2, C.x))*C.k) assert (delop.cross(2*x**2*j, doit=True) == 4*x*k == curl(2*x**2*j)) #Tests for divergence assert delop & Vector.zero == S(0) == divergence(Vector.zero) assert (delop & Vector.zero).doit() == S(0) assert delop.dot(Vector.zero) == delop & Vector.zero assert (delop & i).doit() == S(0) assert (delop & x**2*i).doit() == 2*x == divergence(x**2*i) assert (delop.dot(v, doit=True) == x*y + y*z + z*x == divergence(v)) assert delop & v == delop.dot(v) assert delop.dot(1/(x*y*z) * (i + j + k), doit=True) == \ - 1 / (x*y*z**2) - 1 / (x*y**2*z) - 1 / (x**2*y*z) v = x*i + y*j + z*k assert (delop & v == Derivative(C.x, C.x) + Derivative(C.y, C.y) + Derivative(C.z, C.z)) assert delop.dot(v, doit=True) == 3 == divergence(v) assert delop & v == delop.dot(v) assert simplify((delop & v).doit()) == 3 #Tests for gradient assert (delop.gradient(0, doit=True) == Vector.zero == gradient(0)) assert delop.gradient(0) == delop(0) assert (delop(S(0))).doit() == Vector.zero assert (delop(x) == (Derivative(C.x, C.x))*C.i + (Derivative(C.x, C.y))*C.j + (Derivative(C.x, C.z))*C.k) assert (delop(x)).doit() == i == gradient(x) assert (delop(x*y*z) == (Derivative(C.x*C.y*C.z, C.x))*C.i + (Derivative(C.x*C.y*C.z, C.y))*C.j + (Derivative(C.x*C.y*C.z, C.z))*C.k) assert (delop.gradient(x*y*z, doit=True) == y*z*i + z*x*j + x*y*k == gradient(x*y*z)) assert delop(x*y*z) == delop.gradient(x*y*z) assert (delop(2*x**2)).doit() == 4*x*i assert ((delop(a*sin(y) / x)).doit() == -a*sin(y)/x**2 * i + a*cos(y)/x * j) #Tests for directional derivative assert (Vector.zero & delop)(a) == S(0) assert ((Vector.zero & delop)(a)).doit() == S(0) assert ((v & delop)(Vector.zero)).doit() == Vector.zero assert ((v & delop)(S(0))).doit() == S(0) assert ((i & delop)(x)).doit() == 1 assert ((j & delop)(y)).doit() == 1 assert ((k & delop)(z)).doit() == 1 assert ((i & delop)(x*y*z)).doit() == y*z assert ((v & delop)(x)).doit() == x assert ((v & delop)(x*y*z)).doit() == 3*x*y*z assert (v & delop)(x + y + z) == C.x + C.y + C.z assert ((v & delop)(x + y + z)).doit() == x + y + z assert ((v & delop)(v)).doit() == v assert ((i & delop)(v)).doit() == i assert ((j & delop)(v)).doit() == j assert ((k & delop)(v)).doit() == k assert ((v & delop)(Vector.zero)).doit() == Vector.zero