From af8f23a78c897c80beece1091bf6f9e70243ae19 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 3 May 2012 13:38:59 -0700 Subject: [PATCH] math.distances: adding some distance functions. --- extra/math/distances/authors.txt | 1 + extra/math/distances/distances-tests.factor | 8 ++++++++ extra/math/distances/distances.factor | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 extra/math/distances/authors.txt create mode 100644 extra/math/distances/distances-tests.factor create mode 100644 extra/math/distances/distances.factor diff --git a/extra/math/distances/authors.txt b/extra/math/distances/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/extra/math/distances/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/extra/math/distances/distances-tests.factor b/extra/math/distances/distances-tests.factor new file mode 100644 index 0000000000..1e80dcb0f2 --- /dev/null +++ b/extra/math/distances/distances-tests.factor @@ -0,0 +1,8 @@ +! Copyright (C) 2012 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: math.distances tools.test ; + +IN: math.distances.tests + +{ 1 } [ "hello" "jello" hamming-distance ] unit-test diff --git a/extra/math/distances/distances.factor b/extra/math/distances/distances.factor new file mode 100644 index 0000000000..31b78f0800 --- /dev/null +++ b/extra/math/distances/distances.factor @@ -0,0 +1,21 @@ +! Copyright (C) 2012 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: kernel math math.functions sequences sequences.extras ; + +IN: math.distances + +: hamming-distance ( a b -- n ) + [ = not ] 2count ; inline + +: minkowski-distance ( a b p -- n ) + [ [ [ - abs ] dip ^ ] curry 2map-sum ] keep recip ^ ; + +: euclidian-distance ( a b -- n ) + 2 minkowski-distance ; ! also math.vectors.distance + +: manhattan-distance ( a b -- n ) + 1 minkowski-distance ; + +: chebyshev-distance ( a b -- n ) + [ - abs ] 2map supremum ;