需要 scala breeze 矩阵切片和向量索引的解决方法

Need workaround for scala breeze matrix slicing and vector indexing

由于方法 foo 中的奇怪行为,我无法编写像 bar 这样的方法, 我需要:

import breeze.linalg.DenseMatrix
import breeze.linalg.DenseVector

class Test {

    val dim = 3
    val X:DenseMatrix[Double] = DenseMatrix.rand(dim,dim)
    val u:DenseVector[Double] = DenseVector.fill(dim){1.0}

    def foo:Unit = {

        val i=0;
        val row_i:DenseVector[Double] = X(i,::).t  // OK
        val s = u(i)+u(i)                          // OK

        val j:Integer = 0
        val row_j:DenseVector[Double] = X(j,::).t  // does not compile (A)
        val a = u(j)+u(j)                          // does not compile (B)
    }
    def bar(i:Integer):Double = u(i)+u(i)          // does not compile (C)
}

有解决办法吗? 预先感谢所有回复。

编译错误:

(A) could not find implicit value for parameter canSlice: breeze.linalg.support.CanSlice2[breeze.linalg.DenseMatrix[Double],Integer,collection.immutable.::.type,Result] not enough arguments for method apply: (implicit canSlice: breeze.linalg.support.CanSlice2[breeze.linalg.DenseMatrix[Double],Integer,collection.immutable.::.type,Result])

Result in trait TensorLike. Unspecified value parameter canSlice.

(B), (C)

could not find implicit value for parameter canSlice: breeze.linalg.support.CanSlice[breeze.linalg.DenseVector[Double],Integer,Result] not enough arguments for method apply: (implicit canSlice: breeze.linalg.support.CanSlice[breeze.linalg.DenseVector[Double],Integer,Result])Result in trait TensorLike. Unspecified value parameter canSlice.

首先:将 Integer 转换为 Int 。这至少会解决第一个编译错误。更新您的代码后会编译

import breeze.linalg.DenseMatrix
import breeze.linalg.DenseVector
import breeze.linalg.DenseMatrix
import breeze.linalg.DenseVector

class Test {

    val dim = 3
    val X:DenseMatrix[Double] = DenseMatrix.rand(dim,dim)
    val u:DenseVector[Double] = DenseVector.fill(dim){1.0}

    def foo:Unit = {

        val i=0;
        val row_i:DenseVector[Double] = X(i,::).t  // OK
        val s = u(i)+u(i)                          // OK

        val j:Int = 0
        val row_j:DenseVector[Double] = X(j,::).t  // does not compile (A)
        val a = u(j)+u(j)                          // does not compile (B)
    }
    def bar(i:Int):Double = u(i)+u(i)          // does not compile (C)
}

来自回复:

// Exiting paste mode, now interpreting.

import breeze.linalg.DenseMatrix
import breeze.linalg.DenseVector
defined class Test

所以你的其他错误对我来说也消失了 (scala 2.11.8)。如果您还有其他问题,请告诉我。