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 test_dot(): v1 = C.x * i + C.z * C.z * j v2 = C.x * i + C.y * j + C.z * k assert Dot(v1, v2) == Dot(C.x*C.i + C.z**2*C.j, C.x*C.i + C.y*C.j + C.z*C.k) assert Dot(v1, v2).doit() == C.x**2 + C.y*C.z**2 assert Dot(v1, v2).doit() == C.x**2 + C.y*C.z**2 assert Dot(v1, v2) == Dot(v2, v1)
def divergence(vect, coord_sys=None, doit=True): """ Returns the divergence of a vector field computed wrt the base scalars of the given coordinate system. Parameters ========== vector : Vector The vector operand coord_sys : CoordSys3D The coordinate system to calculate the gradient in Deprecated since version 1.1 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, divergence >>> R = CoordSys3D('R') >>> v1 = R.x*R.y*R.z * (R.i+R.j+R.k) >>> divergence(v1) R.x*R.y + R.x*R.z + R.y*R.z >>> v2 = 2*R.y*R.z*R.j >>> divergence(v2) 2*R.z """ coord_sys = _get_coord_sys_from_expr(vect, coord_sys) if len(coord_sys) == 0: return S.Zero elif len(coord_sys) == 1: if isinstance(vect, (Cross, Curl, Gradient)): return Divergence(vect) # TODO: is case of many coord systems, this gets a random one: coord_sys = next(iter(coord_sys)) i, j, k = coord_sys.base_vectors() x, y, z = coord_sys.base_scalars() h1, h2, h3 = coord_sys.lame_coefficients() vx = _diff_conditional(vect.dot(i), x, h2, h3) \ / (h1 * h2 * h3) vy = _diff_conditional(vect.dot(j), y, h3, h1) \ / (h1 * h2 * h3) vz = _diff_conditional(vect.dot(k), z, h1, h2) \ / (h1 * h2 * h3) res = vx + vy + vz if doit: return res.doit() return res else: if isinstance(vect, (Add, VectorAdd)): return Add.fromiter(divergence(i, doit=doit) for i in vect.args) elif isinstance(vect, (Mul, VectorMul)): vector = [ i for i in vect.args if isinstance(i, (Vector, Cross, Gradient)) ][0] scalar = Mul.fromiter( i for i in vect.args if not isinstance(i, (Vector, Cross, Gradient))) res = Dot( vector, gradient(scalar)) + scalar * divergence(vector, doit=doit) if doit: return res.doit() return res elif isinstance(vect, (Cross, Curl, Gradient)): return Divergence(vect) else: raise Divergence(vect)
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 divergence(vect, coord_sys=None, doit=True): """ Returns the divergence of a vector field computed wrt the base scalars of the given coordinate system. Parameters ========== vector : Vector The vector operand coord_sys : CoordSys3D The coordinate system to calculate the gradient in Deprecated since version 1.1 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, divergence >>> R = CoordSys3D('R') >>> v1 = R.x*R.y*R.z * (R.i+R.j+R.k) >>> divergence(v1) R.x*R.y + R.x*R.z + R.y*R.z >>> v2 = 2*R.y*R.z*R.j >>> divergence(v2) 2*R.z """ coord_sys = _get_coord_sys_from_expr(vect, coord_sys) if len(coord_sys) == 0: return S.Zero elif len(coord_sys) == 1: if isinstance(vect, (Cross, Curl, Gradient)): return Divergence(vect) # TODO: is case of many coord systems, this gets a random one: coord_sys = next(iter(coord_sys)) i, j, k = coord_sys.base_vectors() x, y, z = coord_sys.base_scalars() h1, h2, h3 = coord_sys.lame_coefficients() vx = _diff_conditional(vect.dot(i), x, h2, h3) \ / (h1 * h2 * h3) vy = _diff_conditional(vect.dot(j), y, h3, h1) \ / (h1 * h2 * h3) vz = _diff_conditional(vect.dot(k), z, h1, h2) \ / (h1 * h2 * h3) res = vx + vy + vz if doit: return res.doit() return res else: if isinstance(vect, (Add, VectorAdd)): return Add.fromiter(divergence(i, doit=doit) for i in vect.args) elif isinstance(vect, (Mul, VectorMul)): vector = [i for i in vect.args if isinstance(i, (Vector, Cross, Gradient))][0] scalar = Mul.fromiter(i for i in vect.args if not isinstance(i, (Vector, Cross, Gradient))) res = Dot(vector, gradient(scalar)) + scalar*divergence(vector, doit=doit) if doit: return res.doit() return res elif isinstance(vect, (Cross, Curl, Gradient)): return Divergence(vect) else: raise Divergence(vect)