File: dot_product

package info (click to toggle)
libmath-gsl-perl 0.45-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 192,156 kB
  • sloc: ansic: 895,524; perl: 24,682; makefile: 12
file content (43 lines) | stat: -rwxr-xr-x 921 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/perl -w

use strict;
use warnings;
use Benchmark;
use Math::GSL::Vector qw/:all/;
use Math::GSL::Errno  qw/:all/;
use Math::GSL::RNG  qw/:all/;
use Math::GSL::BLAS qw/:all/;
use Data::Dumper;

my $count = shift || 500_000;
my $dim   = shift || 10;
my $rng   = Math::GSL::RNG->new;
my @nums  = map { $rng->get % 100 } (1..$dim);

my $x = Math::GSL::Vector->new( \@nums );
my $y = $x->copy + int(rand(100));

warn Dumper [ $x->as_list ];
warn Dumper [ $y->as_list ];

warn Dumper [ pp_dot($x,$y) ];
warn Dumper [ gsl_dot($x,$y) ];

timethese(
    $count, {
        'pp_dot       ' => sub { pp_dot($x,$y) },
        'gsl_dot      ' => sub { gsl_dot($x,$y) },
        'overload_dot ' => sub { $x * $y },
    });

sub gsl_dot {
    my ($x,$y) = @_;
    my ($status,$dot) = gsl_blas_ddot($x->raw,$y->raw);
    return $dot;
}


sub pp_dot {
    my ($x,$y) = @_;
    return Math::GSL::Vector::dot_product_pp($x,$y);
}