Copyright | (C) 2015 Ricky Elrod, Tony Morris |
---|---|
License | BSD2 (see LICENSE file) |
Maintainer | Ricky Elrod <ricky@elrod.me> |
Stability | provisional |
Portability | lens |
Safe Haskell | None |
Language | Haskell2010 |
A MetricSpace
is a set together with a notion of distance between
elements. Distance is computed by a function dist
which has the following
four laws:
- non-negative:
forall x y.
dist
x y >= 0 - identity of indiscernibles:
forall x y.
dist
x y == 0 <=> x == y - symmetry:
forall x y. dist x y ==
dist
y x - triangle inequality:
forall x y z.
dist
x z <=dist
x y +dist
y z
See the Wikipedia article on metric spaces for more details.
- newtype MetricSpace a b = MetricSpace {
- dist :: a -> a -> b
- type ClosedMetricSpace a = MetricSpace a a
- newtype FlippedMetricSpace b a = FlippedMetricSpace (MetricSpace a b)
- type ClosedFlippedMetricSpace a = FlippedMetricSpace a a
- (<->) :: MetricSpace a b -> a -> a -> b
- _FlippedMetricSpace :: Iso (MetricSpace a b) (MetricSpace x y) (FlippedMetricSpace b a) (FlippedMetricSpace y x)
- class SwappedMetricSpace m where
- _SwappedMetricSpace :: Iso (m a b) (m x y) (m a b) (m x y)
- levenshtein :: Integral b => MetricSpace String b
- discrete :: (Eq a, Integral b) => MetricSpace (Vector a) b
- euclidean :: RealFloat a => MetricSpace (Vector a) a
- taxicab :: RealFloat a => MetricSpace (Vector a) a
Documentation
>>>
import qualified Data.Vector as V
newtype MetricSpace a b Source
MetricSpace | |
|
Profunctor MetricSpace | |
SwappedMetricSpace MetricSpace | |
Monad (MetricSpace a) | |
Functor (MetricSpace a) | |
Applicative (MetricSpace a) | |
Monoid b => Monoid (MetricSpace a b) | |
Semigroup b => Semigroup (MetricSpace a b) |
type ClosedMetricSpace a = MetricSpace a a Source
newtype FlippedMetricSpace b a Source
type ClosedFlippedMetricSpace a = FlippedMetricSpace a a Source
(<->) :: MetricSpace a b -> a -> a -> b infixl 8 Source
_FlippedMetricSpace :: Iso (MetricSpace a b) (MetricSpace x y) (FlippedMetricSpace b a) (FlippedMetricSpace y x) Source
class SwappedMetricSpace m where Source
Nothing
_SwappedMetricSpace :: Iso (m a b) (m x y) (m a b) (m x y) Source
levenshtein :: Integral b => MetricSpace String b Source
Levenshtein distance between String
s.
>>>
dist levenshtein "foo" "bar"
3
>>>
dist levenshtein "hi" "ha"
1
>>>
dist levenshtein "ff" "ff"
0
discrete :: (Eq a, Integral b) => MetricSpace (Vector a) b Source
Discrete distance over n-dimensional Vector
s.
>>>
dist discrete (V.fromList [3,4]) (V.fromList [3,4])
0
>>>
dist discrete (V.fromList [1,49]) (V.fromList [3,-94])
1
euclidean :: RealFloat a => MetricSpace (Vector a) a Source
Euclidean distance over n-dimensional Vector
s.
>>>
dist euclidean (V.fromList [3,4]) (V.fromList [3,4])
0.0
>>>
dist euclidean (V.fromList [1,49]) (V.fromList [3,-94])
143.01398533010678
taxicab :: RealFloat a => MetricSpace (Vector a) a Source
Taxicab distance over n-dimensional Vector
s.
>>>
dist taxicab (V.fromList [3,4]) (V.fromList [3,4])
0.0
>>>
dist taxicab (V.fromList [1,49]) (V.fromList [3,-94])
145.0