diff options
Diffstat (limited to 'lib/Data')
-rw-r--r-- | lib/Data/Packed/Internal/Signatures.hs | 4 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 20 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 66 |
3 files changed, 69 insertions, 21 deletions
diff --git a/lib/Data/Packed/Internal/Signatures.hs b/lib/Data/Packed/Internal/Signatures.hs index 8c1c5f6..b81efa4 100644 --- a/lib/Data/Packed/Internal/Signatures.hs +++ b/lib/Data/Packed/Internal/Signatures.hs | |||
@@ -24,12 +24,15 @@ type PQ = Ptr (Complex Float) -- | |||
24 | type PC = Ptr (Complex Double) -- | 24 | type PC = Ptr (Complex Double) -- |
25 | type TF = CInt -> PF -> IO CInt -- | 25 | type TF = CInt -> PF -> IO CInt -- |
26 | type TFF = CInt -> PF -> TF -- | 26 | type TFF = CInt -> PF -> TF -- |
27 | type TFV = CInt -> PF -> TV -- | ||
28 | type TVF = CInt -> PD -> TF -- | ||
27 | type TFFF = CInt -> PF -> TFF -- | 29 | type TFFF = CInt -> PF -> TFF -- |
28 | type TV = CInt -> PD -> IO CInt -- | 30 | type TV = CInt -> PD -> IO CInt -- |
29 | type TVV = CInt -> PD -> TV -- | 31 | type TVV = CInt -> PD -> TV -- |
30 | type TVVV = CInt -> PD -> TVV -- | 32 | type TVVV = CInt -> PD -> TVV -- |
31 | type TFM = CInt -> CInt -> PF -> IO CInt -- | 33 | type TFM = CInt -> CInt -> PF -> IO CInt -- |
32 | type TFMFM = CInt -> CInt -> PF -> TFM -- | 34 | type TFMFM = CInt -> CInt -> PF -> TFM -- |
35 | type TFMFMFM = CInt -> CInt -> PF -> TFMFM -- | ||
33 | type TM = CInt -> CInt -> PD -> IO CInt -- | 36 | type TM = CInt -> CInt -> PD -> IO CInt -- |
34 | type TMM = CInt -> CInt -> PD -> TM -- | 37 | type TMM = CInt -> CInt -> PD -> TM -- |
35 | type TVMM = CInt -> PD -> TMM -- | 38 | type TVMM = CInt -> PD -> TMM -- |
@@ -61,6 +64,7 @@ type TQVQVQV = CInt -> PQ -> TQVQV -- | |||
61 | type TQVF = CInt -> PQ -> TF -- | 64 | type TQVF = CInt -> PQ -> TF -- |
62 | type TQM = CInt -> CInt -> PQ -> IO CInt -- | 65 | type TQM = CInt -> CInt -> PQ -> IO CInt -- |
63 | type TQMQM = CInt -> CInt -> PQ -> TQM -- | 66 | type TQMQM = CInt -> CInt -> PQ -> TQM -- |
67 | type TQMQMQM = CInt -> CInt -> PQ -> TQMQM -- | ||
64 | type TCMCV = CInt -> CInt -> PC -> TCV -- | 68 | type TCMCV = CInt -> CInt -> PC -> TCV -- |
65 | type TVCV = CInt -> PD -> TCV -- | 69 | type TVCV = CInt -> PD -> TCV -- |
66 | type TCVM = CInt -> PC -> TM -- | 70 | type TCVM = CInt -> PC -> TM -- |
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index ac2d0d7..c8cc2c2 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -21,7 +21,7 @@ module Data.Packed.Internal.Vector ( | |||
21 | mapVectorM, mapVectorM_, | 21 | mapVectorM, mapVectorM_, |
22 | foldVector, foldVectorG, foldLoop, | 22 | foldVector, foldVectorG, foldLoop, |
23 | createVector, vec, | 23 | createVector, vec, |
24 | asComplex, asReal, | 24 | asComplex, asReal, float2DoubleV, double2FloatV, |
25 | fwriteVector, freadVector, fprintfVector, fscanfVector, | 25 | fwriteVector, freadVector, fprintfVector, fscanfVector, |
26 | cloneVector, | 26 | cloneVector, |
27 | unsafeToForeignPtr, | 27 | unsafeToForeignPtr, |
@@ -274,6 +274,24 @@ asComplex :: (RealFloat a, Storable a) => Vector a -> Vector (Complex a) | |||
274 | asComplex v = unsafeFromForeignPtr (castForeignPtr fp) (i `div` 2) (n `div` 2) | 274 | asComplex v = unsafeFromForeignPtr (castForeignPtr fp) (i `div` 2) (n `div` 2) |
275 | where (fp,i,n) = unsafeToForeignPtr v | 275 | where (fp,i,n) = unsafeToForeignPtr v |
276 | 276 | ||
277 | --------------------------------------------------------------- | ||
278 | |||
279 | float2DoubleV :: Vector Float -> Vector Double | ||
280 | float2DoubleV v = unsafePerformIO $ do | ||
281 | r <- createVector (dim v) | ||
282 | app2 c_float2double vec v vec r "float2double" | ||
283 | return r | ||
284 | |||
285 | double2FloatV :: Vector Double -> Vector Float | ||
286 | double2FloatV v = unsafePerformIO $ do | ||
287 | r <- createVector (dim v) | ||
288 | app2 c_double2float vec v vec r "double2float2" | ||
289 | return r | ||
290 | |||
291 | |||
292 | foreign import ccall "float2double" c_float2double:: TFV | ||
293 | foreign import ccall "double2float" c_double2float:: TVF | ||
294 | |||
277 | ---------------------------------------------------------------- | 295 | ---------------------------------------------------------------- |
278 | 296 | ||
279 | cloneVector :: Storable t => Vector t -> IO (Vector t) | 297 | cloneVector :: Storable t => Vector t -> IO (Vector t) |
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs index 8aa1693..8694249 100644 --- a/lib/Data/Packed/Matrix.hs +++ b/lib/Data/Packed/Matrix.hs | |||
@@ -1,6 +1,10 @@ | |||
1 | {-# LANGUAGE TypeFamilies #-} | 1 | {-# LANGUAGE TypeFamilies #-} |
2 | {-# LANGUAGE FlexibleContexts #-} | 2 | {-# LANGUAGE FlexibleContexts #-} |
3 | {-# LANGUAGE FlexibleInstances #-} | 3 | {-# LANGUAGE FlexibleInstances #-} |
4 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
5 | {-# LANGUAGE FunctionalDependencies #-} | ||
6 | |||
7 | |||
4 | ----------------------------------------------------------------------------- | 8 | ----------------------------------------------------------------------------- |
5 | -- | | 9 | -- | |
6 | -- Module : Data.Packed.Matrix | 10 | -- Module : Data.Packed.Matrix |
@@ -16,8 +20,9 @@ | |||
16 | ----------------------------------------------------------------------------- | 20 | ----------------------------------------------------------------------------- |
17 | 21 | ||
18 | module Data.Packed.Matrix ( | 22 | module Data.Packed.Matrix ( |
19 | Element, Scalar, Container(..), Convert(..), | 23 | Element, RealElement, Container(..), |
20 | RealOf, ComplexOf, SingleOf, DoubleOf, ElementOf, AutoReal(..), | 24 | Convert(..), RealOf, ComplexOf, SingleOf, DoubleOf, ElementOf, |
25 | AutoReal(..), | ||
21 | Matrix,rows,cols, | 26 | Matrix,rows,cols, |
22 | (><), | 27 | (><), |
23 | trans, | 28 | trans, |
@@ -51,7 +56,7 @@ import Data.Binary | |||
51 | import Foreign.Storable | 56 | import Foreign.Storable |
52 | import Control.Monad(replicateM) | 57 | import Control.Monad(replicateM) |
53 | import Control.Arrow((***)) | 58 | import Control.Arrow((***)) |
54 | import GHC.Float(double2Float,float2Double) | 59 | --import GHC.Float(double2Float,float2Double) |
55 | 60 | ||
56 | ------------------------------------------------------------------- | 61 | ------------------------------------------------------------------- |
57 | 62 | ||
@@ -468,17 +473,32 @@ toBlocksEvery r c m = toBlocks rs cs m where | |||
468 | 473 | ||
469 | -- | conversion utilities | 474 | -- | conversion utilities |
470 | 475 | ||
471 | class (Element t, Element (Complex t), Fractional t, RealFloat t) => Scalar t | 476 | class (Element t, Element (Complex t), RealFloat t) => RealElement t |
477 | |||
478 | instance RealElement Double | ||
479 | instance RealElement Float | ||
480 | |||
481 | class (Element s, Element d) => Prec s d | s -> d, d -> s where | ||
482 | double2FloatG :: Vector d -> Vector s | ||
483 | float2DoubleG :: Vector s -> Vector d | ||
484 | |||
485 | instance Prec Float Double where | ||
486 | double2FloatG = double2FloatV | ||
487 | float2DoubleG = float2DoubleV | ||
488 | |||
489 | instance Prec (Complex Float) (Complex Double) where | ||
490 | double2FloatG = asComplex . double2FloatV . asReal | ||
491 | float2DoubleG = asComplex . float2DoubleV . asReal | ||
472 | 492 | ||
473 | instance Scalar Double | ||
474 | instance Scalar Float | ||
475 | 493 | ||
476 | class Container c where | 494 | class Container c where |
477 | toComplex :: (Scalar e) => (c e, c e) -> c (Complex e) | 495 | toComplex :: (RealElement e) => (c e, c e) -> c (Complex e) |
478 | fromComplex :: (Scalar e) => c (Complex e) -> (c e, c e) | 496 | fromComplex :: (RealElement e) => c (Complex e) -> (c e, c e) |
479 | comp :: (Scalar e) => c e -> c (Complex e) | 497 | comp :: (RealElement e) => c e -> c (Complex e) |
480 | conj :: (Scalar e) => c (Complex e) -> c (Complex e) | 498 | conj :: (RealElement e) => c (Complex e) -> c (Complex e) |
481 | cmap :: (Element a, Element b) => (a -> b) -> c a -> c b | 499 | cmap :: (Element a, Element b) => (a -> b) -> c a -> c b |
500 | single :: Prec a b => c b -> c a | ||
501 | double :: Prec a b => c a -> c b | ||
482 | 502 | ||
483 | instance Container Vector where | 503 | instance Container Vector where |
484 | toComplex = toComplexV | 504 | toComplex = toComplexV |
@@ -486,6 +506,8 @@ instance Container Vector where | |||
486 | comp v = toComplex (v,constantD 0 (dim v)) | 506 | comp v = toComplex (v,constantD 0 (dim v)) |
487 | conj = conjV | 507 | conj = conjV |
488 | cmap = mapVector | 508 | cmap = mapVector |
509 | single = double2FloatG | ||
510 | double = float2DoubleG | ||
489 | 511 | ||
490 | instance Container Matrix where | 512 | instance Container Matrix where |
491 | toComplex = uncurry $ liftMatrix2 $ curry toComplex | 513 | toComplex = uncurry $ liftMatrix2 $ curry toComplex |
@@ -494,6 +516,8 @@ instance Container Matrix where | |||
494 | comp = liftMatrix comp | 516 | comp = liftMatrix comp |
495 | conj = liftMatrix conj | 517 | conj = liftMatrix conj |
496 | cmap f = liftMatrix (cmap f) | 518 | cmap f = liftMatrix (cmap f) |
519 | single = liftMatrix single | ||
520 | double = liftMatrix double | ||
497 | 521 | ||
498 | ------------------------------------------------------------------- | 522 | ------------------------------------------------------------------- |
499 | 523 | ||
@@ -534,38 +558,40 @@ type instance ElementOf (Matrix a) = a | |||
534 | 558 | ||
535 | ------------------------------------------------------------------- | 559 | ------------------------------------------------------------------- |
536 | 560 | ||
561 | -- | generic conversion functions | ||
537 | class Convert t where | 562 | class Convert t where |
538 | real' :: Container c => c (RealOf t) -> c t | 563 | real' :: Container c => c (RealOf t) -> c t |
539 | complex' :: Container c => c t -> c (ComplexOf t) | 564 | complex' :: Container c => c t -> c (ComplexOf t) |
540 | single :: Container c => c t -> c (SingleOf t) | 565 | single' :: Container c => c t -> c (SingleOf t) |
541 | double :: Container c => c t -> c (DoubleOf t) | 566 | double' :: Container c => c t -> c (DoubleOf t) |
542 | 567 | ||
543 | instance Convert Double where | 568 | instance Convert Double where |
544 | real' = id | 569 | real' = id |
545 | complex' = comp | 570 | complex' = comp |
546 | single = cmap double2Float | 571 | single' = single |
547 | double = id | 572 | double' = id |
548 | 573 | ||
549 | instance Convert Float where | 574 | instance Convert Float where |
550 | real' = id | 575 | real' = id |
551 | complex' = comp | 576 | complex' = comp |
552 | single = id | 577 | single' = id |
553 | double = cmap float2Double | 578 | double' = double |
554 | 579 | ||
555 | instance Convert (Complex Double) where | 580 | instance Convert (Complex Double) where |
556 | real' = comp | 581 | real' = comp |
557 | complex' = id | 582 | complex' = id |
558 | single = toComplex . (single *** single) . fromComplex | 583 | single' = single |
559 | double = id | 584 | double' = id |
560 | 585 | ||
561 | instance Convert (Complex Float) where | 586 | instance Convert (Complex Float) where |
562 | real' = comp | 587 | real' = comp |
563 | complex' = id | 588 | complex' = id |
564 | single = id | 589 | single' = id |
565 | double = toComplex . (double *** double) . fromComplex | 590 | double' = double |
566 | 591 | ||
567 | ------------------------------------------------------------------- | 592 | ------------------------------------------------------------------- |
568 | 593 | ||
594 | -- | to be replaced by Convert | ||
569 | class AutoReal t where | 595 | class AutoReal t where |
570 | real :: Container c => c Double -> c t | 596 | real :: Container c => c Double -> c t |
571 | complex :: Container c => c t -> c (Complex Double) | 597 | complex :: Container c => c t -> c (Complex Double) |