Thursday, February 16, 2012

Some more Scala experimentation

Ended up with some free time to play with Scala again today. Like many programmers, I learn a language by doing the things I do in other languages.

Today's project was to try out jblas. I gained a greater appreciation of the language from just a few lines of code. The (simple) challenge: create matrix A and matrix B filled with random numbers, then multiply them. As a baseline, in R this can be done using three straightforward lines of code:

A <- matrix(rnorm(12),4,3)
B <- matrix(rnorm(12),3,4)
C <- A %*% B


Here is the Scala code:

object tryblas {
  import org.jblas.DoubleMatrix
 
  def main(args: Array[String]) {
    val A = DoubleMatrix.randn(3,4)
    val B = DoubleMatrix.randn(4,3)
    val C = A.mmul(B)
  }
}

Type inference means I don't have to specify that A is "new DoubleMatrix" - the language figured that out for me! It's more lines than the R program, but not by much. There's a little overhead in a Scala program, and you have to import the jblas library. The Scala IDE even allows you to see the inferred type of A, B, or C by placing the cursor over the name. Impressive! The Java approach for some reason seems reasonable when I use Scala.

4 comments:

Anonymous said...

On September 13, 2007, a five count superseding information was filed in federal court, and Davis pled guilty to all five counts of the information on the same date.. http://officialcanadagoosesoutlet.ca Horses, cattle, swine, sheep, goats, fur-bearing animals, birds, and bees are affected. The punk scene is composed of an assortment of smaller subcultures, such as Oi! and pop punk. He pioneered techniques like pinched harmonics, overtones, feedback, and in particular use of the volume/tone controls.
Buy and Used Grand Prix dayton industrial motor 3kw34g options.. http://www.icanadagooseca.com Socks come in many designs, fabrics, colors and styles. [url=http://officialcanadagoosesoutlet.ca]canada goose sale[/url]
http://flyballbags.com PREVIOUS POSTChristian Louboutin Athletic Shoes Might Possibly Towards Quite Often Be The Foremost A real boots and shoes will be specified to get a state of affairs which generally could quite possibly ensure it is straightforward for are very important it is broken or cracked inside your common together with leisure carryout. [url=http://www.onlinebeatsbydreoutlet.com]buy justin bieber beats by dre[/url]

Anonymous said...

Thank you for sharing your info. I truly appreciate your efforts and I am waiting
for your next post thanks once again.

Feel free to surf to my blog ... tao of badass

Anonymous said...

Excellent blog you have here but I was curious if you knew of any user discussion forums that cover the
same topics talked about in this article? I'd really love to be a part of community where I can get opinions from other experienced people that share the same interest. If you have any suggestions, please let me know. Thank you!

my blog taoofbadass.pdf

Anonymous said...

smokeless cigarette, e cigarette, smokeless cigarettes, e cigarettes, electronic cigarettes, e cig