| 1 | package Math::Vector::Similarity; | ||||||||||
| 2 | # ABSTRACT: Cosine similarity, euclidean distance and other vector comparison functions | ||||||||||
| 3 | 2 | our $VERSION = '0.001'; | |||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use Carp qw( croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Exporter 'import'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | our @EXPORT_OK = qw( | |||||||||
| 11 | cosine_similarity | ||||||||||
| 12 | cosine_distance | ||||||||||
| 13 | euclidean_distance | ||||||||||
| 14 | dot_product | ||||||||||
| 15 | normalize | ||||||||||
| 16 | ); | ||||||||||
| 17 | 2 | our %EXPORT_TAGS = ( all => \@EXPORT_OK ); | |||||||||
| 18 | |||||||||||
| 19 | 5 | ▶ | sub dot_product { | ||||||||
|
Subroutine
dot_product: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 20 | 5 | my ( $a, $b ) = @_; | |||||||||
| 21 | 5 | ▶ | croak "vectors must have same dimensions" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | unless @$a == @$b; | ||||||||||
| 23 | 4 | my $sum = 0; | |||||||||
| 24 | 4 | $sum += $a->[$_] * $b->[$_] for 0..$#$a; | |||||||||
| 25 | 4 | return $sum; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | 3 | ▶ | sub normalize { | ||||||||
|
Subroutine
normalize: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 30 | 3 | my ( $vec ) = @_; | |||||||||
| 31 | 3 | my $norm = 0; | |||||||||
| 32 | 3 | $norm += $_ * $_ for @$vec; | |||||||||
| 33 | 3 | $norm = sqrt($norm); | |||||||||
| 34 | 3 | ▶ | return $vec if $norm == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 2 | return [ map { $_ / $norm } @$vec ]; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | |||||||||||
| 39 | 11 | ▶ | sub cosine_similarity { | ||||||||
|
Subroutine
cosine_similarity: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 40 | 11 | my ( $a, $b ) = @_; | |||||||||
| 41 | 11 | ▶ | croak "vectors must have same dimensions" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | unless @$a == @$b; | ||||||||||
| 43 | 10 | my ( $dot, $norm_a, $norm_b ) = ( 0, 0, 0 ); | |||||||||
| 44 | 10 | for my $i (0..$#$a) { | |||||||||
| 45 | 793 | $dot += $a->[$i] * $b->[$i]; | |||||||||
| 46 | 793 | $norm_a += $a->[$i] * $a->[$i]; | |||||||||
| 47 | 793 | $norm_b += $b->[$i] * $b->[$i]; | |||||||||
| 48 | } | ||||||||||
| 49 | 10 | my $denom = sqrt($norm_a) * sqrt($norm_b); | |||||||||
| 50 | 10 | ▶ | return 0 if $denom == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 9 | return $dot / $denom; | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | 3 | ▶ | sub cosine_distance { | ||||||||
|
Subroutine
cosine_distance: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 56 | 3 | my ( $a, $b ) = @_; | |||||||||
| 57 | 3 | return 1 - cosine_similarity($a, $b); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | 4 | ▶ | sub euclidean_distance { | ||||||||
|
Subroutine
euclidean_distance: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 62 | 4 | my ( $a, $b ) = @_; | |||||||||
| 63 | 4 | ▶ | croak "vectors must have same dimensions" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | unless @$a == @$b; | ||||||||||
| 65 | 3 | my $sum = 0; | |||||||||
| 66 | 3 | for my $i (0..$#$a) { | |||||||||
| 67 | 6 | my $d = $a->[$i] - $b->[$i]; | |||||||||
| 68 | 6 | $sum += $d * $d; | |||||||||
| 69 | } | ||||||||||
| 70 | 3 | return sqrt($sum); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | |||||||||||
| 74 | 2 | 1; | |||||||||
| 75 | |||||||||||
| 76 | __END__ | ||||||||||