Я не уверен на 100%, какие типы ввода для вашей функции, но я переписал это немного, так как кажется, что вам вообще не нужен append
.Просто предварительно выделите массив calculated_gravity
для правильного размера.Это также должно быть обычно быстрее, чем добавление:
import numpy as np
import numba as nb
def gravity_calculator(x, y, h, dx, dy, p):
calculated_gravity = np.empty(len(x))
for i in range(len(x)):
cal = 0
for j in range(len(x)):
x1 = abs((x[j] - x[i])+0.000001)
x2 = x1 + dx
y1 = abs((y[j]-y[i])+0.000001)
y2 = y1 + dy
t1 = np.log((y2 + np.sqrt((x2 ** 2) + (y2 ** 2))) / (y2 + np.sqrt((x2 ** 2) + (y2 ** 2) + (h[j] ** 2))))
t2 = np.log((y1 + np.sqrt((x2 ** 2) + (y1 ** 2))) / (y1 + np.sqrt((x2 ** 2) + (y1 ** 2) + (h[j] ** 2))))
t3 = np.log((y2 + np.sqrt((x1 ** 2) + (y2 ** 2))) / (y2 + np.sqrt((x1 ** 2) + (y2 ** 2) + (h[j] ** 2))))
t4 = np.log((y1 + np.sqrt((x1 ** 2) + (y1 ** 2))) / (y1 + np.sqrt((x1 ** 2) + (y1 ** 2) + (h[j] ** 2))))
t5 = np.log((x2 + np.sqrt((x2 ** 2) + (y2 ** 2))) / (x2 + np.sqrt((x2 ** 2) + (y2 ** 2) + (h[j] ** 2))))
t6 = np.log((x1 + np.sqrt((x1 ** 2) + (y2 ** 2))) / (x1 + np.sqrt((x1 ** 2) + (y2 ** 2) + (h[j] ** 2))))
t7 = np.log((x2 + np.sqrt((x2 ** 2) + (y1 ** 2))) / (x2 + np.sqrt((x2 ** 2) + (y1 ** 2) + (h[j] ** 2))))
t8 = np.log((x1 + np.sqrt((x1 ** 2) + (y1 ** 2))) / (x1 + np.sqrt((x1 ** 2) + (y1 ** 2) + (h[j] ** 2))))
t9 = np.arcsin(((y2 ** 2) + (h[j] ** 2) + y2 * np.sqrt((x2 ** 2) + (y2 ** 2) + (h[j] ** 2))) / (
(y2 + np.sqrt((x2 ** 2) + (y2 ** 2) + (h[j] ** 2))) * np.sqrt((y2 ** 2) + (h[j] ** 2))))
t10 = np.arcsin(((y2 ** 2) + (h[j] ** 2) + y2 * np.sqrt((x1 ** 2) + (y2 ** 2) + (h[j] ** 2))) / (
(y2 + np.sqrt((x1 ** 2) + (y2 ** 2) + (h[j] ** 2))) * np.sqrt((y2 ** 2) + (h[j] ** 2))))
t11 = np.arcsin(((y1 ** 2) + (h[j] ** 2) + y1 * np.sqrt((x2 ** 2) + (y1 ** 2) + (h[j] ** 2))) / (
(y1 + np.sqrt((x2 ** 2) + (y2 ** 2) + (h[j] ** 2))) * np.sqrt((y1 ** 2) + (h[j] ** 2))))
t12 = np.arcsin(((y1 ** 2) + (h[j] ** 2) + y1 * np.sqrt((x1 ** 2) + (y1 ** 2) + (h[j] ** 2))) / (
(y1 + np.sqrt((x1 ** 2) + (y1 ** 2) + (h[j] ** 2))) * np.sqrt((y1 ** 2) + (h[j] ** 2))))
G = (x2 * (t1 - t2) - x1 * (t3 - t4) + y2 * (t5 - t6) - y1 * (t7 - t8) + h[j] * (t9 - t10 - t11 + t12))
cal = cal +(p * G)
calc = cal * 0.00667
calculated_gravity[i] = calc
return calculated_gravity
gravity_calculator_jit = nb.jit(nopython=True)(gravity_calculator)
, а затем я протестировал его, используя:
xi = np.random.normal(size=(10,))
yi = np.random.normal(size=(10,))
initial_depth = np.random.normal(size=(10,))
dx = 1.0
dy = 1.0
print(np.allclose(
gravity_calculator(xi,yi,initial_depth,dx,dy,-0.4),
gravity_calculator_jit(xi,yi,initial_depth,dx,dy,-0.4)))
# True
Также обратите внимание, что когда вы использовали np.append(...)
в исходной функциивам необходимо использовать его следующим образом:
calculated_gravity = np.append(calculated_gravity,calc)
, поскольку он не работает на месте.См .:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html