0
class MatrixMultiplication {
  def singleThreadedMultiplication(m1:Seq[Array[Double]], m2:Array[Array[Double]] ) ={
    val res =  Array.fill(m1.length, m2(0).length)(0.0)

    for(row <- 0 until m1.length;
        col <- 0 until m2(0).length;
        i   <- 0 until m1(0).length){
      res(row)(col) += m1(row)(i) * m2(i)(col)
    }

    res
  }
}

__

object multiplication {
  def main(args : Seq[Array[Double]], args2 : Array[Double]) : Unit = {
    val matrixmult = new MatrixMultiplication
    var b = new Array[Double](4)
    b = Array(2,1,2,1)
    seq: Seq[Double] = WrappedArray(1, 0, 2, 0)
    matrixmult.singleThreadedMultiplication(Seq[Double],b)
  }
}

我只需要知道如何运行单线程乘法方法,因为它的第一个参数是Seq[Array[Double]],我不知道,我也没有设法找到如何在第二类中创建 seq double 数组的任何方法。

4

1 回答 1

0

Seq是一个特征,并且有许多子类实现它。List例如ArrayBuffer。因此,您可以创建一个Array[Array[Double]]并将其传递给singleThreadedMultiplication. 例如,可以创建适当的类型并将val array = Array(Array(1.0,2.0,3.0))其传递给m1或传入。m2singleThreadedMultiplication

object multiplication {
  def main(args : Seq[Array[Double]], args2 : Array[Double]) : Unit = {
    val matrixmult = new MatrixMultiplication
    matrixmult.singleThreadedMultiplication(args, Array(args2)
  }
}

要运行它并使用传入的参数,请删除这些行,因为它们没有被使用。

var b = new Array[Double](4)
b = Array(2,1,2,1)
seq: Seq[Double] = WrappedArray(1, 0, 2, 0)

然后更改调用以使用传入的参数。

matrixmult.singleThreadedMultiplication(args, Array(args2))

这会运行,但你有一个java.lang.ArrayIndexOutOfBoundsException函数singleThreadedMultiplication

scala> val a = Array(2.0,1.0,2.0,1.0)
b: Array[Double] = Array(2.0, 1.0, 2.0, 1.0)

scala> val b = Array(Array(1.0,0.0,2.0,0.0))
c: Array[Array[Double]] = Array(Array(1.0, 0.0, 2.0, 0.0))

scala> multiplication.main(b, a)
java.lang.ArrayIndexOutOfBoundsException: 1
at MatrixMultiplication$$anonfun$singleThreadedMultiplication$1$$anonfun$apply$mcVI$sp$1$$anonfun$apply$mcVI$sp$2.apply$mcVI$sp
...
于 2013-01-09T04:48:33.417 回答