The Computer Language
Benchmarks Game

spectral-norm Perl #5 program

source code

# The Computer Language Benchmarks Game
# http://benchmarksgame.alioth.debian.org/
#
# Contributed by Richard Jelinek

use strict;

my $n = $ARGV[0] // 500;

my @v = multiplyAtAv(
    multiplyAtAv(
        multiplyAtAv(
            (1) x $n
        )
    )
    );

my @u = multiplyAtAv( @v );

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

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

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

sub eval_A {
    use integer;
    my $sum = $_[0] + $_[1];
    my $div = ($sum * ($sum+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 = $_; #my ($i, $sum) = ($_);
        my $sum;
        $sum += eval_A($_, $i) * $_[$_] for 0 .. $#_;
        $sum;
    } 0 .. $#_;
}
    

notes, command-line, and program output

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



Wed, 11 May 2016 01:00:30 GMT

COMMAND LINE:
/usr/local/src/perl-5.24.0/bin/perl spectralnorm.perl-5.perl 5500

PROGRAM OUTPUT:
1.274224153