Created
November 1, 2015 07:26
-
-
Save nebuta/daf3cd6e003e1edf1f1f to your computer and use it in GitHub Desktop.
2D Gaussian function in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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