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 /lib/Data | |
parent | aa8debb5ab389de7bc5b47c5ae5f038349b6efc1 (diff) |
more simplification
Diffstat (limited to 'lib/Data')
-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 |
3 files changed, 58 insertions, 59 deletions
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 | |||