All Downloads are FREE. Search and download functionalities are using the official Maven repository.

breeze.linalg.functions.squaredDistance.scala Maven / Gradle / Ivy

There is a newer version: 1.0
Show newest version
package breeze.linalg

import breeze.generic.UFunc
import breeze.linalg.operators.{OpMulInner, OpSub}
import spire.syntax.cfor._

/**
 * Computes the squared distance between two vectors.
 *
 * @author dlwh
 **/
object squaredDistance extends UFunc with squaredDistanceLowPrio {

  implicit def squaredDistanceFromZippedValues[T, U](implicit zipImpl: zipValues.Impl2[T, U, ZippedValues[Double, Double]]): Impl2[T, U, Double] = {
    new Impl2[T, U, Double] {
      def apply(v: T, v2: U): Double = {
        var squaredDistance = 0.0
        zipValues(v, v2).foreach { (a, b) =>
          val score = a - b
          squaredDistance += (score * score)
        }
        squaredDistance
      }
    }
  }
}

sealed trait squaredDistanceLowPrio extends UFunc { this: squaredDistance.type =>

  implicit def distanceFromDotAndSub[T, U, V]
  (implicit subImpl: OpSub.Impl2[T, U, V],
   dotImpl: OpMulInner.Impl2[V, V, Double]): Impl2[T, U, Double] = {

    new Impl2[T, U, Double] {
      def apply(v: T, v2: U): Double = {
        val diff = subImpl(v, v2)
        dotImpl(diff, diff)
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy