krr*_*r38 5 java compression scala sparse-matrix
当我使用大型稀疏矩阵时,最好使用CCS,CRS等压缩矩阵.
我尝试使用ScalaNLP,la4j,colc来计算100,000*100,000稀疏矩阵.有一些问题.
微风(ScalaNLP/Scalala)
CSCMatrix类型可以有100,000*100,000大小.CSCMatrix与CSCMatrix一样csc1 :* csc2.la4j
COLC
要计算大型稀疏矩阵,我可以使用哪个库?你能告诉我这个例子吗?
我对 Breeze 很好奇,所以我调查了来源。这有点混乱,因为运算符都是从一些 println 样式代码生成中发出的(!)...但我想出了这个:
\n\nimport breeze.linalg.operators.{BinaryOp, OpMulScalar}\n\nobject CSCMatrixExtraOps {\n abstract class CSCMatrixCanMulM_M[@specialized (Int, Float, Long, Double) A]\n extends BinaryOp[CSCMatrix[A], CSCMatrix[A], OpMulScalar, CSCMatrix[A]] {\n\n protected def times(a: A, b: A): A\n\n protected def zeros (rows: Int, cols: Int): CSCMatrix[A]\n protected def builder(rows: Int, cols: Int, sz: Int): CSCMatrix.Builder[A]\n\n final def apply(a: CSCMatrix[A], b: CSCMatrix[A]): CSCMatrix[A] = {\n val rows = a.rows\n val cols = a.cols\n require(rows == b.rows, "Matrices must have same number of rows!")\n require(cols == b.cols, "Matrices must have same number of cols!")\n\n if (cols == 0) return zeros(rows, cols)\nRun Code Online (Sandbox Code Playgroud)\n\n\xc2\xa0
\n\n val res = builder(rows, cols, math.min(a.activeSize, b.activeSize))\n var ci = 0\n var acpStop = a.colPtrs(0)\n var bcpStop = b.colPtrs(0)\n while (ci < cols) {\n val ci1 = ci + 1\n var acp = acpStop\n var bcp = bcpStop\n acpStop = a.colPtrs(ci1)\n bcpStop = b.colPtrs(ci1)\n while (acp < acpStop && bcp < bcpStop) {\n val ari = a.rowIndices(acp)\n val bri = b.rowIndices(bcp)\n if (ari == bri) {\n val v = times(a.data(acp), b.data(bcp))\n res.add(ari, ci, v)\n acp += 1\n bcp += 1\n } else if (ari < bri) {\n acp += 1\n } else /* ari > bri */ {\n bcp += 1\n }\n }\n ci = ci1\n }\n\n res.result()\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n\xc2\xa0
\n\n implicit object CSCMatrixCanMulM_M_Int extends CSCMatrixCanMulM_M[Int] {\n protected def times(a: Int, b: Int) = a * b\n protected def zeros(rows: Int, cols: Int) = CSCMatrix.zeros(rows, cols)\n protected def builder(rows: Int, cols: Int, sz: Int) = \n new CSCMatrix.Builder(rows, cols, sz)\n }\n\n implicit object CSCMatrixCanMulM_M_Double extends CSCMatrixCanMulM_M[Double] {\n protected def times(a: Double, b: Double) = a * b\n protected def zeros(rows: Int, cols: Int) = CSCMatrix.zeros(rows, cols)\n protected def builder(rows: Int, cols: Int, sz: Int) = \n new CSCMatrix.Builder(rows, cols, sz)\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n例子:
\n\nimport breeze.linalg._\nimport CSCMatrixExtraOps._\n\nval m1 = CSCMatrix((0, 0, 0), (0, 5, 0), (0, 0, 10), (0, 13, 0))\nval m2 = CSCMatrix((0, 0, 0), (0, 5, 0), (0, 0, 10), (13, 0, 0))\n(m1 :* m2).toDenseMatrix\nRun Code Online (Sandbox Code Playgroud)\n\n结果:
\n\n0 0 0 \n0 25 0 \n0 0 100 \n0 0 0 \nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
3661 次 |
| 最近记录: |