如何交换功能来自功能的元素?
因此,我定义了一个称为“颗粒”的类:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您打算将粒子的位置和动力随时间存储在
posft
和momft
(编辑:我以前以前以前都认为您只想要当前位置)。如果是这样,它们不应该是方法,而是属性。您还应该有单独的方法来修改这些值,随着T的发展。我建议这样修改您的班级:注意:我假设POSFT和MOMFT使用t = 0初始化。我绝对缺乏知道这是否正确的知识,请根据需要检查并纠正。我在这里专注于代码。
然后,您可以通过调用
calc_at_time(t)
来在时间t上设置新位置,并使用
p [x] .posft [t]在时间t上访问P [X]的位置
。因此,您要做的事情现在应该有效:
含义: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
andmomfT
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: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)
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:
Meaning: posfT of P[1] at [t] becomes posfT of P[2] at t1, and reciprocally.