, . range(0, numy-1), range(numy-1), 0 numy-2, numy-1. , numy-1 0 numy-2. MATLAB 1, Python 0-, . , tri_area = np.zeros((numx, numy), dtype=float), tri_area[ii,jj] , . , range(numy).
fuction t_area() , . , numpy , , .
ap (i, j) a (m, n, 3), (m, n) - x. (m, n, 3) , . , np.cross(a, b) (i, j) a[i,j] b[i,j]. , np.linalg.norm(a, axis=2) (i, j) a[i,j]. (m, n). , , , 2- .
, . , , surface x y.
import numpy as np
def _t_area(a, b, c):
ab = b - a
ac = c - a
return 0.5 * np.linalg.norm(np.cross(ab, ac), axis=2)
def t_area(x, y, surface, dx):
a = np.zeros((x.shape[0], y.shape[0], 3), dtype=float)
b = np.zeros_like(a)
c = np.zeros_like(a)
d = np.zeros_like(a)
a[...,0] = x
a[...,1] = y
a[...,2] = surface[:-1,:-1]
b[...,0] = x + dx
b[...,1] = y
b[...,2] = surface[1:,:-1]
c[...,0] = x
c[...,1] = y + dx
c[...,2] = surface[:-1,1:]
d[...,0] = bp[...,0]
d[...,1] = cp[...,1]
d[...,2] = surface[1:,1:]
return 0.5 * (_t_area(a, b, c) + _t_area(d, b, c) + _t_area(b, a, d) + _t_area(c, a, d))
nx, ny = 250, 250
dx = np.random.random()
x = np.random.random((nx, ny))
y = np.random.random((nx, ny))
surface = np.random.random((nx+1, ny+1))
tri_area = t_area(x, y, surface, dx)
x 0-249, surface 0-250. surface[:-1], surface[0:-1], , 0 , . -1 end MATLAB. , surface[:-1] 0-249. , surface[1:] 1-250, , surface[ii+1].
. , , t_area() . , , , , , , .
, , , x, y,, surface dx . .
, ap, bp, cp dp , . .
. - ( MATLAB ), ii , . , , ?
, .
def t_area(x, y, surface, dx):
tri_area = np.zeros(x.shape, dtype=float)
ap = np.zeros((3,), dtype=float)
bp = np.zeros_like(ap)
cp = np.zeros_like(ap)
dp = np.zeros_like(ap)
for ii in range(x.shape[0]-1): # do you really want range(numx-1) or just range(numx)?
for jj in range(x.shape[1]-1):
xp = x[ii,jj]
yp = y[ii,jj]
zp = surface[ii,jj]
ap[:] = (xp, yp, zp)