{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}

{- FIXME:
Rationale for -fno-warn-orphans:
 * The orphan instances can't be put into Numeric.NonNegative.Wrapper
   since that's in another package.
 * We had to spread the instance declarations
   over the modules defining the typeclasses instantiated.
   Do we want that?
 * We could define the DiscreteMap as newtype.
-}

{- |
DiscreteMap was originally intended as a type class
that unifies Map and Array.
One should be able to simply choose between
 - Map for sparse arrays
 - Array for full arrays.

However, the Edison package provides the class AssocX
which already exists for that purpose.

Currently I use this module for some numeric instances of Data.Map.
-}
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

-- FIXME: Should this be implemented by isZero?
-- | Remove all zero values from the map.
strip :: (Ord i, Eq v, Additive.C v) => Map i v -> Map i v
strip :: Map i v -> Map i v
strip = (v -> Bool) -> Map i v -> Map i v
forall a k. (a -> Bool) -> Map k a -> Map k a
Map.filter (v
forall a. C a => a
zero v -> v -> Bool
forall a. Eq a => a -> a -> Bool
/=)
--strip = Map.filter (((0 /=) .) . (flip const))

instance (Ord i, Eq v, Additive.C v) => Additive.C (Map i v) where
   zero :: Map i v
zero = Map i v
forall k a. Map k a
Map.empty
   + :: Map i v -> Map i v -> Map i v
(+)  = (Map i v -> Map i v
forall i v. (Ord i, Eq v, C v) => Map i v -> Map i v
strip(Map i v -> Map i v) -> (Map i v -> Map i v) -> Map i v -> Map i v
forall b c a. (b -> c) -> (a -> b) -> a -> c
.)((Map i v -> Map i v) -> Map i v -> Map i v)
-> (Map i v -> Map i v -> Map i v) -> Map i v -> Map i v -> Map i v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> v -> v) -> Map i v -> Map i v -> Map i v
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith v -> v -> v
forall a. C a => a -> a -> a
(+)
   --(+) y x = strip (Map.unionWith (+) y x)
   (-) Map i v
x Map i v
y = Map i v -> Map i v -> Map i v
forall a. C a => a -> a -> a
(+) Map i v
x (Map i v -> Map i v
forall a. C a => a -> a
negate Map i v
y)
   {- won't work because Map.unionWith won't negate a value from y if no x value corresponds to it
   (-) x y = strip (Map.unionWith sub x y)
   -}
   negate :: Map i v -> Map i v
negate  = (v -> v) -> Map i v -> Map i v
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap v -> v
forall a. C a => a -> a
negate

instance Ord i => Vector.C (Map i) where
   zero :: Map i a
zero  = Map i a
forall k a. Map k a
Map.empty
   <+> :: Map i a -> Map i a -> Map i a
(<+>) = (a -> a -> a) -> Map i a -> Map i a -> Map i a
forall k a. Ord k => (a -> a -> a) -> Map k a -> Map k a -> Map k a
Map.unionWith a -> a -> a
forall a. C a => a -> a -> a
(+)
   -- requires Eq instance for expo
   -- expo *> x = if expo == zero then zero else Vector.functorScale expo x
   *> :: a -> Map i a -> Map i a
(*>)  = a -> Map i a -> Map i a
forall (v :: * -> *) a. (Functor v, C a) => a -> v a -> v a
Vector.functorScale

instance (Ord i, Eq a, Eq v, Module.C a v)
             => Module.C a (Map i v) where
--   (*>) 0    = \_ -> zero
--   (*>) expo = fmap ((*>) expo)
   *> :: a -> Map i v -> Map i v
(*>) a
expo Map i v
x = if a
expo a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
forall a. C a => a
zero then Map i v
forall a. C a => a
zero else (v -> v) -> Map i v -> Map i v
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a
expo a -> v -> v
forall a v. C a v => a -> v -> v
*>) Map i v
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 :: Map i v -> a
norm = (a -> a -> a) -> a -> [a] -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl a -> a -> a
forall a. C a => a -> a -> a
(+) a
forall a. C a => a
zero ([a] -> a) -> (Map i v -> [a]) -> Map i v -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> a) -> [v] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map v -> a
forall a v. C a v => v -> a
NormedSum.norm ([v] -> [a]) -> (Map i v -> [v]) -> Map i v -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map i v -> [v]
forall k a. Map k a -> [a]
Map.elems

instance (Ord i, Eq a, Eq v, NormedEuc.Sqr a v)
             => NormedEuc.Sqr a (Map i v) where
   normSqr :: Map i v -> a
normSqr = (a -> a -> a) -> a -> [a] -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl a -> a -> a
forall a. C a => a -> a -> a
(+) a
forall a. C a => a
zero ([a] -> a) -> (Map i v -> [a]) -> Map i v -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> a) -> [v] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map v -> a
forall a v. Sqr a v => v -> a
NormedEuc.normSqr ([v] -> [a]) -> (Map i v -> [v]) -> Map i v -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map i v -> [v]
forall k a. Map k a -> [a]
Map.elems

instance (Ord i, Eq a, Eq v, Algebraic.C a, NormedEuc.Sqr a v)
             => NormedEuc.C a (Map i v) where
   norm :: Map i v -> a
norm = Map i v -> a
forall a v. (C a, Sqr a v) => v -> a
NormedEuc.defltNorm

instance (Ord i, Eq a, Eq v, NormedMax.C a v)
             => NormedMax.C a (Map i v) where
   norm :: Map i v -> a
norm = (a -> a -> a) -> a -> [a] -> a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl a -> a -> a
forall a. Ord a => a -> a -> a
max a
forall a. C a => a
zero ([a] -> a) -> (Map i v -> [a]) -> Map i v -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (v -> a) -> [v] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map v -> a
forall a v. C a v => v -> a
NormedMax.norm ([v] -> [a]) -> (Map i v -> [v]) -> Map i v -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map i v -> [v]
forall k a. Map k a -> [a]
Map.elems