Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactoring.

  • Loading branch information...
commit ab11ed3a4b12fd9007e238d9c75e1d5365c25014 1 parent b49e1d1
@kuzuha authored
View
2  lib/Plann/ANN/BruteForce.pm
@@ -51,7 +51,7 @@ sub search {
my $res = [];
for my $entry (values $self->entries) {
- my $distance = euclidean_distance(map {$_->[0]->[0]} $entry->vector, $matrix);
+ my $distance = euclidean_distance($entry->vector->array_ref, $matrix->array_ref);
next if $options->{threshold} && $distance > $options->{threshold};
push $res, {
View
27 lib/Plann/Hash/LSH.pm
@@ -4,8 +4,13 @@ use true;
use Mouse;
use Smart::Args;
+use Sub::Exporter -setup => {
+ exports => [ qw/random_matrix/ ],
+ groups => { default => [ qw/random_matrix/ ] },
+};
+
use Plann::Subtypes;
-use Math::MatrixReal;
+use Plann::Matrix;
use Math::Trig qw( pi );
=head1 Interface
@@ -34,7 +39,7 @@ The matrix that initialized by the normal distribution.
Get matrix that constructed.
-C<< $lsh->matrix() >>: L<Math::MatrixReal>
+C<< $lsh->matrix() >>: L<Plann::Matrix>
=cut
@@ -51,7 +56,7 @@ has 'matrix' => (
Generate a hash matrix using Locality-Sensitive Hashing algorithm.
-C<< $lsh->hash($matrix) >> :L<Math::MtrixReal>
+C<< $lsh->hash($matrix) >> :L<Plann::Mtrix>
=head4 Parameters
@@ -72,13 +77,13 @@ sub hash {
($matrix * $self->matrix)->each(sub{ $_[0] > 0 ? 1 : 0 });
}
-=head2 Class Methods
+=head2 Functions
=head3 random_matrix
Generate a new matrix that initialized by the normal distribution.
-C<< Plann::Hash::LSH->random_matrix($rows, $cols) >>: L<Math::MatrixReal>
+C<random_matrix($rows, $cols)>: L<Plann::Matrix>
=head4 Parameters
@@ -97,18 +102,10 @@ The column dimension.
=cut
sub random_matrix {
- args_pos my $class => 'ClassName',
- my $rows => 'Int',
+ args_pos my $rows => 'Int',
my $cols => 'Int';
- my $matrix = [];
- for my $i (0..$rows-1) {
- push $matrix, [];
- for my $j (0..$cols-1) {
- $matrix->[$i]->[$j] = _random_normal();
- }
- }
- Math::MatrixReal->new_from_rows($matrix);
+ Plann::Matrix->new($rows, $cols)->each(sub { _random_normal() });
}
sub _random_normal {
View
14 lib/Plann/Subtypes.pm
@@ -3,29 +3,29 @@ package Plann::Subtypes;
use true;
use Mouse::Util::TypeConstraints;
-use Math::MatrixReal;
+use Plann::Matrix;
=head1 Subtypes
=head2 Matrix
-A subtype of Math::MatrixReal.
+A subtype of Plann::Matrix.
=head3 coerce
=head4 from ArrayRef[ArrayRef]
-[[0,0,0], [1,1,1]] => C<<Math::MatrixReal->new_from_rows([[0,0,0], [1,1,1]])>>
+[[0,0,0], [1,1,1]] => C<<Plann::Matrix->new_from_rows([[0,0,0], [1,1,1]])>>
=head4 from ArrayRef
-[0,0,0] => C<<Math::MatrixReal->new_from_rows([0,0,0])>>
+[0,0,0] => C<<Plann::Matrix->new_from_rows([0,0,0])>>
=cut
subtype 'Matrix'
=> as 'Object'
- => where { $_->isa('Math::MatrixReal') };
+ => where { $_->isa('Plann::Matrix') };
coerce 'Matrix'
=> from 'ArrayRef[ArrayRef]'
- => via { Math::MatrixReal->new_from_rows( $_ ) }
+ => via { Plann::Matrix->new_from_rows( $_ ) }
=> from 'ArrayRef'
- => via { Math::MatrixReal->new_from_rows( [$_] ) }
+ => via { Plann::Matrix->new_from_rows( [$_] ) }
View
6 t/usual/hash/lsh.t
@@ -6,8 +6,8 @@ use Plann::Hash::LSH;
#
# random_matrix
#
-my $matrix = Plann::Hash::LSH->random_matrix(20, 10);
-isa_ok($matrix, 'Math::MatrixReal');
+my $matrix = random_matrix(20, 10);
+isa_ok($matrix, 'Plann::Matrix');
is($matrix->[1], 20);
is($matrix->[2], 10);
@@ -22,7 +22,7 @@ is($lsh->matrix, $matrix);
#
my $hash = $lsh->hash([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]);
-isa_ok($hash, 'Math::MatrixReal');
+isa_ok($hash, 'Plann::Matrix');
ok($hash->is_row_vector());
is($matrix->[2], 10);
ok($hash->is_binary);
View
8 t/usual/matrix.t
@@ -0,0 +1,8 @@
+use 5.14.0;
+use Test::Most tests => 3;
+
+use Plann::Matrix;
+
+is_deeply(Plann::Matrix->new_from_rows([[1,2,3,4]])->array_ref, [1,2,3,4]);
+is_deeply(Plann::Matrix->new_from_cols([[1,2,3,4]])->array_ref, [[1],[2],[3],[4]]);
+is_deeply(Plann::Matrix->new_from_rows([[1,2],[3,4]])->array_ref, [[1,2],[3,4]]);
View
8 t/usual/subtype/matrix.t
@@ -3,7 +3,7 @@ use Test::Most tests => 3;
use Mouse::Meta::Attribute;
use Mouse::Meta::Class;
-use Math::MatrixReal;
+use Plann::Matrix;
use Plann::Subtypes;
my $anon_class = Mouse::Meta::Class->create_anon_class;
@@ -18,11 +18,11 @@ $anon_class->add_attribute(
my $anon = $anon_class->new_object;
$anon->matrix([1,2,3]);
-is($anon->matrix, Math::MatrixReal->new_from_rows([[1,2,3]]));
+is($anon->matrix, Plann::Matrix->new_from_rows([[1,2,3]]));
$anon->matrix([[1,2], [3,4]]);
-is($anon->matrix, Math::MatrixReal->new_from_rows([[1,2], [3,4]]));
+is($anon->matrix, Plann::Matrix->new_from_rows([[1,2], [3,4]]));
-my $matrix = Math::MatrixReal->new_from_rows([[1,1,1], [2,2,2]]);
+my $matrix = Plann::Matrix->new_from_rows([[1,1,1], [2,2,2]]);
$anon->matrix($matrix);
is($anon->matrix, $matrix);
Please sign in to comment.
Something went wrong with that request. Please try again.