metric 0.1.4 → 0.2.0
raw patch · 4 files changed
+10/−49 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Data.Metric: Cosine :: Vector Double -> Cosine
- Data.Metric: getCosine :: Cosine -> Vector Double
- Data.Metric: newtype Cosine
- Data.Metric.Vector.Real: Cosine :: Vector Double -> Cosine
- Data.Metric.Vector.Real: getCosine :: Cosine -> Vector Double
- Data.Metric.Vector.Real: instance Eq Cosine
- Data.Metric.Vector.Real: instance Metric Cosine
- Data.Metric.Vector.Real: instance Show Cosine
- Data.Metric.Vector.Real: newtype Cosine
Files
- metric.cabal +1/−1
- src/Data/Metric.hs +1/−2
- src/Data/Metric/Vector/Real.lhs +7/−34
- test/Main.hs +1/−12
metric.cabal view
@@ -1,5 +1,5 @@ name: metric-version: 0.1.4+version: 0.2.0 synopsis: Metric spaces. license: MIT license-file: LICENSE
src/Data/Metric.hs view
@@ -6,7 +6,6 @@ RestrictedDamerauLevenshtein(..), Euclidean(..), Taxicab(..),- Cosine(..), Chebyshev(..), PostOffice(..) ) where@@ -14,4 +13,4 @@ import Data.Metric.Class (Metric(..)) import Data.Metric.Set (Discrete(..)) import Data.Metric.String (Hamming(..), Levenshtein(..), RestrictedDamerauLevenshtein(..)) -import Data.Metric.Vector.Real (Euclidean(..), Taxicab(..), Cosine(..), Chebyshev(..), PostOffice(..))+import Data.Metric.Vector.Real (Euclidean(..), Taxicab(..), Chebyshev(..), PostOffice(..))
src/Data/Metric/Vector/Real.lhs view
@@ -5,15 +5,14 @@ > module Data.Metric.Vector.Real ( > Euclidean(..), > Taxicab(..),-> Cosine(..), > Chebyshev(..), > PostOffice(..) > ) where > -> import Prelude hiding (zipWith, map, foldr1, maximum, length)+> import Prelude hiding (zipWith, map, maximum, length, sum) > import Data.Function (on) > import Data.Packed.Matrix.Extras (fromVectors)-> import Data.Vector (Vector(..), zipWith, map, foldr1, maximum, length)+> import Data.Vector (Vector(..), zipWith, map, maximum, length, sum) > import Data.Vector.Extras (zero) > import Numeric.LinearAlgebra.Algorithms (rank) > import Data.Metric.Class (Metric(..))@@ -33,7 +32,7 @@ > } deriving (Eq, Show) > > instance Metric Euclidean where-> distance = sqrt . foldr1 (+) . map (**2) <$$> zipWith (-) `on` getEuclidean+> distance = sqrt . sum . map (**2) <$$> zipWith (-) `on` getEuclidean `Taxicab` describes the length of the path connecting the two vectors along only vertical and horizontal lines (`_|`) without backtracing.@@ -45,36 +44,7 @@ > } deriving (Eq, Show) > > instance Metric Taxicab where-> distance = foldr1 (+) . map abs <$$> zipWith (-) `on` getTaxicab- -`Cosine` wraps cosine similarity, measuring the cosine of the angle-between two vectors using the Euclidean dot product formula. Unlike the-other distance functions, this describes orientation, rather than-magnitude; this is useful when comparing tf-idf weights, as it-implicitly normalises for document length.--Edge case: the denominator of this function is the product of the-vectors norms; when either of the vectors have no magnitude, this-describes division by zero. A runtime error occurs in this case.--> newtype Cosine = Cosine-> { getCosine :: Vector Double-> } deriving (Eq, Show)-> -> instance Metric Cosine where-> Cosine v0 <-> Cosine v1 -> | norm == 0 = error "zero magnitude vector"-> | otherwise = (v0 `dot` v1) / norm-> where norm = (v0 |*| v1)->-> mag :: Vector Double -> Double-> mag = sqrt . foldr1 (+) . map (**2)->-> dot :: Vector Double -> Vector Double -> Double-> dot = foldr1 (+) <$$> zipWith (*)->-> (|*|) :: Vector Double -> Vector Double -> Double-> (|*|) = (*) `on` mag+> distance = sum . map abs <$$> zipWith (-) `on` getTaxicab `Chebyshev` wraps Chebyshev distance, in which the distance between two vectors is defined to be the maximum of their differences along any@@ -111,3 +81,6 @@ > > (|+|) :: Vector Double -> Vector Double -> Double > (|+|) = (+) `on` mag+>+> mag :: Vector Double -> Double+> mag = sqrt . sum . map (**2)
test/Main.hs view
@@ -1,6 +1,6 @@ import Data.Metric.Set (Discrete(..)) import Data.Metric.String (Hamming(..), Levenshtein(..), RestrictedDamerauLevenshtein(..))-import Data.Metric.Vector.Real (Euclidean(..), Taxicab(..), Cosine(..), Chebyshev(..), PostOffice(..))+import Data.Metric.Vector.Real (Euclidean(..), Taxicab(..), Chebyshev(..), PostOffice(..)) import Control.Applicative ((<$>)) import Data.Vector (Vector(..), fromList) import Data.Ratio ((%))@@ -36,16 +36,6 @@ prop_TaxicabMetric :: Taxicab -> Taxicab -> Taxicab -> Property prop_TaxicabMetric = prop_Metric --- | Cosine--instance Arbitrary Cosine where- arbitrary = oneof [choose (1,1000), choose (-1,-1000)]- >>= vectorOf 3 . return . fromRational . (%100)- >>= return . Cosine . fromList--prop_CosineMetric :: Cosine -> Cosine -> Cosine -> Property-prop_CosineMetric = prop_Metric- -- | Chebyshev instance Arbitrary Chebyshev where@@ -93,7 +83,6 @@ [ testProperty "discrete" $ prop_DiscreteMetric , testProperty "euclidean" $ prop_EuclideanMetric , testProperty "taxicab" $ prop_TaxicabMetric- , testProperty "cosine" $ prop_CosineMetric , testProperty "chebyshev" $ prop_ChebyshevMetric , testProperty "postoffice" $ prop_PostOfficeMetric , testProperty "hamming" $ prop_HammingMetric