Fel*_*lix 2 generics null scala matrix scala-2.8
我开始在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
}
Run Code Online (Sandbox Code Playgroud)
一切都编译,我甚至可以运行我的测试,但它在某一点失败.我正在运行的测试如下:
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
}
Run Code Online (Sandbox Code Playgroud)
这失败,出现以下异常:
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)
Run Code Online (Sandbox Code Playgroud)
有人能告诉我为什么班级清单在这一点上是空的吗?很抱歉放了这么多代码,但我无法确定我的错误所在,所以我必须报告所有内容.(当我得到一个好的答案时,也许我可以删除无关紧要的部分).
好的,现在代码可以工作,但当然它比使用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
}
Run Code Online (Sandbox Code Playgroud)
这似乎有效:
class Matrix[T: Numeric: ClassManifest](val data: Array[Array[T]]) {
val num = implicitly[Numeric[T]]
val cm: ClassManifest[T] = implicitly
...
Run Code Online (Sandbox Code Playgroud)