module Numeric.GSL.Vector (
sumF, sumR, sumQ, sumC,
prodF, prodR, prodQ, prodC,
dotF, dotR, dotQ, dotC,
FunCodeS(..), toScalarR, toScalarF, toScalarC, toScalarQ,
FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, vectorMapQ,
FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, vectorMapValQ,
FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, vectorZipQ,
RandDist(..), randomVector
) where
import Data.Packed.Internal.Common
import Data.Packed.Internal.Signatures
import Data.Packed.Internal.Vector
import Data.Complex
import Foreign.Marshal.Alloc(free)
import Foreign.Marshal.Array(newArray)
import Foreign.Ptr(Ptr)
import Foreign.C.Types
import System.IO.Unsafe(unsafePerformIO)
fromei x = fromIntegral (fromEnum x) :: CInt
data FunCodeV = Sin
| Cos
| Tan
| Abs
| ASin
| ACos
| ATan
| Sinh
| Cosh
| Tanh
| ASinh
| ACosh
| ATanh
| Exp
| Log
| Sign
| Sqrt
deriving Enum
data FunCodeSV = Scale
| Recip
| AddConstant
| Negate
| PowSV
| PowVS
deriving Enum
data FunCodeVV = Add
| Sub
| Mul
| Div
| Pow
| ATan2
deriving Enum
data FunCodeS = Norm2
| AbsSum
| MaxIdx
| Max
| MinIdx
| Min
deriving Enum
sumF :: Vector Float -> Float
sumF x = unsafePerformIO $ do
r <- createVector 1
app2 c_sumF vec x vec r "sumF"
return $ r @> 0
sumR :: Vector Double -> Double
sumR x = unsafePerformIO $ do
r <- createVector 1
app2 c_sumR vec x vec r "sumR"
return $ r @> 0
sumQ :: Vector (Complex Float) -> Complex Float
sumQ x = unsafePerformIO $ do
r <- createVector 1
app2 c_sumQ vec x vec r "sumQ"
return $ r @> 0
sumC :: Vector (Complex Double) -> Complex Double
sumC x = unsafePerformIO $ do
r <- createVector 1
app2 c_sumC vec x vec r "sumC"
return $ r @> 0
foreign import ccall unsafe "gsl-aux.h sumF" c_sumF :: TFF
foreign import ccall unsafe "gsl-aux.h sumR" c_sumR :: TVV
foreign import ccall unsafe "gsl-aux.h sumQ" c_sumQ :: TQVQV
foreign import ccall unsafe "gsl-aux.h sumC" c_sumC :: TCVCV
prodF :: Vector Float -> Float
prodF x = unsafePerformIO $ do
r <- createVector 1
app2 c_prodF vec x vec r "prodF"
return $ r @> 0
prodR :: Vector Double -> Double
prodR x = unsafePerformIO $ do
r <- createVector 1
app2 c_prodR vec x vec r "prodR"
return $ r @> 0
prodQ :: Vector (Complex Float) -> Complex Float
prodQ x = unsafePerformIO $ do
r <- createVector 1
app2 c_prodQ vec x vec r "prodQ"
return $ r @> 0
prodC :: Vector (Complex Double) -> Complex Double
prodC x = unsafePerformIO $ do
r <- createVector 1
app2 c_prodC vec x vec r "prodC"
return $ r @> 0
foreign import ccall unsafe "gsl-aux.h prodF" c_prodF :: TFF
foreign import ccall unsafe "gsl-aux.h prodR" c_prodR :: TVV
foreign import ccall unsafe "gsl-aux.h prodQ" c_prodQ :: TQVQV
foreign import ccall unsafe "gsl-aux.h prodC" c_prodC :: TCVCV
dotF :: Vector Float -> Vector Float -> Float
dotF x y = unsafePerformIO $ do
r <- createVector 1
app3 c_dotF vec x vec y vec r "dotF"
return $ r @> 0
dotR :: Vector Double -> Vector Double -> Double
dotR x y = unsafePerformIO $ do
r <- createVector 1
app3 c_dotR vec x vec y vec r "dotR"
return $ r @> 0
dotQ :: Vector (Complex Float) -> Vector (Complex Float) -> Complex Float
dotQ x y = unsafePerformIO $ do
r <- createVector 1
app3 c_dotQ vec x vec y vec r "dotQ"
return $ r @> 0
dotC :: Vector (Complex Double) -> Vector (Complex Double) -> Complex Double
dotC x y = unsafePerformIO $ do
r <- createVector 1
app3 c_dotC vec x vec y vec r "dotC"
return $ r @> 0
foreign import ccall unsafe "gsl-aux.h dotF" c_dotF :: TFFF
foreign import ccall unsafe "gsl-aux.h dotR" c_dotR :: TVVV
foreign import ccall unsafe "gsl-aux.h dotQ" c_dotQ :: TQVQVQV
foreign import ccall unsafe "gsl-aux.h dotC" c_dotC :: TCVCVCV
toScalarAux fun code v = unsafePerformIO $ do
r <- createVector 1
app2 (fun (fromei code)) vec v vec r "toScalarAux"
return (r `at` 0)
vectorMapAux fun code v = unsafePerformIO $ do
r <- createVector (dim v)
app2 (fun (fromei code)) vec v vec r "vectorMapAux"
return r
vectorMapValAux fun code val v = unsafePerformIO $ do
r <- createVector (dim v)
pval <- newArray [val]
app2 (fun (fromei code) pval) vec v vec r "vectorMapValAux"
free pval
return r
vectorZipAux fun code u v = unsafePerformIO $ do
r <- createVector (dim u)
app3 (fun (fromei code)) vec u vec v vec r "vectorZipAux"
return r
toScalarR :: FunCodeS -> Vector Double -> Double
toScalarR oper = toScalarAux c_toScalarR (fromei oper)
foreign import ccall unsafe "gsl-aux.h toScalarR" c_toScalarR :: CInt -> TVV
toScalarF :: FunCodeS -> Vector Float -> Float
toScalarF oper = toScalarAux c_toScalarF (fromei oper)
foreign import ccall unsafe "gsl-aux.h toScalarF" c_toScalarF :: CInt -> TFF
toScalarC :: FunCodeS -> Vector (Complex Double) -> Double
toScalarC oper = toScalarAux c_toScalarC (fromei oper)
foreign import ccall unsafe "gsl-aux.h toScalarC" c_toScalarC :: CInt -> TCVV
toScalarQ :: FunCodeS -> Vector (Complex Float) -> Float
toScalarQ oper = toScalarAux c_toScalarQ (fromei oper)
foreign import ccall unsafe "gsl-aux.h toScalarQ" c_toScalarQ :: CInt -> TQVF
vectorMapR :: FunCodeV -> Vector Double -> Vector Double
vectorMapR = vectorMapAux c_vectorMapR
foreign import ccall unsafe "gsl-aux.h mapR" c_vectorMapR :: CInt -> TVV
vectorMapC :: FunCodeV -> Vector (Complex Double) -> Vector (Complex Double)
vectorMapC oper = vectorMapAux c_vectorMapC (fromei oper)
foreign import ccall unsafe "gsl-aux.h mapC" c_vectorMapC :: CInt -> TCVCV
vectorMapF :: FunCodeV -> Vector Float -> Vector Float
vectorMapF = vectorMapAux c_vectorMapF
foreign import ccall unsafe "gsl-aux.h mapF" c_vectorMapF :: CInt -> TFF
vectorMapQ :: FunCodeV -> Vector (Complex Float) -> Vector (Complex Float)
vectorMapQ = vectorMapAux c_vectorMapQ
foreign import ccall unsafe "gsl-aux.h mapQ" c_vectorMapQ :: CInt -> TQVQV
vectorMapValR :: FunCodeSV -> Double -> Vector Double -> Vector Double
vectorMapValR oper = vectorMapValAux c_vectorMapValR (fromei oper)
foreign import ccall unsafe "gsl-aux.h mapValR" c_vectorMapValR :: CInt -> Ptr Double -> TVV
vectorMapValC :: FunCodeSV -> Complex Double -> Vector (Complex Double) -> Vector (Complex Double)
vectorMapValC = vectorMapValAux c_vectorMapValC
foreign import ccall unsafe "gsl-aux.h mapValC" c_vectorMapValC :: CInt -> Ptr (Complex Double) -> TCVCV
vectorMapValF :: FunCodeSV -> Float -> Vector Float -> Vector Float
vectorMapValF oper = vectorMapValAux c_vectorMapValF (fromei oper)
foreign import ccall unsafe "gsl-aux.h mapValF" c_vectorMapValF :: CInt -> Ptr Float -> TFF
vectorMapValQ :: FunCodeSV -> Complex Float -> Vector (Complex Float) -> Vector (Complex Float)
vectorMapValQ oper = vectorMapValAux c_vectorMapValQ (fromei oper)
foreign import ccall unsafe "gsl-aux.h mapValQ" c_vectorMapValQ :: CInt -> Ptr (Complex Float) -> TQVQV
vectorZipR :: FunCodeVV -> Vector Double -> Vector Double -> Vector Double
vectorZipR = vectorZipAux c_vectorZipR
foreign import ccall unsafe "gsl-aux.h zipR" c_vectorZipR :: CInt -> TVVV
vectorZipC :: FunCodeVV -> Vector (Complex Double) -> Vector (Complex Double) -> Vector (Complex Double)
vectorZipC = vectorZipAux c_vectorZipC
foreign import ccall unsafe "gsl-aux.h zipC" c_vectorZipC :: CInt -> TCVCVCV
vectorZipF :: FunCodeVV -> Vector Float -> Vector Float -> Vector Float
vectorZipF = vectorZipAux c_vectorZipF
foreign import ccall unsafe "gsl-aux.h zipF" c_vectorZipF :: CInt -> TFFF
vectorZipQ :: FunCodeVV -> Vector (Complex Float) -> Vector (Complex Float) -> Vector (Complex Float)
vectorZipQ = vectorZipAux c_vectorZipQ
foreign import ccall unsafe "gsl-aux.h zipQ" c_vectorZipQ :: CInt -> TQVQVQV
data RandDist = Uniform
| Gaussian
deriving Enum
randomVector :: Int
-> RandDist
-> Int
-> Vector Double
randomVector seed dist n = unsafePerformIO $ do
r <- createVector n
app1 (c_random_vector (fi seed) ((fi.fromEnum) dist)) vec r "randomVector"
return r
foreign import ccall unsafe "random_vector" c_random_vector :: CInt -> CInt -> TV