From ff8ba85a52acdd1e30f45ba73ae0c40986c8a9d4 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Wed, 12 May 2010 06:15:35 +0000 Subject: more simplification --- INSTALL | 41 +++++-------------------- 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 ++++ 10 files changed, 83 insertions(+), 103 deletions(-) diff --git a/INSTALL b/INSTALL index f0426b0..9d63761 100644 --- a/INSTALL +++ b/INSTALL @@ -1,61 +1,36 @@ INSTALLATION -Recommended method (ok in Ubuntu/Debian systems): +Recommended method (tested on Ubuntu/Debian systems): $ sudo apt-get install libgsl0-dev liblapack-dev $ cabal install hmatrix Detailed installation instructions: http://code.haskell.org/hmatrix/install.html -INSTALLATION ON WINDOWS (hmatrix-0.9.x.x) +INSTALLATION ON WINDOWS -1) Install the Haskell Platform (tested with 2009.2.0.2). +1) Install the Haskell Platform (tested on 2009.2.0.2). 2) Download and unzip the following file into a stable folder %GSL% http://code.haskell.org/hmatrix/gsl-lapack-windows.zip -3.a) In a msys shell +3.a) In a msys shell installation should be fully automatic: $ cabal install hmatrix --extra-lib-dir=${GSL} --extra-include-dir=${GSL} -3.b) In a windows cmd +3.b) Alternatively, in a normal windows cmd: > cabal unpack hmatrix - Change build-type to "Simple" in hmatrix.cabal, line 18 + Edit hmatrix.cabal, in line 18 change build-type to "Simple", and then > cabal install --extra-lib-dir=%GSL% --extra-include-dir=%GSL% 4) If everything is ok we can run the tests: - > runhaskell examples\tests.hs - -INSTALLATION ON WINDOWS (hmatrix-0.8.3.1) - -1) Install the Haskell Platform (tested with 2009.2.0.2). - -2) Download and uncompress hmatrix from Hackage - - > cd c:\haskell - > cabal unpack hmatrix - -3) Download and uncompress the following file into the hmatrix - directory, into a subdirectory named gsl-lapack-windows: - - http://code.haskell.org/hmatrix/gsl-lapack-windows.zip - -4) Replace hmatrix.cabal by - - http://code.haskell.org/hmatrix/hmatrix-0.8.3.1-windows.cabal - -5) In the hmatrix folder run - - > cabal install - -6) If everything is ok we can run the tests: - - > runhaskell examples\tests.hs + > ghci + Prelude> Numeric.LinearAlgebra.Tests.runTests 20 NOTE: The examples using graphics do not yet work in windows. 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