diff options
Diffstat (limited to 'packages/base/src/Internal/Conversion.hs')
-rw-r--r-- | packages/base/src/Internal/Conversion.hs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/packages/base/src/Internal/Conversion.hs b/packages/base/src/Internal/Conversion.hs new file mode 100644 index 0000000..2f4a9c7 --- /dev/null +++ b/packages/base/src/Internal/Conversion.hs | |||
@@ -0,0 +1,89 @@ | |||
1 | {-# LANGUAGE TypeFamilies #-} | ||
2 | {-# LANGUAGE FlexibleContexts #-} | ||
3 | {-# LANGUAGE FlexibleInstances #-} | ||
4 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
5 | {-# LANGUAGE FunctionalDependencies #-} | ||
6 | {-# LANGUAGE UndecidableInstances #-} | ||
7 | |||
8 | ----------------------------------------------------------------------------- | ||
9 | -- | | ||
10 | -- Module : Numeric.Conversion | ||
11 | -- Copyright : (c) Alberto Ruiz 2010 | ||
12 | -- License : BSD3 | ||
13 | -- Maintainer : Alberto Ruiz | ||
14 | -- Stability : provisional | ||
15 | -- | ||
16 | -- Conversion routines | ||
17 | -- | ||
18 | ----------------------------------------------------------------------------- | ||
19 | |||
20 | |||
21 | module Internal.Conversion ( | ||
22 | Complexable(..), RealElement, | ||
23 | module Data.Complex | ||
24 | ) where | ||
25 | |||
26 | import Internal.Vector | ||
27 | import Internal.Matrix | ||
28 | import Internal.Vectorized | ||
29 | import Data.Complex | ||
30 | import Control.Arrow((***)) | ||
31 | |||
32 | ------------------------------------------------------------------- | ||
33 | |||
34 | -- | Supported single-double precision type pairs | ||
35 | class (Element s, Element d) => Precision s d | s -> d, d -> s where | ||
36 | double2FloatG :: Vector d -> Vector s | ||
37 | float2DoubleG :: Vector s -> Vector d | ||
38 | |||
39 | instance Precision Float Double where | ||
40 | double2FloatG = double2FloatV | ||
41 | float2DoubleG = float2DoubleV | ||
42 | |||
43 | instance Precision (Complex Float) (Complex Double) where | ||
44 | double2FloatG = asComplex . double2FloatV . asReal | ||
45 | float2DoubleG = asComplex . float2DoubleV . asReal | ||
46 | |||
47 | -- | Supported real types | ||
48 | class (Element t, Element (Complex t), RealFloat t) | ||
49 | => RealElement t | ||
50 | |||
51 | instance RealElement Double | ||
52 | instance RealElement Float | ||
53 | |||
54 | |||
55 | -- | Structures that may contain complex numbers | ||
56 | class Complexable c where | ||
57 | toComplex' :: (RealElement e) => (c e, c e) -> c (Complex e) | ||
58 | fromComplex' :: (RealElement e) => c (Complex e) -> (c e, c e) | ||
59 | comp' :: (RealElement e) => c e -> c (Complex e) | ||
60 | single' :: Precision a b => c b -> c a | ||
61 | double' :: Precision a b => c a -> c b | ||
62 | |||
63 | |||
64 | instance Complexable Vector where | ||
65 | toComplex' = toComplexV | ||
66 | fromComplex' = fromComplexV | ||
67 | comp' v = toComplex' (v,constantD 0 (dim v)) | ||
68 | single' = double2FloatG | ||
69 | double' = float2DoubleG | ||
70 | |||
71 | |||
72 | -- | creates a complex vector from vectors with real and imaginary parts | ||
73 | toComplexV :: (RealElement a) => (Vector a, Vector a) -> Vector (Complex a) | ||
74 | toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i] | ||
75 | |||
76 | -- | the inverse of 'toComplex' | ||
77 | fromComplexV :: (RealElement a) => Vector (Complex a) -> (Vector a, Vector a) | ||
78 | fromComplexV z = (r,i) where | ||
79 | [r,i] = toColumns $ reshape 2 $ asReal z | ||
80 | |||
81 | |||
82 | instance Complexable Matrix where | ||
83 | toComplex' = uncurry $ liftMatrix2 $ curry toComplex' | ||
84 | fromComplex' z = (reshape c *** reshape c) . fromComplex' . flatten $ z | ||
85 | where c = cols z | ||
86 | comp' = liftMatrix comp' | ||
87 | single' = liftMatrix single' | ||
88 | double' = liftMatrix double' | ||
89 | |||