简体   繁体   English

需要针对Scala微风矩阵切片和矢量索引的解决方法

[英]Need workaround for scala breeze matrix slicing and vector indexing

Because of the odd behaviour in method foo I cannot write methods like bar, which I need: 由于方法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)
}

Is there a workaround? 有解决方法吗? Thanks in advance for all replies. 预先感谢您的所有回复。

Compilation errors: 编译错误:

(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]) (A)找不到参数canSlice的隐式值:breeze.linalg.support.CanSlice2 [breeze.linalg.DenseMatrix [Double],Integer,collection.immutable。::。type,Result]没有足够的参数适用于方法:(隐式canSlice:breeze.linalg.support.CanSlice2 [breeze.linalg.DenseMatrix [Double],Integer,collection.immutable。::。type,Result])

Result in trait TensorLike. 结果为特征TensorLike。 Unspecified value parameter canSlice. 未指定的值参数canSlice。

(B), (C) (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. 找不到参数canSlice的隐式值:breeze.linalg.support.CanSlice [breeze.linalg.DenseVector [Double],Integer,Result]没有足够的参数适用于方法:(隐式canSlice:breeze.linalg.support.CanSlice [breeze .linalg.DenseVector [Double],Integer,Result])产生特征TensorLike。 Unspecified value parameter canSlice. 未指定的值参数canSlice。

First off: convert your Integer to Int . 首先,将Integer转换为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). 因此,您的其他错误对我也消失了(scala 2.11.8)。 Let me know if you have further issues. 让我知道您是否还有其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM