{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
module MathObj.DiscreteMap where
import qualified Algebra.NormedSpace.Sum as NormedSum
import qualified Algebra.NormedSpace.Euclidean as NormedEuc
import qualified Algebra.NormedSpace.Maximum as NormedMax
import qualified Algebra.VectorSpace as VectorSpace
import qualified Algebra.Module as Module
import qualified Algebra.Vector as Vector
import qualified Algebra.Algebraic as Algebraic
import qualified Algebra.Additive as Additive
import Algebra.Module ((*>))
import Algebra.Additive (zero,(+),negate)
import qualified Data.Map as Map
import Data.Map (Map)
import NumericPrelude.Base
strip :: (Ord i, Eq v, Additive.C v) => Map i v -> Map i v
strip = Map.filter (zero /=)
instance (Ord i, Eq v, Additive.C v) => Additive.C (Map i v) where
zero = Map.empty
(+) = (strip.). Map.unionWith (+)
(-) x y = (+) x (negate y)
negate = fmap negate
instance Ord i => Vector.C (Map i) where
zero = Map.empty
(<+>) = Map.unionWith (+)
(*>) = Vector.functorScale
instance (Ord i, Eq a, Eq v, Module.C a v)
=> Module.C a (Map i v) where
(*>) expo x = if expo == zero then zero else fmap (expo *>) x
instance (Ord i, Eq a, Eq v, VectorSpace.C a v)
=> VectorSpace.C a (Map i v)
instance (Ord i, Eq a, Eq v, NormedSum.C a v)
=> NormedSum.C a (Map i v) where
norm = foldl (+) zero . map NormedSum.norm . Map.elems
instance (Ord i, Eq a, Eq v, NormedEuc.Sqr a v)
=> NormedEuc.Sqr a (Map i v) where
normSqr = foldl (+) zero . map NormedEuc.normSqr . Map.elems
instance (Ord i, Eq a, Eq v, Algebraic.C a, NormedEuc.Sqr a v)
=> NormedEuc.C a (Map i v) where
norm = NormedEuc.defltNorm
instance (Ord i, Eq a, Eq v, NormedMax.C a v)
=> NormedMax.C a (Map i v) where
norm = foldl max zero . map NormedMax.norm . Map.elems