def test_coordinate_vars(): """Tests the coordinate variables functionality""" A = ReferenceFrame('A') assert CoordinateSym('Ax', A, 0) == A[0] assert CoordinateSym('Ax', A, 1) == A[1] assert CoordinateSym('Ax', A, 2) == A[2] raises(ValueError, lambda: CoordinateSym('Ax', A, 3)) q = dynamicsymbols('q') qd = dynamicsymbols('q', 1) assert isinstance(A[0], CoordinateSym) and \ isinstance(A[0], CoordinateSym) and \ isinstance(A[0], CoordinateSym) assert A.variable_map(A) == {A[0]: A[0], A[1]: A[1], A[2]: A[2]} assert A[0].frame == A B = A.orientnew('B', 'Axis', [q, A.z]) assert B.variable_map(A) == { B[2]: A[2], B[1]: -A[0] * sin(q) + A[1] * cos(q), B[0]: A[0] * cos(q) + A[1] * sin(q) } assert A.variable_map(B) == { A[0]: B[0] * cos(q) - B[1] * sin(q), A[1]: B[0] * sin(q) + B[1] * cos(q), A[2]: B[2] } assert time_derivative(B[0], A) == -A[0] * sin(q) * qd + A[1] * cos(q) * qd assert time_derivative(B[1], A) == -A[0] * cos(q) * qd - A[1] * sin(q) * qd assert time_derivative(B[2], A) == 0 assert express(B[0], A, variables=True) == A[0] * cos(q) + A[1] * sin(q) assert express(B[1], A, variables=True) == -A[0] * sin(q) + A[1] * cos(q) assert express(B[2], A, variables=True) == A[2] assert time_derivative(A[0] * A.x + A[1] * A.y + A[2] * A.z, B) == A[1] * qd * A.x - A[0] * qd * A.y assert time_derivative(B[0] * B.x + B[1] * B.y + B[2] * B.z, A) == -B[1] * qd * B.x + B[0] * qd * B.y assert express(B[0]*B[1]*B[2], A, variables=True) == \ A[2]*(-A[0]*sin(q) + A[1]*cos(q))*(A[0]*cos(q) + A[1]*sin(q)) assert (time_derivative(B[0] * B[1] * B[2], A) - (A[2] * (-A[0]**2 * cos(2 * q) - 2 * A[0] * A[1] * sin(2 * q) + A[1]**2 * cos(2 * q)) * qd)).trigsimp() == 0 assert express(B[0]*B.x + B[1]*B.y + B[2]*B.z, A) == \ (B[0]*cos(q) - B[1]*sin(q))*A.x + (B[0]*sin(q) + \ B[1]*cos(q))*A.y + B[2]*A.z assert express(B[0]*B.x + B[1]*B.y + B[2]*B.z, A, variables=True) == \ A[0]*A.x + A[1]*A.y + A[2]*A.z assert express(A[0]*A.x + A[1]*A.y + A[2]*A.z, B) == \ (A[0]*cos(q) + A[1]*sin(q))*B.x + \ (-A[0]*sin(q) + A[1]*cos(q))*B.y + A[2]*B.z assert express(A[0]*A.x + A[1]*A.y + A[2]*A.z, B, variables=True) == \ B[0]*B.x + B[1]*B.y + B[2]*B.z N = B.orientnew('N', 'Axis', [-q, B.z]) assert N.variable_map(A) == {N[0]: A[0], N[2]: A[2], N[1]: A[1]} C = A.orientnew('C', 'Axis', [q, A.x + A.y + A.z]) mapping = A.variable_map(C) assert mapping[A[0]] == 2*C[0]*cos(q)/3 + C[0]/3 - 2*C[1]*sin(q + pi/6)/3 +\ C[1]/3 - 2*C[2]*cos(q + pi/3)/3 + C[2]/3 assert mapping[A[1]] == -2*C[0]*cos(q + pi/3)/3 + \ C[0]/3 + 2*C[1]*cos(q)/3 + C[1]/3 - 2*C[2]*sin(q + pi/6)/3 + C[2]/3 assert mapping[A[2]] == -2*C[0]*sin(q + pi/6)/3 + C[0]/3 - \ 2*C[1]*cos(q + pi/3)/3 + C[1]/3 + 2*C[2]*cos(q)/3 + C[2]/3
def g_DF(self): #gDF = TM_ds * b_hex(self.a, self.c) * self.g gC = b_hex(self.a, self.c) * self.g # still matrix form gCF = gC[0] * self.CF.x + gC[1] * self.CF.y + gC[ 2] * self.CF.z # vector explicitely defined gDF = express(gCF, self.DF) return gDF
def express(self, otherframe, variables=False): """ Returns a Vector equivalent to this one, expressed in otherframe. Uses the global express method. Parameters ========== otherframe : ReferenceFrame The frame for this Vector to be described in variables : boolean If True, the coordinate symbols(if present) in this Vector are re-expressed in terms otherframe Examples ======== >>> from sympy.physics.vector import ReferenceFrame, Vector, dynamicsymbols >>> q1 = dynamicsymbols('q1') >>> N = ReferenceFrame('N') >>> A = N.orientnew('A', 'Axis', [q1, N.y]) >>> A.x.express(N) cos(q1)*N.x - sin(q1)*N.z """ from sympy.physics.vector import express return express(self, otherframe, variables=variables)
def gradient(scalar, frame): """ Returns the vector gradient of a scalar field computed wrt the coordinate symbols of the given frame. Parameters ========== scalar : sympifiable The scalar field to take the gradient of frame : ReferenceFrame The frame to calculate the gradient in Examples ======== >>> from sympy.physics.vector import ReferenceFrame >>> from sympy.physics.vector import gradient >>> R = ReferenceFrame('R') >>> s1 = R[0]*R[1]*R[2] >>> gradient(s1, R) R_y*R_z*R.x + R_x*R_z*R.y + R_x*R_y*R.z >>> s2 = 5*R[0]**2*R[2] >>> gradient(s2, R) 10*R_x*R_z*R.x + 5*R_x**2*R.z """ _check_frame(frame) outvec = Vector(0) scalar = express(scalar, frame, variables=True) for i, x in enumerate(frame): outvec += diff(scalar, frame[i]) * x return outvec
def ghex_DF(self): gC = b_hex(self.a, self.c) * self.g # g in real space Cartesian coordinates gCF = gC[0] * self.CF.x + gC[1] * self.CF.y + gC[ 2] * self.CF.z # vector explicitely defined gDF = express(gCF, self.DF) return gDF
def b_SF(self): bDF = self.b * self.DF.z bSF = express(bDF, self.SF) x_b = bSF.dot(self.SF.x) y_b = bSF.dot(self.SF.y) z_b = bSF.dot(self.SF.z) return [x_b, y_b, z_b]
def Beta_hex_RincF(self): ''' numerical function of above for plotting Return result in incident beam frame''' betaRincF = express(self.Beta_hex_DF(), self.RincF, variables=True) betaRincF_n = betaRincF.subs([(self.RincF[0], x), (self.RincF[1], y), (self.RincF[2], z)], simultaneous=True) return betaRincF_n
def rinc_SF(self): ''' the incident beam is along the lab cartesian direction (0,0,-1) return in vector form in sample frame ''' rinc_LF = -1. * self.LF.z rinc_SF = express(rinc_LF, self.SF) return rinc_SF
def Beta_hex_RincF_n(self, xN, yN, zN): ''' numerical function of above for plotting Return result in incident beam frame''' betaRincF = express(self.Beta_hex_DF(), self.RincF, variables=True) betaRincF_n = betaRincF.subs([(self.RincF[0], x), (self.RincF[1], y), (self.RincF[2], z)], simultaneous=True) np_function = lambdify((x, y, z), betaRincF_n, "numpy") return np_function(xN, yN, zN)
def Beta_cubic_SF_n(self, xN, yN, zN): ''' numerical function of above for plotting Return result in sample frame''' betaSF = express(self.Beta_cubic_DF(), self.SF, variables=True) betaSF_n = betaSF.subs([(self.SF[0], x), (self.SF[1], y), (self.SF[2], z)], simultaneous=True) np_function = lambdify((x, y, z), betaSF_n, "numpy") return np_function(xN, yN, zN)
def gcubic_SF(self): gC = b_cubic(self.a) * self.g # g in real space Cartesian coordinates gCF = gC[0] * self.CF.x + gC[1] * self.CF.y + gC[ 2] * self.CF.z # vector explicitely defined gSF = express(gCF, self.SF) x_g = gSF.dot(self.SF.x) y_g = gSF.dot(self.SF.y) z_g = gSF.dot(self.SF.z) return [x_g, y_g, z_g]
def b_SF(self): # b = a * [100] in displacement frame #bDF = self.b * self.DF.y # vector explicitely defined bDF = self.b * self.DF.x bSF = express(bDF, self.SF) x_b = bSF.dot(self.SF.x) y_b = bSF.dot(self.SF.y) z_b = bSF.dot(self.SF.z) return [x_b, y_b, z_b]
def test_coordinate_vars(): """Tests the coordinate variables functionality""" A = ReferenceFrame('A') assert CoordinateSym('Ax', A, 0) == A[0] assert CoordinateSym('Ax', A, 1) == A[1] assert CoordinateSym('Ax', A, 2) == A[2] raises(ValueError, lambda: CoordinateSym('Ax', A, 3)) q = dynamicsymbols('q') qd = dynamicsymbols('q', 1) assert isinstance(A[0], CoordinateSym) and \ isinstance(A[0], CoordinateSym) and \ isinstance(A[0], CoordinateSym) assert A.variable_map(A) == {A[0]:A[0], A[1]:A[1], A[2]:A[2]} assert A[0].frame == A B = A.orientnew('B', 'Axis', [q, A.z]) assert B.variable_map(A) == {B[2]: A[2], B[1]: -A[0]*sin(q) + A[1]*cos(q), B[0]: A[0]*cos(q) + A[1]*sin(q)} assert A.variable_map(B) == {A[0]: B[0]*cos(q) - B[1]*sin(q), A[1]: B[0]*sin(q) + B[1]*cos(q), A[2]: B[2]} assert time_derivative(B[0], A) == -A[0]*sin(q)*qd + A[1]*cos(q)*qd assert time_derivative(B[1], A) == -A[0]*cos(q)*qd - A[1]*sin(q)*qd assert time_derivative(B[2], A) == 0 assert express(B[0], A, variables=True) == A[0]*cos(q) + A[1]*sin(q) assert express(B[1], A, variables=True) == -A[0]*sin(q) + A[1]*cos(q) assert express(B[2], A, variables=True) == A[2] assert time_derivative(A[0]*A.x + A[1]*A.y + A[2]*A.z, B) == A[1]*qd*A.x - A[0]*qd*A.y assert time_derivative(B[0]*B.x + B[1]*B.y + B[2]*B.z, A) == - B[1]*qd*B.x + B[0]*qd*B.y assert express(B[0]*B[1]*B[2], A, variables=True) == \ A[2]*(-A[0]*sin(q) + A[1]*cos(q))*(A[0]*cos(q) + A[1]*sin(q)) assert (time_derivative(B[0]*B[1]*B[2], A) - (A[2]*(-A[0]**2*cos(2*q) - 2*A[0]*A[1]*sin(2*q) + A[1]**2*cos(2*q))*qd)).trigsimp() == 0 assert express(B[0]*B.x + B[1]*B.y + B[2]*B.z, A) == \ (B[0]*cos(q) - B[1]*sin(q))*A.x + (B[0]*sin(q) + \ B[1]*cos(q))*A.y + B[2]*A.z assert express(B[0]*B.x + B[1]*B.y + B[2]*B.z, A, variables=True) == \ A[0]*A.x + A[1]*A.y + A[2]*A.z assert express(A[0]*A.x + A[1]*A.y + A[2]*A.z, B) == \ (A[0]*cos(q) + A[1]*sin(q))*B.x + \ (-A[0]*sin(q) + A[1]*cos(q))*B.y + A[2]*B.z assert express(A[0]*A.x + A[1]*A.y + A[2]*A.z, B, variables=True) == \ B[0]*B.x + B[1]*B.y + B[2]*B.z N = B.orientnew('N', 'Axis', [-q, B.z]) assert N.variable_map(A) == {N[0]: A[0], N[2]: A[2], N[1]: A[1]} C = A.orientnew('C', 'Axis', [q, A.x + A.y + A.z]) mapping = A.variable_map(C) assert mapping[A[0]] == 2*C[0]*cos(q)/3 + C[0]/3 - 2*C[1]*sin(q + pi/6)/3 +\ C[1]/3 - 2*C[2]*cos(q + pi/3)/3 + C[2]/3 assert mapping[A[1]] == -2*C[0]*cos(q + pi/3)/3 + \ C[0]/3 + 2*C[1]*cos(q)/3 + C[1]/3 - 2*C[2]*sin(q + pi/6)/3 + C[2]/3 assert mapping[A[2]] == -2*C[0]*sin(q + pi/6)/3 + C[0]/3 - \ 2*C[1]*cos(q + pi/3)/3 + C[1]/3 + 2*C[2]*cos(q)/3 + C[2]/3
def scalar_potential(field, frame): """ Returns the scalar potential function of a field in a given frame (without the added integration constant). Parameters ========== field : Vector The vector field whose scalar potential function is to be calculated frame : ReferenceFrame The frame to do the calculation in Examples ======== >>> from sympy.physics.vector import ReferenceFrame >>> from sympy.physics.vector import scalar_potential, gradient >>> R = ReferenceFrame('R') >>> scalar_potential(R.z, R) == R[2] True >>> scalar_field = 2*R[0]**2*R[1]*R[2] >>> grad_field = gradient(scalar_field, R) >>> scalar_potential(grad_field, R) 2*R_x**2*R_y*R_z """ #Check whether field is conservative if not is_conservative(field): raise ValueError("Field is not conservative") if field == Vector(0): return S(0) #Express the field exntirely in frame #Subsitute coordinate variables also _check_frame(frame) field = express(field, frame, variables=True) #Make a list of dimensions of the frame dimensions = [x for x in frame] #Calculate scalar potential function temp_function = integrate(field.dot(dimensions[0]), frame[0]) for i, dim in enumerate(dimensions[1:]): partial_diff = diff(temp_function, frame[i + 1]) partial_diff = field.dot(dim) - partial_diff temp_function += integrate(partial_diff, frame[i + 1]) return temp_function
def curl(vect, frame): """ Returns the curl of a vector field computed wrt the coordinate symbols of the given frame. Parameters ========== vect : Vector The vector operand frame : ReferenceFrame The reference frame to calculate the curl in Examples ======== >>> from sympy.physics.vector import ReferenceFrame >>> from sympy.physics.vector import curl >>> R = ReferenceFrame('R') >>> v1 = R[1]*R[2]*R.x + R[0]*R[2]*R.y + R[0]*R[1]*R.z >>> curl(v1, R) 0 >>> v2 = R[0]*R[1]*R[2]*R.x >>> curl(v2, R) R_x*R_y*R.y - R_x*R_z*R.z """ _check_vector(vect) if vect == 0: return Vector(0) vect = express(vect, frame, variables=True) #A mechanical approach to avoid looping overheads vectx = vect.dot(frame.x) vecty = vect.dot(frame.y) vectz = vect.dot(frame.z) outvec = Vector(0) outvec += (diff(vectz, frame[1]) - diff(vecty, frame[2])) * frame.x outvec += (diff(vectx, frame[2]) - diff(vectz, frame[0])) * frame.y outvec += (diff(vecty, frame[0]) - diff(vectx, frame[1])) * frame.z return outvec
def divergence(vect, frame): """ Returns the divergence of a vector field computed wrt the coordinate symbols of the given frame. Parameters ========== vect : Vector The vector operand frame : ReferenceFrame The reference frame to calculate the divergence in Examples ======== >>> from sympy.physics.vector import ReferenceFrame >>> from sympy.physics.vector import divergence >>> R = ReferenceFrame('R') >>> v1 = R[0]*R[1]*R[2] * (R.x+R.y+R.z) >>> divergence(v1, R) R_x*R_y + R_x*R_z + R_y*R_z >>> v2 = 2*R[1]*R[2]*R.y >>> divergence(v2, R) 2*R_z """ _check_vector(vect) if vect == 0: return S(0) vect = express(vect, frame, variables=True) vectx = vect.dot(frame.x) vecty = vect.dot(frame.y) vectz = vect.dot(frame.z) out = S(0) out += diff(vectx, frame[0]) out += diff(vecty, frame[1]) out += diff(vectz, frame[2]) return out
def test_coordinate_vars(): """Tests the coordinate variables functionality""" A = ReferenceFrame("A") assert CoordinateSym("Ax", A, 0) == A[0] assert CoordinateSym("Ax", A, 1) == A[1] assert CoordinateSym("Ax", A, 2) == A[2] raises(ValueError, lambda: CoordinateSym("Ax", A, 3)) q = dynamicsymbols("q") qd = dynamicsymbols("q", 1) assert (isinstance(A[0], CoordinateSym) and isinstance(A[0], CoordinateSym) and isinstance(A[0], CoordinateSym)) assert A.variable_map(A) == {A[0]: A[0], A[1]: A[1], A[2]: A[2]} assert A[0].frame == A B = A.orientnew("B", "Axis", [q, A.z]) assert B.variable_map(A) == { B[2]: A[2], B[1]: -A[0] * sin(q) + A[1] * cos(q), B[0]: A[0] * cos(q) + A[1] * sin(q), } assert A.variable_map(B) == { A[0]: B[0] * cos(q) - B[1] * sin(q), A[1]: B[0] * sin(q) + B[1] * cos(q), A[2]: B[2], } assert time_derivative(B[0], A) == -A[0] * sin(q) * qd + A[1] * cos(q) * qd assert time_derivative(B[1], A) == -A[0] * cos(q) * qd - A[1] * sin(q) * qd assert time_derivative(B[2], A) == 0 assert express(B[0], A, variables=True) == A[0] * cos(q) + A[1] * sin(q) assert express(B[1], A, variables=True) == -A[0] * sin(q) + A[1] * cos(q) assert express(B[2], A, variables=True) == A[2] assert (time_derivative(A[0] * A.x + A[1] * A.y + A[2] * A.z, B) == A[1] * qd * A.x - A[0] * qd * A.y) assert (time_derivative(B[0] * B.x + B[1] * B.y + B[2] * B.z, A) == -B[1] * qd * B.x + B[0] * qd * B.y) assert express(B[0] * B[1] * B[2], A, variables=True) == A[2] * ( -A[0] * sin(q) + A[1] * cos(q)) * (A[0] * cos(q) + A[1] * sin(q)) assert (time_derivative(B[0] * B[1] * B[2], A) - (A[2] * (-A[0]**2 * cos(2 * q) - 2 * A[0] * A[1] * sin(2 * q) + A[1]**2 * cos(2 * q)) * qd)).trigsimp() == 0 assert (express(B[0] * B.x + B[1] * B.y + B[2] * B.z, A) == (B[0] * cos(q) - B[1] * sin(q)) * A.x + (B[0] * sin(q) + B[1] * cos(q)) * A.y + B[2] * A.z) assert (express(B[0] * B.x + B[1] * B.y + B[2] * B.z, A, variables=True) == A[0] * A.x + A[1] * A.y + A[2] * A.z) assert (express(A[0] * A.x + A[1] * A.y + A[2] * A.z, B) == (A[0] * cos(q) + A[1] * sin(q)) * B.x + (-A[0] * sin(q) + A[1] * cos(q)) * B.y + A[2] * B.z) assert (express(A[0] * A.x + A[1] * A.y + A[2] * A.z, B, variables=True) == B[0] * B.x + B[1] * B.y + B[2] * B.z) N = B.orientnew("N", "Axis", [-q, B.z]) assert N.variable_map(A) == {N[0]: A[0], N[2]: A[2], N[1]: A[1]} C = A.orientnew("C", "Axis", [q, A.x + A.y + A.z]) mapping = A.variable_map(C) assert (mapping[A[0]] == 2 * C[0] * cos(q) / 3 + C[0] / 3 - 2 * C[1] * sin(q + pi / 6) / 3 + C[1] / 3 - 2 * C[2] * cos(q + pi / 3) / 3 + C[2] / 3) assert (mapping[A[1]] == -2 * C[0] * cos(q + pi / 3) / 3 + C[0] / 3 + 2 * C[1] * cos(q) / 3 + C[1] / 3 - 2 * C[2] * sin(q + pi / 6) / 3 + C[2] / 3) assert (mapping[A[2]] == -2 * C[0] * sin(q + pi / 6) / 3 + C[0] / 3 - 2 * C[1] * cos(q + pi / 3) / 3 + C[1] / 3 + 2 * C[2] * cos(q) / 3 + C[2] / 3)
def test_express(): assert express(Vector(0), N) == Vector(0) assert express(S(0), N) == S(0) assert express(A.x, C) == cos(q3) * C.x + sin(q3) * C.z assert express(A.y, C) == sin(q2)*sin(q3)*C.x + cos(q2)*C.y - \ sin(q2)*cos(q3)*C.z assert express(A.z, C) == -sin(q3)*cos(q2)*C.x + sin(q2)*C.y + \ cos(q2)*cos(q3)*C.z assert express(A.x, N) == cos(q1) * N.x + sin(q1) * N.y assert express(A.y, N) == -sin(q1) * N.x + cos(q1) * N.y assert express(A.z, N) == N.z assert express(A.x, A) == A.x assert express(A.y, A) == A.y assert express(A.z, A) == A.z assert express(A.x, B) == B.x assert express(A.y, B) == cos(q2) * B.y - sin(q2) * B.z assert express(A.z, B) == sin(q2) * B.y + cos(q2) * B.z assert express(A.x, C) == cos(q3) * C.x + sin(q3) * C.z assert express(A.y, C) == sin(q2)*sin(q3)*C.x + cos(q2)*C.y - \ sin(q2)*cos(q3)*C.z assert express(A.z, C) == -sin(q3)*cos(q2)*C.x + sin(q2)*C.y + \ cos(q2)*cos(q3)*C.z # Check to make sure UnitVectors get converted properly assert express(N.x, N) == N.x assert express(N.y, N) == N.y assert express(N.z, N) == N.z assert express(N.x, A) == (cos(q1) * A.x - sin(q1) * A.y) assert express(N.y, A) == (sin(q1) * A.x + cos(q1) * A.y) assert express(N.z, A) == A.z assert express(N.x, B) == (cos(q1) * B.x - sin(q1) * cos(q2) * B.y + sin(q1) * sin(q2) * B.z) assert express(N.y, B) == (sin(q1) * B.x + cos(q1) * cos(q2) * B.y - sin(q2) * cos(q1) * B.z) assert express(N.z, B) == (sin(q2) * B.y + cos(q2) * B.z) assert express( N.x, C) == ((cos(q1) * cos(q3) - sin(q1) * sin(q2) * sin(q3)) * C.x - sin(q1) * cos(q2) * C.y + (sin(q3) * cos(q1) + sin(q1) * sin(q2) * cos(q3)) * C.z) assert express( N.y, C) == ((sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1)) * C.x + cos(q1) * cos(q2) * C.y + (sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3)) * C.z) assert express(N.z, C) == (-sin(q3) * cos(q2) * C.x + sin(q2) * C.y + cos(q2) * cos(q3) * C.z) assert express(A.x, N) == (cos(q1) * N.x + sin(q1) * N.y) assert express(A.y, N) == (-sin(q1) * N.x + cos(q1) * N.y) assert express(A.z, N) == N.z assert express(A.x, A) == A.x assert express(A.y, A) == A.y assert express(A.z, A) == A.z assert express(A.x, B) == B.x assert express(A.y, B) == (cos(q2) * B.y - sin(q2) * B.z) assert express(A.z, B) == (sin(q2) * B.y + cos(q2) * B.z) assert express(A.x, C) == (cos(q3) * C.x + sin(q3) * C.z) assert express(A.y, C) == (sin(q2) * sin(q3) * C.x + cos(q2) * C.y - sin(q2) * cos(q3) * C.z) assert express(A.z, C) == (-sin(q3) * cos(q2) * C.x + sin(q2) * C.y + cos(q2) * cos(q3) * C.z) assert express(B.x, N) == (cos(q1) * N.x + sin(q1) * N.y) assert express(B.y, N) == (-sin(q1) * cos(q2) * N.x + cos(q1) * cos(q2) * N.y + sin(q2) * N.z) assert express(B.z, N) == (sin(q1) * sin(q2) * N.x - sin(q2) * cos(q1) * N.y + cos(q2) * N.z) assert express(B.x, A) == A.x assert express(B.y, A) == (cos(q2) * A.y + sin(q2) * A.z) assert express(B.z, A) == (-sin(q2) * A.y + cos(q2) * A.z) assert express(B.x, B) == B.x assert express(B.y, B) == B.y assert express(B.z, B) == B.z assert express(B.x, C) == (cos(q3) * C.x + sin(q3) * C.z) assert express(B.y, C) == C.y assert express(B.z, C) == (-sin(q3) * C.x + cos(q3) * C.z) assert express( C.x, N) == ((cos(q1) * cos(q3) - sin(q1) * sin(q2) * sin(q3)) * N.x + (sin(q1) * cos(q3) + sin(q2) * sin(q3) * cos(q1)) * N.y - sin(q3) * cos(q2) * N.z) assert express(C.y, N) == (-sin(q1) * cos(q2) * N.x + cos(q1) * cos(q2) * N.y + sin(q2) * N.z) assert express( C.z, N) == ((sin(q3) * cos(q1) + sin(q1) * sin(q2) * cos(q3)) * N.x + (sin(q1) * sin(q3) - sin(q2) * cos(q1) * cos(q3)) * N.y + cos(q2) * cos(q3) * N.z) assert express(C.x, A) == (cos(q3) * A.x + sin(q2) * sin(q3) * A.y - sin(q3) * cos(q2) * A.z) assert express(C.y, A) == (cos(q2) * A.y + sin(q2) * A.z) assert express(C.z, A) == (sin(q3) * A.x - sin(q2) * cos(q3) * A.y + cos(q2) * cos(q3) * A.z) assert express(C.x, B) == (cos(q3) * B.x - sin(q3) * B.z) assert express(C.y, B) == B.y assert express(C.z, B) == (sin(q3) * B.x + cos(q3) * B.z) assert express(C.x, C) == C.x assert express(C.y, C) == C.y assert express(C.z, C) == C.z == (C.z) # Check to make sure Vectors get converted back to UnitVectors assert N.x == express((cos(q1) * A.x - sin(q1) * A.y), N) assert N.y == express((sin(q1) * A.x + cos(q1) * A.y), N) assert N.x == express( (cos(q1) * B.x - sin(q1) * cos(q2) * B.y + sin(q1) * sin(q2) * B.z), N) assert N.y == express( (sin(q1) * B.x + cos(q1) * cos(q2) * B.y - sin(q2) * cos(q1) * B.z), N) assert N.z == express((sin(q2) * B.y + cos(q2) * B.z), N) """ These don't really test our code, they instead test the auto simplification (or lack thereof) of SymPy. assert N.x == express(( (cos(q1)*cos(q3)-sin(q1)*sin(q2)*sin(q3))*C.x - sin(q1)*cos(q2)*C.y + (sin(q3)*cos(q1)+sin(q1)*sin(q2)*cos(q3))*C.z), N) assert N.y == express(( (sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*C.x + cos(q1)*cos(q2)*C.y + (sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*C.z), N) assert N.z == express((-sin(q3)*cos(q2)*C.x + sin(q2)*C.y + cos(q2)*cos(q3)*C.z), N) """ assert A.x == express((cos(q1) * N.x + sin(q1) * N.y), A) assert A.y == express((-sin(q1) * N.x + cos(q1) * N.y), A) assert A.y == express((cos(q2) * B.y - sin(q2) * B.z), A) assert A.z == express((sin(q2) * B.y + cos(q2) * B.z), A) assert A.x == express((cos(q3) * C.x + sin(q3) * C.z), A) # Tripsimp messes up here too. #print express((sin(q2)*sin(q3)*C.x + cos(q2)*C.y - # sin(q2)*cos(q3)*C.z), A) assert A.y == express( (sin(q2) * sin(q3) * C.x + cos(q2) * C.y - sin(q2) * cos(q3) * C.z), A) assert A.z == express( (-sin(q3) * cos(q2) * C.x + sin(q2) * C.y + cos(q2) * cos(q3) * C.z), A) assert B.x == express((cos(q1) * N.x + sin(q1) * N.y), B) assert B.y == express( (-sin(q1) * cos(q2) * N.x + cos(q1) * cos(q2) * N.y + sin(q2) * N.z), B) assert B.z == express( (sin(q1) * sin(q2) * N.x - sin(q2) * cos(q1) * N.y + cos(q2) * N.z), B) assert B.y == express((cos(q2) * A.y + sin(q2) * A.z), B) assert B.z == express((-sin(q2) * A.y + cos(q2) * A.z), B) assert B.x == express((cos(q3) * C.x + sin(q3) * C.z), B) assert B.z == express((-sin(q3) * C.x + cos(q3) * C.z), B) """ assert C.x == express(( (cos(q1)*cos(q3)-sin(q1)*sin(q2)*sin(q3))*N.x + (sin(q1)*cos(q3)+sin(q2)*sin(q3)*cos(q1))*N.y - sin(q3)*cos(q2)*N.z), C) assert C.y == express(( -sin(q1)*cos(q2)*N.x + cos(q1)*cos(q2)*N.y + sin(q2)*N.z), C) assert C.z == express(( (sin(q3)*cos(q1)+sin(q1)*sin(q2)*cos(q3))*N.x + (sin(q1)*sin(q3)-sin(q2)*cos(q1)*cos(q3))*N.y + cos(q2)*cos(q3)*N.z), C) """ assert C.x == express( (cos(q3) * A.x + sin(q2) * sin(q3) * A.y - sin(q3) * cos(q2) * A.z), C) assert C.y == express((cos(q2) * A.y + sin(q2) * A.z), C) assert C.z == express( (sin(q3) * A.x - sin(q2) * cos(q3) * A.y + cos(q2) * cos(q3) * A.z), C) assert C.x == express((cos(q3) * B.x - sin(q3) * B.z), C) assert C.z == express((sin(q3) * B.x + cos(q3) * B.z), C)
def scalar_potential_difference(field, frame, point1, point2, origin): """ Returns the scalar potential difference between two points in a certain frame, wrt a given field. If a scalar field is provided, its values at the two points are considered. If a conservative vector field is provided, the values of its scalar potential function at the two points are used. Returns (potential at position 2) - (potential at position 1) Parameters ========== field : Vector/sympyfiable The field to calculate wrt frame : ReferenceFrame The frame to do the calculations in point1 : Point The initial Point in given frame position2 : Point The second Point in the given frame origin : Point The Point to use as reference point for position vector calculation Examples ======== >>> from sympy.physics.vector import ReferenceFrame, Point >>> from sympy.physics.vector import scalar_potential_difference >>> R = ReferenceFrame('R') >>> O = Point('O') >>> P = O.locatenew('P', R[0]*R.x + R[1]*R.y + R[2]*R.z) >>> vectfield = 4*R[0]*R[1]*R.x + 2*R[0]**2*R.y >>> scalar_potential_difference(vectfield, R, O, P, O) 2*R_x**2*R_y >>> Q = O.locatenew('O', 3*R.x + R.y + 2*R.z) >>> scalar_potential_difference(vectfield, R, P, Q, O) -2*R_x**2*R_y + 18 """ _check_frame(frame) if isinstance(field, Vector): #Get the scalar potential function scalar_fn = scalar_potential(field, frame) else: #Field is a scalar scalar_fn = field #Express positions in required frame position1 = express(point1.pos_from(origin), frame, variables=True) position2 = express(point2.pos_from(origin), frame, variables=True) #Get the two positions as substitution dicts for coordinate variables subs_dict1 = {} subs_dict2 = {} for i, x in enumerate(frame): subs_dict1[frame[i]] = x.dot(position1) subs_dict2[frame[i]] = x.dot(position2) return scalar_fn.subs(subs_dict2) - scalar_fn.subs(subs_dict1)
def test_express(): assert express(Vector(0), N) == Vector(0) assert express(S(0), N) == S(0) assert express(A.x, C) == cos(q3)*C.x + sin(q3)*C.z assert express(A.y, C) == sin(q2)*sin(q3)*C.x + cos(q2)*C.y - \ sin(q2)*cos(q3)*C.z assert express(A.z, C) == -sin(q3)*cos(q2)*C.x + sin(q2)*C.y + \ cos(q2)*cos(q3)*C.z assert express(A.x, N) == cos(q1)*N.x + sin(q1)*N.y assert express(A.y, N) == -sin(q1)*N.x + cos(q1)*N.y assert express(A.z, N) == N.z assert express(A.x, A) == A.x assert express(A.y, A) == A.y assert express(A.z, A) == A.z assert express(A.x, B) == B.x assert express(A.y, B) == cos(q2)*B.y - sin(q2)*B.z assert express(A.z, B) == sin(q2)*B.y + cos(q2)*B.z assert express(A.x, C) == cos(q3)*C.x + sin(q3)*C.z assert express(A.y, C) == sin(q2)*sin(q3)*C.x + cos(q2)*C.y - \ sin(q2)*cos(q3)*C.z assert express(A.z, C) == -sin(q3)*cos(q2)*C.x + sin(q2)*C.y + \ cos(q2)*cos(q3)*C.z # Check to make sure UnitVectors get converted properly assert express(N.x, N) == N.x assert express(N.y, N) == N.y assert express(N.z, N) == N.z assert express(N.x, A) == (cos(q1)*A.x - sin(q1)*A.y) assert express(N.y, A) == (sin(q1)*A.x + cos(q1)*A.y) assert express(N.z, A) == A.z assert express(N.x, B) == (cos(q1)*B.x - sin(q1)*cos(q2)*B.y + sin(q1)*sin(q2)*B.z) assert express(N.y, B) == (sin(q1)*B.x + cos(q1)*cos(q2)*B.y - sin(q2)*cos(q1)*B.z) assert express(N.z, B) == (sin(q2)*B.y + cos(q2)*B.z) assert express(N.x, C) == ( (cos(q1)*cos(q3) - sin(q1)*sin(q2)*sin(q3))*C.x - sin(q1)*cos(q2)*C.y + (sin(q3)*cos(q1) + sin(q1)*sin(q2)*cos(q3))*C.z) assert express(N.y, C) == ( (sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*C.x + cos(q1)*cos(q2)*C.y + (sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*C.z) assert express(N.z, C) == (-sin(q3)*cos(q2)*C.x + sin(q2)*C.y + cos(q2)*cos(q3)*C.z) assert express(A.x, N) == (cos(q1)*N.x + sin(q1)*N.y) assert express(A.y, N) == (-sin(q1)*N.x + cos(q1)*N.y) assert express(A.z, N) == N.z assert express(A.x, A) == A.x assert express(A.y, A) == A.y assert express(A.z, A) == A.z assert express(A.x, B) == B.x assert express(A.y, B) == (cos(q2)*B.y - sin(q2)*B.z) assert express(A.z, B) == (sin(q2)*B.y + cos(q2)*B.z) assert express(A.x, C) == (cos(q3)*C.x + sin(q3)*C.z) assert express(A.y, C) == (sin(q2)*sin(q3)*C.x + cos(q2)*C.y - sin(q2)*cos(q3)*C.z) assert express(A.z, C) == (-sin(q3)*cos(q2)*C.x + sin(q2)*C.y + cos(q2)*cos(q3)*C.z) assert express(B.x, N) == (cos(q1)*N.x + sin(q1)*N.y) assert express(B.y, N) == (-sin(q1)*cos(q2)*N.x + cos(q1)*cos(q2)*N.y + sin(q2)*N.z) assert express(B.z, N) == (sin(q1)*sin(q2)*N.x - sin(q2)*cos(q1)*N.y + cos(q2)*N.z) assert express(B.x, A) == A.x assert express(B.y, A) == (cos(q2)*A.y + sin(q2)*A.z) assert express(B.z, A) == (-sin(q2)*A.y + cos(q2)*A.z) assert express(B.x, B) == B.x assert express(B.y, B) == B.y assert express(B.z, B) == B.z assert express(B.x, C) == (cos(q3)*C.x + sin(q3)*C.z) assert express(B.y, C) == C.y assert express(B.z, C) == (-sin(q3)*C.x + cos(q3)*C.z) assert express(C.x, N) == ( (cos(q1)*cos(q3) - sin(q1)*sin(q2)*sin(q3))*N.x + (sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*N.y - sin(q3)*cos(q2)*N.z) assert express(C.y, N) == ( -sin(q1)*cos(q2)*N.x + cos(q1)*cos(q2)*N.y + sin(q2)*N.z) assert express(C.z, N) == ( (sin(q3)*cos(q1) + sin(q1)*sin(q2)*cos(q3))*N.x + (sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*N.y + cos(q2)*cos(q3)*N.z) assert express(C.x, A) == (cos(q3)*A.x + sin(q2)*sin(q3)*A.y - sin(q3)*cos(q2)*A.z) assert express(C.y, A) == (cos(q2)*A.y + sin(q2)*A.z) assert express(C.z, A) == (sin(q3)*A.x - sin(q2)*cos(q3)*A.y + cos(q2)*cos(q3)*A.z) assert express(C.x, B) == (cos(q3)*B.x - sin(q3)*B.z) assert express(C.y, B) == B.y assert express(C.z, B) == (sin(q3)*B.x + cos(q3)*B.z) assert express(C.x, C) == C.x assert express(C.y, C) == C.y assert express(C.z, C) == C.z == (C.z) # Check to make sure Vectors get converted back to UnitVectors assert N.x == express((cos(q1)*A.x - sin(q1)*A.y), N) assert N.y == express((sin(q1)*A.x + cos(q1)*A.y), N) assert N.x == express((cos(q1)*B.x - sin(q1)*cos(q2)*B.y + sin(q1)*sin(q2)*B.z), N) assert N.y == express((sin(q1)*B.x + cos(q1)*cos(q2)*B.y - sin(q2)*cos(q1)*B.z), N) assert N.z == express((sin(q2)*B.y + cos(q2)*B.z), N) """ These don't really test our code, they instead test the auto simplification (or lack thereof) of SymPy. assert N.x == express(( (cos(q1)*cos(q3)-sin(q1)*sin(q2)*sin(q3))*C.x - sin(q1)*cos(q2)*C.y + (sin(q3)*cos(q1)+sin(q1)*sin(q2)*cos(q3))*C.z), N) assert N.y == express(( (sin(q1)*cos(q3) + sin(q2)*sin(q3)*cos(q1))*C.x + cos(q1)*cos(q2)*C.y + (sin(q1)*sin(q3) - sin(q2)*cos(q1)*cos(q3))*C.z), N) assert N.z == express((-sin(q3)*cos(q2)*C.x + sin(q2)*C.y + cos(q2)*cos(q3)*C.z), N) """ assert A.x == express((cos(q1)*N.x + sin(q1)*N.y), A) assert A.y == express((-sin(q1)*N.x + cos(q1)*N.y), A) assert A.y == express((cos(q2)*B.y - sin(q2)*B.z), A) assert A.z == express((sin(q2)*B.y + cos(q2)*B.z), A) assert A.x == express((cos(q3)*C.x + sin(q3)*C.z), A) # Tripsimp messes up here too. #print express((sin(q2)*sin(q3)*C.x + cos(q2)*C.y - # sin(q2)*cos(q3)*C.z), A) assert A.y == express((sin(q2)*sin(q3)*C.x + cos(q2)*C.y - sin(q2)*cos(q3)*C.z), A) assert A.z == express((-sin(q3)*cos(q2)*C.x + sin(q2)*C.y + cos(q2)*cos(q3)*C.z), A) assert B.x == express((cos(q1)*N.x + sin(q1)*N.y), B) assert B.y == express((-sin(q1)*cos(q2)*N.x + cos(q1)*cos(q2)*N.y + sin(q2)*N.z), B) assert B.z == express((sin(q1)*sin(q2)*N.x - sin(q2)*cos(q1)*N.y + cos(q2)*N.z), B) assert B.y == express((cos(q2)*A.y + sin(q2)*A.z), B) assert B.z == express((-sin(q2)*A.y + cos(q2)*A.z), B) assert B.x == express((cos(q3)*C.x + sin(q3)*C.z), B) assert B.z == express((-sin(q3)*C.x + cos(q3)*C.z), B) """ assert C.x == express(( (cos(q1)*cos(q3)-sin(q1)*sin(q2)*sin(q3))*N.x + (sin(q1)*cos(q3)+sin(q2)*sin(q3)*cos(q1))*N.y - sin(q3)*cos(q2)*N.z), C) assert C.y == express(( -sin(q1)*cos(q2)*N.x + cos(q1)*cos(q2)*N.y + sin(q2)*N.z), C) assert C.z == express(( (sin(q3)*cos(q1)+sin(q1)*sin(q2)*cos(q3))*N.x + (sin(q1)*sin(q3)-sin(q2)*cos(q1)*cos(q3))*N.y + cos(q2)*cos(q3)*N.z), C) """ assert C.x == express((cos(q3)*A.x + sin(q2)*sin(q3)*A.y - sin(q3)*cos(q2)*A.z), C) assert C.y == express((cos(q2)*A.y + sin(q2)*A.z), C) assert C.z == express((sin(q3)*A.x - sin(q2)*cos(q3)*A.y + cos(q2)*cos(q3)*A.z), C) assert C.x == express((cos(q3)*B.x - sin(q3)*B.z), C) assert C.z == express((sin(q3)*B.x + cos(q3)*B.z), C)