The code below simulates a given Newtonian 3 body system. Each row per slice is supposed to represent a Cartesian component for the initial trajectory vector and for the distance vectors to the other two objects.
What are some possible improvements to this? For instance is there any benefit to implement something to cut the redundancy with the duplicate distance vectors? How about using something like NumPy instead of vanilla?
# GRAV_MASS_CONSTANT = 1
# masses = [1, 1, 1]
def updated_trajectory_component(row):
# a = masses * GRAV_MASS_CONSTANT
a = 1
return row[0] + sum([a / i for i in row[1:]])
def updated_distance_component(row):
return [i - row[0] for i in row[1:]]
def iterate_row(row):
dist = updated_distance_component(row)
return [updated_trajectory_component(row)] + dist
def iterate_slice(slice):
return [iterate_row(i) for i in slice]
def iterate_cube(cube):
return [iterate_slice(i) for i in cube]
def num_iter(cube, iter):
print(cube)
if iter == 1: return iterate_cube(cube)
else: return num_iter(iterate_cube(cube), iter - 1)
cube = [
[
[0, 0.1, 1],
[0, 1, 0.1],
[0, 1, 1]
],
[
[0, 0.1, 1],
[0, -1, 1],
[0, -1, 0.1]
],
[
[0, -1, -1],
[0, 0.1, -1],
[0, -1, 0.1]
]
]