# GaussianMatrix function issue

Here is my implementation of the built-in GaussianMatrix function.

myGaussianMatrix[{x_, y_}, sigma]:= Exp[-{x^2 + y2}/(2 sigma^2)]

(*generate a grid with r = 2 means sigma = 1*)
grid = grid = Flatten[Table[{x, y}, {x, -r, r}, {y, -r, r}], 1]
kernel = myGaussianMatrix[#, 1] & /@ grid
(*Normalize*)
ArrayReshape[(kernel/Total[kernel]), {2r+1, 2r+1}]

(*output*)
{{0.00296902, 0.0133062, 0.0219382, 0.0133062,
0.00296902}, {0.0133062, 0.0596343, 0.0983203, 0.0596343,
0.0133062}, {0.0219382, 0.0983203, 0.162103, 0.0983203,
0.0219382}, {0.0133062, 0.0596343, 0.0983203, 0.0596343,
0.0133062}, {0.00296902, 0.0133062, 0.0219382, 0.0133062,
0.00296902}}


Built-in

GaussianMatrix[2]
(*Outputs*)

{{0.002589, 0.0107788, 0.0241466, 0.0107788, 0.002589}, {0.0107788,
0.0448755, 0.10053, 0.0448755, 0.0107788}, {0.0241466, 0.10053,
0.225206, 0.10053, 0.0241466}, {0.0107788, 0.0448755, 0.10053,
0.0448755, 0.0107788}, {0.002589, 0.0107788, 0.0241466, 0.0107788,
0.002589}}


What is the issue? Is it the normalization? or Is it a bug?

With the option setting Method->"Gaussian", GaussianMatrix gives the same result as your approach:

GaussianMatrix[2, Method -> "Gaussian"]


{{0.00296902, 0.0133062, 0.0219382, 0.0133062, 0.00296902}, {0.0133062, 0.0596343, 0.0983203, 0.0596343, 0.0133062}, {0.0219382, 0.0983203, 0.162103, 0.0983203, 0.0219382}, {0.0133062, 0.0596343, 0.0983203, 0.0596343, 0.0133062}, {0.00296902, 0.0133062, 0.0219382, 0.0133062, 0.00296902}}

The default setting is "Bessel":

GaussianMatrix[2] == GaussianMatrix[2, Method -> "Bessel"]


True

• In addition, setting WorkingPrecision -> Infinity will show the explicit expressions used for the entries. Commented Apr 3, 2016 at 10:17