The Computer Language
24.11 Benchmarks Game

spectral-norm Perl #2 program

source code

# The Computer Language Benchmarks Game
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
#
# Contributed by Andrew Rodland

use strict;

sub eval_A {
  use integer;
  my $div = ( ($_[0] + $_[1]) * ($_[0] + $_[1] + 1) / 2) + $_[0] + 1;
  no integer;
  1 / $div;
}

sub multiplyAv {
  return map {
    my ($i, $sum) = ($_);
    $sum += eval_A($i, $_) * $_[$_] for 0 .. $#_;
    $sum;
  } 0 .. $#_;
}

sub multiplyAtv {
  return map {
    my ($i, $sum) = ($_);
    $sum += eval_A($_, $i) * $_[$_] for 0 .. $#_;
    $sum;
  } 0 .. $#_;
}

sub multiplyAtAv {
  return multiplyAtv( multiplyAv( @_ ) );
}

my $n = @ARGV ? shift : 500;
my @u = (1) x $n;
my @v;
for (0 .. 9) {
  @v = multiplyAtAv( @u );
  @u = multiplyAtAv( @v );
}

my ($vBv, $vv);
for my $i (0 .. $#u) {
  $vBv += $u[$i] * $v[$i];
  $vv += $v[$i] ** 2;
}

printf( "%0.9f\n", sqrt( $vBv / $vv ) );

    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
This is perl 5, version 40
subversion 0 (v5.40.0)
x86_64-linux-thread-multi


 Mon, 10 Jun 2024 21:33:03 GMT

COMMAND LINE:
 /opt/src/perl-5.40.0/bin/perl spectralnorm.perl-2.perl 5500

PROGRAM OUTPUT:
1.274224153