alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (t0421.scala)

This example Scala source code file (t0421.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

app, app, array, array, classmanifest, classmanifest, test, test

The Scala t0421.scala source code

// ticket #421
object Test extends App {

  def transpose[A: ClassManifest](xss: Array[Array[A]]) = {
    for (i <- Array.range(0, xss(0).length)) yield
      for (xs <- xss) yield xs(i)
  }

  def scalprod(xs: Array[Double], ys: Array[Double]) = {
    var acc = 0.0 
    for ((x, y) <- xs zip ys) acc = acc + x * y  
    acc
  }

  def matmul(xss: Array[Array[Double]], yss: Array[Array[Double]]) = {
    val ysst = transpose(yss) 
    val ysst1: Array[Array[Double]] = yss.transpose
    assert(ysst.deep == ysst1.deep)
    for (xs <- xss) yield
      for (yst <- ysst) yield 
        scalprod(xs, yst)
  }

  val a1 = Array(Array(0, 2, 4), Array(1, 3, 5))
  println(transpose(a1).deepMkString("[", ",", "]"))

  println(matmul(Array(Array(2, 3)), Array(Array(5), Array(7))).deepMkString("[", ",", "]"))
  
  println(matmul(Array(Array(4)), Array(Array(6, 8))).deepMkString("[", ",", "]"))
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala t0421.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.