From 4730254f061832591d4a44c86d3bdfa4620f4322 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Tue, 30 Jun 2015 12:30:57 +0200 Subject: CTrans class to preserve slices in real transposes --- packages/base/src/Internal/Algorithms.hs | 1 + packages/base/src/Internal/Container.hs | 1 + packages/base/src/Internal/Matrix.hs | 2 +- packages/base/src/Internal/Modular.hs | 4 ++++ packages/base/src/Internal/Numeric.hs | 23 +++++++++++++++++++---- 5 files changed, 26 insertions(+), 5 deletions(-) (limited to 'packages/base/src') diff --git a/packages/base/src/Internal/Algorithms.hs b/packages/base/src/Internal/Algorithms.hs index 1235da3..6ce1830 100644 --- a/packages/base/src/Internal/Algorithms.hs +++ b/packages/base/src/Internal/Algorithms.hs @@ -45,6 +45,7 @@ class (Product t, Normed Matrix t, Normed Vector t, Floating t, + CTrans t, RealOf t ~ Double) => Field t where svd' :: Matrix t -> (Matrix t, Vector Double, Matrix t) thinSVD' :: Matrix t -> (Matrix t, Vector Double, Matrix t) diff --git a/packages/base/src/Internal/Container.hs b/packages/base/src/Internal/Container.hs index 1c158ff..8926fac 100644 --- a/packages/base/src/Internal/Container.hs +++ b/packages/base/src/Internal/Container.hs @@ -245,6 +245,7 @@ class ( Container Vector t , Container Matrix t , Konst t Int Vector , Konst t (Int,Int) Matrix + , CTrans t , Product t ) => Numeric t diff --git a/packages/base/src/Internal/Matrix.hs b/packages/base/src/Internal/Matrix.hs index a789cae..df56207 100644 --- a/packages/base/src/Internal/Matrix.hs +++ b/packages/base/src/Internal/Matrix.hs @@ -241,7 +241,7 @@ createMatrix ord r c = do p <- createVector (r*c) return (matrixFromVector ord r c p) -{- | Creates a matrix from a vector by grouping the elements in rows with the desired number of columns. (GNU-Octave groups by columns. To do it you can define @reshapeF r = trans . reshape r@ +{- | Creates a matrix from a vector by grouping the elements in rows with the desired number of columns. (GNU-Octave groups by columns. To do it you can define @reshapeF r = tr' . reshape r@ where r is the desired number of rows.) >>> reshape 4 (fromList [1..12]) diff --git a/packages/base/src/Internal/Modular.hs b/packages/base/src/Internal/Modular.hs index 8fa2747..1d8f761 100644 --- a/packages/base/src/Internal/Modular.hs +++ b/packages/base/src/Internal/Modular.hs @@ -159,6 +159,10 @@ instance KnownNat m => Element (Mod m Z) m' = fromIntegral . natVal $ (undefined :: Proxy m) +instance forall m . KnownNat m => CTrans (Mod m I) +instance forall m . KnownNat m => CTrans (Mod m Z) + + instance forall m . KnownNat m => Container Vector (Mod m I) where conj' = id diff --git a/packages/base/src/Internal/Numeric.hs b/packages/base/src/Internal/Numeric.hs index efcde2c..4d34885 100644 --- a/packages/base/src/Internal/Numeric.hs +++ b/packages/base/src/Internal/Numeric.hs @@ -782,9 +782,6 @@ buildV n f = fromList [f k | k <- ks] where ks = map fromIntegral [0 .. (n-1)] -------------------------------------------------------- --- | conjugate transpose -ctrans :: (Container Vector e, Element e) => Matrix e -> Matrix e -ctrans = liftMatrix conj' . trans -- | Creates a square matrix with a given diagonal. diag :: (Num a, Element a) => Vector a -> Matrix a @@ -843,6 +840,24 @@ selectCV f c l e t = f (toInt c') l' e' t' -------------------------------------------------------------------------------- +class CTrans t + where + ctrans :: Matrix t -> Matrix t + ctrans = trans + +instance CTrans Float +instance CTrans R +instance CTrans I +instance CTrans Z + +instance CTrans C + where + ctrans = conj . trans + +instance CTrans (Complex Float) + where + ctrans = conj . trans + class Transposable m mt | m -> mt, mt -> m where -- | conjugate transpose @@ -850,7 +865,7 @@ class Transposable m mt | m -> mt, mt -> m -- | transpose tr' :: m -> mt -instance (Container Vector t) => Transposable (Matrix t) (Matrix t) +instance (CTrans t, Container Vector t) => Transposable (Matrix t) (Matrix t) where tr = ctrans tr' = trans -- cgit v1.2.3