Home > database >  A class for spin up and down particles?
A class for spin up and down particles?

Time:05-20

So I have a bunch of particles which can be of two kinds let's say "up" and "down". I am new to python but I know that this segregation can be done with the help of a class.

Let's say I have a list:

T=np.linspace(0,1,1000)

I want each element of this list to be either one of the two particles but I am not sure if I should create two classes with one "kind" each or a single class with two "instances" and assign each element an instance.

In the end, what I want to do is randomly distribute this property of being "up" and "down" over the list T.

CodePudding user response:

There is not enough info. You most likely don't want 2 instances when dealing with 1000 items - each item should be probably represented by separate instance of a class. But that doesn't mean that having 2 classes is a way to go. Are the particles relatively similiar and besides the difference of a single property they have the same behaviour? If so, I'd go for single class, 1000 instances and storing the up/down property in some instance attribute.

EDIT: This is how I would personally implement this with for the current requirements in question:

class Particle:
    def __init__(self, posInit, momInit, spin):
        self.posInit = posInit
        self.momInit = momInit
        self.spin = spin
    def momOfT(self, t):
        return self.momInit*(math.cos(t)) self.posInit*(math.sin(t))
    def posOfT(self, t):
        return self.posInit*(math.cos(t))-self.momInit*(math.sin(t))


T = [Particle(posInit = i, momInit = 1-i, spin = random.choice(["up", "down"])) for i in np.linspace(0, 1, 1001)]

print([T[0].posOfT(i) for i in np.linspace(0, 3.1415*2, 1000)])
  • Related