Skip to content

Commit

Permalink
signAnalysis
Browse files Browse the repository at this point in the history
  • Loading branch information
stla committed May 4, 2024
1 parent 85254ba commit d11ba34
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions scripts/signAnalysis.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{-# LANGUAGE FlexibleContexts #-}
import qualified Algebra.Absolute as AlgAbs
import qualified Algebra.Additive as AlgAdd
import qualified Algebra.Ring as AlgRing

runLengthEncoding :: Eq a => [a] -> [(a,Int)]
Expand All @@ -9,8 +11,8 @@ runLengthEncoding = foldr code []
| c == x = (x,n+1):ts
| otherwise = (c,1):(x,n):ts

signPermancesAndVariations :: (Eq a, AlgRing.C a, AlgAbs.C a) -> [a] -> (Int, Int)
signPermancesAndVariations as = (permances, variations)
signPermancesAndVariations :: (Eq a, AlgRing.C a, AlgAbs.C a) => [a] -> (Int, Int)
signPermancesAndVariations as = (permanences, variations)
where
sign a = if AlgAbs.signum a == AlgRing.one then '+' else '-'
signs = map sign as
Expand All @@ -20,15 +22,16 @@ signPermancesAndVariations as = (permances, variations)
permanences = sum lengths - l
variations = l - 1

signVariations' :: (Eq a, AlgRing.C a, AlgAbs.C a) -> [a] -> Int
signVariations' :: forall a. (Eq a, AlgRing.C a, AlgAbs.C a) => [a] -> Int
signVariations' as = v1 + v2 + 2*v3
where
count x xs = sum (map (fromEnum . (== x)) xs)
l = length as
sign a = case AlgAbs.signum a of
AlgRing.zero -> '0'
AlgRing.one -> '+'
_ -> '-'
sign :: a -> Char
sign a
| AlgAbs.signum a == AlgAdd.zero = '0'
| AlgAbs.signum a == AlgRing.one = '+'
| otherwise = '-'
signs = map sign as
chunks2 = [(signs !! i, signs !! (i+1)) | i <- [0 .. l-2]]
v1 = count ('+', '-') chunks2
Expand Down

0 comments on commit d11ba34

Please sign in to comment.