diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-05-12 06:15:35 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-05-12 06:15:35 +0000 |
commit | ff8ba85a52acdd1e30f45ba73ae0c40986c8a9d4 (patch) | |
tree | 1c4ac1d9256a687817633fea426261d6a18ecc31 | |
parent | aa8debb5ab389de7bc5b47c5ae5f038349b6efc1 (diff) |
more simplification
-rw-r--r-- | INSTALL | 41 | ||||
-rw-r--r-- | lib/Data/Packed.hs | 54 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 14 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 49 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra.hs | 3 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Algorithms.hs | 6 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Interface.hs | 4 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/LAPACK.hs | 5 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Linear.hs | 4 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Tests.hs | 6 |
10 files changed, 83 insertions, 103 deletions
@@ -1,61 +1,36 @@ | |||
1 | INSTALLATION | 1 | INSTALLATION |
2 | 2 | ||
3 | Recommended method (ok in Ubuntu/Debian systems): | 3 | Recommended method (tested on Ubuntu/Debian systems): |
4 | $ sudo apt-get install libgsl0-dev liblapack-dev | 4 | $ sudo apt-get install libgsl0-dev liblapack-dev |
5 | $ cabal install hmatrix | 5 | $ cabal install hmatrix |
6 | 6 | ||
7 | Detailed installation instructions: | 7 | Detailed installation instructions: |
8 | http://code.haskell.org/hmatrix/install.html | 8 | http://code.haskell.org/hmatrix/install.html |
9 | 9 | ||
10 | INSTALLATION ON WINDOWS (hmatrix-0.9.x.x) | 10 | INSTALLATION ON WINDOWS |
11 | 11 | ||
12 | 1) Install the Haskell Platform (tested with 2009.2.0.2). | 12 | 1) Install the Haskell Platform (tested on 2009.2.0.2). |
13 | 13 | ||
14 | 2) Download and unzip the following file into a stable folder %GSL% | 14 | 2) Download and unzip the following file into a stable folder %GSL% |
15 | 15 | ||
16 | http://code.haskell.org/hmatrix/gsl-lapack-windows.zip | 16 | http://code.haskell.org/hmatrix/gsl-lapack-windows.zip |
17 | 17 | ||
18 | 3.a) In a msys shell | 18 | 3.a) In a msys shell installation should be fully automatic: |
19 | 19 | ||
20 | $ cabal install hmatrix --extra-lib-dir=${GSL} --extra-include-dir=${GSL} | 20 | $ cabal install hmatrix --extra-lib-dir=${GSL} --extra-include-dir=${GSL} |
21 | 21 | ||
22 | 3.b) In a windows cmd | 22 | 3.b) Alternatively, in a normal windows cmd: |
23 | 23 | ||
24 | > cabal unpack hmatrix | 24 | > cabal unpack hmatrix |
25 | 25 | ||
26 | Change build-type to "Simple" in hmatrix.cabal, line 18 | 26 | Edit hmatrix.cabal, in line 18 change build-type to "Simple", and then |
27 | 27 | ||
28 | > cabal install --extra-lib-dir=%GSL% --extra-include-dir=%GSL% | 28 | > cabal install --extra-lib-dir=%GSL% --extra-include-dir=%GSL% |
29 | 29 | ||
30 | 4) If everything is ok we can run the tests: | 30 | 4) If everything is ok we can run the tests: |
31 | 31 | ||
32 | > runhaskell examples\tests.hs | 32 | > ghci |
33 | 33 | Prelude> Numeric.LinearAlgebra.Tests.runTests 20 | |
34 | INSTALLATION ON WINDOWS (hmatrix-0.8.3.1) | ||
35 | |||
36 | 1) Install the Haskell Platform (tested with 2009.2.0.2). | ||
37 | |||
38 | 2) Download and uncompress hmatrix from Hackage | ||
39 | |||
40 | > cd c:\haskell | ||
41 | > cabal unpack hmatrix | ||
42 | |||
43 | 3) Download and uncompress the following file into the hmatrix | ||
44 | directory, into a subdirectory named gsl-lapack-windows: | ||
45 | |||
46 | http://code.haskell.org/hmatrix/gsl-lapack-windows.zip | ||
47 | |||
48 | 4) Replace hmatrix.cabal by | ||
49 | |||
50 | http://code.haskell.org/hmatrix/hmatrix-0.8.3.1-windows.cabal | ||
51 | |||
52 | 5) In the hmatrix folder run | ||
53 | |||
54 | > cabal install | ||
55 | |||
56 | 6) If everything is ok we can run the tests: | ||
57 | |||
58 | > runhaskell examples\tests.hs | ||
59 | 34 | ||
60 | NOTE: The examples using graphics do not yet work in windows. | 35 | NOTE: The examples using graphics do not yet work in windows. |
61 | 36 | ||
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 @@ | |||
1 | {-# OPTIONS_GHC -fglasgow-exts #-} | ||
2 | ----------------------------------------------------------------------------- | 1 | ----------------------------------------------------------------------------- |
3 | {- | | 2 | {- | |
4 | Module : Data.Packed | 3 | Module : Data.Packed |
@@ -17,59 +16,12 @@ The Vector and Matrix types and some utilities. | |||
17 | module Data.Packed ( | 16 | module Data.Packed ( |
18 | module Data.Packed.Vector, | 17 | module Data.Packed.Vector, |
19 | module Data.Packed.Matrix, | 18 | module Data.Packed.Matrix, |
20 | module Data.Complex, | 19 | module Data.Packed.Random, |
21 | Container(..) | 20 | module Data.Complex |
22 | ) where | 21 | ) where |
23 | 22 | ||
24 | import Data.Packed.Vector | 23 | import Data.Packed.Vector |
25 | import Data.Packed.Matrix | 24 | import Data.Packed.Matrix |
25 | import Data.Packed.Random | ||
26 | import Data.Complex | 26 | import Data.Complex |
27 | import Data.Packed.Internal(fromComplex,toComplex,conj) | ||
28 | 27 | ||
29 | -- | conversion utilities | ||
30 | class (Element e) => Container c e where | ||
31 | toComplex :: RealFloat e => (c e, c e) -> c (Complex e) | ||
32 | fromComplex :: RealFloat e => c (Complex e) -> (c e, c e) | ||
33 | comp :: RealFloat e => c e -> c (Complex e) | ||
34 | conj :: RealFloat e => c (Complex e) -> c (Complex e) | ||
35 | real :: c Double -> c e | ||
36 | complex :: c e -> c (Complex Double) | ||
37 | |||
38 | instance Container Vector Double where | ||
39 | toComplex = Data.Packed.Internal.toComplex | ||
40 | fromComplex = Data.Packed.Internal.fromComplex | ||
41 | comp = internalcomp | ||
42 | conj = Data.Packed.Internal.conj | ||
43 | real = id | ||
44 | complex = Data.Packed.comp | ||
45 | |||
46 | instance Container Vector (Complex Double) where | ||
47 | toComplex = undefined -- can't match | ||
48 | fromComplex = undefined | ||
49 | comp = undefined | ||
50 | conj = undefined | ||
51 | real = Data.Packed.comp | ||
52 | complex = id | ||
53 | |||
54 | instance Container Matrix Double where | ||
55 | toComplex = uncurry $ liftMatrix2 $ curry Data.Packed.toComplex | ||
56 | fromComplex z = (reshape c r, reshape c i) | ||
57 | where (r,i) = Data.Packed.fromComplex (flatten z) | ||
58 | c = cols z | ||
59 | comp = liftMatrix internalcomp | ||
60 | conj = liftMatrix Data.Packed.Internal.conj | ||
61 | real = id | ||
62 | complex = Data.Packed.comp | ||
63 | |||
64 | instance Container Matrix (Complex Double) where | ||
65 | toComplex = undefined | ||
66 | fromComplex = undefined | ||
67 | comp = undefined | ||
68 | conj = undefined | ||
69 | real = Data.Packed.comp | ||
70 | complex = id | ||
71 | |||
72 | |||
73 | -- | converts a real vector into a complex representation (with zero imaginary parts) | ||
74 | internalcomp :: Vector Double -> Vector (Complex Double) | ||
75 | 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( | |||
29 | liftMatrix, liftMatrix2, | 29 | liftMatrix, liftMatrix2, |
30 | (@@>), | 30 | (@@>), |
31 | saveMatrix, | 31 | saveMatrix, |
32 | fromComplex, toComplex, conj, | 32 | fromComplexV, toComplexV, conjV, |
33 | singleton | 33 | singleton |
34 | ) where | 34 | ) where |
35 | 35 | ||
@@ -368,16 +368,16 @@ subMatrix' (r0,c0) (rt,ct) m = trans $ subMatrix' (c0,r0) (ct,rt) (trans m) | |||
368 | -------------------------------------------------------------------------- | 368 | -------------------------------------------------------------------------- |
369 | 369 | ||
370 | -- | obtains the complex conjugate of a complex vector | 370 | -- | obtains the complex conjugate of a complex vector |
371 | conj :: Vector (Complex Double) -> Vector (Complex Double) | 371 | conjV :: Vector (Complex Double) -> Vector (Complex Double) |
372 | conj = mapVector conjugate | 372 | conjV = mapVector conjugate |
373 | 373 | ||
374 | -- | creates a complex vector from vectors with real and imaginary parts | 374 | -- | creates a complex vector from vectors with real and imaginary parts |
375 | toComplex :: (Vector Double, Vector Double) -> Vector (Complex Double) | 375 | toComplexV :: (Vector Double, Vector Double) -> Vector (Complex Double) |
376 | toComplex (r,i) = asComplex $ flatten $ fromColumns [r,i] | 376 | toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i] |
377 | 377 | ||
378 | -- | the inverse of 'toComplex' | 378 | -- | the inverse of 'toComplex' |
379 | fromComplex :: Vector (Complex Double) -> (Vector Double, Vector Double) | 379 | fromComplexV :: Vector (Complex Double) -> (Vector Double, Vector Double) |
380 | fromComplex z = (r,i) where | 380 | fromComplexV z = (r,i) where |
381 | [r,i] = toColumns $ reshape 2 $ asReal z | 381 | [r,i] = toColumns $ reshape 2 $ asReal z |
382 | 382 | ||
383 | -------------------------------------------------------------------------- | 383 | -------------------------------------------------------------------------- |
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 @@ | |||
1 | {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-} | ||
1 | ----------------------------------------------------------------------------- | 2 | ----------------------------------------------------------------------------- |
2 | -- | | 3 | -- | |
3 | -- Module : Data.Packed.Matrix | 4 | -- Module : Data.Packed.Matrix |
@@ -13,7 +14,7 @@ | |||
13 | ----------------------------------------------------------------------------- | 14 | ----------------------------------------------------------------------------- |
14 | 15 | ||
15 | module Data.Packed.Matrix ( | 16 | module Data.Packed.Matrix ( |
16 | Element, | 17 | Element, Container(..), |
17 | Matrix,rows,cols, | 18 | Matrix,rows,cols, |
18 | (><), | 19 | (><), |
19 | trans, | 20 | trans, |
@@ -421,3 +422,49 @@ toBlocksEvery r c m = toBlocks rs cs m where | |||
421 | (qc,rc) = cols m `divMod` c | 422 | (qc,rc) = cols m `divMod` c |
422 | rs = replicate qr r ++ if rr > 0 then [rr] else [] | 423 | rs = replicate qr r ++ if rr > 0 then [rr] else [] |
423 | cs = replicate qc c ++ if rc > 0 then [rc] else [] | 424 | cs = replicate qc c ++ if rc > 0 then [rc] else [] |
425 | |||
426 | ------------------------------------------------------------------- | ||
427 | |||
428 | -- | conversion utilities | ||
429 | class (Element e) => Container c e where | ||
430 | toComplex :: RealFloat e => (c e, c e) -> c (Complex e) | ||
431 | fromComplex :: RealFloat e => c (Complex e) -> (c e, c e) | ||
432 | comp :: RealFloat e => c e -> c (Complex e) | ||
433 | conj :: RealFloat e => c (Complex e) -> c (Complex e) | ||
434 | real :: c Double -> c e | ||
435 | complex :: c e -> c (Complex Double) | ||
436 | |||
437 | instance Container Vector Double where | ||
438 | toComplex = toComplexV | ||
439 | fromComplex = fromComplexV | ||
440 | comp v = toComplex (v,constant 0 (dim v)) | ||
441 | conj = conjV | ||
442 | real = id | ||
443 | complex = comp | ||
444 | |||
445 | instance Container Vector (Complex Double) where | ||
446 | toComplex = undefined -- can't match | ||
447 | fromComplex = undefined | ||
448 | comp = undefined | ||
449 | conj = undefined | ||
450 | real = comp | ||
451 | complex = id | ||
452 | |||
453 | instance Container Matrix Double where | ||
454 | toComplex = uncurry $ liftMatrix2 $ curry toComplex | ||
455 | fromComplex z = (reshape c r, reshape c i) | ||
456 | where (r,i) = fromComplex (flatten z) | ||
457 | c = cols z | ||
458 | comp = liftMatrix comp | ||
459 | conj = liftMatrix conj | ||
460 | real = id | ||
461 | complex = comp | ||
462 | |||
463 | instance Container Matrix (Complex Double) where | ||
464 | toComplex = undefined | ||
465 | fromComplex = undefined | ||
466 | comp = undefined | ||
467 | conj = undefined | ||
468 | real = comp | ||
469 | complex = id | ||
470 | |||
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 | |||
14 | ----------------------------------------------------------------------------- | 14 | ----------------------------------------------------------------------------- |
15 | module Numeric.LinearAlgebra ( | 15 | module Numeric.LinearAlgebra ( |
16 | module Data.Packed, | 16 | module Data.Packed, |
17 | module Data.Packed.Random, | ||
18 | module Numeric.LinearAlgebra.Algorithms, | 17 | module Numeric.LinearAlgebra.Algorithms, |
19 | module Numeric.LinearAlgebra.Interface | 18 | module Numeric.LinearAlgebra.Interface |
20 | ) where | 19 | ) where |
21 | 20 | ||
22 | import Data.Packed | 21 | import Data.Packed |
23 | import Data.Packed.Random | ||
24 | import Numeric.LinearAlgebra.Algorithms | 22 | import Numeric.LinearAlgebra.Algorithms |
25 | import Numeric.LinearAlgebra.Instances() | ||
26 | import Numeric.LinearAlgebra.Interface | 23 | 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 ( | |||
76 | ) where | 76 | ) where |
77 | 77 | ||
78 | 78 | ||
79 | import Data.Packed.Internal hiding (fromComplex, toComplex, conj, (//)) | 79 | import Data.Packed.Internal hiding ((//)) |
80 | import Data.Packed | 80 | import Data.Packed.Vector |
81 | import Data.Packed.Matrix | ||
82 | import Data.Complex | ||
81 | import Numeric.GSL.Vector | 83 | import Numeric.GSL.Vector |
82 | import Numeric.LinearAlgebra.LAPACK as LAPACK | 84 | import Numeric.LinearAlgebra.LAPACK as LAPACK |
83 | import Numeric.LinearAlgebra.Linear | 85 | 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) | |||
9 | Stability : provisional | 9 | Stability : provisional |
10 | Portability : portable | 10 | Portability : portable |
11 | 11 | ||
12 | Operators for frequent operations. | 12 | Some useful operators, and Show, Read, Eq, Num, Fractional, and Floating instances for Vector and Matrix. |
13 | |||
14 | This module exports Show, Read, Eq, Num, Fractional, and Floating instances for Vector and Matrix. | ||
15 | 13 | ||
16 | In the context of the standard numeric operators, one-component vectors and matrices automatically expand to match the dimensions of the other operand. | 14 | In the context of the standard numeric operators, one-component vectors and matrices automatically expand to match the dimensions of the other operand. |
17 | 15 | ||
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 ( | |||
41 | schurR, schurC | 41 | schurR, schurC |
42 | ) where | 42 | ) where |
43 | 43 | ||
44 | import Data.Packed.Internal hiding (toComplex) | 44 | import Data.Packed.Internal |
45 | import Data.Packed | 45 | import Data.Packed.Matrix |
46 | import Data.Complex | ||
46 | import Numeric.GSL.Vector(vectorMapValR, FunCodeSV(Scale)) | 47 | import Numeric.GSL.Vector(vectorMapValR, FunCodeSV(Scale)) |
47 | import Foreign | 48 | import Foreign |
48 | import Foreign.C.Types (CInt) | 49 | 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 ( | |||
18 | Linear(..) | 18 | Linear(..) |
19 | ) where | 19 | ) where |
20 | 20 | ||
21 | import Data.Packed | 21 | import Data.Packed.Vector |
22 | import Data.Packed.Matrix | ||
23 | import Data.Complex | ||
22 | import Numeric.GSL.Vector | 24 | import Numeric.GSL.Vector |
23 | 25 | ||
24 | -- | Basic element-by-element functions. | 26 | -- | 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)) | |||
78 | 78 | ||
79 | --------------------------------------------------------------------- | 79 | --------------------------------------------------------------------- |
80 | 80 | ||
81 | derivTest = abs (d (\x-> x * d (\y-> x+y) 1) 1 - 1) < 1E-10 | ||
82 | where d f x = fst $ derivCentral 0.01 f x | ||
83 | |||
84 | --------------------------------------------------------------------- | ||
85 | |||
81 | -- besselTest = utest "bessel_J0_e" ( abs (r-expected) < e ) | 86 | -- besselTest = utest "bessel_J0_e" ( abs (r-expected) < e ) |
82 | -- where (r,e) = bessel_J0_e 5.0 | 87 | -- where (r,e) = bessel_J0_e 5.0 |
83 | -- expected = -0.17759677131433830434739701 | 88 | -- expected = -0.17759677131433830434739701 |
@@ -319,6 +324,7 @@ runTests n = do | |||
319 | -- , utest "gamma" (gamma 5 == 24.0) | 324 | -- , utest "gamma" (gamma 5 == 24.0) |
320 | -- , besselTest | 325 | -- , besselTest |
321 | -- , exponentialTest | 326 | -- , exponentialTest |
327 | , utest "deriv" derivTest | ||
322 | , utest "integrate" (abs (volSphere 2.5 - 4/3*pi*2.5^3) < 1E-8) | 328 | , utest "integrate" (abs (volSphere 2.5 - 4/3*pi*2.5^3) < 1E-8) |
323 | , utest "polySolve" (polySolveProp [1,2,3,4]) | 329 | , utest "polySolve" (polySolveProp [1,2,3,4]) |
324 | , minimizationTest | 330 | , minimizationTest |