module Data.Sparse.Internal.IntM where
import Data.Sparse.Utils
import Numeric.LinearAlgebra.Class
import GHC.Exts
import Data.Complex
import Data.VectorSpace
import qualified Data.IntMap as IM
newtype IntM a = IntM {unIM :: IM.IntMap a} deriving (Eq, Show, Functor, Foldable)
empty = IntM IM.empty
size (IntM x) = IM.size x
singleton i x = IntM $ IM.singleton i x
filterWithKey f im = IntM $ IM.filterWithKey f (unIM im)
insert k x (IntM im) = IntM $ IM.insert k x im
filterI f (IntM im) = IntM $ IM.filter f im
lookup x (IntM im) = IM.lookup x im
lookupLT x (IntM im) = IM.lookupLT x im
foldlWithKey f z (IntM im) = IM.foldlWithKey f z im
foldlWithKey' f z (IntM im) = IM.foldlWithKey' f z im
mapWithKey f (IntM im) = IntM $ IM.mapWithKey f im
keys (IntM im) = IM.keys im
mapKeys f (IntM im) = IntM $ IM.mapKeys f im
union (IntM a) (IntM b) = IntM $ IM.union a b
findMin (IntM im) = IM.findMin im
findMax (IntM im) = IM.findMax im
(IntM im) ! i = im IM.! i
instance IsList (IntM a) where
type Item (IntM a) = (Int, a)
fromList = IntM . IM.fromList
toList = IM.toList . unIM
instance Set IntM where
liftU2 f (IntM a) (IntM b) = IntM $ IM.unionWith f a b
liftI2 f (IntM a) (IntM b) = IntM $ IM.intersectionWith f a b
instance Num a => AdditiveGroup (IntM a) where
zeroV = IntM IM.empty
(^+^) = liftU2 (+)
(^-^) = liftU2 ()
negateV = fmap negate
#define ParamInstance(f, t) \
instance VectorSpace (f t) where {type (Scalar (f (t))) = (t); n *^ im = fmap (* n) im};\
instance VectorSpace (f (Complex t)) where {type (Scalar (f (Complex t))) = Complex (t); n *^ im = fmap (* n) im};\
instance InnerSpace (f t) where {a <.> b = sum $ liftI2 (*) a b};\
instance InnerSpace (f (Complex t)) where {a <.> b = sum $ liftI2 (*) (conjugate <$> a) b};\
instance Normed (IntM Double) where
type RealScalar (IntM Double) = Double
type Magnitude (IntM Double) = Double
norm1 a = sum (abs <$> a)
norm2Sq a = sum $ liftI2 (*) a a
normP p v = sum u**(1/p) where u = fmap (**p) v
normalize p v = v ./ normP p v
normalize2 v = v ./ norm2 v
instance Normed (IntM (Complex Double)) where
type RealScalar (IntM (Complex Double)) = Double
type Magnitude (IntM (Complex Double)) = Double
norm1 a = realPart $ sum (abs <$> a)
norm2Sq a = realPart $ sum $ liftI2 (*) (conjugate <$> a) a
normP p v = realPart $ sum u**(1/(p :+ 0)) where u = fmap (**(p :+ 0)) v
normalize p v = v ./ toC (normP p v)
normalize2 v = v ./ toC (norm2 v)
#define IntMapInstance(t) \
ParamInstance( IntM, t )
IntMapInstance(Double)
mkIm xs = fromList $ denseIxArray xs :: IntM Double
mkImC xs = fromList $ denseIxArray xs :: IntM (Complex Double)