Skip to content

Instantly share code, notes, and snippets.

@vxf
Last active April 25, 2017 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vxf/952abc85e745e4824a35e993d0882967 to your computer and use it in GitHub Desktop.
Save vxf/952abc85e745e4824a35e993d0882967 to your computer and use it in GitHub Desktop.
"""
gausskernel.py
Vasco Flores
unidim:
0.00081721 0.02804124 0.23392411 0.47442500 0.23392411 0.02804124 0.00081721
sum:
0.9999901346984609
bidim:
0.00000067 0.00002292 0.00019117 0.00038771 0.00019117 0.00002292 0.00000067
0.00002292 0.00078631 0.00655952 0.01330347 0.00655952 0.00078631 0.00002292
0.00019117 0.00655952 0.05472049 0.11097945 0.05472049 0.00655952 0.00019117
0.00038771 0.01330347 0.11097945 0.22507908 0.11097945 0.01330347 0.00038771
0.00019117 0.00655952 0.05472049 0.11097945 0.05472049 0.00655952 0.00019117
0.00002292 0.00078631 0.00655952 0.01330347 0.00655952 0.00078631 0.00002292
0.00000067 0.00002292 0.00019117 0.00038771 0.00019117 0.00002292 0.00000067
sum:
0.9999802694942458
"""
import math
def gauss1(x, sigma) :
t = 2.0*sigma*sigma
return math.exp(-(x*x)/t)/(math.sqrt(math.pi*t))
def gauss2(x, y, sigma) :
t = 2.0*sigma*sigma
return math.exp(-(x*x + y*y)/t)/(math.pi*t)
def genGauss1(sigma, r):
a = []
for x in range(-r, r+1):
a.append(gauss1(x, sigma))
return a
def genGauss2(sigma, r):
a = []
for y in range(-r, r+1):
b = []
for x in range(-r, r+1):
b.append(gauss2(x, y, sigma))
a.append(b)
return a
sigma = 0.84089642
r = math.ceil(3*sigma)
uni = genGauss1(sigma, r)
bidi = genGauss2(sigma, r)
print("unidim:")
print(" ".join(["{:10.8f}".format(n) for n in uni]))
print("sum:")
print(sum(uni))
print("\nbidim:")
for l in bidi:
print(" ".join(["{:10.8f}".format(n) for n in l]))
print("sum:")
print(sum(sum(l) for l in bidi))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment