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

时间:2016-06-18 09:55:43

标签: scala matrix vector breeze

由于方法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)找不到参数canSlice的隐含值:       breeze.linalg.support.CanSlice2 [breeze.linalg.DenseMatrix [双],整型collection.immutable。::。类型,结果]       没有足够的方法适用于方法:(隐式canSlice:   breeze.linalg.support.CanSlice2 [breeze.linalg.DenseMatrix [双],整型collection.immutable。::。类型,结果])

     
    

导致特质TensorLike。未指定的值参数canSlice。

  

(B),(C)

  

找不到参数canSlice的隐含值:   breeze.linalg.support.CanSlice [breeze.linalg.DenseVector [双],整型结果]           方法适用的参数不足:(隐式canSlice:breeze.linalg.support.CanSlice [breeze.linalg.DenseVector [Double],Integer,Result])结果   特质TensorLike。未指定的值参数canSlice。

1 个答案:

答案 0 :(得分:0)

First off: convert your Integer to Int . That would take care of at least the first compilation error. Following update to your code does compile

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)
}

From the repl:

// Exiting paste mode, now interpreting.

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

So your other errors also disappear for me (scala 2.11.8). Let me know if you have further issues.