math.distances: adding a couple more distance functions.

db4
John Benediktsson 2012-05-03 15:42:12 -07:00
parent 31c7bd8e86
commit d3d109aa3e
2 changed files with 22 additions and 4 deletions

View File

@ -1,8 +1,16 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: math.distances tools.test ;
USING: kernel math.distances math.functions tools.test ;
IN: math.distances.tests
{ 1 } [ "hello" "jello" hamming-distance ] unit-test
{ 0.0 } [ { 1 2 3 } dup cosine-distance ] unit-test
{ t } [ { 1 2 3 } { 4 5 6 } cosine-distance 0.02536815380292379 1e-10 ~ ] unit-test
{ t } [ { 1 2 3 } { 1 -2 3 } cosine-distance 0.5714285714285714 1e-10 ~ ] unit-test
{ 143/105 } [ { 1 2 3 } { 4 5 6 } canberra-distance ] unit-test
{ 3/7 } [ { 1 2 3 } { 4 5 6 } bray-curtis-distance ] unit-test

View File

@ -1,7 +1,8 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: kernel math math.functions sequences sequences.extras ;
USING: kernel math math.functions math.vectors sequences
sequences.extras ;
IN: math.distances
@ -17,5 +18,14 @@ IN: math.distances
: manhattan-distance ( a b -- n )
1 minkowski-distance ;
: chebyshev-distance ( a b -- n )
[ - abs ] 2map supremum ;
: chebyshev-distance ( a b -- n ) ! also chessboard-distance
v- vabs supremum ;
: cosine-distance ( a b -- n )
[ v* sum ] [ [ norm ] bi@ * ] 2bi / 1 swap - ;
: canberra-distance ( a b -- n )
[ v- vabs ] [ [ vabs ] bi@ v+ ] 2bi v/ sum ;
: bray-curtis-distance ( a b -- n )
[ v- ] [ v+ ] 2bi [ vabs sum ] bi@ / ;