summaryrefslogtreecommitdiff
path: root/lib/Data/Packed/Matrix.hs
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-08-25 16:29:37 +0000
committerAlberto Ruiz <aruiz@um.es>2010-08-25 16:29:37 +0000
commitf541d7dbdc8338b1dd1c0538751d837a16740bd8 (patch)
tree02b3383ed3ffdb3f3f5c0f61ce0243c73e160a24 /lib/Data/Packed/Matrix.hs
parent2b5b266bb02c7205262bc8a0b584477b6043a112 (diff)
simpler Container typeclass
Diffstat (limited to 'lib/Data/Packed/Matrix.hs')
-rw-r--r--lib/Data/Packed/Matrix.hs166
1 files changed, 115 insertions, 51 deletions
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index d5a287d..8aa1693 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -1,4 +1,6 @@
1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-} 1{-# LANGUAGE TypeFamilies #-}
2{-# LANGUAGE FlexibleContexts #-}
3{-# LANGUAGE FlexibleInstances #-}
2----------------------------------------------------------------------------- 4-----------------------------------------------------------------------------
3-- | 5-- |
4-- Module : Data.Packed.Matrix 6-- Module : Data.Packed.Matrix
@@ -14,7 +16,8 @@
14----------------------------------------------------------------------------- 16-----------------------------------------------------------------------------
15 17
16module Data.Packed.Matrix ( 18module Data.Packed.Matrix (
17 Element, Container(..), 19 Element, Scalar, Container(..), Convert(..),
20 RealOf, ComplexOf, SingleOf, DoubleOf, ElementOf, AutoReal(..),
18 Matrix,rows,cols, 21 Matrix,rows,cols,
19 (><), 22 (><),
20 trans, 23 trans,
@@ -47,6 +50,8 @@ import Data.Complex
47import Data.Binary 50import Data.Binary
48import Foreign.Storable 51import Foreign.Storable
49import Control.Monad(replicateM) 52import Control.Monad(replicateM)
53import Control.Arrow((***))
54import GHC.Float(double2Float,float2Double)
50 55
51------------------------------------------------------------------- 56-------------------------------------------------------------------
52 57
@@ -462,62 +467,121 @@ toBlocksEvery r c m = toBlocks rs cs m where
462------------------------------------------------------------------- 467-------------------------------------------------------------------
463 468
464-- | conversion utilities 469-- | conversion utilities
465class (Element e) => Container c e where
466 toComplex :: RealFloat e => (c e, c e) -> c (Complex e)
467 fromComplex :: RealFloat e => c (Complex e) -> (c e, c e)
468 comp :: RealFloat e => c e -> c (Complex e)
469 conj :: RealFloat e => c (Complex e) -> c (Complex e)
470 -- these next two are now weird given we have Floats as well
471 real :: c Double -> c e
472 complex :: c e -> c (Complex Double)
473
474instance Container Vector Float where
475 toComplex = toComplexV
476 fromComplex = fromComplexV
477 comp v = toComplex (v,constantD 0 (dim v))
478 conj = conjV
479 real = mapVector realToFrac
480 complex = (mapVector (\(r :+ i) -> (realToFrac r :+ realToFrac i))) . comp
481 470
482instance Container Vector Double where 471class (Element t, Element (Complex t), Fractional t, RealFloat t) => Scalar t
472
473instance Scalar Double
474instance Scalar Float
475
476class Container c where
477 toComplex :: (Scalar e) => (c e, c e) -> c (Complex e)
478 fromComplex :: (Scalar e) => c (Complex e) -> (c e, c e)
479 comp :: (Scalar e) => c e -> c (Complex e)
480 conj :: (Scalar e) => c (Complex e) -> c (Complex e)
481 cmap :: (Element a, Element b) => (a -> b) -> c a -> c b
482
483instance Container Vector where
483 toComplex = toComplexV 484 toComplex = toComplexV
484 fromComplex = fromComplexV 485 fromComplex = fromComplexV
485 comp v = toComplex (v,constantD 0 (dim v)) 486 comp v = toComplex (v,constantD 0 (dim v))
486 conj = conjV 487 conj = conjV
487 real = id 488 cmap = mapVector
488 complex = comp 489
489 490instance Container Matrix where
490instance Container Vector (Complex Float) where
491 toComplex = undefined -- can't match
492 fromComplex = undefined
493 comp = undefined
494 conj = undefined
495 real = comp . mapVector realToFrac
496 complex = mapVector (\(r :+ i) -> realToFrac r :+ realToFrac i)
497
498instance Container Vector (Complex Double) where
499 toComplex = undefined -- can't match
500 fromComplex = undefined
501 comp = undefined
502 conj = undefined
503 real = comp
504 complex = id
505
506instance Container Matrix Double where
507 toComplex = uncurry $ liftMatrix2 $ curry toComplex 491 toComplex = uncurry $ liftMatrix2 $ curry toComplex
508 fromComplex z = (reshape c r, reshape c i) 492 fromComplex z = (reshape c *** reshape c) . fromComplex . flatten $ z
509 where (r,i) = fromComplex (flatten z) 493 where c = cols z
510 c = cols z
511 comp = liftMatrix comp 494 comp = liftMatrix comp
512 conj = liftMatrix conj 495 conj = liftMatrix conj
513 real = id 496 cmap f = liftMatrix (cmap f)
514 complex = comp 497
498-------------------------------------------------------------------
499
500type family RealOf x
501
502type instance RealOf Double = Double
503type instance RealOf (Complex Double) = Double
504
505type instance RealOf Float = Float
506type instance RealOf (Complex Float) = Float
507
508type family ComplexOf x
509
510type instance ComplexOf Double = Complex Double
511type instance ComplexOf (Complex Double) = Complex Double
512
513type instance ComplexOf Float = Complex Float
514type instance ComplexOf (Complex Float) = Complex Float
515
516type family SingleOf x
517
518type instance SingleOf Double = Float
519type instance SingleOf Float = Float
520
521type instance SingleOf (Complex a) = Complex (SingleOf a)
522
523type family DoubleOf x
524
525type instance DoubleOf Double = Double
526type instance DoubleOf Float = Double
527
528type instance DoubleOf (Complex a) = Complex (DoubleOf a)
529
530type family ElementOf c
531
532type instance ElementOf (Vector a) = a
533type instance ElementOf (Matrix a) = a
534
535-------------------------------------------------------------------
536
537class Convert t where
538 real' :: Container c => c (RealOf t) -> c t
539 complex' :: Container c => c t -> c (ComplexOf t)
540 single :: Container c => c t -> c (SingleOf t)
541 double :: Container c => c t -> c (DoubleOf t)
542
543instance Convert Double where
544 real' = id
545 complex' = comp
546 single = cmap double2Float
547 double = id
548
549instance Convert Float where
550 real' = id
551 complex' = comp
552 single = id
553 double = cmap float2Double
554
555instance Convert (Complex Double) where
556 real' = comp
557 complex' = id
558 single = toComplex . (single *** single) . fromComplex
559 double = id
560
561instance Convert (Complex Float) where
562 real' = comp
563 complex' = id
564 single = id
565 double = toComplex . (double *** double) . fromComplex
566
567-------------------------------------------------------------------
568
569class AutoReal t where
570 real :: Container c => c Double -> c t
571 complex :: Container c => c t -> c (Complex Double)
572
573instance AutoReal Double where
574 real = real'
575 complex = complex'
576
577instance AutoReal (Complex Double) where
578 real = real'
579 complex = complex'
515 580
516instance Container Matrix (Complex Double) where 581instance AutoReal Float where
517 toComplex = undefined 582 real = real' . single
518 fromComplex = undefined 583 complex = double . complex'
519 comp = undefined
520 conj = undefined
521 real = comp
522 complex = id
523 584
585instance AutoReal (Complex Float) where
586 real = real' . single
587 complex = double . complex'