【发布时间】:2021-02-22 10:56:13
【问题描述】:
import numpy as np
class Body(object): # Class for planets / moons / asteroids
def __init__(self, mass, velocity, position):
self.mass = mass # Defining variables
self.velocity = velocity
self.position = position
def move(self, force, t):
self.velocity = np.add(self.velocity + force * t / self.mass) # Code to move with time t
self.position = np.add(self.position + self.velocity * t)
return self
def main():
mars = Body(6.4185*10**23, np.array([0, 0]), np.array([0, 0]))
mars.move(np.array([10, 10]), 10) # Force defined to be [10, 10] and time 10
main()
错误
Traceback (most recent call last):
File "c:\Users\p\Documents\dev\sodump\npadd.py", line 23, in <module>
main()
File "c:\Users\p\Documents\dev\sodump\npadd.py", line 20, in main
mars.move(np.array([10, 10]), 10) # Force defined to be [10, 10] and time 10
File "c:\Users\p\Documents\dev\sodump\npadd.py", line 13, in move
self.velocity = np.add(self.velocity + force * t / self.mass) # Code to move with time t
ValueError: invalid number of arguments
【问题讨论】:
-
请同时添加错误堆栈跟踪
-
您的意思可能是
np.add(self.position, self.velocity * t)而不是np.add(self.position + self.velocity * t)?该函数接受两个参数并将它们相加。您正在使用常规的+运算符进行添加。 Here'snp.add文档 -
看起来 np.add 有两个参数,但您只提供了一个。
-
此外,鉴于
+运算符可用,您可能想写self.position = self.position + self.velocity * t甚至self.position += self.velocity * t;比np.add(...)符号更容易阅读... -
谢谢保罗,按预期工作,掌心