There is a code:

import math import numpy as np X = [ 0.0, 0.0, 0.0, 0.0, 0.0] Y = [ 0.0, 0.25, 0.5, 0.75, 1.0] G = [ -0.5818708816896146, -0.17982884214560324, -1.3877787807814457e-17, 0.1798288421456033, 0.5818708816896146] X, Y, G = map(np.array, [X, Y,G ]) # Подсчет скоростей r22 = ((X[:] - X[:,None]) ** 2 + (Y[:] - Y[:,None]) ** 2) Vx = (Y[:] - Y[:,None]) * G[:] / (2 * math.pi * r22) Vy = (X[:] - X[:,None]) * G[:] / -(2 * math.pi * r22) 

As a result, we get:

 #r22 [[ 0. 0.0625 0.25 0.5625 1. ] [ 0.0625 0. 0.0625 0.25 0.5625] [ 0.25 0.0625 0. 0.0625 0.25 ] [ 0.5625 0.25 0.0625 0. 0.0625] [ 1. 0.5625 0.25 0.0625 0. ]] #Vx [[ nan -0.11448 -0. 0.03816 0.09261] [ 0.37043 nan -0. 0.05724 0.12348] [ 0.18522 0.11448 nan 0.11448 0.18522] [ 0.12348 0.05724 0. nan 0.37043] [ 0.09261 0.03816 0. -0.11448 nan]] #Vy [[ nan 0. 0. -0. -0.] [ 0. nan 0. -0. -0.] [ 0. 0. nan -0. -0.] [ 0. 0. 0. nan -0.] [ 0. 0. 0. -0. nan]] 

A similar question was asked here , but that was the case for one sheet of X, but there are several sheets here , but I failed to set the condition correctly using the previous answer.

The essence of the question is: how to set the condition correctly in the list generator or in the numpy code so that there are no zeros in r22 and, accordingly, division by zero in subsequent Vx and Vy (if i is an element of sheet X and j is an element of sheet Y, then if i = j - do not subtract).

  #Нужный результат r22 [[ 0.0625 0.25 0.5625 1. ] [ 0.0625 0.0625 0.25 0.5625] [ 0.25 0.0625 0.0625 0.25 ] [ 0.5625 0.25 0.0625 0.0625] [ 1. 0.5625 0.25 0.0625]] #Нужный результат Vx [[ -0.11448 -0. 0.03816 0.09261] [ 0.37043 -0. 0.05724 0.12348] [ 0.18522 0.11448 0.11448 0.18522] [ 0.12348 0.05724 0. 0.37043] [ 0.09261 0.03816 0. -0.11448]] #Нужный результат Vy [[ 0. 0. -0. -0.] [ 0. 0. -0. -0.] [ 0. 0. -0. -0.] [ 0. 0. 0. -0.] [ 0. 0. 0. -0.]] 
  • Can you explain what you think? And what is X , Y , G ? - MaxU
  • X, Y - sheets with coordinates of points, G - a certain coefficient. It is necessary to calculate the values ​​of velocities in points (Vx, Vy) using formulas. - Aleksandr Aleksandrov

1 answer 1

It is possible so:

 In [44]: r22[r22!=0].reshape(r22.shape[0]-1, -1) Out[44]: array([[ 0.0625, 0.25 , 0.5625, 1. , 0.0625], [ 0.0625, 0.25 , 0.5625, 0.25 , 0.0625], [ 0.0625, 0.25 , 0.5625, 0.25 , 0.0625], [ 0.0625, 1. , 0.5625, 0.25 , 0.0625]]) 
  • This option is not suitable, since there may be values ​​close to zero (with a change in the coefficient). You can set a condition so that when counting the element itself does not take away (for example, r2 [0] [0] was not considered, r2 [1] [1], r2 [2] [2] and so on)? - Aleksandr Aleksandrov