Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ sealed trait Vector extends Serializable {
*/
@Since("2.0.0")
def argmax: Int

/**
* Calculate the dot product of this vector with another.
*
* If `size` does not match an [[IllegalArgumentException]] is thrown.
*/
@Since("3.0.0")
def dot(v: Vector): Double = BLAS.dot(this, v)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,27 @@ class VectorsSuite extends SparkMLFunSuite {
Vectors.sparse(-1, Array((1, 2.0)))
}
}

test("dot product only supports vectors of same size") {
val vSize4 = Vectors.dense(arr)
val vSize1 = Vectors.zeros(1)
intercept[IllegalArgumentException]{ vSize1.dot(vSize4) }
}

test("dense vector dot product") {
val dv = Vectors.dense(arr)
assert(dv.dot(dv) === 0.26)
}

test("sparse vector dot product") {
val sv = Vectors.sparse(n, indices, values)
assert(sv.dot(sv) === 0.26)
}

test("mixed sparse and dense vector dot product") {
val sv = Vectors.sparse(n, indices, values)
val dv = Vectors.dense(arr)
assert(sv.dot(dv) === 0.26)
assert(dv.dot(sv) === 0.26)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ sealed trait Vector extends Serializable {
*/
@Since("2.0.0")
def asML: newlinalg.Vector

/**
* Calculate the dot product of this vector with another.
*
* If `size` does not match an [[IllegalArgumentException]] is thrown.
*/
@Since("3.0.0")
def dot(v: Vector): Double = BLAS.dot(this, v)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,4 +510,27 @@ class VectorsSuite extends SparkFunSuite with Logging {
Vectors.sparse(-1, Array((1, 2.0)))
}
}

test("dot product only supports vectors of same size") {
val vSize4 = Vectors.dense(arr)
val vSize1 = Vectors.zeros(1)
intercept[IllegalArgumentException]{ vSize1.dot(vSize4) }
}

test("dense vector dot product") {
val dv = Vectors.dense(arr)
assert(dv.dot(dv) === 0.26)
}

test("sparse vector dot product") {
val sv = Vectors.sparse(n, indices, values)
assert(sv.dot(sv) === 0.26)
}

test("mixed sparse and dense vector dot product") {
val sv = Vectors.sparse(n, indices, values)
val dv = Vectors.dense(arr)
assert(sv.dot(dv) === 0.26)
assert(dv.dot(sv) === 0.26)
}
}