Skip to content

Instantly share code, notes, and snippets.

@tixxit
Created May 17, 2013 19:33
Show Gist options
  • Select an option

  • Save tixxit/5601424 to your computer and use it in GitHub Desktop.

Select an option

Save tixxit/5601424 to your computer and use it in GitHub Desktop.
final case class Epsilon[A](value: A)
object Epsilon {
def apply[A: Field](x: Double): A = Field[A].fromDouble(x)
}
// This can be part of the MetricSpace ops.
implicit class ApproxEq[A](x: A) {
def =~=[B](y: A)(implicit ms: MetricSpace[A, B], e: Epsilon[B], o: Order[B]): Boolean =
MetricSpace.close(x, y)(ms, e, o)
}
// In algebra/MetricSpace.scala
object MetricSpace {
def close[A, B](x: A, y: A)(implicit ms: MetricSpace[A, B], e: Epsilon[B], o: Order[B]): Boolean = {
ms.distance(x, y) < e.value
}
implicit def trivialMetricSpace[A: IsReal: Rng] = new MetricSpace[A, A] {
def distance(x: A, y: A): A = (x - y).abs
}
}
@tixxit
Copy link
Copy Markdown
Author

tixxit commented May 17, 2013

Examples:

implicit val e = Epsilon(1)
"Thomas" =~= "thomas"

implicit val e2 = Epsilon(0.00001)
Vector(1.0, 1.0) =~= Vector(1.00000001, 0.99999999)

I think we'd actually want to somehow tie Epsilon to both types in the MEtricSpace too....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment