summaryrefslogtreecommitdiff
path: root/lib/Data
diff options
context:
space:
mode:
authorVivian McPhail <haskell.vivian.mcphail@gmail.com>2010-06-29 09:03:19 +0000
committerVivian McPhail <haskell.vivian.mcphail@gmail.com>2010-06-29 09:03:19 +0000
commit4957cff8af91cbb23c12382e25f5373fe96acb95 (patch)
tree2f2968d5ca88f7d76e208982b8938c4dfc46ce8a /lib/Data
parentd18a86d37d55a39d4ec9b16397dd59f35aa13688 (diff)
add-vector-float
Diffstat (limited to 'lib/Data')
-rw-r--r--lib/Data/Packed/Internal/Matrix.hs15
-rw-r--r--lib/Data/Packed/Internal/Signatures.hs6
-rw-r--r--lib/Data/Packed/Internal/Vector.hs4
-rw-r--r--lib/Data/Packed/Matrix.hs8
-rw-r--r--lib/Data/Packed/Vector.hs13
5 files changed, 41 insertions, 5 deletions
diff --git a/lib/Data/Packed/Internal/Matrix.hs b/lib/Data/Packed/Internal/Matrix.hs
index 003e8ee..7b3b305 100644
--- a/lib/Data/Packed/Internal/Matrix.hs
+++ b/lib/Data/Packed/Internal/Matrix.hs
@@ -257,6 +257,10 @@ class (Storable a, Floating a) => Element a where
257 constantD :: a -> Int -> Vector a 257 constantD :: a -> Int -> Vector a
258 constantD = constant' 258 constantD = constant'
259 259
260instance Element Float where
261 transdata = transdataAux ctransF
262 constantD = constantAux cconstantF
263
260instance Element Double where 264instance Element Double where
261 transdata = transdataAux ctransR 265 transdata = transdataAux ctransR
262 constantD = constantAux cconstantR 266 constantD = constantAux cconstantR
@@ -308,6 +312,7 @@ transdataAux fun c1 d c2 =
308 r2 = dim d `div` c2 312 r2 = dim d `div` c2
309 noneed = r1 == 1 || c1 == 1 313 noneed = r1 == 1 || c1 == 1
310 314
315foreign import ccall "transF" ctransF :: TFMFM
311foreign import ccall "transR" ctransR :: TMM 316foreign import ccall "transR" ctransR :: TMM
312foreign import ccall "transC" ctransC :: TCMCM 317foreign import ccall "transC" ctransC :: TCMCM
313---------------------------------------------------------------------- 318----------------------------------------------------------------------
@@ -329,6 +334,10 @@ constantAux fun x n = unsafePerformIO $ do
329 free px 334 free px
330 return v 335 return v
331 336
337constantF :: Float -> Int -> Vector Float
338constantF = constantAux cconstantF
339foreign import ccall "constantF" cconstantF :: Ptr Float -> TF
340
332constantR :: Double -> Int -> Vector Double 341constantR :: Double -> Int -> Vector Double
333constantR = constantAux cconstantR 342constantR = constantAux cconstantR
334foreign import ccall "constantR" cconstantR :: Ptr Double -> TV 343foreign import ccall "constantR" cconstantR :: Ptr Double -> TV
@@ -368,15 +377,15 @@ subMatrix' (r0,c0) (rt,ct) m = trans $ subMatrix' (c0,r0) (ct,rt) (trans m)
368-------------------------------------------------------------------------- 377--------------------------------------------------------------------------
369 378
370-- | obtains the complex conjugate of a complex vector 379-- | obtains the complex conjugate of a complex vector
371conjV :: Vector (Complex Double) -> Vector (Complex Double) 380conjV :: (Storable a, RealFloat a) => Vector (Complex a) -> Vector (Complex a)
372conjV = mapVector conjugate 381conjV = mapVector conjugate
373 382
374-- | creates a complex vector from vectors with real and imaginary parts 383-- | creates a complex vector from vectors with real and imaginary parts
375toComplexV :: (Vector Double, Vector Double) -> Vector (Complex Double) 384toComplexV :: Element a => (Vector a, Vector a) -> Vector (Complex a)
376toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i] 385toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i]
377 386
378-- | the inverse of 'toComplex' 387-- | the inverse of 'toComplex'
379fromComplexV :: Vector (Complex Double) -> (Vector Double, Vector Double) 388fromComplexV :: Element a => Vector (Complex a) -> (Vector a, Vector a)
380fromComplexV z = (r,i) where 389fromComplexV z = (r,i) where
381 [r,i] = toColumns $ reshape 2 $ asReal z 390 [r,i] = toColumns $ reshape 2 $ asReal z
382 391
diff --git a/lib/Data/Packed/Internal/Signatures.hs b/lib/Data/Packed/Internal/Signatures.hs
index d3ce121..4d984e3 100644
--- a/lib/Data/Packed/Internal/Signatures.hs
+++ b/lib/Data/Packed/Internal/Signatures.hs
@@ -18,11 +18,17 @@ import Foreign
18import Data.Complex 18import Data.Complex
19import Foreign.C.Types 19import Foreign.C.Types
20 20
21type PF = Ptr Float --
21type PD = Ptr Double -- 22type PD = Ptr Double --
22type PC = Ptr (Complex Double) -- 23type PC = Ptr (Complex Double) --
24type TF = CInt -> PF -> IO CInt --
25type TFF = CInt -> PF -> TF --
26type TFFF = CInt -> PF -> TFF --
23type TV = CInt -> PD -> IO CInt -- 27type TV = CInt -> PD -> IO CInt --
24type TVV = CInt -> PD -> TV -- 28type TVV = CInt -> PD -> TV --
25type TVVV = CInt -> PD -> TVV -- 29type TVVV = CInt -> PD -> TVV --
30type TFM = CInt -> CInt -> PF -> IO CInt --
31type TFMFM = CInt -> CInt -> PF -> TFM --
26type TM = CInt -> CInt -> PD -> IO CInt -- 32type TM = CInt -> CInt -> PD -> IO CInt --
27type TMM = CInt -> CInt -> PD -> TM -- 33type TMM = CInt -> CInt -> PD -> TM --
28type TVMM = CInt -> PD -> TMM -- 34type TVMM = CInt -> PD -> TMM --
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs
index a6868d9..d97a86d 100644
--- a/lib/Data/Packed/Internal/Vector.hs
+++ b/lib/Data/Packed/Internal/Vector.hs
@@ -264,13 +264,13 @@ takesV ms w | sum ms > dim w = error $ "takesV " ++ show ms ++ " on dim = " ++ (
264--------------------------------------------------------------- 264---------------------------------------------------------------
265 265
266-- | transforms a complex vector into a real vector with alternating real and imaginary parts 266-- | transforms a complex vector into a real vector with alternating real and imaginary parts
267asReal :: Vector (Complex Double) -> Vector Double 267asReal :: Vector (Complex a) -> Vector a
268--asReal v = V { ioff = 2*ioff v, idim = 2*dim v, fptr = castForeignPtr (fptr v) } 268--asReal v = V { ioff = 2*ioff v, idim = 2*dim v, fptr = castForeignPtr (fptr v) }
269asReal v = unsafeFromForeignPtr (castForeignPtr fp) (2*i) (2*n) 269asReal v = unsafeFromForeignPtr (castForeignPtr fp) (2*i) (2*n)
270 where (fp,i,n) = unsafeToForeignPtr v 270 where (fp,i,n) = unsafeToForeignPtr v
271 271
272-- | transforms a real vector into a complex vector with alternating real and imaginary parts 272-- | transforms a real vector into a complex vector with alternating real and imaginary parts
273asComplex :: Vector Double -> Vector (Complex Double) 273asComplex :: Vector a -> Vector (Complex a)
274--asComplex v = V { ioff = ioff v `div` 2, idim = dim v `div` 2, fptr = castForeignPtr (fptr v) } 274--asComplex v = V { ioff = ioff v `div` 2, idim = dim v `div` 2, fptr = castForeignPtr (fptr v) }
275asComplex v = unsafeFromForeignPtr (castForeignPtr fp) (i `div` 2) (n `div` 2) 275asComplex v = unsafeFromForeignPtr (castForeignPtr fp) (i `div` 2) (n `div` 2)
276 where (fp,i,n) = unsafeToForeignPtr v 276 where (fp,i,n) = unsafeToForeignPtr v
diff --git a/lib/Data/Packed/Matrix.hs b/lib/Data/Packed/Matrix.hs
index 0c21b97..c6d8a90 100644
--- a/lib/Data/Packed/Matrix.hs
+++ b/lib/Data/Packed/Matrix.hs
@@ -455,6 +455,14 @@ class (Element e) => Container c e where
455 real :: c Double -> c e 455 real :: c Double -> c e
456 complex :: c e -> c (Complex Double) 456 complex :: c e -> c (Complex Double)
457 457
458instance Container Vector Float where
459 toComplex = toComplexV
460 fromComplex = fromComplexV
461 comp v = toComplex (v,constant 0 (dim v))
462 conj = conjV
463 real = mapVector realToFrac
464 complex = (mapVector (\(r :+ i) -> (realToFrac r :+ realToFrac i))) . comp
465
458instance Container Vector Double where 466instance Container Vector Double where
459 toComplex = toComplexV 467 toComplex = toComplexV
460 fromComplex = fromComplexV 468 fromComplex = fromComplexV
diff --git a/lib/Data/Packed/Vector.hs b/lib/Data/Packed/Vector.hs
index f6b3fc6..66aa71d 100644
--- a/lib/Data/Packed/Vector.hs
+++ b/lib/Data/Packed/Vector.hs
@@ -19,6 +19,7 @@ module Data.Packed.Vector (
19 subVector, takesV, join, 19 subVector, takesV, join,
20 constant, linspace, 20 constant, linspace,
21 vecdisp, 21 vecdisp,
22 vectorFMax, vectorFMin, vectorFMaxIndex, vectorFMinIndex,
22 vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex, 23 vectorMax, vectorMin, vectorMaxIndex, vectorMinIndex,
23 mapVector, zipVector, 24 mapVector, zipVector,
24 fscanfVector, fprintfVector, freadVector, fwriteVector, 25 fscanfVector, fprintfVector, freadVector, fwriteVector,
@@ -63,6 +64,18 @@ linspace n (a,b) = add a $ scale s $ fromList [0 .. fromIntegral n-1]
63 add = vectorMapValR AddConstant 64 add = vectorMapValR AddConstant
64 s = (b-a)/fromIntegral (n-1) 65 s = (b-a)/fromIntegral (n-1)
65 66
67vectorFMax :: Vector Float -> Float
68vectorFMax = toScalarF Max
69
70vectorFMin :: Vector Float -> Float
71vectorFMin = toScalarF Min
72
73vectorFMaxIndex :: Vector Float -> Int
74vectorFMaxIndex = round . toScalarF MaxIdx
75
76vectorFMinIndex :: Vector Float -> Int
77vectorFMinIndex = round . toScalarF MinIdx
78
66vectorMax :: Vector Double -> Double 79vectorMax :: Vector Double -> Double
67vectorMax = toScalarR Max 80vectorMax = toScalarR Max
68 81