0

我有一段代码:

def this(vectors: List[DenseVector[Double]]) {
    this(vectors.length)
    var resultVector = vectors.head
    for (vector <- vectors) {
        resultVector = kron(resultVector.toDenseMatrix, vector.toDenseMatrix).toDenseVector
    }
    _vector = resultVector
}

它的工作方式正是我想要的。问题是我需要复杂的值而不是双精度值。导入breeze.math.Complex 后,我将代码更改为:

def this(vectors: List[DenseVector[Complex]]) {
    this(vectors.length)
    var resultVector = vectors.head
    for (vector <- vectors) {
       resultVector = kron(resultVector.toDenseMatrix, vector.toDenseMatrix).toDenseVector
    }
    _vector = resultVector
}

然而,这会导致错误:

Error:(42, 26) could not find implicit value for parameter impl: breeze.linalg.kron.Impl2[breeze.linalg.DenseMatrix[breeze.math.Complex],breeze.linalg.DenseMatrix[breeze.math.Complex],VR]
      resultVector = kron(resultVector.toDenseMatrix, vector.toDenseMatrix).toDenseVector
                         ^

Error:(42, 26) not enough arguments for method apply: (implicit impl: breeze.linalg.kron.Impl2[breeze.linalg.DenseMatrix[breeze.math.Complex],breeze.linalg.DenseMatrix[breeze.math.Complex],VR])VR in trait UFunc.
Unspecified value parameter impl.
      resultVector = kron(resultVector.toDenseMatrix, vector.toDenseMatrix).toDenseVector
                         ^

这是一个错误还是我忘记做某事?

4

1 回答 1

0

我通过以下方式发现了问题:

  1. 我首先重写了函数以使用更少的矩阵转换
  2. 由于implkron 的隐式变量有问题,我还重写了函数调用,以显式声明要使用哪个变量

.

def this(vectors: List[DenseVector[Complex]]) {
    this(vectors.length)
    var resultMatrix = vectors.head.toDenseMatrix
    for (i <- 1 until vectors.length) {
         resultMatrix = kron(resultMatrix, vectors(i).toDenseMatrix)(kron.kronDM_M[Complex, Complex, DenseMatrix[Complex], Complex])
    }
    _vector = resultMatrix.toDenseVector
}

这告诉我没有ScalarMulOpfor V2, M, DenseMatrix[RV]whereM是 a Matrix[V1], V1andV2是输入类型并且RV是输出类型ScalarMulOp

挖掘微风的源代码,我发现上面的类型DenseMatrixOps只有一个隐含的if和是,和. 通过复制函数并使其特定于复数,我能够让 kronecker 产品工作。现在我还可以删除显式使用. 有问题的功能是:ScalarMulOpV1V2RVIntLongFloatDouble(kron.kronDM_M[Complex, Complex, DenseMatrix[Complex], Complex])ScalarMulOp

implicit def s_dm_op_Complex_OpMulScalar(implicit op: OpMulScalar.Impl2[Complex, Complex, Complex]):
  OpMulScalar.Impl2[Complex, DenseMatrix[Complex], DenseMatrix[Complex]] =

    new OpMulScalar.Impl2[Complex, DenseMatrix[Complex], DenseMatrix[Complex]] {
      def apply(b: Complex, a: DenseMatrix[Complex]): DenseMatrix[Complex] = {
        val res: DenseMatrix[Complex] = DenseMatrix.zeros[Complex](a.rows, a.cols)
        val resd: Array[Complex] = res.data
        val ad: Array[Complex] = a.data
        var c = 0

        var off = 0
        while (c < a.cols) {
          var r = 0
          while (r < a.rows) {
            resd(off) = op(b, ad(a.linearIndex(r, c)))
            r += 1
            off += 1
          }
          c += 1
        }

        res
      }

      implicitly[BinaryRegistry[Complex, Matrix[Complex], OpMulScalar.type, Matrix[Complex]]].register(this)
    }
于 2016-06-13T04:40:44.427 回答