Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to multiply dense vectors with CSC matrices #809

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions math/src/main/codegen/breeze/linalg/operators/CSCMatrixOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ trait CSCMatrixOps extends CSCMatrixOps_Ring { this: CSCMatrix.type =>
}
}

implicit def canMulDVt_CSC_eq_DVt[T](
implicit op: OpMulMatrix.Impl2[DenseMatrix[T], CSCMatrix[T], DenseMatrix[T]],
zero: Zero[T],
ct: ClassTag[T]): OpMulMatrix.Impl2[Transpose[DenseVector[T]], CSCMatrix[T], Transpose[DenseVector[T]]] =
new OpMulMatrix.Impl2[Transpose[DenseVector[T]], CSCMatrix[T], Transpose[DenseVector[T]]] {
def apply(v: Transpose[DenseVector[T]], v2: CSCMatrix[T]): Transpose[DenseVector[T]] = {
require(v2.rows == v.inner.length)

val dm = v.inner.toDenseMatrix
val multiplied = op(dm, v2)

new Transpose[DenseVector[T]](multiplied.toDenseVector)

}
}

@expand
@expand.valify
implicit def csc_OpNeg[@expand.args(Int, Double, Float, Long) T]: OpNeg.Impl[CSCMatrix[T], CSCMatrix[T]] = {
Expand Down
25 changes: 25 additions & 0 deletions math/src/main/scala/breeze/linalg/DenseVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,14 @@ class DenseVector[@spec(Double, Int, Float, Long) V](
def toScalaVector()(implicit cm: ClassTag[V]): scala.Vector[V] = this.toArray.toVector
// </editor-fold>

def asCscRow(implicit man: ClassTag[V], zero: Zero[V]): CSCMatrix[V] = {
if (length == 0)
CSCMatrix.zeros[V](1, length)
else {
CSCMatrix.create(1, length, data)
}
}

@throws(classOf[ObjectStreamException])
protected def writeReplace(): Object = {
new DenseVector.SerializedForm(data, offset, stride, length)
Expand Down Expand Up @@ -402,6 +410,23 @@ object DenseVector
result
}

def fromCSCMatrix[V: ClassTag](csc: CSCMatrix[V]): DenseVector[V] = {
assert(csc.rows == 1)
val indices = new Array[V](csc.cols)
var i = 0
while (i < csc.cols) {
var j = csc.colPtrs(i)
while (j < csc.colPtrs(i + 1)) {
indices(i) = csc.data(j)
j += 1
}
i += 1
}

DenseVector.create(indices, 0 ,1, csc.cols)

}

// capabilities

implicit def canCreateZerosLike[V: ClassTag: Zero]: CanCreateZerosLike[DenseVector[V], DenseVector[V]] =
Expand Down
17 changes: 17 additions & 0 deletions math/src/test/scala/breeze/linalg/DenseVectorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,23 @@ class DenseVectorTest extends FunSuite with Checkers {
assert(fromNew === slice)
}
}

test("#715 - transpose DV * CSCMatrix") {
val dv = DenseVector(1,2,3,4)

val csc = CSCMatrix.zeros[Int](4, 4)
csc(1, 1) = 1
csc(1, 2) = 2
csc(2, 1) = 2
csc(2, 2) = 4

val multiplied = dv.t * csc

val expected = DenseVector(0,8,16,0).t

assert(multiplied == expected)
}

}

abstract class DenseVectorPropertyTestBase[T: ClassTag] extends TensorSpaceTestBase[DenseVector[T], Int, T] {
Expand Down