summaryrefslogtreecommitdiff
path: root/lib/Numeric/Vector.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric/Vector.hs')
-rw-r--r--lib/Numeric/Vector.hs141
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
21module Numeric.Vector ( 21module 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
27import Data.Complex 31import Data.Complex
28 32
29import Control.Monad(ap)
30
31import Data.Packed.Vector 33import Data.Packed.Vector
32import Data.Packed.Internal.Matrix(Element(..)) 34import Data.Packed.Internal.Matrix
33import Data.Packed.Internal.Vector(asComplex,asReal) 35--import Data.Packed.Internal.Vector(asComplex,asReal)
34import Data.Packed.Matrix(toColumns,fromColumns,flatten,reshape) 36--import Data.Packed.Matrix(toColumns,fromColumns,flatten,reshape)
35import Numeric.GSL.Vector 37import Numeric.GSL.Vector
36 38
37import Numeric.Container 39import 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-}
95linspace :: (Enum e, Linear Vector e) => Int -> (e, e) -> Vector e 97linspace :: (Enum e, Container Vector e) => Int -> (e, e) -> Vector e
96linspace n (a,b) = addConstant a $ scale s $ fromList [0 .. fromIntegral n-1] 98linspace 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
103infixl 7 <.>
104(<.>) = dot
105
99------------------------------------------------------------------ 106------------------------------------------------------------------
100 107
101adaptScalar f1 f2 f3 x y 108adaptScalar 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
110instance Linear Vector a => Eq (Vector a) where 117instance 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
149instance (Linear Vector a, Num (Vector a)) => Fractional (Vector a) where 156instance (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
260conjV :: (RealElement a) => Vector (Complex a) -> Vector (Complex a)
261conjV = mapVector conjugate
262
263-- | creates a complex vector from vectors with real and imaginary parts
264toComplexV :: (RealElement a) => (Vector a, Vector a) -> Vector (Complex a)
265toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i]
266
267-- | the inverse of 'toComplex'
268fromComplexV :: (RealElement a) => Vector (Complex a) -> (Vector a, Vector a)
269fromComplexV z = (r,i) where
270 [r,i] = toColumns $ reshape 2 $ asReal z
271 264
272-------------------------------------------------------------------------- 265--------------------------------------------------------------------------
273 266
274instance 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
285instance 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 --
296instance 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
306instance 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 --
317instance 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
327instance 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 --
338instance 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
348instance 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 --
359instance 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---------------------------------------------------------------