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 +++++++++++++++++++++++++++++- lib/Numeric/LinearAlgebra.hs | 3 -- lib/Numeric/LinearAlgebra/Algorithms.hs | 6 ++-- lib/Numeric/LinearAlgebra/Interface.hs | 4 +-- lib/Numeric/LinearAlgebra/LAPACK.hs | 5 +-- lib/Numeric/LinearAlgebra/Linear.hs | 4 ++- lib/Numeric/LinearAlgebra/Tests.hs | 6 ++++ 9 files changed, 75 insertions(+), 70 deletions(-) (limited to 'lib') 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 + diff --git a/lib/Numeric/LinearAlgebra.hs b/lib/Numeric/LinearAlgebra.hs index 2652f2d..7664a9f 100644 --- a/lib/Numeric/LinearAlgebra.hs +++ b/lib/Numeric/LinearAlgebra.hs @@ -14,13 +14,10 @@ This module reexports all normally required functions for Linear Algebra applica ----------------------------------------------------------------------------- module Numeric.LinearAlgebra ( module Data.Packed, - module Data.Packed.Random, module Numeric.LinearAlgebra.Algorithms, module Numeric.LinearAlgebra.Interface ) where import Data.Packed -import Data.Packed.Random import Numeric.LinearAlgebra.Algorithms -import Numeric.LinearAlgebra.Instances() import Numeric.LinearAlgebra.Interface diff --git a/lib/Numeric/LinearAlgebra/Algorithms.hs b/lib/Numeric/LinearAlgebra/Algorithms.hs index 7de9587..55398e0 100644 --- a/lib/Numeric/LinearAlgebra/Algorithms.hs +++ b/lib/Numeric/LinearAlgebra/Algorithms.hs @@ -76,8 +76,10 @@ module Numeric.LinearAlgebra.Algorithms ( ) where -import Data.Packed.Internal hiding (fromComplex, toComplex, conj, (//)) -import Data.Packed +import Data.Packed.Internal hiding ((//)) +import Data.Packed.Vector +import Data.Packed.Matrix +import Data.Complex import Numeric.GSL.Vector import Numeric.LinearAlgebra.LAPACK as LAPACK import Numeric.LinearAlgebra.Linear diff --git a/lib/Numeric/LinearAlgebra/Interface.hs b/lib/Numeric/LinearAlgebra/Interface.hs index 750670b..30547d9 100644 --- a/lib/Numeric/LinearAlgebra/Interface.hs +++ b/lib/Numeric/LinearAlgebra/Interface.hs @@ -9,9 +9,7 @@ Maintainer : Alberto Ruiz (aruiz at um dot es) Stability : provisional Portability : portable -Operators for frequent operations. - -This module exports Show, Read, Eq, Num, Fractional, and Floating instances for Vector and Matrix. +Some useful operators, and Show, Read, Eq, Num, Fractional, and Floating instances for Vector and Matrix. In the context of the standard numeric operators, one-component vectors and matrices automatically expand to match the dimensions of the other operand. diff --git a/lib/Numeric/LinearAlgebra/LAPACK.hs b/lib/Numeric/LinearAlgebra/LAPACK.hs index f5af8be..7f057ba 100644 --- a/lib/Numeric/LinearAlgebra/LAPACK.hs +++ b/lib/Numeric/LinearAlgebra/LAPACK.hs @@ -41,8 +41,9 @@ module Numeric.LinearAlgebra.LAPACK ( schurR, schurC ) where -import Data.Packed.Internal hiding (toComplex) -import Data.Packed +import Data.Packed.Internal +import Data.Packed.Matrix +import Data.Complex import Numeric.GSL.Vector(vectorMapValR, FunCodeSV(Scale)) import Foreign import Foreign.C.Types (CInt) diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs index 2568410..c802712 100644 --- a/lib/Numeric/LinearAlgebra/Linear.hs +++ b/lib/Numeric/LinearAlgebra/Linear.hs @@ -18,7 +18,9 @@ module Numeric.LinearAlgebra.Linear ( Linear(..) ) where -import Data.Packed +import Data.Packed.Vector +import Data.Packed.Matrix +import Data.Complex import Numeric.GSL.Vector -- | Basic element-by-element functions. diff --git a/lib/Numeric/LinearAlgebra/Tests.hs b/lib/Numeric/LinearAlgebra/Tests.hs index ac26466..65b0e4c 100644 --- a/lib/Numeric/LinearAlgebra/Tests.hs +++ b/lib/Numeric/LinearAlgebra/Tests.hs @@ -78,6 +78,11 @@ volSphere r = 8 * quad2 (\x y -> sqrt (r*r-x*x-y*y)) --------------------------------------------------------------------- +derivTest = abs (d (\x-> x * d (\y-> x+y) 1) 1 - 1) < 1E-10 + where d f x = fst $ derivCentral 0.01 f x + +--------------------------------------------------------------------- + -- besselTest = utest "bessel_J0_e" ( abs (r-expected) < e ) -- where (r,e) = bessel_J0_e 5.0 -- expected = -0.17759677131433830434739701 @@ -319,6 +324,7 @@ runTests n = do -- , utest "gamma" (gamma 5 == 24.0) -- , besselTest -- , exponentialTest + , utest "deriv" derivTest , utest "integrate" (abs (volSphere 2.5 - 4/3*pi*2.5^3) < 1E-8) , utest "polySolve" (polySolveProp [1,2,3,4]) , minimizationTest -- cgit v1.2.3