Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,10 @@ class SparseMatrix @Since("1.3.0") (
values: Array[Double]) = this(numRows, numCols, colPtrs, rowIndices, values, false)

override def equals(o: Any): Boolean = o match {
case m: Matrix => toBreeze == m.toBreeze
case m: Matrix =>
val thisIteratorSet = toBreeze.activeIterator.toSet
val mIteratorSet = m.toBreeze.activeIterator.toSet.filter(p => p._2 != 0.0)
thisIteratorSet == mIteratorSet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than converting to sets (fairly expensive), how about comparing the iterators directly? It's a bit more complex (handling zeros) but would then be linear time, with a small constant. It would also allow early stopping as soon as a non-matching value was found.

Also, this method should compare the matrix dimensions before looking at the values.

case _ => false
}

Expand Down