From ff8ba85a52acdd1e30f45ba73ae0c40986c8a9d4 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Wed, 12 May 2010 06:15:35 +0000 Subject: more simplification --- lib/Data/Packed.hs | 54 +++----------------------------------- lib/Data/Packed/Internal/Matrix.hs | 14 +++++----- lib/Data/Packed/Matrix.hs | 49 +++++++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 59 deletions(-) (limited to 'lib/Data') diff --git a/lib/Data/Packed.hs b/lib/Data/Packed.hs index 7d6d200..87695ee 100644 --- a/lib/Data/Packed.hs +++ b/lib/Data/Packed.hs @@ -1,4 +1,3 @@ -{-# OPTIONS_GHC -fglasgow-exts #-} ----------------------------------------------------------------------------- {- | Module : Data.Packed @@ -17,59 +16,12 @@ The Vector and Matrix types and some utilities. module Data.Packed ( module Data.Packed.Vector, module Data.Packed.Matrix, - module Data.Complex, - Container(..) + module Data.Packed.Random, + module Data.Complex ) where import Data.Packed.Vector import Data.Packed.Matrix +import Data.Packed.Random import Data.Complex -import Data.Packed.Internal(fromComplex,toComplex,conj) --- | conversion utilities -class (Element e) => Container c e where - toComplex :: RealFloat e => (c e, c e) -> c (Complex e) - fromComplex :: RealFloat e => c (Complex e) -> (c e, c e) - comp :: RealFloat e => c e -> c (Complex e) - conj :: RealFloat e => c (Complex e) -> c (Complex e) - real :: c Double -> c e - complex :: c e -> c (Complex Double) - -instance Container Vector Double where - toComplex = Data.Packed.Internal.toComplex - fromComplex = Data.Packed.Internal.fromComplex - comp = internalcomp - conj = Data.Packed.Internal.conj - real = id - complex = Data.Packed.comp - -instance Container Vector (Complex Double) where - toComplex = undefined -- can't match - fromComplex = undefined - comp = undefined - conj = undefined - real = Data.Packed.comp - complex = id - -instance Container Matrix Double where - toComplex = uncurry $ liftMatrix2 $ curry Data.Packed.toComplex - fromComplex z = (reshape c r, reshape c i) - where (r,i) = Data.Packed.fromComplex (flatten z) - c = cols z - comp = liftMatrix internalcomp - conj = liftMatrix Data.Packed.Internal.conj - real = id - complex = Data.Packed.comp - -instance Container Matrix (Complex Double) where - toComplex = undefined - fromComplex = undefined - comp = undefined - conj = undefined - real = Data.Packed.comp - complex = id - - --- | converts a real vector into a complex representation (with zero imaginary parts) -internalcomp :: Vector Double -> Vector (Complex Double) -internalcomp v = Data.Packed.Internal.toComplex (v,constant 0 (dim v)) diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs index d879ee6..003e8ee 100644 --- a/lib/Data/Packed/Internal/Matrix.hs +++ b/lib/Data/Packed/Internal/Matrix.hs @@ -29,7 +29,7 @@ module Data.Packed.Internal.Matrix( liftMatrix, liftMatrix2, (@@>), saveMatrix, - fromComplex, toComplex, conj, + fromComplexV, toComplexV, conjV, singleton ) where @@ -368,16 +368,16 @@ subMatrix' (r0,c0) (rt,ct) m = trans $ subMatrix' (c0,r0) (ct,rt) (trans m) -------------------------------------------------------------------------- -- | obtains the complex conjugate of a complex vector -conj :: Vector (Complex Double) -> Vector (Complex Double) -conj = mapVector conjugate +conjV :: Vector (Complex Double) -> Vector (Complex Double) +conjV = mapVector conjugate -- | creates a complex vector from vectors with real and imaginary parts -toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double) -toComplex (r,i) = asComplex $ flatten $ fromColumns [r,i] +toComplexV :: (Vector Double, Vector Double) -> Vector (Complex Double) +toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i] -- | the inverse of 'toComplex' -fromComplex :: Vector (Complex Double) -> (Vector Double, Vector Double) -fromComplex z = (r,i) where +fromComplexV :: Vector (Complex Double) -> (Vector Double, Vector Double) +fromComplexV z = (r,i) where [r,i] = toColumns $ reshape 2 $ asReal z -------------------------------------------------------------------------- diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 4fdd2c6..3147e13 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-} ----------------------------------------------------------------------------- -- | -- Module : Data.Packed.Matrix @@ -13,7 +14,7 @@ ----------------------------------------------------------------------------- module Data.Packed.Matrix ( - Element, + Element, Container(..), Matrix,rows,cols, (><), trans, @@ -421,3 +422,49 @@ toBlocksEvery r c m = toBlocks rs cs m where (qc,rc) = cols m `divMod` c rs = replicate qr r ++ if rr > 0 then [rr] else [] cs = replicate qc c ++ if rc > 0 then [rc] else [] + +------------------------------------------------------------------- + +-- | conversion utilities +class (Element e) => Container c e where + toComplex :: RealFloat e => (c e, c e) -> c (Complex e) + fromComplex :: RealFloat e => c (Complex e) -> (c e, c e) + comp :: RealFloat e => c e -> c (Complex e) + conj :: RealFloat e => c (Complex e) -> c (Complex e) + real :: c Double -> c e + complex :: c e -> c (Complex Double) + +instance Container Vector Double where + toComplex = toComplexV + fromComplex = fromComplexV + comp v = toComplex (v,constant 0 (dim v)) + conj = conjV + real = id + complex = comp + +instance Container Vector (Complex Double) where + toComplex = undefined -- can't match + fromComplex = undefined + comp = undefined + conj = undefined + real = comp + complex = id + +instance Container Matrix Double where + toComplex = uncurry $ liftMatrix2 $ curry toComplex + fromComplex z = (reshape c r, reshape c i) + where (r,i) = fromComplex (flatten z) + c = cols z + comp = liftMatrix comp + conj = liftMatrix conj + real = id + complex = comp + +instance Container Matrix (Complex Double) where + toComplex = undefined + fromComplex = undefined + comp = undefined + conj = undefined + real = comp + complex = id + -- cgit v1.2.3