如何交换功能来自功能的元素?

发布于 2025-01-31 05:48:25 字数 769 浏览 5 评论 0原文

因此,我定义了一个称为“颗粒”的类:

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

P = [Particle(posInit = i, momInit = j, spin = choice(["up", "down"])) for i,j in zip(Zinitial,Pinitial)]

我现在想做的是如果满足某个条件,请切换粒子的位置。 因此,类似以下内容:

if cond==True:
  P[1].posfT[t], P[2].posfT[t1] = P[2].posfT[t1], P[1].posfT[t]

但是以上内容不起作用,因为我试图将函数分配给一个值。

所以我不确定该怎么做?

So I have defined a class called "Particles":

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

P = [Particle(posInit = i, momInit = j, spin = choice(["up", "down"])) for i,j in zip(Zinitial,Pinitial)]

What I now want to do is switch the positions of the particles if a certain condition is met.
So something like the following:

if cond==True:
  P[1].posfT[t], P[2].posfT[t1] = P[2].posfT[t1], P[1].posfT[t]

But the above does not work since I am trying to assign to a function a value.

So I am not sure how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔岸观火 2025-02-07 05:48:25

看来您打算将粒子的位置和动力随时间存储在posftmomft (编辑:我以前以前以前都认为您只想要当前位置)。如果是这样,它们不应该是方法,而是属性。您还应该有单独的方法来修改这些值,随着T的发展。我建议这样修改您的班级:

class Particle:
    
    def __init__(self, posInit, momInit, spin):
        self.posInit = posInit
        self.momInit = momInit
        self.spin = spin
        self.momfT = dict()
        self.posfT = dict()
        self.calc_at_time(0)
    
    def calc_at_time(self, t):
        self.momfT[t] = self.momInit*(math.cos(t))-self.posInit*(math.sin(t))
        self.posfT[t] = self.posInit*(math.cos(t))+self.momInit*(math.sin(t))

注意:我假设POSFT和MOMFT使用t = 0初始化。我绝对缺乏知道这是否正确的知识,请根据需要检查并纠正。我在这里专注于代码。

然后,您可以通过调用calc_at_time(t)来在时间t上设置新位置

t = 1
t1 = 2
P[1].calc_at_time(t)
P[1].calc_at_time(t1)
P[2].calc_at_time(t)
P[2].calc_at_time(t1)

,并使用p [x] .posft [t]在时间t上访问P [X]的位置

因此,您要做的事情现在应该有效:

if cond == True:
  P[1].posfT[t], P[2].posfT[t1] = P[2].posfT[t1], P[1].posfT[t]

含义:p [1]的p [t]的posft变成t1 p [2]的p [t],并相互互惠。

It seems that you intend to store the position and momentum of your particle over time in posfT and momfT respectfully (edit: I previously thought you wanted only the current position). If so, they should not be methods, but attributes. You should also have separate methods to modify those values as t evolves. I suggest to modify your class like this:

class Particle:
    
    def __init__(self, posInit, momInit, spin):
        self.posInit = posInit
        self.momInit = momInit
        self.spin = spin
        self.momfT = dict()
        self.posfT = dict()
        self.calc_at_time(0)
    
    def calc_at_time(self, t):
        self.momfT[t] = self.momInit*(math.cos(t))-self.posInit*(math.sin(t))
        self.posfT[t] = self.posInit*(math.cos(t))+self.momInit*(math.sin(t))

Note: I am assuming that posfT and momfT are initialized with t=0. I absolutely lack the knowledge to know if this is correct, please check and correct as necessary. I am focusing on code here.

You will then be able to set new positions at time t by calling calc_at_time(t)

t = 1
t1 = 2
P[1].calc_at_time(t)
P[1].calc_at_time(t1)
P[2].calc_at_time(t)
P[2].calc_at_time(t1)

And access the position of P[x] at time t with P[x].posfT[t].

Accordingly, what you're trying to do should now work:

if cond == True:
  P[1].posfT[t], P[2].posfT[t1] = P[2].posfT[t1], P[1].posfT[t]

Meaning: posfT of P[1] at [t] becomes posfT of P[2] at t1, and reciprocally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文