Python Forum
Trying to replace for loops with numpy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to replace for loops with numpy
#7
Hi

I took the opportunity to build a more realistic model, and to validate it using a professional tool; I got the same results (see pictures).

[Image: 1564649081-test-distance.png]
[Image: 1564649073-check.png]
[Image: 1564649502-spyder.png]

Unfortunately I don't know how to share the files on the forum, especially the original meshes (send me a PM); I just can share the python file

hope it helps a bit

Paul

import numpy as np;
import os, time;
import h5py;

PATH = str(os.getcwd());
FileNameHDF5 = 'Distance_min.h5';

t0 = time.time();
with h5py.File(PATH + '/' + FileNameHDF5, 'r') as h5:    
    data = h5.get('Saddle/Saddle_nodes');
    Floor = np.array(data); del data;
    
    data = h5.get('Sphere/Sphere_nodes');
    Ball = np.array(data); del data;    
    

## get the number of rows/nodes per part
n1 = np.shape(Floor)[0];    # for the floor
n2 = np.shape(Ball)[0];     # for the ball


index1 = np.arange(0,n1, dtype = np.int); 
index2 = np.arange(0,n2, dtype = np.int);
 
  
# ball: the vect 1 is n1 blocs of n2 rows
# vect 1 = [0 0 0 ... 0 0 0 1 1 1 1 ... 1 1 1  and son on],
# floor; vect 2 is the blox of n2 nodes (floor) repeated n1 times
# vect 1 and vect 2 are vectors of indexes of floor and ball respectively
 

j1 = np.ones(n2, dtype = np.int); # warning: indexes must be integer
j2 = np.ones(n1, dtype = np.int); # warning: indexes must be integer

vect1 = np.kron(index1,j1);     # vect 1 returns the index of the Floor rows
vect2 = np.kron(j2,index2);     # vect 2 returns the same for the Ball rows

NumberOfRows = np.shape(vect1)[0];
k = np.arange(0,NumberOfRows, dtype = np.int);      # k allows to 
# 
distance_vector = np.sqrt( (Ball[vect2[k], 1] - Floor[vect1[k], 1])**2 + (Ball[vect2[k], 2] - Floor[vect1[k], 2])**2 + (Ball[vect2[k], 3] - Floor[vect1[k], 3])**2 )
minimum_distance = np.min(distance_vector);
del n1; del n2; del j1; del j2; del vect1; del vect2;
t1 = time.time();


print("The minimum distance has been found to {}".format(minimum_distance))
print("the {} calculations took {} second".format(NumberOfRows,t1-t0));
Reply


Messages In This Thread
RE: Trying to replace for loops with numpy - by paul18fr - Aug-01-2019, 08:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 639 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,643 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  replace sets of values in an array without using loops paul18fr 7 1,763 Jun-20-2022, 08:15 PM
Last Post: paul18fr
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 3,012 Apr-09-2019, 04:54 PM
Last Post: PhysChem

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020