From aa14e6615533e7bd5e2b15acdc3ec76afbe1aac4 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Thu, 21 Jun 2007 08:39:22 +0000 Subject: working with tensors --- lib/Data/Packed/Internal/Common.hs | 4 ++-- lib/Data/Packed/Internal/Tensor.hs | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/Data/Packed/Internal/Common.hs b/lib/Data/Packed/Internal/Common.hs index acefe92..91985f7 100644 --- a/lib/Data/Packed/Internal/Common.hs +++ b/lib/Data/Packed/Internal/Common.hs @@ -74,8 +74,8 @@ check msg ls f = do mapM_ (touchForeignPtr . fptr) ls return () -class (Storable a, Typeable a) => Field a where -instance (Storable a, Typeable a) => Field a where +class (Storable a, Typeable a) => Field a +instance (Storable a, Typeable a) => Field a isReal w x = typeOf (undefined :: Double) == typeOf (w x) isComp w x = typeOf (undefined :: Complex Double) == typeOf (w x) diff --git a/lib/Data/Packed/Internal/Tensor.hs b/lib/Data/Packed/Internal/Tensor.hs index 123270d..27fce6a 100644 --- a/lib/Data/Packed/Internal/Tensor.hs +++ b/lib/Data/Packed/Internal/Tensor.hs @@ -18,7 +18,7 @@ import Data.Packed.Internal import Data.Packed.Internal.Vector import Data.Packed.Internal.Matrix import Foreign.Storable -import Data.List(sort) +import Data.List(sort,elemIndex,nub) data IdxTp = Covariant | Contravariant deriving (Show,Eq) @@ -107,3 +107,18 @@ normal t = tridx (names t) t contractions t1 t2 = [ contraction t1 n1 t2 n2 | n1 <- names t1, n2 <- names t2, compatIdx t1 n1 t2 n2 ] +-- sent to Haskell-Cafe by Sebastian Sylvan +perms [x] = [[x]] +perms xs = [y:ps | (y,ys) <- selections xs , ps <- perms ys] +selections [] = [] +selections (x:xs) = (x,xs) : [(y,x:ys) | (y,ys) <- selections xs] + + +interchanges ls = sum (map (count ls) ls) + where count l p = n + where Just pel = elemIndex p l + n = length $ filter (>p) $ take pel l + +signature l | length (nub l) < length l = 0 + | even (interchanges l) = 1 + | otherwise = -1 -- cgit v1.2.3