2 years ago
#158377

EmptySet
Is there a way to create a vector field from a list of vectors in numpy?
I have some code where I record a list of vectors over a trajectory. For each of these I have the N dimensional point from which it originates, and the vector from that point.
I have all of these ordered in an array from start to finish, and I would like to have these N dimensional vectors placed at points in the space relating to their origin so that I can calculate the divergence.
Example origin points:
[[-0.03194652 -0.02481244 0.02337171 -0.04088087]
[-0.03244277 0.16996671 0.02255409 -0.32609914]
[-0.02904343 0.3647604 0.01603211 -0.61158502]
[-0.02174823 0.16941809 0.00380041 -0.31389597]
[-0.01835986 -0.02575779 -0.00247751 -0.02001695]
[-0.01887502 0.1693996 -0.00287785 -0.31348053]
[-0.01548703 -0.02568124 -0.00914746 -0.02170657]
[-0.01600065 -0.22067082 -0.00958159 0.26807625]
[-0.02041407 -0.02541345 -0.00422007 -0.02761331]
[-0.02092234 0.16976877 -0.00477233 -0.32162472]]
Example vectors at each point
[[-0.00049625 0.19477914 -0.00081762 -0.28521826]
[ 0.00339933 0.19479369 -0.00652198 -0.28548588]
[ 0.00729521 -0.19534231 -0.0122317 0.29768905]
[ 0.00338836 -0.19517588 -0.00627792 0.29387903]
[-0.00051516 0.19515739 -0.00040034 -0.29346358]
[ 0.00338799 -0.19508084 -0.00626961 0.29177396]
[-0.00051362 -0.19498958 -0.00043413 0.28978282]
[-0.00441342 0.19525737 0.00536152 -0.29568956]
[-0.00050827 0.19518221 -0.00055227 -0.29401141]
[ 0.00339538 -0.19505367 -0.00643249 0.29117411]]
I attempted to use np.meshgrid over the initial points for each dimension, but I had to use sparse=True to save memory.
I am a bit stuck here, can anyone help?
Edit:
For my specific example I have a particle x in 4 dimensional space. I have a collection of trajectories in a list for it moving through this N dimensional space of a set length of 10 time steps. Each entry in the trajectory is an entry [(t_start, t_end)]
initial_array = []
vector_array = []
for trajectory in range(trajectories):
for t in range(timesteps):
initial_state = trajectory[t][0]
vector_at_state = trajectory[t][0] - initial_state
initial_array.append(initial_state)
vector_array.append(vector_at_state)
return np.array(initial_array), np.array(vector_array)
I then want to plot these vectors at the points they began from to generate a "sample" vector field from what I can observe, and then use this function:
def divergence(f, h):
num_dims = len(f)
return np.ufunc.reduce(np.add, [np.gradient(f[i], h[i],axis=i) for i in range(num_dims)])
To calculate the divergence over the sampled trajectories.
python
vector
calculus
0 Answers
Your Answer