diff options
Diffstat (limited to 'lib/Numeric/Conversion.hs')
-rw-r--r-- | lib/Numeric/Conversion.hs | 137 |
1 files changed, 15 insertions, 122 deletions
diff --git a/lib/Numeric/Conversion.hs b/lib/Numeric/Conversion.hs index 809ac51..fbf608a 100644 --- a/lib/Numeric/Conversion.hs +++ b/lib/Numeric/Conversion.hs | |||
@@ -20,24 +20,15 @@ | |||
20 | ----------------------------------------------------------------------------- | 20 | ----------------------------------------------------------------------------- |
21 | 21 | ||
22 | module Numeric.Conversion ( | 22 | module Numeric.Conversion ( |
23 | RealElement, --Precision, | 23 | Complexable(..), RealElement, |
24 | ComplexContainer(toComplex,fromComplex,conj,comp), | ||
25 | Convert(..), --AutoReal(..), | ||
26 | RealOf, ComplexOf, SingleOf, DoubleOf, | ||
27 | module Data.Complex | 24 | module Data.Complex |
28 | ) where | 25 | ) where |
29 | 26 | ||
30 | |||
31 | import Data.Packed.Internal.Vector | 27 | import Data.Packed.Internal.Vector |
32 | import Data.Packed.Internal.Matrix | 28 | import Data.Packed.Internal.Matrix |
33 | --import Numeric.GSL.Vector | ||
34 | |||
35 | import Data.Complex | 29 | import Data.Complex |
36 | --import Control.Monad(ap) | ||
37 | import Control.Arrow((***)) | 30 | import Control.Arrow((***)) |
38 | 31 | ||
39 | --import Numeric.LinearAlgebra.LAPACK(multiplyR,multiplyC,multiplyF,multiplyQ) | ||
40 | |||
41 | ------------------------------------------------------------------- | 32 | ------------------------------------------------------------------- |
42 | 33 | ||
43 | -- | Supported single-double precision type pairs | 34 | -- | Supported single-double precision type pairs |
@@ -60,24 +51,22 @@ class (Element t, Element (Complex t), RealFloat t | |||
60 | => RealElement t | 51 | => RealElement t |
61 | 52 | ||
62 | instance RealElement Double | 53 | instance RealElement Double |
63 | |||
64 | instance RealElement Float | 54 | instance RealElement Float |
65 | 55 | ||
66 | -- | Conversion utilities | 56 | |
67 | class ComplexContainer c where | 57 | -- | Structures that may contain complex numbers |
68 | toComplex :: (RealElement e) => (c e, c e) -> c (Complex e) | 58 | class Complexable c where |
69 | fromComplex :: (RealElement e) => c (Complex e) -> (c e, c e) | 59 | toComplex' :: (RealElement e) => (c e, c e) -> c (Complex e) |
70 | comp :: (RealElement e) => c e -> c (Complex e) | 60 | fromComplex' :: (RealElement e) => c (Complex e) -> (c e, c e) |
71 | conj :: (RealElement e) => c (Complex e) -> c (Complex e) | 61 | comp' :: (RealElement e) => c e -> c (Complex e) |
72 | single' :: Precision a b => c b -> c a | 62 | single' :: Precision a b => c b -> c a |
73 | double' :: Precision a b => c a -> c b | 63 | double' :: Precision a b => c a -> c b |
74 | 64 | ||
75 | 65 | ||
76 | instance ComplexContainer Vector where | 66 | instance Complexable Vector where |
77 | toComplex = toComplexV | 67 | toComplex' = toComplexV |
78 | fromComplex = fromComplexV | 68 | fromComplex' = fromComplexV |
79 | comp v = toComplex (v,constantD 0 (dim v)) | 69 | comp' v = toComplex' (v,constantD 0 (dim v)) |
80 | conj = conjugateD | ||
81 | single' = double2FloatG | 70 | single' = double2FloatG |
82 | double' = float2DoubleG | 71 | double' = float2DoubleG |
83 | 72 | ||
@@ -92,107 +81,11 @@ fromComplexV z = (r,i) where | |||
92 | [r,i] = toColumns $ reshape 2 $ asReal z | 81 | [r,i] = toColumns $ reshape 2 $ asReal z |
93 | 82 | ||
94 | 83 | ||
95 | instance ComplexContainer Matrix where | 84 | instance Complexable Matrix where |
96 | toComplex = uncurry $ liftMatrix2 $ curry toComplex | 85 | toComplex' = uncurry $ liftMatrix2 $ curry toComplex' |
97 | fromComplex z = (reshape c *** reshape c) . fromComplex . flatten $ z | 86 | fromComplex' z = (reshape c *** reshape c) . fromComplex' . flatten $ z |
98 | where c = cols z | 87 | where c = cols z |
99 | comp = liftMatrix comp | 88 | comp' = liftMatrix comp' |
100 | conj = liftMatrix conj | ||
101 | single' = liftMatrix single' | 89 | single' = liftMatrix single' |
102 | double' = liftMatrix double' | 90 | double' = liftMatrix double' |
103 | 91 | ||
104 | ------------------------------------------------------------------- | ||
105 | |||
106 | type family RealOf x | ||
107 | |||
108 | type instance RealOf Double = Double | ||
109 | type instance RealOf (Complex Double) = Double | ||
110 | |||
111 | type instance RealOf Float = Float | ||
112 | type instance RealOf (Complex Float) = Float | ||
113 | |||
114 | type family ComplexOf x | ||
115 | |||
116 | type instance ComplexOf Double = Complex Double | ||
117 | type instance ComplexOf (Complex Double) = Complex Double | ||
118 | |||
119 | type instance ComplexOf Float = Complex Float | ||
120 | type instance ComplexOf (Complex Float) = Complex Float | ||
121 | |||
122 | type family SingleOf x | ||
123 | |||
124 | type instance SingleOf Double = Float | ||
125 | type instance SingleOf Float = Float | ||
126 | |||
127 | type instance SingleOf (Complex a) = Complex (SingleOf a) | ||
128 | |||
129 | type family DoubleOf x | ||
130 | |||
131 | type instance DoubleOf Double = Double | ||
132 | type instance DoubleOf Float = Double | ||
133 | |||
134 | type instance DoubleOf (Complex a) = Complex (DoubleOf a) | ||
135 | |||
136 | type family ElementOf c | ||
137 | |||
138 | type instance ElementOf (Vector a) = a | ||
139 | type instance ElementOf (Matrix a) = a | ||
140 | |||
141 | |||
142 | ------------------------------------------------------------------- | ||
143 | |||
144 | class (Element t, Element (RealOf t)) => Convert t where | ||
145 | real :: ComplexContainer c => c (RealOf t) -> c t | ||
146 | complex :: ComplexContainer c => c t -> c (ComplexOf t) | ||
147 | single :: ComplexContainer c => c t -> c (SingleOf t) | ||
148 | double :: ComplexContainer c => c t -> c (DoubleOf t) | ||
149 | |||
150 | |||
151 | instance Convert Double where | ||
152 | real = id | ||
153 | complex = comp | ||
154 | single = single' | ||
155 | double = id | ||
156 | |||
157 | instance Convert Float where | ||
158 | real = id | ||
159 | complex = comp | ||
160 | single = id | ||
161 | double = double' | ||
162 | |||
163 | instance Convert (Complex Double) where | ||
164 | real = comp | ||
165 | complex = id | ||
166 | single = single' | ||
167 | double = id | ||
168 | |||
169 | instance Convert (Complex Float) where | ||
170 | real = comp | ||
171 | complex = id | ||
172 | single = id | ||
173 | double = double' | ||
174 | |||
175 | ------------------------------------------------------------------- | ||
176 | |||
177 | -- | to be replaced by Convert | ||
178 | class Convert t => AutoReal t where | ||
179 | real'' :: ComplexContainer c => c Double -> c t | ||
180 | complex'' :: ComplexContainer c => c t -> c (Complex Double) | ||
181 | |||
182 | |||
183 | instance AutoReal Double where | ||
184 | real'' = real | ||
185 | complex'' = complex | ||
186 | |||
187 | instance AutoReal (Complex Double) where | ||
188 | real'' = real | ||
189 | complex'' = complex | ||
190 | |||
191 | instance AutoReal Float where | ||
192 | real'' = real . single | ||
193 | complex'' = double . complex | ||
194 | |||
195 | instance AutoReal (Complex Float) where | ||
196 | real'' = real . single | ||
197 | complex'' = double . complex | ||
198 | |||