The Computer Language
24.11 Benchmarks Game

spectral-norm Java #8 program

source code

/* The Computer Language Benchmarks Game
   https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

   Naive transliteration from Sebastien Loisel's C program
   contributed by Isaac Gouy
*/

public final class spectralnorm
{
  double eval_A(int i, int j) { return 1.0/((i+j)*(i+j+1)/2+i+1); }   
   
  void eval_A_times_u(int N, final double u[], double Au[])
  {
    int i,j;
    for(i=0;i<N;i++)
      {
        Au[i]=0;
        for(j=0;j<N;j++) Au[i]+=eval_A(i,j)*u[j];
      }
  }    
  
  void eval_At_times_u(int N, final double u[], double Au[])
  {
    int i,j;
    for(i=0;i<N;i++)
      {
        Au[i]=0;
        for(j=0;j<N;j++) Au[i]+=eval_A(j,i)*u[j];
      }
  }  
  
  void eval_AtA_times_u(int N, final double u[], double AtAu[])
  { var v = new double[N]; eval_A_times_u(N,u,v); eval_At_times_u(N,v,AtAu); }  
  
  public static void main(String[] args) 
  {
    int i;    
    final int N = args.length > 0 ? Integer.parseInt(args[0]) : 100; 
    var nonStatic = new spectralnorm();
    var u = new double[N];      
    var v = new double[N];  
    double vBv, vv;        
    for (i=0; i<N; i++) u[i] = 1;    
    for(i=0; i<10; i++)
    {
      nonStatic.eval_AtA_times_u(N,u,v);
      nonStatic.eval_AtA_times_u(N,v,u);
    }        
    vBv = vv = 0;
    for(i=0; i<N; i++) { vBv += u[i]*v[i]; vv += v[i]*v[i]; }          
    System.out.printf("%.9f\n", Math.sqrt(vBv/vv));  
  }
}  
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
java 23 2024-09-17
Java HotSpot(TM) 64-Bit Server VM
(build 23+37-2369, 
mixed mode, sharing)


 Sun, 20 Oct 2024 20:37:17 GMT

MAKE:
mv spectralnorm.java-8.java spectralnorm.java
/opt/src/jdk-23/bin/javac -d . -cp .  spectralnorm.java

2.82s to complete and log all make actions

COMMAND LINE:
 /opt/src/jdk-23/bin/java  -cp . spectralnorm 5500

PROGRAM OUTPUT:
1.274224153