symbolic Four vectors

38 views
Skip to first unread message

Moises Zeleny

unread,
Apr 6, 2018, 2:02:31 AM4/6/18
to sympy
Hi, everyone

I am a new sympy user and usually I work with Jupyter notebook. I would like construct a new symbolic class named FV (Four vector), I have made this

from sympy import *
init_printing()

class FV:
   
    def __init__(self,p0,p1,p2,p3):
        self.p0 = p0
        self.p1 = p1
        self.p2 = p2
        self.p3 = p3
   
    def __str__(self):
        return '({a},{b},{c},{d})'.format(a=self.p0, b=self.p1, c=self.p2, d=self.p3)
   
    def __repr__(self):
        return self.__str__()
   
    def __add__(self,other):
        p0,p1,p2,p3 = self.p0,self.p1,self.p2,self.p3
        k0,k1,k2,k3 = other.p0,other.p1,other.p2,other.p3
        return FV(p0+k0, p1+k1, p2+k2, p3+k3)
   
    def __mul__(self,other):
        p0,p1,p2,p3 = self.p0,self.p1,self.p2,self.p3
        k0,k1,k2,k3 = other.p0,other.p1,other.p2,other.p3
        return FV(p0*k0,-p1*k1,-p2*k2,-p3*k3)
   
    def __abs__(self):
        from sympy import sqrt
        p0,p1,p2,p3 = self.p0,self.p1,self.p2,self.p3
        return sqrt(p0**2 - p1**2 - p2**2 - p3**2)
   
    def __eq__(self,other):
        return self.p0 == other.p0 and self.p1 == other.p1 and self.p2 == other.p2 and self.p3 == other.p3
   
    def __neq__(self,other):
        return not self.__eq__(other)

but this works while I use float or int python objects for the components in FV, when I use symbols I obtained

p = {i:symbols('{{p^{a}}}'.format(a=i)) for i in range(4)}
pmu = FV(p[0],p[1],p[2],p[3])
pmu

({p^0},{p^1},{p^2},{p^3})

this output do not used a init_printing(). Can someone help me please?

Francesco Bonazzi

unread,
Apr 6, 2018, 4:54:26 AM4/6/18
to sympy
Hi,

I would suggest you to use objects that are already available in SymPy. For example Array. You could call Array([p0, p1, p2, p3]). Unfortunately the behaviour under multiplication is different than yours, so I would suggest you to subclass Array and overload the methods you need to use.
Message has been deleted

Moises Zeleny

unread,
Apr 6, 2018, 12:17:48 PM4/6/18
to sympy


Thank you so much, I am going to follow your suggestion Francesco
Reply all
Reply to author
Forward
0 new messages