diff options
Diffstat (limited to 'lib/Data/Packed')
-rw-r--r-- | lib/Data/Packed/Internal/Matrix.hs | 15 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Signatures.hs | 6 | ||||
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 4 | ||||
-rw-r--r-- | lib/Data/Packed/Matrix.hs | 8 | ||||
-rw-r--r-- | lib/Data/Packed/Vector.hs | 13 |
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 | ||
260 | instance Element Float where | ||
261 | transdata = transdataAux ctransF | ||
262 | constantD = constantAux cconstantF | ||
263 | |||
260 | instance Element Double where | 264 | instance 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 | ||
315 | foreign import ccall "transF" ctransF :: TFMFM | ||
311 | foreign import ccall "transR" ctransR :: TMM | 316 | foreign import ccall "transR" ctransR :: TMM |
312 | foreign import ccall "transC" ctransC :: TCMCM | 317 | foreign 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 | ||
337 | constantF :: Float -> Int -> Vector Float | ||
338 | constantF = constantAux cconstantF | ||
339 | foreign import ccall "constantF" cconstantF :: Ptr Float -> TF | ||
340 | |||
332 | constantR :: Double -> Int -> Vector Double | 341 | constantR :: Double -> Int -> Vector Double |
333 | constantR = constantAux cconstantR | 342 | constantR = constantAux cconstantR |
334 | foreign import ccall "constantR" cconstantR :: Ptr Double -> TV | 343 | foreign 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 |
371 | conjV :: Vector (Complex Double) -> Vector (Complex Double) | 380 | conjV :: (Storable a, RealFloat a) => Vector (Complex a) -> Vector (Complex a) |
372 | conjV = mapVector conjugate | 381 | conjV = 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 |
375 | toComplexV :: (Vector Double, Vector Double) -> Vector (Complex Double) | 384 | toComplexV :: Element a => (Vector a, Vector a) -> Vector (Complex a) |
376 | toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i] | 385 | toComplexV (r,i) = asComplex $ flatten $ fromColumns [r,i] |
377 | 386 | ||
378 | -- | the inverse of 'toComplex' | 387 | -- | the inverse of 'toComplex' |
379 | fromComplexV :: Vector (Complex Double) -> (Vector Double, Vector Double) | 388 | fromComplexV :: Element a => Vector (Complex a) -> (Vector a, Vector a) |
380 | fromComplexV z = (r,i) where | 389 | fromComplexV 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 | |||
18 | import Data.Complex | 18 | import Data.Complex |
19 | import Foreign.C.Types | 19 | import Foreign.C.Types |
20 | 20 | ||
21 | type PF = Ptr Float -- | ||
21 | type PD = Ptr Double -- | 22 | type PD = Ptr Double -- |
22 | type PC = Ptr (Complex Double) -- | 23 | type PC = Ptr (Complex Double) -- |
24 | type TF = CInt -> PF -> IO CInt -- | ||
25 | type TFF = CInt -> PF -> TF -- | ||
26 | type TFFF = CInt -> PF -> TFF -- | ||
23 | type TV = CInt -> PD -> IO CInt -- | 27 | type TV = CInt -> PD -> IO CInt -- |
24 | type TVV = CInt -> PD -> TV -- | 28 | type TVV = CInt -> PD -> TV -- |
25 | type TVVV = CInt -> PD -> TVV -- | 29 | type TVVV = CInt -> PD -> TVV -- |
30 | type TFM = CInt -> CInt -> PF -> IO CInt -- | ||
31 | type TFMFM = CInt -> CInt -> PF -> TFM -- | ||
26 | type TM = CInt -> CInt -> PD -> IO CInt -- | 32 | type TM = CInt -> CInt -> PD -> IO CInt -- |
27 | type TMM = CInt -> CInt -> PD -> TM -- | 33 | type TMM = CInt -> CInt -> PD -> TM -- |
28 | type TVMM = CInt -> PD -> TMM -- | 34 | type 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 |
267 | asReal :: Vector (Complex Double) -> Vector Double | 267 | asReal :: 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) } |
269 | asReal v = unsafeFromForeignPtr (castForeignPtr fp) (2*i) (2*n) | 269 | asReal 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 |
273 | asComplex :: Vector Double -> Vector (Complex Double) | 273 | asComplex :: 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) } |
275 | asComplex v = unsafeFromForeignPtr (castForeignPtr fp) (i `div` 2) (n `div` 2) | 275 | asComplex 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 | ||
458 | instance 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 | |||
458 | instance Container Vector Double where | 466 | instance 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 | ||
67 | vectorFMax :: Vector Float -> Float | ||
68 | vectorFMax = toScalarF Max | ||
69 | |||
70 | vectorFMin :: Vector Float -> Float | ||
71 | vectorFMin = toScalarF Min | ||
72 | |||
73 | vectorFMaxIndex :: Vector Float -> Int | ||
74 | vectorFMaxIndex = round . toScalarF MaxIdx | ||
75 | |||
76 | vectorFMinIndex :: Vector Float -> Int | ||
77 | vectorFMinIndex = round . toScalarF MinIdx | ||
78 | |||
66 | vectorMax :: Vector Double -> Double | 79 | vectorMax :: Vector Double -> Double |
67 | vectorMax = toScalarR Max | 80 | vectorMax = toScalarR Max |
68 | 81 | ||