The Computer Language
Benchmarks Game

spectral-norm Scala program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/
   contributed by Isaac Gouy
   modified by Meiko Rachimow
   updated for 2.8 by Rex Kerr
*/

object spectralnorm {
  def main(args: Array[String]) = {
    val n = (if (args.length>0) args(0).toInt else 100)
    printf("%.09f\n", (new SpectralNorm(n)).approximate())
  }
}

class SpectralNorm(n: Int) {

  // Ordinary and transposed versions of infinite matrix
  val A = (i: Int, j: Int) => 1.0/((i+j)*(i+j+1)/2 +i+1)
  val At = (j: Int, i: Int) => 1.0/((i+j)*(i+j+1)/2 +i+1)

  // Matrix multiplication w <- M*v
  def mult(v: Array[Double], w: Array[Double], M: (Int,Int)=> Double ) {
    var i = 0
    while (i < n) {
     var s = 0.0
     var j = 0
     while (j < n) { s += M(i,j)*v(j); j += 1 }
     w(i) =  s
     i += 1
    }
  }

  def approximate() = {
    val u,v,w = Array.fill(n)(1.0)

    var i = 0
    while (i < 10) {
      // Multiply by matrix & transpose
      mult(u,w,A)
      mult(w,v,At)
      mult(v,w,A)
      mult(w,u,At)
      i += 1
    }

    var vbv,vv = 0.0
    i = 0
    while (i < n) {
      vbv += u(i)*v(i)
      vv += v(i)*v(i)
      i += 1
    }

    math.sqrt(vbv/vv)
  }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Scala compiler version 2.12.1 -- Copyright 2002-2016, LAMP/EPFL and Lightbend, Inc.
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)


Mon, 06 Feb 2017 05:54:38 GMT

MAKE:
mv spectralnorm.scala spectralnorm.scala
mv: 'spectralnorm.scala' and 'spectralnorm.scala' are the same file
/home/dunham/benchmarksgame/nanobench/makefiles/u64q.programs.Makefile:600: recipe for target 'spectralnorm.scala_run' failed
make: [spectralnorm.scala_run] Error 1 (ignored)
/usr/local/src/scala-2.12.1/bin/scalac -optimise -target:jvm-1.8 spectralnorm.scala
warning: there was one deprecation warning; re-run with -deprecation for details
one warning found
5.90s to complete and log all make actions

COMMAND LINE:
 /usr/local/src/jdk1.8.0_121/bin/java  -Xbootclasspath/a:/usr/local/src/scala-2.12.1/lib/scala-library.jar:/usr/local/src/scala-2.12.1/lib/scala-actors-2.11.0.jar:/usr/local/src/scala-2.12.1/lib/akka-actor_2.11-2.3.4.jar:/usr/local/src/scala-2.12.1/lib/config-1.2.1.jar spectralnorm 5500

PROGRAM OUTPUT:
1.274224153