我开始在Scala中搞乱泛型,这是一个难以破解的cookie。我的想法是通过编写一个通用矩阵类来学习它,以便在我的并行计算项目中使用。
但是,在实例化新矩阵时我无法使用Array.fill()。这是我的Matrix代码:
(小心砖墙)
package dm8xx.matrix.immutable
import scala.util.Random
/*
* Bring your sunglasses...
*/
object Matrix {
def apply[T](m: Int, n: Int, v: T )(implicit nn:Numeric[T],mm:ClassManifest[T]): Matrix[T] = {
println("making new mat")
println(mm)
new Matrix[T](Array.fill(m,n)(v))
}
def apply[T: Numeric](m: Int, n: Int): Matrix[T] = Matrix(m, n)
def apply(m: Int, n: Int, r: Random): Matrix[Double] =
new Matrix(Array.fill[Double](m, n) { Math.floor(r.nextDouble() * 100) })
}
class Matrix[T: Numeric](val data: Array[Array[T]]) {
val num = implicitly[Numeric[T]]
implicit val cm: ClassManifest[T] = implicitly
val m = data.size
val n = data(0).size
def apply(i: Int, j: Int) = data(i)(j)
def map(f:T=>T)= new Matrix(data.map(_.map(f)))
def pieceWise(f: (T, T) => T, that: Matrix[T]): Matrix[T] = {
val temp: Array[Array[T]] = Array.ofDim[T](m, n)
for (i <- 0 until m; j <- 0 until n) temp(i)(j) = f(data(i)(j), that.data(i)(j))
return new Matrix(temp)
}
def +(that: Matrix[T]) = pieceWise(num.plus(_, _), that)
def -(that: Matrix[T]) = pieceWise(num.minus(_, _), that)
def *(that: Matrix[T]) = {
val temp = Matrix[T](m,that.n,num.one)
for (i <- 0 until m; j <- 0 until n; k <- 0 until that.n)
temp.data(i)(k) = num.plus(temp.data(i)(k) , num.times( data(i)(j) , that.data(j)(k)))
temp
}
override def toString() = (for (i <- 0 until m) yield "\n" + (for (j <- 0 until n) yield data(i)(j))).toString
}
一切都在编译,我甚至可以运行我的测试,但它在某个时刻失败了。我正在运行的测试如下:
val M = 100
val N = 100
val threads = false
def main(args: Array[String]) {
val r = new Random(2)
val a: Matrix[Double] = Matrix(M, N, r)
val b: Matrix[Double] = Matrix(N, M, r)
println(a)
println(b)
var m12 = a * b
}
此操作失败,但出现以下异常:
Exception in thread "main" scala.MatchError: null
at scala.reflect.ClassManifest$.arrayType(ClassManifest.scala:206)
at scala.Array$.fill(Array.scala:254)
at dm8xx.matrix.immutable.Matrix$.apply(Matrix.scala:12)
at dm8xx.matrix.immutable.Matrix.$times(Matrix.scala:37)
at dm8xx.matrix.MatrixTest$.main(MatrixTest.scala:19)
at dm8xx.matrix.MatrixTest.main(MatrixTest.scala)
有人能告诉我为什么班级清单在此时为空吗?很抱歉放了这么多代码,但我无法确定我的错误所在,所以我必须报告所有内容。 (当我得到一个好的答案时,也许我可以删除无关紧要的部分。)
好的,现在代码可以运行,但当然它比使用Double的直接实现慢了至少10倍......这就是最终矩阵的样子(如果有人有兴趣的话):
package dm8xx.matrix.immutable
import scala.util.Random
/*
* Bring your sunglasses...
*/
object Matrix {
def apply[T](m: Int, n: Int, v: T)(implicit nn: Numeric[T], mm: ClassManifest[T]): Matrix[T] = {
new Matrix[T](Array.fill(m, n)(v))
}
def apply[T](m: Int, n: Int)(implicit num: Numeric[T], cm: ClassManifest[T]): Matrix[T] = Matrix[T](m, n, num.zero)
def apply(m: Int, n: Int, r: Random): Matrix[Double] =
new Matrix(Array.fill[Double](m, n) { Math.floor(r.nextDouble() * 100) })
}
class Matrix[T: Numeric: ClassManifest](val data: Array[Array[T]]) {
val num = implicitly[Numeric[T]]
val cm: ClassManifest[T] = implicitly
val m = data.size
val n = data(0).size
def apply(i: Int, j: Int) = data(i)(j)
def map(f: T => T) = new Matrix(data.map(_.map(f)))
def pieceWise(f: (T, T) => T, that: Matrix[T]): Matrix[T] = {
val temp: Array[Array[T]] = Array.ofDim[T](m, n)
for (i <- 0 until m; j <- 0 until n) temp(i)(j) = f(data(i)(j), that.data(i)(j))
return new Matrix(temp)
}
def +(that: Matrix[T]) = pieceWise(num.plus(_, _), that)
def -(that: Matrix[T]) = pieceWise(num.minus(_, _), that)
def *(that: Matrix[T]) = {
val temp = Matrix[T](m, that.n, num.zero)
for (i <- 0 until m; j <- 0 until n; k <- 0 until that.n)
temp.data(i)(k) = num.plus(temp.data(i)(k), num.times(data(i)(j), that.data(j)(k)))
temp
}
override def toString() = (for (i <- 0 until m) yield "\n" + (for (j <- 0 until n) yield data(i)(j))).toString
}
答案 0 :(得分:5)
这似乎有效:
class Matrix[T: Numeric: ClassManifest](val data: Array[Array[T]]) {
val num = implicitly[Numeric[T]]
val cm: ClassManifest[T] = implicitly
...
答案 1 :(得分:0)
尝试使用Manifest
代替ClassManifest
当包含基元时,数组需要额外的处理,因为它们是有效的,但是ClassManifest
旨在用于类(名称是赠品)