标量和矢量场功能¶
介绍¶
向量和标量¶
在物理学中,我们处理两种类型的量——标量和矢量。
标量是一个只有大小而没有方向的实体。标量的例子包括质量、电荷、温度、距离等。
另一方面,向量是一个由大小和方向表征的实体。向量的例子包括位移、速度、磁场等。
标量可以通过一个数字来表示,例如温度300 K。另一方面,像加速度这样的矢量量通常用矢量表示。给定一个矢量 \(\mathbf{V}\),相应量的大小可以计算为矢量本身的大小 \(\Vert \mathbf{V} \Vert\),而方向将由原始矢量方向上的单位矢量指定, \(\mathbf{\hat{V}} = \frac{\mathbf{V}}{\Vert \mathbf{V} \Vert}\)。
例如,考虑一个位移为 \((3\mathbf{\hat{i}} + 4\mathbf{\hat{j}} + 5\mathbf{\hat{k}})\) 米,其中,按照标准惯例,\(\mathbf{\hat{i}}\)、\(\mathbf{\hat{j}}\) 和 \(\mathbf{\hat{k}}\) 分别表示 \(\mathbf{X}\)、\(\mathbf{Y}\) 和 \(\mathbf{Z}\) 方向上的单位向量。因此,可以得出所行距离为 \(\Vert 3\mathbf{\hat{i}} + 4\mathbf{\hat{j}} + 5\mathbf{\hat{k}} \Vert\) 米 = \(5\sqrt{2}\) 米。行进方向由单位向量 \(\frac{3}{5\sqrt{2}}\mathbf{\hat{i}} + \frac{4}{5\sqrt{2}}\mathbf{\hat{j}} + \frac{5}{5\sqrt{2}}\mathbf{\hat{k}}\) 给出。
字段¶
一般来说,一个 \(场\) 是一个可以在空间中任意位置指定的矢量或标量量,作为位置的函数(注意,一般来说,场也可能依赖于时间和其它自定义变量)。在这个模块中,我们只处理三维空间。因此,场被定义为 \(x\)、\(y\) 和 \(z\) 坐标的函数,对应于三维空间中的一个位置。
例如,三维空间中的温度(温度场)可以写成 \(T(x, y, z)\) – 位置的标量函数。电磁学中标量场的一个例子是电势。
同样地,一个矢量场可以定义为空间中任意一点位置 \((x, y, z)\) 的矢量函数。
例如,地球上的每一点都可以被认为是处于地球的重力场中。我们可以通过重力引起的加速度(即单位质量的力)的大小和方向来指定场 \(g(x, y, z)\) 在空间中的每一点。
以电磁学为例,考虑一个形式为 \(2{x}^{2}y\) 的电势,这是一个三维空间中的标量场。相应的保守电场可以通过电势函数的梯度计算得出,并表示为 \(4xy\mathbf{\hat{i}} + 2{x}^{2}\mathbf{\hat{j}}\)。该电场的大小可以反过来表示为一个形式为 \(\sqrt{4{x}^{4} + 16{x}^{2}{y}^{2}}\) 的标量场。
sympy.physics.vector 中字段的实现¶
In sympy.physics.vector
, every ReferenceFrame
instance is
assigned basis vectors corresponding to the \(X\), \(Y\) and \(Z\)
directions. These can be accessed using the attributes named x
, y
and
z
respectively. Hence, to define a vector \(\mathbf{v}\) of the form
\(3\mathbf{\hat{i}} + 4\mathbf{\hat{j}} + 5\mathbf{\hat{k}}\) with respect
to a given frame \(\mathbf{R}\), you would do
>>> from sympy.physics.vector import ReferenceFrame
>>> R = ReferenceFrame('R')
>>> v = 3*R.x + 4*R.y + 5*R.z
关于向量的数学和基本微积分运算,在本模块文档的其他部分已经详细阐述。
On the other hand, base scalars (or coordinate variables) are implemented
as special SymPy Symbol
s assigned to every frame, one for each
direction from \(X\), \(Y\) and \(Z\). For a frame
R
, the \(X\), \(Y\) and \(Z\)
base scalar Symbol
s can be accessed using the R[0]
, R[1]
and R[2]
expressions respectively.
因此,要生成上述电势场 \(2{x}^{2}y\) 的表达式,您必须执行
>>> from sympy.physics.vector import ReferenceFrame
>>> R = ReferenceFrame('R')
>>> electric_potential = 2*R[0]**2*R[1]
>>> electric_potential
2*R_x**2*R_y
In string representation, R_x
denotes the \(X\) base
scalar assigned to ReferenceFrame
R
. Essentially, R_x
is
the string representation of R[0]
.
Scalar fields can be treated just as any other SymPy expression, for any
math/calculus functionality. Hence, to differentiate the above electric
potential with respect to \(x\) (i.e. R[0]
), you would have to use the
diff
function.
>>> from sympy.physics.vector import ReferenceFrame
>>> R = ReferenceFrame('R')
>>> electric_potential = 2*R[0]**2*R[1]
>>> from sympy import diff
>>> diff(electric_potential, R[0])
4*R_x*R_y
Like vectors (and vector fields), scalar fields can also be re-expressed in
other frames of reference, apart from the one they were defined in – assuming
that an orientation relationship exists between the concerned frames. This can
be done using the sympy.physics.vector.vector.Vector.express
method, in a way
similar to vectors - but with the variables
parameter set to
True
.
>>> from sympy.physics.vector import ReferenceFrame
>>> R = ReferenceFrame('R')
>>> electric_potential = 2*R[0]**2*R[1]
>>> from sympy.physics.vector import dynamicsymbols, express
>>> q = dynamicsymbols('q')
>>> R1 = R.orientnew('R1', rot_type = 'Axis', amounts = [q, R.z])
>>> express(electric_potential, R1, variables=True)
2*(R1_x*sin(q(t)) + R1_y*cos(q(t)))*(R1_x*cos(q(t)) - R1_y*sin(q(t)))**2
Moreover, considering scalars can also be functions of time just as vectors,
differentiation with respect to time is also possible. Depending on the
Symbol
s present in the expression and the frame
with respect to which the time differentiation is being done, the output will
change/remain the same.
>>> from sympy.physics.vector import ReferenceFrame
>>> R = ReferenceFrame('R')
>>> electric_potential = 2*R[0]**2*R[1]
>>> q = dynamicsymbols('q')
>>> R1 = R.orientnew('R1', rot_type = 'Axis', amounts = [q, R.z])
>>> from sympy.physics.vector import time_derivative
>>> time_derivative(electric_potential, R)
0
>>> time_derivative(electric_potential, R1).simplify()
2*(R1_x*cos(q(t)) - R1_y*sin(q(t)))*(3*R1_x**2*cos(2*q(t))/2 -
R1_x**2/2 - 3*R1_x*R1_y*sin(2*q(t)) - 3*R1_y**2*cos(2*q(t))/2 -
R1_y**2/2)*Derivative(q(t), t)