Skip to content

Instantly share code, notes, and snippets.

@salzig
Created April 24, 2012 19:18
Show Gist options
  • Select an option

  • Save salzig/2482861 to your computer and use it in GitHub Desktop.

Select an option

Save salzig/2482861 to your computer and use it in GitHub Desktop.
class Matrix[T: Numeric: Manifest](val xDim:Int, val yDim:Int, private val data:Array[T]) {
require(data.size == xDim*yDim, "provided data dosn't match required dimensions")
val numeric = implicitly[Numeric[T]]
val manifest:Manifest[T] = implicitly
//...
override def toString = "Matrix[%s](%s)" format (manifest, data.grouped(xDim).map( "(%s)" format _.mkString(", ") ).mkString(", "))
}
object Matrix {
// Matrix(dim, *values)
def apply[T](dim:Int, values:T*):Matrix[T] = new Matrix(dim, dim, values)
//...
}
object Application {
def main(args:Array[String]) {
println(Matrix(3)(1, 2, 3))
println(Matrix(3)(1.0, 2, 3))
println(Matrix(2)(BigDecimal(2)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment