Created
July 23, 2010 02:22
-
-
Save tom-lpsd/486926 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Vector; | |
use strict; | |
use warnings; | |
use List::Util qw(sum); | |
use List::MoreUtils qw(pairwise); | |
our ($a, $b); | |
sub new { | |
my ($class, $data) = @_; | |
bless $data, $class; | |
} | |
sub inner_prod { | |
my ($self, $other) = @_; | |
die 'dimension inconsistency was detected' unless @$self == @$other; | |
return sum pairwise { $a * $b } @$self, @$other; | |
} | |
sub dimension { | |
my $self = shift; | |
return scalar(@$self); | |
} | |
package Matrix; | |
use strict; | |
use warnings; | |
use List::MoreUtils qw(all); | |
sub new { | |
my ($class, $vecs) = @_; | |
if (@$vecs) { | |
my $dim = @{$vecs->[0]}; | |
die 'invalid matrix' unless all { @$_ == $dim } @$vecs; | |
} | |
bless [map { Vector->new($_) } @$vecs], $class; | |
} | |
sub mul { | |
my ($self, $other) = @_; | |
die 'dimension incosistency was detected' | |
unless ($self->dimension)[1] == ($other->dimension)[0]; | |
my $transposed = $other->transpose; | |
Matrix->new([ | |
map { | |
my $v = $_; | |
[map { $v->inner_prod($_) } @$transposed] | |
} @$self | |
]); | |
} | |
sub transpose { | |
my $self = shift; | |
Matrix->new([ | |
map { | |
my $ix = $_; | |
Vector->new([map { $_->[$ix] } @$self]), | |
} (0..(($self->dimension)[1]-1)) | |
]); | |
} | |
sub dimension { | |
my $self = shift; | |
return (0, 0) unless @$self; | |
return (scalar(@$self), scalar(@{$self->[0]})); | |
} | |
package main; | |
use strict; | |
use warnings; | |
sub random_matrix { | |
my ($n, $m) = @_; | |
Matrix->new([ | |
map { | |
Vector->new([ | |
map { int rand 10000 } (0..$m-1) | |
]) | |
} (0..$n-1) | |
]); | |
} | |
sub unit_matrix { | |
my $n = shift; | |
Matrix->new([ | |
map { | |
my $i = $_; | |
Vector->new([ | |
map { $i == $_ || 0 } (0..$n-1) | |
]); | |
} (0..$n-1) | |
]); | |
} | |
random_matrix(100, 300)->mul(random_matrix(300, 100))->dimension; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment