我可以在sympy中使用符号叉积运算吗

2024-06-16 08:52:44 发布

您现在位置:Python中文网/ 问答频道 /正文

在 嗨

我想写一个符号库来计算三维多项式 以分析的方式(变量是一个实值t,多项式的单项式是3D向量)。特别是,我想计算两个多项式的叉积(在that question的后续部分中)。在Sympy中,我发现我可以:

  1. 使用来自物理力学专题包,在本例中定义了叉积。在
  2. 使用矩阵符号,但不定义这些符号的叉积。在

有没有什么方法可以在sympy中象征性地表示叉积?在

编辑:特别是,我对两个相同向量的叉积求零感兴趣,并在用其中一个向量对多项式进行因式分解时考虑到反互易性。在

新建编辑: 为了让我自己更清楚,我想停留在一个“象征性的层面”。也就是说, 我不想沿着每个变量发展向量的项。在

例如,下面是计算贝塞尔曲线的代码:

from sympy import *

init_printing(use_unicode=True)

from scipy.special import binom

#default control points of a Bezier curve
p_is = [symbols('p'+str(i))  for i in range(5)] 


class Bezier:    
    #eq is the equation of the curve, pis are the stored control point
    #for special purpose such as the derivatives
    def __init__(self, deg, eq = None, pis = None):
        assert(deg)
        self.deg = deg;
        n_pts = deg +1 
        if pis == None:
            self.pis = p_is[:n_pts]
        else:
            self.pis = pis[:]; 
        if eq == None:
            #computing the bernstein polynoms for a given degree
            factors = [binom(deg,i) * t**i * (1-t)**(deg-i)*pis[i] for i in range(n_pts)]
            self.eq = sum(factors);
        else:
            self.eq = eq


    def __repr__(self):
        res  = "Degree : "        + str(self.deg) 
        res += "\nEquation : "    + self.eq.__repr__()
        res += "\nwaypoints :\n"  + str(self.pis) 
        return res


    def __str__(self):
        return self.__repr__()


b = Bezier(3)

print b 
# Degree : 3
# Equation : 1.0*p0*(-t + 1)**3 + 3.0*p1*t*(-t + 1)**2 + 3.0*p2*t**2*(-t + 1) + 1.0*p3*t**3
# waypoints :
# [p0, p1, p2, p3]

print b.eq
# 1.0*p0*(-t + 1)**3 + 3.0*p1*t*(-t + 1)**2 + 3.0*p2*t**2*(-t + 1) + 1.0*p3*t**3

正如我们所看到的,变量p_是向量这一事实实际上并不相关,除非在交叉积出现的情况下。 如果我要计算b与自身的叉积,那么几个项就会消失,因为一些向量会与它们自己交叉。在

我试着用一个简单的乘法来“模拟”叉积,然后迭代得到的方程,去掉所有平方项(对应于零)。但这还不够 因为产物没有保留交叉产物的反突变特性。在

我真正想要的是等式中实际的叉积符号(比如X)。我希望我更清楚

非常感谢你的帮助

史蒂夫


Tags: theselfnoneforisdef符号res
1条回答
网友
1楼 · 发布于 2024-06-16 08:52:44

我找到了一个适合我的解决方案(我只应用一次叉积,需要一些反射来考虑扩展)。其思想是使用额外的符号来表示实现叉积的斜对称矩阵。例如,p0^p1将写入Cp0*p1。我实现了一个实现这一点的方法,并确保如果pi^pi=0。在

为了实现更好的因式分解,我引入了基于符号字母顺序的任意顺序的转换。 这意味着p2^p1将被写为-Cp1*p2 这样做的原因是sympy不会检测到Cp2^p1+Cp1^p2=0之类的简化。在

不管怎样,这是相当粗糙的,但在我的情况下,它允许我写我的库,所以在这里。在

执行叉积的方法是“cross”,位于文件末尾。在

#declaring vector symbols variables
p_is = [symbols('p'+str(i))  for i in range(20)]
#the cross product will be represented 
#by the skew symmetric matrix Cpi for a vector pi.
#Since in the resulting equation, symbols seem to appeer
#in alphabetic order, the matrix multiplication will be coherent
p_isX = [symbols('Cp'+str(i)+'')  for i in range(20)]

#achieves the cross product between two elements
#s0 and s1 are the considered vector symbols (here, two pis)
#e0 and e1 are the scalar multiplier of each term
def crossElement(s0,e0,s1,e1):
    if s0 == s1:
        return 0
    else:
        # here, take order of apparition into account to allow 
        # future factorization. Otherwise
        # something like p0 X p1 + p1 X p0 will not be dientified as zero
        id0 = p_is.index(s0)
        id1 = p_is.index(s1)
        if(id0 < id1):
            return simplify(e1*e0*p_isX[id0]*s1)
        else:
            return simplify((-e1)*e0*p_isX[id1]*s0)

#retrieve all the pis and associate scalar factors
#from an equation
def getAllSymbols(eq):
    res = []
    for el in p_is:
        dic = eq.as_poly(el).as_dict()
        if(len(dic) > 1): 
            res += [(el, dic[(1,)])]
    return res;

#generates the cross product of two equations,
#where each pi term is a vector, and others
#are scalar variable.
#cross product
def cross(eq1,eq2):
    decomp1 = getAllSymbols(eq1)
    decomp2 = getAllSymbols(eq2)
    res = 0
    #applies distributive cross product between
    #all terms of both equations
    for (s0, e0) in decomp1:
        for (s1, e1) in decomp2:
            res += crossElement(s0,e0,s1,e1)
    return res

相关问题 更多 >