Skip to content

Instantly share code, notes, and snippets.

@nebuta
Created November 1, 2015 07:26
Show Gist options
  • Save nebuta/daf3cd6e003e1edf1f1f to your computer and use it in GitHub Desktop.
Save nebuta/daf3cd6e003e1edf1f1f to your computer and use it in GitHub Desktop.
2D Gaussian function in Scala
import scala.math._
def gauss(dim: Int, sigma: Float): Array[Array[Float]] = {
if(dim%2==0)
throw new Exception("dim must be odd.")
val arr = Array.fromFunction(i=>{
Array.fromFunction({j=>
val x = (i - (dim-1)/2).toFloat
val y = (j - (dim-1)/2).toFloat
(exp(-1 * pow(x/sigma,2)) * exp(-1 * pow(y/sigma,2))).toFloat
})(dim)})(dim)
val ret = arr.map(a=>a.map(b=>b/sum(arr)))
ret
}
def sum(arr: Array[Array[Float]]): Float = {
arr.map(row=>row.sum).sum
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment