diff options
Diffstat (limited to 'lib/Numeric/GSL/Vector.hs')
-rw-r--r-- | lib/Numeric/GSL/Vector.hs | 145 |
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 | |||
17 | module Numeric.GSL.Vector ( | ||
18 | FunCodeS(..), toScalarR, | ||
19 | FunCodeV(..), vectorMapR, vectorMapC, | ||
20 | FunCodeSV(..), vectorMapValR, vectorMapValC, | ||
21 | FunCodeVV(..), vectorZipR, vectorZipC | ||
22 | ) where | ||
23 | |||
24 | import Data.Packed.Internal.Common | ||
25 | import Data.Packed.Internal.Vector | ||
26 | |||
27 | import Complex | ||
28 | import Foreign | ||
29 | |||
30 | data 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 | |||
49 | data FunCodeSV = Scale | ||
50 | | Recip | ||
51 | | AddConstant | ||
52 | | Negate | ||
53 | | PowSV | ||
54 | | PowVS | ||
55 | deriving Enum | ||
56 | |||
57 | data FunCodeVV = Add | ||
58 | | Sub | ||
59 | | Mul | ||
60 | | Div | ||
61 | | Pow | ||
62 | | ATan2 | ||
63 | deriving Enum | ||
64 | |||
65 | data FunCodeS = Norm2 | ||
66 | | AbsSum | ||
67 | | MaxIdx | ||
68 | | Max | ||
69 | | MinIdx | ||
70 | | Min | ||
71 | deriving Enum | ||
72 | |||
73 | ------------------------------------------------------------------ | ||
74 | |||
75 | toScalarAux 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 | |||
80 | vectorMapAux 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 | |||
85 | vectorMapValAux 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 | |||
92 | vectorZipAux 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. | ||
100 | toScalarR :: FunCodeS -> Vector Double -> Double | ||
101 | toScalarR oper = toScalarAux c_toScalarR (fromEnum oper) | ||
102 | |||
103 | foreign import ccall safe "gsl-aux.h toScalarR" c_toScalarR :: Int -> TVV | ||
104 | |||
105 | ------------------------------------------------------------------ | ||
106 | |||
107 | -- | map of real vectors with given function | ||
108 | vectorMapR :: FunCodeV -> Vector Double -> Vector Double | ||
109 | vectorMapR = vectorMapAux c_vectorMapR | ||
110 | |||
111 | foreign import ccall safe "gsl-aux.h mapR" c_vectorMapR :: Int -> TVV | ||
112 | |||
113 | -- | map of complex vectors with given function | ||
114 | vectorMapC :: FunCodeV -> Vector (Complex Double) -> Vector (Complex Double) | ||
115 | vectorMapC oper = vectorMapAux c_vectorMapC (fromEnum oper) | ||
116 | |||
117 | foreign import ccall safe "gsl-aux.h mapC" c_vectorMapC :: Int -> TCVCV | ||
118 | |||
119 | ------------------------------------------------------------------- | ||
120 | |||
121 | -- | map of real vectors with given function | ||
122 | vectorMapValR :: FunCodeSV -> Double -> Vector Double -> Vector Double | ||
123 | vectorMapValR oper = vectorMapValAux c_vectorMapValR (fromEnum oper) | ||
124 | |||
125 | foreign import ccall safe "gsl-aux.h mapValR" c_vectorMapValR :: Int -> Ptr Double -> TVV | ||
126 | |||
127 | -- | map of complex vectors with given function | ||
128 | vectorMapValC :: FunCodeSV -> Complex Double -> Vector (Complex Double) -> Vector (Complex Double) | ||
129 | vectorMapValC = vectorMapValAux c_vectorMapValC | ||
130 | |||
131 | foreign import ccall safe "gsl-aux.h mapValC" c_vectorMapValC :: Int -> Ptr (Complex Double) -> TCVCV | ||
132 | |||
133 | ------------------------------------------------------------------- | ||
134 | |||
135 | -- | elementwise operation on real vectors | ||
136 | vectorZipR :: FunCodeVV -> Vector Double -> Vector Double -> Vector Double | ||
137 | vectorZipR = vectorZipAux c_vectorZipR | ||
138 | |||
139 | foreign import ccall safe "gsl-aux.h zipR" c_vectorZipR :: Int -> TVVV | ||
140 | |||
141 | -- | elementwise operation on complex vectors | ||
142 | vectorZipC :: FunCodeVV -> Vector (Complex Double) -> Vector (Complex Double) -> Vector (Complex Double) | ||
143 | vectorZipC = vectorZipAux c_vectorZipC | ||
144 | |||
145 | foreign import ccall safe "gsl-aux.h zipC" c_vectorZipC :: Int -> TCVCVCV | ||