diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-09-08 08:14:27 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-09-08 08:14:27 +0000 |
commit | a858bf910291b63603a226c3190ecb36de01b5ba (patch) | |
tree | 1c855b7e29175c8497e3a68c6d3547930ed69d6a /lib/Numeric/Vector.hs | |
parent | 7e103b8ada6fa1479790eac80eda997f5fdaf33f (diff) |
re-export changes
Diffstat (limited to 'lib/Numeric/Vector.hs')
-rw-r--r-- | lib/Numeric/Vector.hs | 141 |
1 files changed, 19 insertions, 122 deletions
diff --git a/lib/Numeric/Vector.hs b/lib/Numeric/Vector.hs index 55d645a..9427243 100644 --- a/lib/Numeric/Vector.hs +++ b/lib/Numeric/Vector.hs | |||
@@ -14,24 +14,26 @@ | |||
14 | -- Stability : provisional | 14 | -- Stability : provisional |
15 | -- Portability : portable | 15 | -- Portability : portable |
16 | -- | 16 | -- |
17 | -- Numeric instances and functions for 'Data.Packed.Vector's | 17 | -- Numeric instances and functions for 'Vector'. |
18 | -- | 18 | -- |
19 | ----------------------------------------------------------------------------- | 19 | ----------------------------------------------------------------------------- |
20 | 20 | ||
21 | module Numeric.Vector ( | 21 | module Numeric.Vector ( |
22 | -- * Vector creation | 22 | -- * Basic vector functions |
23 | constant, linspace, | 23 | module Data.Packed.Vector, |
24 | module Data.Packed.Vector | 24 | module Numeric.Container, |
25 | -- * Vector creation | ||
26 | constant, linspace, | ||
27 | -- * Operators | ||
28 | (<.>) | ||
25 | ) where | 29 | ) where |
26 | 30 | ||
27 | import Data.Complex | 31 | import Data.Complex |
28 | 32 | ||
29 | import Control.Monad(ap) | ||
30 | |||
31 | import Data.Packed.Vector | 33 | import Data.Packed.Vector |
32 | import Data.Packed.Internal.Matrix(Element(..)) | 34 | import Data.Packed.Internal.Matrix |
33 | import Data.Packed.Internal.Vector(asComplex,asReal) | 35 | --import Data.Packed.Internal.Vector(asComplex,asReal) |
34 | import Data.Packed.Matrix(toColumns,fromColumns,flatten,reshape) | 36 | --import Data.Packed.Matrix(toColumns,fromColumns,flatten,reshape) |
35 | import Numeric.GSL.Vector | 37 | import Numeric.GSL.Vector |
36 | 38 | ||
37 | import Numeric.Container | 39 | import Numeric.Container |
@@ -92,10 +94,15 @@ Logarithmic spacing can be defined as follows: | |||
92 | 94 | ||
93 | @logspace n (a,b) = 10 ** linspace n (a,b)@ | 95 | @logspace n (a,b) = 10 ** linspace n (a,b)@ |
94 | -} | 96 | -} |
95 | linspace :: (Enum e, Linear Vector e) => Int -> (e, e) -> Vector e | 97 | linspace :: (Enum e, Container Vector e) => Int -> (e, e) -> Vector e |
96 | linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] | 98 | linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] |
97 | where s = (b-a)/fromIntegral (n-1) | 99 | where s = (b-a)/fromIntegral (n-1) |
98 | 100 | ||
101 | -- | Dot product: @u \<.\> v = dot u v@ | ||
102 | (<.>) :: Vectors Vector t => Vector t -> Vector t -> t | ||
103 | infixl 7 <.> | ||
104 | (<.>) = dot | ||
105 | |||
99 | ------------------------------------------------------------------ | 106 | ------------------------------------------------------------------ |
100 | 107 | ||
101 | adaptScalar f1 f2 f3 x y | 108 | adaptScalar f1 f2 f3 x y |
@@ -107,7 +114,7 @@ adaptScalar f1 f2 f3 x y | |||
107 | 114 | ||
108 | #ifndef VECTOR | 115 | #ifndef VECTOR |
109 | 116 | ||
110 | instance Linear Vector a => Eq (Vector a) where | 117 | instance Container Vector a => Eq (Vector a) where |
111 | (==) = equal | 118 | (==) = equal |
112 | 119 | ||
113 | #endif | 120 | #endif |
@@ -146,7 +153,7 @@ instance Num (Vector (Complex Float)) where | |||
146 | 153 | ||
147 | --------------------------------------------------- | 154 | --------------------------------------------------- |
148 | 155 | ||
149 | instance (Linear Vector a, Num (Vector a)) => Fractional (Vector a) where | 156 | instance (Container Vector a, Num (Vector a)) => Fractional (Vector a) where |
150 | fromRational n = fromList [fromRational n] | 157 | fromRational n = fromList [fromRational n] |
151 | (/) = adaptScalar f divide g where | 158 | (/) = adaptScalar f divide g where |
152 | r `f` v = scaleRecip r v | 159 | r `f` v = scaleRecip r v |
@@ -254,116 +261,6 @@ instance Floating (Vector (Complex Float)) where | |||
254 | -- instance (NFData a, Element a) => NFData (Matrix a) where | 261 | -- instance (NFData a, Element a) => NFData (Matrix a) where |
255 | -- rnf = rnf . flatten | 262 | -- rnf = rnf . flatten |
256 | 263 | ||
257 | --------------------------------------------------------------- | ||
258 | |||
259 | -- | obtains the complex conjugate of a complex vector | ||
260 | conjV :: (RealElement a) => Vector (Complex a) -> Vector (Complex a) | ||
261 | conjV = mapVector conjugate | ||
262 | |||
263 | -- | creates a complex vector from vectors with real and imaginary parts | ||
264 | toComplexV :: (RealElement a) => (Vector a, Vector a) -> Vector (Complex a) | ||
265 | toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i] | ||
266 | |||
267 | -- | the inverse of 'toComplex' | ||
268 | fromComplexV :: (RealElement a) => Vector (Complex a) -> (Vector a, Vector a) | ||
269 | fromComplexV z = (r,i) where | ||
270 | [r,i] = toColumns $ reshape 2 $ asReal z | ||
271 | 264 | ||
272 | -------------------------------------------------------------------------- | 265 | -------------------------------------------------------------------------- |
273 | 266 | ||
274 | instance NumericContainer Vector where | ||
275 | toComplex = toComplexV | ||
276 | fromComplex = fromComplexV | ||
277 | complex' v = toComplex (v,constant 0 (dim v)) | ||
278 | conj = conjV | ||
279 | -- cmap = mapVector | ||
280 | single' = double2FloatG | ||
281 | double' = float2DoubleG | ||
282 | |||
283 | -------------------------------------------------------------------------- | ||
284 | |||
285 | instance Linear Vector Float where | ||
286 | scale = vectorMapValF Scale | ||
287 | scaleRecip = vectorMapValF Recip | ||
288 | addConstant = vectorMapValF AddConstant | ||
289 | add = vectorZipF Add | ||
290 | sub = vectorZipF Sub | ||
291 | mul = vectorZipF Mul | ||
292 | divide = vectorZipF Div | ||
293 | equal u v = dim u == dim v && maxElement (vectorMapF Abs (sub u v)) == 0.0 | ||
294 | scalar x = fromList [x] | ||
295 | -- | ||
296 | instance Container Vector Float where | ||
297 | cmap = mapVector | ||
298 | atIndex = (@>) | ||
299 | minIndex = round . toScalarF MinIdx | ||
300 | maxIndex = round . toScalarF MaxIdx | ||
301 | minElement = toScalarF Min | ||
302 | maxElement = toScalarF Max | ||
303 | sumElements = sumF | ||
304 | prodElements = prodF | ||
305 | |||
306 | instance Linear Vector Double where | ||
307 | scale = vectorMapValR Scale | ||
308 | scaleRecip = vectorMapValR Recip | ||
309 | addConstant = vectorMapValR AddConstant | ||
310 | add = vectorZipR Add | ||
311 | sub = vectorZipR Sub | ||
312 | mul = vectorZipR Mul | ||
313 | divide = vectorZipR Div | ||
314 | equal u v = dim u == dim v && maxElement (vectorMapR Abs (sub u v)) == 0.0 | ||
315 | scalar x = fromList [x] | ||
316 | -- | ||
317 | instance Container Vector Double where | ||
318 | cmap = mapVector | ||
319 | atIndex = (@>) | ||
320 | minIndex = round . toScalarR MinIdx | ||
321 | maxIndex = round . toScalarR MaxIdx | ||
322 | minElement = toScalarR Min | ||
323 | maxElement = toScalarR Max | ||
324 | sumElements = sumR | ||
325 | prodElements = prodR | ||
326 | |||
327 | instance Linear Vector (Complex Double) where | ||
328 | scale = vectorMapValC Scale | ||
329 | scaleRecip = vectorMapValC Recip | ||
330 | addConstant = vectorMapValC AddConstant | ||
331 | add = vectorZipC Add | ||
332 | sub = vectorZipC Sub | ||
333 | mul = vectorZipC Mul | ||
334 | divide = vectorZipC Div | ||
335 | equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 | ||
336 | scalar x = fromList [x] | ||
337 | -- | ||
338 | instance Container Vector (Complex Double) where | ||
339 | cmap = mapVector | ||
340 | atIndex = (@>) | ||
341 | minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | ||
342 | maxIndex = maxIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | ||
343 | minElement = ap (@>) minIndex | ||
344 | maxElement = ap (@>) maxIndex | ||
345 | sumElements = sumC | ||
346 | prodElements = prodC | ||
347 | |||
348 | instance Linear Vector (Complex Float) where | ||
349 | scale = vectorMapValQ Scale | ||
350 | scaleRecip = vectorMapValQ Recip | ||
351 | addConstant = vectorMapValQ AddConstant | ||
352 | add = vectorZipQ Add | ||
353 | sub = vectorZipQ Sub | ||
354 | mul = vectorZipQ Mul | ||
355 | divide = vectorZipQ Div | ||
356 | equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 | ||
357 | scalar x = fromList [x] | ||
358 | -- | ||
359 | instance Container Vector (Complex Float) where | ||
360 | cmap = mapVector | ||
361 | atIndex = (@>) | ||
362 | minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | ||
363 | maxIndex = maxIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | ||
364 | minElement = ap (@>) minIndex | ||
365 | maxElement = ap (@>) maxIndex | ||
366 | sumElements = sumQ | ||
367 | prodElements = prodQ | ||
368 | |||
369 | --------------------------------------------------------------- | ||