summaryrefslogtreecommitdiff
path: root/lib/Numeric/Container.hs
blob: a0e489bf7feec37b7f1ddd7551d57a48defa3356 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  Numeric.Container
-- Copyright   :  (c) Alberto Ruiz 2007
-- License     :  GPL-style
--
-- Maintainer  :  Alberto Ruiz <aruiz@um.es>
-- Stability   :  provisional
-- Portability :  portable
--
-- Numeric classes for containers of numbers, including conversion routines
--
-----------------------------------------------------------------------------

module Numeric.Container (
    Linear(..),
    Container(..), RealElement, Precision(..), NumericContainer(..), comp, 
--    Complexable(..), Precisionable(..),
    Convert(..), --AutoReal(..), 
    RealOf, ComplexOf, SingleOf, DoubleOf, 

--    ElementOf, 

    IndexOf,

    module Data.Complex
) where

import Data.Packed.Vector
import Data.Packed.Matrix
import Data.Packed.Internal.Vector
--import Data.Packed.Internal.Matrix
--import qualified Data.Packed.ST as ST

--import Control.Arrow((***))
import Data.Complex

-------------------------------------------------------------------

-- | Supported single-double precision type pairs
class (Element s, Element d) => Precision s d | s -> d, d -> s where
    double2FloatG :: Vector d -> Vector s
    float2DoubleG :: Vector s -> Vector d

instance Precision Float Double where
    double2FloatG = double2FloatV
    float2DoubleG = float2DoubleV

instance Precision (Complex Float) (Complex Double) where
    double2FloatG = asComplex . double2FloatV . asReal
    float2DoubleG = asComplex . float2DoubleV . asReal

-- | Supported real types
class (Element t, Element (Complex t), RealFloat t
--       , RealOf t ~ t, RealOf (Complex t) ~ t
       )
    => RealElement t

instance RealElement Double

instance RealElement Float

-- | Conversion utilities
class NumericContainer c where
    toComplex   :: (RealElement e) => (c e, c e) -> c (Complex e)
    fromComplex :: (RealElement e) => c (Complex e) -> (c e, c e)
    complex'    :: (RealElement e) => c e -> c (Complex e)
    conj        :: (RealElement e) => c (Complex e) -> c (Complex e)
--    cmap        :: (Element a, Element b) => (a -> b) -> c a -> c b
    single'      :: Precision a b => c b -> c a
    double'      :: Precision a b => c a -> c b

-- | a synonym for "complex'"
comp :: (NumericContainer c, RealElement e) => c e -> c (Complex e)
comp x = complex' x

-------------------------------------------------------------------

type family RealOf x

type instance RealOf Double = Double
type instance RealOf (Complex Double) = Double

type instance RealOf Float = Float
type instance RealOf (Complex Float) = Float

type family ComplexOf x

type instance ComplexOf Double = Complex Double
type instance ComplexOf (Complex Double) = Complex Double

type instance ComplexOf Float = Complex Float
type instance ComplexOf (Complex Float) = Complex Float

type family SingleOf x

type instance SingleOf Double = Float
type instance SingleOf Float  = Float

type instance SingleOf (Complex a) = Complex (SingleOf a)

type family DoubleOf x

type instance DoubleOf Double = Double
type instance DoubleOf Float  = Double

type instance DoubleOf (Complex a) = Complex (DoubleOf a)

type family ElementOf c

type instance ElementOf (Vector a) = a
type instance ElementOf (Matrix a) = a

type family IndexOf c

type instance IndexOf Vector = Int
type instance IndexOf Matrix = (Int,Int)

