本文整理汇总了Python中autograd.numpy.meshgrid方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.meshgrid方法的具体用法?Python numpy.meshgrid怎么用?Python numpy.meshgrid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类autograd.numpy
的用法示例。
在下文中一共展示了numpy.meshgrid方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: advect
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import meshgrid [as 别名]
def advect(f, vx, vy):
"""Move field f according to x and y velocities (u and v)
using an implicit Euler integrator."""
rows, cols = f.shape
cell_xs, cell_ys = np.meshgrid(np.arange(cols), np.arange(rows))
center_xs = (cell_xs - vx).ravel()
center_ys = (cell_ys - vy).ravel()
# Compute indices of source cells.
left_ix = np.floor(center_ys).astype(int)
top_ix = np.floor(center_xs).astype(int)
rw = center_ys - left_ix # Relative weight of right-hand cells.
bw = center_xs - top_ix # Relative weight of bottom cells.
left_ix = np.mod(left_ix, rows) # Wrap around edges of simulation.
right_ix = np.mod(left_ix + 1, rows)
top_ix = np.mod(top_ix, cols)
bot_ix = np.mod(top_ix + 1, cols)
# A linearly-weighted sum of the 4 surrounding cells.
flat_f = (1 - rw) * ((1 - bw)*f[left_ix, top_ix] + bw*f[left_ix, bot_ix]) \
+ rw * ((1 - bw)*f[right_ix, top_ix] + bw*f[right_ix, bot_ix])
return np.reshape(flat_f, (rows, cols))
示例2: advect
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import meshgrid [as 别名]
def advect(f, vx, vy):
"""Move field f according to x and y velocities (u and v)
using an implicit Euler integrator."""
rows, cols = f.shape
cell_ys, cell_xs = np.meshgrid(np.arange(rows), np.arange(cols))
center_xs = (cell_xs - vx).ravel()
center_ys = (cell_ys - vy).ravel()
# Compute indices of source cells.
left_ix = np.floor(center_xs).astype(int)
top_ix = np.floor(center_ys).astype(int)
rw = center_xs - left_ix # Relative weight of right-hand cells.
bw = center_ys - top_ix # Relative weight of bottom cells.
left_ix = np.mod(left_ix, rows) # Wrap around edges of simulation.
right_ix = np.mod(left_ix + 1, rows)
top_ix = np.mod(top_ix, cols)
bot_ix = np.mod(top_ix + 1, cols)
# A linearly-weighted sum of the 4 surrounding cells.
flat_f = (1 - rw) * ((1 - bw)*f[left_ix, top_ix] + bw*f[left_ix, bot_ix]) \
+ rw * ((1 - bw)*f[right_ix, top_ix] + bw*f[right_ix, bot_ix])
return np.reshape(flat_f, (rows, cols))
示例3: compute_f
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import meshgrid [as 别名]
def compute_f(theta, lambda0, dL, shape):
""" Compute the 'vacuum' field vector """
# get plane wave k vector components (in units of grid cells)
k0 = 2 * npa.pi / lambda0 * dL
kx = k0 * npa.sin(theta)
ky = -k0 * npa.cos(theta) # negative because downwards
# array to write into
f_src = npa.zeros(shape, dtype=npa.complex128)
# get coordinates
Nx, Ny = shape
xpoints = npa.arange(Nx)
ypoints = npa.arange(Ny)
xv, yv = npa.meshgrid(xpoints, ypoints, indexing='ij')
# compute values and insert into array
x_PW = npa.exp(1j * xpoints * kx)[:, None]
y_PW = npa.exp(1j * ypoints * ky)[:, None]
f_src[xv, yv] = npa.outer(x_PW, y_PW)
return f_src.flatten()
示例4: moffat
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import meshgrid [as 别名]
def moffat(y, x, alpha=4.7, beta=1.5, bbox=None):
"""Symmetric 2D Moffat function
.. math::
(1+\frac{(x-x0)^2+(y-y0)^2}{\alpha^2})^{-\beta}
Parameters
----------
y: float
Vertical coordinate of the center
x: float
Horizontal coordinate of the center
alpha: float
Core width
beta: float
Power-law index
bbox: Box
Bounding box over which to evaluate the function
Returns
-------
result: array
A 2D circular gaussian sampled at the coordinates `(y_i, x_j)`
for all i and j in `shape`.
"""
Y = np.arange(bbox.shape[1]) + bbox.origin[1]
X = np.arange(bbox.shape[2]) + bbox.origin[2]
X, Y = np.meshgrid(X, Y)
# TODO: has no pixel-integration formula
return ((1 + ((X - x) ** 2 + (Y - y) ** 2) / alpha ** 2) ** -beta)[None, :, :]
示例5: grid
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import meshgrid [as 别名]
def grid(num, ndim, large=False):
"""Build a uniform grid with num points along each of ndim axes."""
if not large:
_check_not_too_large(np.power(num, ndim) * ndim)
x = np.linspace(0, 1, num, dtype='float64')
w = 1 / (num - 1)
points = np.stack(
np.meshgrid(*[x for _ in range(ndim)], indexing='ij'), axis=-1)
return points, w
示例6: plot_isocontours
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import meshgrid [as 别名]
def plot_isocontours(ax, func, xlimits=[-2, 2], ylimits=[-4, 2],
numticks=101, cmap=None):
x = np.linspace(*xlimits, num=numticks)
y = np.linspace(*ylimits, num=numticks)
X, Y = np.meshgrid(x, y)
zs = func(np.concatenate([np.atleast_2d(X.ravel()), np.atleast_2d(Y.ravel())]).T)
Z = zs.reshape(X.shape)
plt.contour(X, Y, Z, cmap=cmap)
ax.set_yticks([])
ax.set_xticks([])
示例7: plot_isocontours
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import meshgrid [as 别名]
def plot_isocontours(ax, func, xlimits=[-2, 2], ylimits=[-4, 2], numticks=101):
x = np.linspace(*xlimits, num=numticks)
y = np.linspace(*ylimits, num=numticks)
X, Y = np.meshgrid(x, y)
zs = func(np.concatenate([np.atleast_2d(X.ravel()), np.atleast_2d(Y.ravel())]).T)
Z = zs.reshape(X.shape)
plt.contour(X, Y, Z)
ax.set_yticks([])
ax.set_xticks([])
# Set up figure.
示例8: box_meshgrid
# 需要导入模块: from autograd import numpy [as 别名]
# 或者: from autograd.numpy import meshgrid [as 别名]
def box_meshgrid(func, xbound, ybound, nx=50, ny=50):
"""
Form a meshed grid (to be used with a contour plot) on a box
specified by xbound, ybound. Evaluate the grid with [func]: (n x 2) -> n.
- xbound: a tuple (xmin, xmax)
- ybound: a tuple (ymin, ymax)
- nx: number of points to evluate in the x direction
return XX, YY, ZZ where XX is a 2D nd-array of size nx x ny
"""
# form a test location grid to try
minx, maxx = xbound
miny, maxy = ybound
loc0_cands = np.linspace(minx, maxx, nx)
loc1_cands = np.linspace(miny, maxy, ny)
lloc0, lloc1 = np.meshgrid(loc0_cands, loc1_cands)
# nd1 x nd0 x 2
loc3d = np.dstack((lloc0, lloc1))
# #candidates x 2
all_loc2s = np.reshape(loc3d, (-1, 2) )
# evaluate the function
func_grid = func(all_loc2s)
func_grid = np.reshape(func_grid, (ny, nx))
assert lloc0.shape[0] == ny
assert lloc0.shape[1] == nx
assert np.all(lloc0.shape == lloc1.shape)
return lloc0, lloc1, func_grid