Last active
September 18, 2015 09:20
-
-
Save yu-iskw/4e0d3a2f999effbcf640 to your computer and use it in GitHub Desktop.
A weighted Euclidean distance function implementation
This file contains 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
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