-------------------------------------------------------------------
{-
-- | Supported single-double precision type pairs
class (Element e) => V_Precision e where
    v_double2FloatG :: Vector e -> Vector (SingleOf e)
    v_float2DoubleG :: Vector (SingleOf e) -> Vector e
{-
instance V_Precision Float where
    v_double2FloatG = double2FloatV
    v_float2DoubleG = float2DoubleV
-}
instance V_Precision Double where
    v_double2FloatG = double2FloatV
    v_float2DoubleG = float2DoubleV
{-
instance V_Precision (Complex Float) where
    v_double2FloatG = asComplex . double2FloatV . asReal
    v_float2DoubleG = asComplex . float2DoubleV . asReal
-}
instance V_Precision (Complex Double) where
    v_double2FloatG = asComplex . double2FloatV . asReal
    v_float2DoubleG = asComplex . float2DoubleV . asReal
-}
-------------------------------------------------------------------
{-
-- | converting to/from complex containers
class RealElement t => Complexable c t where
    v_toComplex   :: (c t, c t) -> c (Complex t)
    v_fromComplex :: c (Complex t) -> (c t, c t)
    v_complex'    :: c t -> c (Complex t)
    v_conj        :: c (Complex t) -> c (Complex t)

-- | converting to/from single/double precision numbers
class (Element (SingleOf t), Element t, RealElement (RealOf t)) => Precisionable c t where
    v_single'      :: (V_Precision (DoubleOf t)) => c t -> c (SingleOf t)
    v_double'      :: (V_Precision (DoubleOf t)) => c t -> c (DoubleOf t)

-- | generic conversion functions
class (Element t, RealElement (RealOf t)) => V_Convert t where
    -- | real/complex
    v_real    :: Complexable c (RealOf t) => c (RealOf t) -> c t    -- from the instances, this looks like it turns a real object into a complex object WHEN the context is a complex object
    v_complex :: Complexable c (RealOf t) => c t -> c (ComplexOf t)
    -- | single/double
    v_single  :: Precisionable c t => c t -> c (SingleOf t)
    v_double  :: Precisionable c t => c t -> c (DoubleOf t)
-}
-------------------------------------------------------------------
{-
instance Precisionable Vector Float where
    v_single' = id
    v_double' = float2DoubleG

instance Precisionable Vector Double where
    v_single' = double2FloatG
    v_double' = id

instance Precisionable Vector (Complex Float) where
    v_single' = id
    v_double' = float2DoubleG

instance Precisionable Vector (Complex Double) where
    v_single' = double2FloatG
    v_double' = id
-}
-------------------------------------------------------------------

class (Element t, Element (RealOf t)) => Convert t where
    real    :: NumericContainer c => c (RealOf t) -> c t
    complex :: NumericContainer c => c t -> c (ComplexOf t)
    single  :: NumericContainer c => c t -> c (SingleOf t)
    double  :: NumericContainer c => c t -> c (DoubleOf t)


instance Convert Double where
    real = id
    complex = complex'
    single = single'
    double = id

instance Convert Float where
    real = id
    complex = complex'
    single = id
    double = double'

instance Convert (Complex Double) where
    real = complex'
    complex = id
    single = single'
    double = id

instance Convert (Complex Float) where
    real = complex'
    complex = id
    single = id
    double = double'

-------------------------------------------------------------------

-- | to be replaced by Convert
class Convert t => AutoReal t where
    real'' :: NumericContainer c => c Double -> c t
    complex'' :: NumericContainer c => c t -> c (Complex Double)


instance AutoReal Double where
    real'' = real
    complex'' = complex

instance AutoReal (Complex Double) where
    real'' = real
    complex'' = complex

instance AutoReal Float where
    real'' = real . single
    complex'' = double . complex

instance AutoReal (Complex Float) where
    real'' = real . single
    complex'' = double . complex

-------------------------------------------------------------------

-- | Basic element-by-element functions for numeric containers
class (Element e) => Container c e where
{-
    -- | create a structure with a single element
    scalar      :: e -> c e
    -- | multiply every element by a scalar
    scale       :: e -> c e -> c e
    -- | scale the element by element reciprocal of the object:
    --
    -- @scaleRecip 2 (fromList [5,i]) == 2 |> [0.4 :+ 0.0,0.0 :+ (-2.0)]@
    scaleRecip  :: e -> c e -> c e
    -- | add a constant to each element
    addConstant :: e -> c e -> c e
    add         :: c e -> c e -> c e
    sub         :: c e -> c e -> c e
    -- | element by element multiplication
    mul         :: c e -> c e -> c e
    -- | element by element division
    divide      :: c e -> c e -> c e
    equal       :: c e -> c e -> Bool
-}
    -- | cannot implement instance Functor because of Element class constraint
    cmap        :: (Element a, Element b) => (a -> b) -> c a -> c b
    --
    -- | indexing function
    atIndex     :: c e -> IndexOf c -> e
    -- | index of min/max element
    minIndex    :: c e -> IndexOf c
    maxIndex    :: c e -> IndexOf c
    -- | value of min/max element
    minElement  :: c e -> e
    maxElement  :: c e -> e
    -- the C functions sumX/prodX are twice as fast as using foldVector
    -- | the sum/product of elements (faster than using @fold@
    sumElements :: c e -> e
    prodElements :: c e -> e

-- | Basic element-by-element functions.
class (Element e, Container c e) => Linear c e where
    -- | create a structure with a single element
    scalar      :: e -> c e
    scale       :: e -> c e -> c e
    -- | scale the element by element reciprocal of the object:
    --
    -- @scaleRecip 2 (fromList [5,i]) == 2 |> [0.4 :+ 0.0,0.0 :+ (-2.0)]@
    scaleRecip  :: e -> c e -> c e
    addConstant :: e -> c e -> c e
    add         :: c e -> c e -> c e
    sub         :: c e -> c e -> c e
    -- | element by element multiplication
    mul         :: c e -> c e -> c e
    -- | element by element division
    divide      :: c e -> c e -> c e
    equal       :: c e -> c e -> Bool