summaryrefslogtreecommitdiff
path: root/lib/Numeric/GSL/Vector.hs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Numeric/GSL/Vector.hs')
-rw-r--r--lib/Numeric/GSL/Vector.hs145
1 files changed, 145 insertions, 0 deletions
diff --git a/lib/Numeric/GSL/Vector.hs b/lib/Numeric/GSL/Vector.hs
new file mode 100644
index 0000000..ef3d5e8
--- /dev/null
+++ b/lib/Numeric/GSL/Vector.hs
@@ -0,0 +1,145 @@
1{-# OPTIONS_GHC -fglasgow-exts #-}
2-----------------------------------------------------------------------------
3-- |
4-- Module : Numeric.GSL.Vector
5-- Copyright : (c) Alberto Ruiz 2007
6-- License : GPL-style
7--
8-- Maintainer : Alberto Ruiz <aruiz@um.es>
9-- Stability : provisional
10-- Portability : portable (uses FFI)
11--
12-- Vector operations
13--
14-----------------------------------------------------------------------------
15-- #hide
16
17module Numeric.GSL.Vector (
18 FunCodeS(..), toScalarR,
19 FunCodeV(..), vectorMapR, vectorMapC,
20 FunCodeSV(..), vectorMapValR, vectorMapValC,
21 FunCodeVV(..), vectorZipR, vectorZipC
22) where
23
24import Data.Packed.Internal.Common
25import Data.Packed.Internal.Vector
26
27import Complex
28import Foreign
29
30data FunCodeV = Sin
31 | Cos
32 | Tan
33 | Abs
34 | ASin
35 | ACos
36 | ATan
37 | Sinh
38 | Cosh
39 | Tanh
40 | ASinh
41 | ACosh
42 | ATanh
43 | Exp
44 | Log
45 | Sign
46 | Sqrt
47 deriving Enum
48
49data FunCodeSV = Scale
50 | Recip
51 | AddConstant
52 | Negate
53 | PowSV
54 | PowVS
55 deriving Enum
56
57data FunCodeVV = Add
58 | Sub
59 | Mul
60 | Div
61 | Pow
62 | ATan2
63 deriving Enum
64
65data FunCodeS = Norm2
66 | AbsSum
67 | MaxIdx
68 | Max
69 | MinIdx
70 | Min
71 deriving Enum
72
73------------------------------------------------------------------
74
75toScalarAux fun code v = unsafePerformIO $ do
76 r <- createVector 1
77 fun (fromEnum code) // vec v // vec r // check "toScalarAux" [v]
78 return (r `at` 0)
79
80vectorMapAux fun code v = unsafePerformIO $ do
81 r <- createVector (dim v)
82 fun (fromEnum code) // vec v // vec r // check "vectorMapAux" [v]
83 return r
84
85vectorMapValAux fun code val v = unsafePerformIO $ do
86 r <- createVector (dim v)
87 pval <- newArray [val]
88 fun (fromEnum code) pval // vec v // vec r // check "vectorMapValAux" [v]
89 free pval
90 return r
91
92vectorZipAux fun code u v = unsafePerformIO $ do
93 r <- createVector (dim u)
94 fun (fromEnum code) // vec u // vec v // vec r // check "vectorZipAux" [u,v]
95 return r
96
97---------------------------------------------------------------------
98
99-- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc.
100toScalarR :: FunCodeS -> Vector Double -> Double
101toScalarR oper = toScalarAux c_toScalarR (fromEnum oper)
102
103foreign import ccall safe "gsl-aux.h toScalarR" c_toScalarR :: Int -> TVV
104
105------------------------------------------------------------------
106
107-- | map of real vectors with given function
108vectorMapR :: FunCodeV -> Vector Double -> Vector Double
109vectorMapR = vectorMapAux c_vectorMapR
110
111foreign import ccall safe "gsl-aux.h mapR" c_vectorMapR :: Int -> TVV
112
113-- | map of complex vectors with given function
114vectorMapC :: FunCodeV -> Vector (Complex Double) -> Vector (Complex Double)
115vectorMapC oper = vectorMapAux c_vectorMapC (fromEnum oper)
116
117foreign import ccall safe "gsl-aux.h mapC" c_vectorMapC :: Int -> TCVCV
118
119-------------------------------------------------------------------
120
121-- | map of real vectors with given function
122vectorMapValR :: FunCodeSV -> Double -> Vector Double -> Vector Double
123vectorMapValR oper = vectorMapValAux c_vectorMapValR (fromEnum oper)
124
125foreign import ccall safe "gsl-aux.h mapValR" c_vectorMapValR :: Int -> Ptr Double -> TVV
126
127-- | map of complex vectors with given function
128vectorMapValC :: FunCodeSV -> Complex Double -> Vector (Complex Double) -> Vector (Complex Double)
129vectorMapValC = vectorMapValAux c_vectorMapValC
130
131foreign import ccall safe "gsl-aux.h mapValC" c_vectorMapValC :: Int -> Ptr (Complex Double) -> TCVCV
132
133-------------------------------------------------------------------
134
135-- | elementwise operation on real vectors
136vectorZipR :: FunCodeVV -> Vector Double -> Vector Double -> Vector Double
137vectorZipR = vectorZipAux c_vectorZipR
138
139foreign import ccall safe "gsl-aux.h zipR" c_vectorZipR :: Int -> TVVV
140
141-- | elementwise operation on complex vectors
142vectorZipC :: FunCodeVV -> Vector (Complex Double) -> Vector (Complex Double) -> Vector (Complex Double)
143vectorZipC = vectorZipAux c_vectorZipC
144
145foreign import ccall safe "gsl-aux.h zipC" c_vectorZipC :: Int -> TCVCVCV