Scala implicit ClassManifest[T] is null in Array.fill(..)
I started messing around with generics in Scala, and it is a tough cookie to break. My idea is to learn it by writing a generic matrix class for use in my parallel computing project.
However, I have trouble using Array.fill() when instantiating a new matrix. Here is my code for the Matrix:
(beware of brickwall)
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
}
Everything compiles and I can even run my test but it fails at a certain point. The test I am running looks as follows:
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
}
This fails with the following exception:
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)
Can someone tell me why the class manifest i开发者_如何学运维s null at this point? Sorry about putting so much code, but I cannot tell at which point my error lies, so I must report everything. (Maybe I can delete irrelevent parts when I get a good answer).
Okay, now the code works, but of course its at least 10 times slower than a direct implementation with Double for example...this is what the final matrix looks like (if anyone is interested):
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
}
This seems to work:
class Matrix[T: Numeric: ClassManifest](val data: Array[Array[T]]) {
val num = implicitly[Numeric[T]]
val cm: ClassManifest[T] = implicitly
...
Try using Manifest
instead of ClassManifest
Arrays need extra handling when containing primitives because they're reified, but ClassManifest
is intended to work with classes (the name is a give away)
精彩评论