5

当我使用大型稀疏矩阵时,最好使用CCS,CRS等压缩矩阵。

我尝试使用 ScalaNLP、la4j、colc 来计算 100,000*100,000 稀疏矩阵。有一些问题。

  1. 微风 (ScalaNLP/Scalala)

    • 它给了我CSCMatrix可以有 100,000*100,000 大小的类型。
    • 但问题是它正在开发中。
    • CSCMatrix所以我们不能计算with的元素乘积CSCMatrix,比如csc1 :* csc2
    • 而且你不能相互添加 CSCMatrixes。
  2. 拉4j

    • 它有CCSMatrix和CRSMatrix。
    • 但是在创建 (new CCSMatrixFactory).createMatrix(100000, 100000) 时,会出现 OutOfMemoryError。
    • 矩阵应该是零,所以它不应该使用大的内存空间。
  3. 科尔克

    • 它有 SparseDoubleMatrix2D。
    • 但是当创建像 new SparseDoubleMatrix2d(100000, 100000) 这样的矩阵时,它会说 IllegalArgumentException: matrix too large。

要计算大型稀疏矩阵,我可以使用什么库?你能给我看看这个例子吗?

4

2 回答 2

3

我对微风很好奇,所以我调查了源头。这有点混乱,因为运算符都是从一些 println 样式代码生成(!)发出的......但我想出了这个:

import breeze.linalg.operators.{BinaryOp, OpMulScalar}

object CSCMatrixExtraOps {
  abstract class CSCMatrixCanMulM_M[@specialized (Int, Float, Long, Double) A]
    extends BinaryOp[CSCMatrix[A], CSCMatrix[A], OpMulScalar, CSCMatrix[A]] {

    protected def times(a: A, b: A): A

    protected def zeros  (rows: Int, cols: Int): CSCMatrix[A]
    protected def builder(rows: Int, cols: Int, sz: Int): CSCMatrix.Builder[A]

    final def apply(a: CSCMatrix[A], b: CSCMatrix[A]): CSCMatrix[A] = {
      val rows  = a.rows
      val cols  = a.cols
      require(rows == b.rows, "Matrices must have same number of rows!")
      require(cols == b.cols, "Matrices must have same number of cols!")

      if (cols == 0) return zeros(rows, cols)

 

      val res     = builder(rows, cols, math.min(a.activeSize, b.activeSize))
      var ci      = 0
      var acpStop = a.colPtrs(0)
      var bcpStop = b.colPtrs(0)
      while (ci < cols) {
        val ci1 = ci + 1
        var acp = acpStop
        var bcp = bcpStop
        acpStop = a.colPtrs(ci1)
        bcpStop = b.colPtrs(ci1)
        while (acp < acpStop && bcp < bcpStop) {
          val ari = a.rowIndices(acp)
          val bri = b.rowIndices(bcp)
          if (ari == bri) {
            val v = times(a.data(acp), b.data(bcp))
            res.add(ari, ci, v)
            acp += 1
            bcp += 1
          } else if (ari < bri) {
            acp += 1
          } else /* ari > bri */ {
            bcp += 1
          }
        }
        ci = ci1
      }

      res.result()
    }
  }

 

  implicit object CSCMatrixCanMulM_M_Int extends CSCMatrixCanMulM_M[Int] {
    protected def times(a: Int, b: Int) = a * b
    protected def zeros(rows: Int, cols: Int) = CSCMatrix.zeros(rows, cols)
    protected def builder(rows: Int, cols: Int, sz: Int) = 
      new CSCMatrix.Builder(rows, cols, sz)
  }

  implicit object CSCMatrixCanMulM_M_Double extends CSCMatrixCanMulM_M[Double] {
    protected def times(a: Double, b: Double) = a * b
    protected def zeros(rows: Int, cols: Int) = CSCMatrix.zeros(rows, cols)
    protected def builder(rows: Int, cols: Int, sz: Int) = 
      new CSCMatrix.Builder(rows, cols, sz)
  }
}

例子:

import breeze.linalg._
import CSCMatrixExtraOps._

val m1 = CSCMatrix((0, 0, 0), (0, 5, 0), (0, 0, 10), (0, 13, 0))
val m2 = CSCMatrix((0, 0, 0), (0, 5, 0), (0, 0, 10), (13, 0, 0))
(m1 :* m2).toDenseMatrix

结果:

0  0   0    
0  25  0    
0  0   100  
0  0   0    
于 2013-06-15T17:50:47.247 回答
2

我是la4j库的作者。让我给你一些建议。因此,当您创建一个新的 CRS/CCS 矩阵时,la4j 仅为其分配 32 长度的数组(这是默认的最小大小)。因此,它不会抛出 OOM 错误(我刚刚检查过):

Matrix a = Matrices.CRS_FACTORY.createMatrix(100000, 100000);

但是,最好使用公共构造函数:

Matrix a = new CCSMatrix(100000, 100000);

无论如何,如果您仍然收到此错误,请尝试使用-Xmx1024m -Xms512m.

“矩阵应该是零,所以它不应该使用大的内存空间”是什么意思。我不确定我是否理解正确。

顺便说一句,使用 la4j 的最新版本:0.4.0可能,您发现的问题已通过此 pull-request修复。

于 2013-06-17T04:10:46.673 回答