diff options
author | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-09-06 09:59:08 +0000 |
---|---|---|
committer | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-09-06 09:59:08 +0000 |
commit | 9004922ad91c0b4ad8498c31171a3a1a1e27d9f2 (patch) | |
tree | c6d4759e584b7933029e405a73974e173046ddcc /lib/Numeric/Vector.hs | |
parent | 29099e3bfb4eec87ac3d4d675d7cfc82234c20d6 (diff) |
Merged changes with conversion-linear
Diffstat (limited to 'lib/Numeric/Vector.hs')
-rw-r--r-- | lib/Numeric/Vector.hs | 98 |
1 files changed, 57 insertions, 41 deletions
diff --git a/lib/Numeric/Vector.hs b/lib/Numeric/Vector.hs index d92a5e4..5cc51ac 100644 --- a/lib/Numeric/Vector.hs +++ b/lib/Numeric/Vector.hs | |||
@@ -30,10 +30,12 @@ import Control.Monad(ap) | |||
30 | 30 | ||
31 | import Data.Packed.Vector | 31 | import Data.Packed.Vector |
32 | import Data.Packed.Internal.Matrix(Element(..)) | 32 | import Data.Packed.Internal.Matrix(Element(..)) |
33 | import Data.Packed.Internal.Vector(asComplex,asReal) | ||
34 | import Data.Packed.Matrix(toColumns,fromColumns,flatten,reshape) | ||
33 | import Numeric.GSL.Vector | 35 | import Numeric.GSL.Vector |
34 | 36 | ||
35 | import Numeric.Container | 37 | import Numeric.Container |
36 | import Numeric.LinearAlgebra.Linear | 38 | --import Numeric.LinearAlgebra.Linear |
37 | 39 | ||
38 | ------------------------------------------------------------------- | 40 | ------------------------------------------------------------------- |
39 | 41 | ||
@@ -254,6 +256,40 @@ instance Floating (Vector (Complex Float)) where | |||
254 | 256 | ||
255 | --------------------------------------------------------------- | 257 | --------------------------------------------------------------- |
256 | 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 | |||
272 | -------------------------------------------------------------------------- | ||
273 | |||
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 RealElement e => Complexable Vector e where | ||
286 | v_toComplex = toComplexV | ||
287 | v_fromComplex = fromComplexV | ||
288 | v_conj = conjV | ||
289 | v_complex' v = toComplex (v,constantD 0 (dim v)) | ||
290 | -} | ||
291 | ------------------------------------------------------------------- | ||
292 | |||
257 | instance Linear Vector Float where | 293 | instance Linear Vector Float where |
258 | scale = vectorMapValF Scale | 294 | scale = vectorMapValF Scale |
259 | scaleRecip = vectorMapValF Recip | 295 | scaleRecip = vectorMapValF Recip |
@@ -264,12 +300,16 @@ instance Linear Vector Float where | |||
264 | divide = vectorZipF Div | 300 | divide = vectorZipF Div |
265 | equal u v = dim u == dim v && maxElement (vectorMapF Abs (sub u v)) == 0.0 | 301 | equal u v = dim u == dim v && maxElement (vectorMapF Abs (sub u v)) == 0.0 |
266 | scalar x = fromList [x] | 302 | scalar x = fromList [x] |
267 | 303 | -- | |
268 | instance Container Vector Float where | 304 | instance Container Vector Float where |
305 | cmap = mapVector | ||
306 | atIndex = (@>) | ||
269 | minIndex = round . toScalarF MinIdx | 307 | minIndex = round . toScalarF MinIdx |
270 | maxIndex = round . toScalarF MaxIdx | 308 | maxIndex = round . toScalarF MaxIdx |
271 | minElement = toScalarF Min | 309 | minElement = toScalarF Min |
272 | maxElement = toScalarF Max | 310 | maxElement = toScalarF Max |
311 | sumElements = sumF | ||
312 | prodElements = prodF | ||
273 | 313 | ||
274 | instance Linear Vector Double where | 314 | instance Linear Vector Double where |
275 | scale = vectorMapValR Scale | 315 | scale = vectorMapValR Scale |
@@ -281,12 +321,16 @@ instance Linear Vector Double where | |||
281 | divide = vectorZipR Div | 321 | divide = vectorZipR Div |
282 | equal u v = dim u == dim v && maxElement (vectorMapR Abs (sub u v)) == 0.0 | 322 | equal u v = dim u == dim v && maxElement (vectorMapR Abs (sub u v)) == 0.0 |
283 | scalar x = fromList [x] | 323 | scalar x = fromList [x] |
284 | 324 | -- | |
285 | instance Container Vector Double where | 325 | instance Container Vector Double where |
326 | cmap = mapVector | ||
327 | atIndex = (@>) | ||
286 | minIndex = round . toScalarR MinIdx | 328 | minIndex = round . toScalarR MinIdx |
287 | maxIndex = round . toScalarR MaxIdx | 329 | maxIndex = round . toScalarR MaxIdx |
288 | minElement = toScalarR Min | 330 | minElement = toScalarR Min |
289 | maxElement = toScalarR Max | 331 | maxElement = toScalarR Max |
332 | sumElements = sumR | ||
333 | prodElements = prodR | ||
290 | 334 | ||
291 | instance Linear Vector (Complex Double) where | 335 | instance Linear Vector (Complex Double) where |
292 | scale = vectorMapValC Scale | 336 | scale = vectorMapValC Scale |
@@ -298,12 +342,16 @@ instance Linear Vector (Complex Double) where | |||
298 | divide = vectorZipC Div | 342 | divide = vectorZipC Div |
299 | equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 | 343 | equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 |
300 | scalar x = fromList [x] | 344 | scalar x = fromList [x] |
301 | 345 | -- | |
302 | instance Container Vector (Complex Double) where | 346 | instance Container Vector (Complex Double) where |
347 | cmap = mapVector | ||
348 | atIndex = (@>) | ||
303 | minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | 349 | minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) |
304 | maxIndex = maxIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | 350 | maxIndex = maxIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) |
305 | minElement = ap (@>) minIndex | 351 | minElement = ap (@>) minIndex |
306 | maxElement = ap (@>) maxIndex | 352 | maxElement = ap (@>) maxIndex |
353 | sumElements = sumC | ||
354 | prodElements = prodC | ||
307 | 355 | ||
308 | instance Linear Vector (Complex Float) where | 356 | instance Linear Vector (Complex Float) where |
309 | scale = vectorMapValQ Scale | 357 | scale = vectorMapValQ Scale |
@@ -315,47 +363,15 @@ instance Linear Vector (Complex Float) where | |||
315 | divide = vectorZipQ Div | 363 | divide = vectorZipQ Div |
316 | equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 | 364 | equal u v = dim u == dim v && maxElement (mapVector magnitude (sub u v)) == 0.0 |
317 | scalar x = fromList [x] | 365 | scalar x = fromList [x] |
318 | 366 | -- | |
319 | instance Container Vector (Complex Float) where | 367 | instance Container Vector (Complex Float) where |
368 | cmap = mapVector | ||
369 | atIndex = (@>) | ||
320 | minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | 370 | minIndex = minIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) |
321 | maxIndex = maxIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) | 371 | maxIndex = maxIndex . fst . fromComplex . (zipVectorWith (*) `ap` mapVector conjugate) |
322 | minElement = ap (@>) minIndex | 372 | minElement = ap (@>) minIndex |
323 | maxElement = ap (@>) maxIndex | 373 | maxElement = ap (@>) maxIndex |
374 | sumElements = sumQ | ||
375 | prodElements = prodQ | ||
324 | 376 | ||
325 | --------------------------------------------------------------- | 377 | --------------------------------------------------------------- |
326 | |||
327 | instance Vectors Vector Float where | ||
328 | vectorSum = sumF | ||
329 | vectorProd = prodF | ||
330 | norm2 = toScalarF Norm2 | ||
331 | absSum = toScalarF AbsSum | ||
332 | dot = dotF | ||
333 | norm1 = toScalarF AbsSum | ||
334 | normInf = maxElement . vectorMapF Abs | ||
335 | |||
336 | instance Vectors Vector Double where | ||
337 | vectorSum = sumR | ||
338 | vectorProd = prodR | ||
339 | norm2 = toScalarR Norm2 | ||
340 | absSum = toScalarR AbsSum | ||
341 | dot = dotR | ||
342 | norm1 = toScalarR AbsSum | ||
343 | normInf = maxElement . vectorMapR Abs | ||
344 | |||
345 | instance Vectors Vector (Complex Float) where | ||
346 | vectorSum = sumQ | ||
347 | vectorProd = prodQ | ||
348 | norm2 = (:+ 0) . toScalarQ Norm2 | ||
349 | absSum = (:+ 0) . toScalarQ AbsSum | ||
350 | dot = dotQ | ||
351 | norm1 = (:+ 0) . vectorSum . fst . fromComplex . vectorMapQ Abs | ||
352 | normInf = (:+ 0) . maxElement . fst . fromComplex . vectorMapQ Abs | ||
353 | |||
354 | instance Vectors Vector (Complex Double) where | ||
355 | vectorSum = sumC | ||
356 | vectorProd = prodC | ||
357 | norm2 = (:+ 0) . toScalarC Norm2 | ||
358 | absSum = (:+ 0) . toScalarC AbsSum | ||
359 | dot = dotC | ||
360 | norm1 = (:+ 0) . vectorSum . fst . fromComplex . vectorMapC Abs | ||
361 | normInf = (:+ 0) . maxElement . fst . fromComplex . vectorMapC Abs \ No newline at end of file | ||