Skip to content

Instantly share code, notes, and snippets.

@yu-iskw
Last active September 18, 2015 09:20
Show Gist options
  • Save yu-iskw/4e0d3a2f999effbcf640 to your computer and use it in GitHub Desktop.
Save yu-iskw/4e0d3a2f999effbcf640 to your computer and use it in GitHub Desktop.
A weighted Euclidean distance function implementation
package breeze.linalg.functions
import breeze.generic.UFunc
import breeze.linalg.{SparseVector, DenseVector}
import breeze.numerics.sqrt
/**
* A weighted Euclidean distance function implementation
*/
object weightedEuclideanDistance extends UFunc {
implicit object weightedEuclideanFromDenseVector_DV_DV_DV_D
extends Impl3[DenseVector[Double], DenseVector[Double], DenseVector[Double], Double] {
override def apply(v: DenseVector[Double],
v2: DenseVector[Double],
weights: DenseVector[Double]): Double = {
val d = v - v2
sqrt(d dot (weights :* d))
}
}
implicit object weightedEuclideanFromSparseVector_SV_SV_SV_D
extends Impl3[SparseVector[Double], SparseVector[Double], SparseVector[Double], Double] {
override def apply(v: SparseVector[Double],
v2: SparseVector[Double],
weights: SparseVector[Double]): Double = {
val d = v - v2
sqrt(d dot (weights :* d))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment