o.a.s.u.Vector instances for equality

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

o.a.s.u.Vector instances for equality

Oleksandr Olgashko
Hello. How should i better check two Vector's for equality?

val a = new Vector(Array(1))
val b = new Vector(Array(1))
println(a == b)
// false
Reply | Threaded
Open this post in threaded view
|

Re: o.a.s.u.Vector instances for equality

Shixiong Zhu
Vector is an enhanced Array[Double]. You can compare it like Array[Double]. E.g., 

scala> val v1 = Vector(1.0, 2.0)
v1: org.apache.spark.util.Vector = (1.0, 2.0)

scala> val v2 = Vector(1.0, 2.0)
v2: org.apache.spark.util.Vector = (1.0, 2.0)

scala> val exactResult = v1.elements.sameElements(v2.elements) // exact comparison
exactResult: Boolean = true

scala> val delta = 1E-6
delta: Double = 1.0E-6

scala> val inexactResult = v1.elements.length == v2.elements.length && v1.elements.zip(v2.elements).forall { case (x, y) => (x - y).abs < delta } // inexact comparison
inexactResult : Boolean = true

Best Regards,

Shixiong Zhu


2014-03-04 4:23 GMT+08:00 Oleksandr Olgashko <[hidden email]>:
Hello. How should i better check two Vector's for equality?

val a = new Vector(Array(1))
val b = new Vector(Array(1))
println(a == b)
// false

Reply | Threaded
Open this post in threaded view
|

Re: o.a.s.u.Vector instances for equality

Oleksandr Olgashko
Thanks.
Does it make sence to add ==/equals method for Vector with this (or same) behavior?


2014-03-04 6:00 GMT+02:00 Shixiong Zhu <[hidden email]>:
Vector is an enhanced Array[Double]. You can compare it like Array[Double]. E.g., 

scala> val v1 = Vector(1.0, 2.0)
v1: org.apache.spark.util.Vector = (1.0, 2.0)

scala> val v2 = Vector(1.0, 2.0)
v2: org.apache.spark.util.Vector = (1.0, 2.0)

scala> val exactResult = v1.elements.sameElements(v2.elements) // exact comparison
exactResult: Boolean = true

scala> val delta = 1E-6
delta: Double = 1.0E-6

scala> val inexactResult = v1.elements.length == v2.elements.length && v1.elements.zip(v2.elements).forall { case (x, y) => (x - y).abs < delta } // inexact comparison
inexactResult : Boolean = true

Best Regards,

Shixiong Zhu


2014-03-04 4:23 GMT+08:00 Oleksandr Olgashko <[hidden email]>:

Hello. How should i better check two Vector's for equality?

val a = new Vector(Array(1))
val b = new Vector(Array(1))
println(a == b)
// false