当我使用大型稀疏矩阵时,最好使用压缩矩阵,如CCS、CRS等。
我试着用ScalaNLP,la4j,colc来计算100,000*100,000稀疏矩阵。有一些问题。
- it gives me the `CSCMatrix` type which can have 100,000\*100,000 size.
- but the problem is it's under development.
- so we cannot calc element-wise product of `CSCMatrix` with `CSCMatrix`, like `csc1 :* csc2`.
- and also you cannot add CSCMatrixes to each other.
- It has CCSMatrix and CRSMatrix.
- but when creating (new CCSMatrixFactory).createMatrix(100000, 100000), it occur OutOfMemoryError.
- The matrix should be zeros, so it should not use large memory spaces.
- It has SparseDoubleMatrix2D.
- but when creating matrix like new SparseDoubleMatrix2d(100000, 100000), it says IllegalArgumentException: matrix too large.
对于计算大型稀疏矩阵,我可以使用哪些库?你能给我举个例子吗?
发布于 2013-06-15 17:50:47
我对Breeze很好奇,所以我查了一下消息来源。这有点混乱,因为操作符都是由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-17 04:10:46
我是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的最后一个版本。可能,您发现的问题是由这个拉扯请求解决的。
https://stackoverflow.com/questions/17119765
复制相似问题