I am trying to create a specific output pattern for a textfile I wanted to later load into C . I wrote a file in Python that creates random coordinates and movement values in a circle. The output is supposed to have the output:
Place_1 Place_2 Place_3 Movement_1 Movement_2 Movement_3\n
Place_1 Place_2 Place_3 Movement_1 Movement_2 Movement_3\n
Place_1 Place_2 Place_3 Movement_1 Movement_2 Movement_3
The Code is use is
import numpy as np
file = open('log.txt', 'a')
def f(n, center, radius, ecc):
pos = np.zeros((n,6))
r = ecc * radius
for i in range(n):
while 1:
x_1 = -1 2 * np.random.rand(1)
x_2 = -1 2 * np.random.rand(1)
if (x_1*x_1 x_2*x_2)<1 :
pos[i,0] = center[0] r * 2 * x_1 * np.sqrt(1 - x_1*x_1 - x_2*x_2)
pos[i,1] = center[1] r * 2 * x_2 * np.sqrt(1 - x_1*x_1 - x_2*x_2)
pos[i,2] = center[2] r * (1 - 2 * (x_1*x_1 x_2*x_2))
pos[i,3] = (-1 2 * np.random.rand(1))
pos[i,4] = (-1 2 * np.random.rand(1))
pos[i,5] = (-1 2 * np.random.rand(1))
break
string = str(pos[i,:]).strip('[]').rstrip('\n')
file.write(string)
return
f(10000, np.array((127,127,127)), 92, 0.9)
file.close()
The log I create is however very badly formated. How can I get the required format?
CodePudding user response:
You're going to a lot of trouble here that you don't need. This seems to solve the problem, simply.
import numpy as np
file = open('log.txt', 'a')
def f(n, center, radius, ecc):
r = ecc * radius
for i in range(n):
while 1:
pos = [0]*6
x_1 = -1 2 * np.random.rand(1)[0]
x_2 = -1 2 * np.random.rand(1)[0]
if (x_1*x_1 x_2*x_2) < 1:
pos[0] = center[0] r * 2 * x_1 * np.sqrt(1 - x_1*x_1 - x_2*x_2)
pos[1] = center[1] r * 2 * x_2 * np.sqrt(1 - x_1*x_1 - x_2*x_2)
pos[2] = center[2] r * (1 - 2 * (x_1*x_1 x_2*x_2))
pos[3] = (-1 2 * np.random.rand(1)[0])
pos[4] = (-1 2 * np.random.rand(1)[0])
pos[5] = (-1 2 * np.random.rand(1)[0])
break
print(*pos, file=file)
f(10000, np.array((127,127,127)), 92, 0.9)
file.close()
A solution without numpy:
import math
import random
file = open('log.txt', 'a')
def f(n, center, radius, ecc):
r = ecc * radius
for _ in range(n):
pos = [0]*6
while 1:
x_1 = 2 * random.random() - 1
x_2 = 2 * random.random() - 1
vector = x_1*x_1 x_2*x_2
if vector < 1:
break
pos[0] = center[0] r * 2 * x_1 * math.sqrt(1 - vector)
pos[1] = center[1] r * 2 * x_2 * math.sqrt(1 - vector)
pos[2] = center[2] r * (1 - 2 * vector)
pos[3] = 2 * random.random() -1
pos[4] = 2 * random.random() -1
pos[5] = 2 * random.random() -1
print(*pos, file=file)
f(10000, (127,127,127), 92, 0.9)
file.close()
CodePudding user response:
Use np.savetxt
:
import numpy as np
def f(n, center, radius, ecc):
pos = np.zeros((n,6))
r = ecc * radius
for i in range(n):
while 1:
x_1 = -1 2 * np.random.rand(1)
x_2 = -1 2 * np.random.rand(1)
if (x_1*x_1 x_2*x_2)<1 :
pos[i,0] = center[0] r * 2 * x_1 * np.sqrt(1 - x_1*x_1 - x_2*x_2)
pos[i,1] = center[1] r * 2 * x_2 * np.sqrt(1 - x_1*x_1 - x_2*x_2)
pos[i,2] = center[2] r * (1 - 2 * (x_1*x_1 x_2*x_2))
pos[i,3] = (-1 2 * np.random.rand(1))
pos[i,4] = (-1 2 * np.random.rand(1))
pos[i,5] = (-1 2 * np.random.rand(1))
break
np.savetxt('file.txt',pos,delimiter=';')
return
f(100, np.array((127,127,127)), 92, 0.9)
CodePudding user response:
For the formatting issue you can make the coordinates into characters (What I mean is that turn coordinates into a character that would be printed like _ or / something like that) Also you can put empty space in a print like print(" Hello.") also to go to a different line in the console do print("\n Hello."). This probably was not what you were looking for but I hope this somewhat helps.