Merge pull request #255 from Nin17/master

correct 2D Gaussian + add separable alternative answer
This commit is contained in:
Nicolas P. Rougier
2026-04-29 08:34:09 +02:00
committed by GitHub

View File

@@ -719,10 +719,18 @@ Generate a generic 2D Gaussian-like array (★★☆)
hint: np.meshgrid, np.exp hint: np.meshgrid, np.exp
< a56 < a56
X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10)) X, Y = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10))
D = np.sqrt(X*X+Y*Y)
sigma, mu = 1.0, 0.0 sigma, mu = 1.0, 0.0
G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) ) G = np.exp(-((X - mu) ** 2 + (Y - mu) ** 2) / (2.0 * sigma**2))
print(G)
# Another solution using the fact that a 2D Gaussian is separable
# Author: Nin17
x = np.linspace(-1, 1, 10)
y = np.linspace(-1, 1, 10)
gx = np.exp(-((x - mu) ** 2 / (2.0 * sigma**2)))
gy = np.exp(-((y - mu) ** 2 / (2.0 * sigma**2)))
G = gy[:, None] * gx[None, :]
print(G) print(G)
< q57 < q57