在 sympy 中取未知矩阵与未知函数的叉积
Taking the cross product of an unknown matrix with an unknown function in sympy
我希望 sympy 确认这一点,给定等式:
$$
r'(t) = A \times r(t)
$$
(即"the derivative of an unknown function r is the cross product of an unknown matrix A with r"),则:
$$
r''(t) = A \times r'(t)
$$
(即 "the second derivative of r is the cross product of A with the first derivative of r")。
从文档看来我想对 A
使用 MatrixSymbol
,但是 MatrixSymbol
没有定义 cross
:
from sympy import *
from sympy.abc import *
r = Function('r')(t)
A = MatrixSymbol('A', 4, 4) # dummy dimensions
Derivative(A.cross(r))
给我:
AttributeError Traceback (most recent call last)
<ipython-input-52-4c8dc7c142cf> in <module>
4 A = MatrixSymbol('A', 4, 4)
5
----> 6 Derivative(A.cross(r))
AttributeError: 'MatrixSymbol' object has no attribute 'cross'
正确的做法是什么?
SymPy 的向量 class 与矩阵 class 完全分开,如果您习惯于将向量视为一种特定类型的矩阵,这可能会造成混淆:
https://docs.sympy.org/latest/modules/vector/index.html
我将演示如何使用向量 class 执行此操作。这可以更紧凑地完成,但我会详细说明:
In [23]: from sympy.vector import CoordSys3D
...: N = CoordSys3D('N')
In [24]: a1, a2, a3 = symbols('a1:4')
In [25]: r1, r2, r3 = [ri(t) for ri in symbols('r1:4', cls=Function)]
In [26]: A = a1*i + a2*j + a3*k
In [27]: r = r1*i + r2*j + r3*k
In [28]: A
Out[28]: a1*N.i + a2*N.j + a3*N.k
In [29]: r
Out[29]: (r1(t))*N.i + (r2(t))*N.j + (r3(t))*N.k
In [30]: (A.cross(r.diff(t))).diff(t) == A.cross(r.diff(t, 2))
Out[30]: True
我希望 sympy 确认这一点,给定等式:
$$ r'(t) = A \times r(t) $$
(即"the derivative of an unknown function r is the cross product of an unknown matrix A with r"),则:
$$ r''(t) = A \times r'(t) $$
(即 "the second derivative of r is the cross product of A with the first derivative of r")。
从文档看来我想对 A
使用 MatrixSymbol
,但是 MatrixSymbol
没有定义 cross
:
from sympy import *
from sympy.abc import *
r = Function('r')(t)
A = MatrixSymbol('A', 4, 4) # dummy dimensions
Derivative(A.cross(r))
给我:
AttributeError Traceback (most recent call last)
<ipython-input-52-4c8dc7c142cf> in <module>
4 A = MatrixSymbol('A', 4, 4)
5
----> 6 Derivative(A.cross(r))
AttributeError: 'MatrixSymbol' object has no attribute 'cross'
正确的做法是什么?
SymPy 的向量 class 与矩阵 class 完全分开,如果您习惯于将向量视为一种特定类型的矩阵,这可能会造成混淆: https://docs.sympy.org/latest/modules/vector/index.html
我将演示如何使用向量 class 执行此操作。这可以更紧凑地完成,但我会详细说明:
In [23]: from sympy.vector import CoordSys3D
...: N = CoordSys3D('N')
In [24]: a1, a2, a3 = symbols('a1:4')
In [25]: r1, r2, r3 = [ri(t) for ri in symbols('r1:4', cls=Function)]
In [26]: A = a1*i + a2*j + a3*k
In [27]: r = r1*i + r2*j + r3*k
In [28]: A
Out[28]: a1*N.i + a2*N.j + a3*N.k
In [29]: r
Out[29]: (r1(t))*N.i + (r2(t))*N.j + (r3(t))*N.k
In [30]: (A.cross(r.diff(t))).diff(t) == A.cross(r.diff(t, 2))
Out[30]: True