forked from solresol/padiclinear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Metric.lhs
38 lines (35 loc) · 1.2 KB
/
Metric.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
> {-# LANGUAGE FlexibleInstances #-}
> module Metric where
> import Data.Ratio
>
> data Metric = Padic Integer | Euclidean
> -- Maybe one day I'll care about manhattan and other more exotic metrics
>
> class Measure a where
> metric :: Metric -> a -> Double
This should perhaps be a class with methods
- metric
- norm
- maybe other stuff also (regression tests)
I don't think the Haskell type system is capable of handling
this properly though.
> padic_rational_metric :: Integer -> Rational -> Double
> padic_rational_metric p n
> | numerator n == 0 = 0.0
> | otherwise = (fromInteger p) ** (- (fromInteger expon))
> where number_of_divisors m
> | remainder == 0 = 1 + (number_of_divisors quotient)
> | otherwise = 0
> where (quotient, remainder) = m `quotRem` p
> expon = (number_of_divisors $! (numerator n)) -
> (number_of_divisors $! (denominator n))
>
> instance Measure Integer where
> metric (Padic p) n = padic_rational_metric p (n % 1)
> metric (Euclidean) n = fromInteger (abs n)
>
> instance Measure (Rational) where
> metric (Padic p) n = padic_rational_metric p n
> metric (Euclidean) n = abs (fromRational n)
>
> euclidean = abs