diff options
Diffstat (limited to 'lib/Numeric')
-rw-r--r-- | lib/Numeric/GSL/Vector.hs | 14 | ||||
-rw-r--r-- | lib/Numeric/GSL/gsl-aux.c | 29 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Interface.hs | 2 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Linear.hs | 49 |
4 files changed, 66 insertions, 28 deletions
diff --git a/lib/Numeric/GSL/Vector.hs b/lib/Numeric/GSL/Vector.hs index 97a0f9c..0148c4f 100644 --- a/lib/Numeric/GSL/Vector.hs +++ b/lib/Numeric/GSL/Vector.hs | |||
@@ -16,7 +16,7 @@ | |||
16 | module Numeric.GSL.Vector ( | 16 | module Numeric.GSL.Vector ( |
17 | sumF, sumR, sumQ, sumC, | 17 | sumF, sumR, sumQ, sumC, |
18 | dotF, dotR, dotQ, dotC, | 18 | dotF, dotR, dotQ, dotC, |
19 | FunCodeS(..), toScalarR, toScalarF, | 19 | FunCodeS(..), toScalarR, toScalarF, toScalarC, toScalarQ, |
20 | FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, | 20 | FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, |
21 | FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, | 21 | FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, |
22 | FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, | 22 | FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, |
@@ -182,6 +182,18 @@ toScalarF oper = toScalarAux c_toScalarF (fromei oper) | |||
182 | 182 | ||
183 | foreign import ccall safe "gsl-aux.h toScalarF" c_toScalarF :: CInt -> TFF | 183 | foreign import ccall safe "gsl-aux.h toScalarF" c_toScalarF :: CInt -> TFF |
184 | 184 | ||
185 | -- | obtains different functions of a vector: only norm1, norm2 | ||
186 | toScalarC :: FunCodeS -> Vector (Complex Double) -> Double | ||
187 | toScalarC oper = toScalarAux c_toScalarC (fromei oper) | ||
188 | |||
189 | foreign import ccall safe "gsl-aux.h toScalarC" c_toScalarC :: CInt -> TCVV | ||
190 | |||
191 | -- | obtains different functions of a vector: only norm1, norm2 | ||
192 | toScalarQ :: FunCodeS -> Vector (Complex Float) -> Float | ||
193 | toScalarQ oper = toScalarAux c_toScalarQ (fromei oper) | ||
194 | |||
195 | foreign import ccall safe "gsl-aux.h toScalarQ" c_toScalarQ :: CInt -> TQVF | ||
196 | |||
185 | ------------------------------------------------------------------ | 197 | ------------------------------------------------------------------ |
186 | 198 | ||
187 | -- | map of real vectors with given function | 199 | -- | map of real vectors with given function |
diff --git a/lib/Numeric/GSL/gsl-aux.c b/lib/Numeric/GSL/gsl-aux.c index fe33766..3f9eeba 100644 --- a/lib/Numeric/GSL/gsl-aux.c +++ b/lib/Numeric/GSL/gsl-aux.c | |||
@@ -234,6 +234,35 @@ int toScalarF(int code, KFVEC(x), FVEC(r)) { | |||
234 | } | 234 | } |
235 | 235 | ||
236 | 236 | ||
237 | int toScalarC(int code, KCVEC(x), RVEC(r)) { | ||
238 | REQUIRES(rn==1,BAD_SIZE); | ||
239 | DEBUGMSG("toScalarC"); | ||
240 | KCVVIEW(x); | ||
241 | double res; | ||
242 | switch(code) { | ||
243 | case 0: { res = gsl_blas_dznrm2(V(x)); break; } | ||
244 | case 1: { res = gsl_blas_dzasum(V(x)); break; } | ||
245 | default: ERROR(BAD_CODE); | ||
246 | } | ||
247 | rp[0] = res; | ||
248 | OK | ||
249 | } | ||
250 | |||
251 | int toScalarQ(int code, KQVEC(x), FVEC(r)) { | ||
252 | REQUIRES(rn==1,BAD_SIZE); | ||
253 | DEBUGMSG("toScalarQ"); | ||
254 | KQVVIEW(x); | ||
255 | float res; | ||
256 | switch(code) { | ||
257 | case 0: { res = gsl_blas_scnrm2(V(x)); break; } | ||
258 | case 1: { res = gsl_blas_scasum(V(x)); break; } | ||
259 | default: ERROR(BAD_CODE); | ||
260 | } | ||
261 | rp[0] = res; | ||
262 | OK | ||
263 | } | ||
264 | |||
265 | |||
237 | inline double sign(double x) { | 266 | inline double sign(double x) { |
238 | if(x>0) { | 267 | if(x>0) { |
239 | return +1.0; | 268 | return +1.0; |
diff --git a/lib/Numeric/LinearAlgebra/Interface.hs b/lib/Numeric/LinearAlgebra/Interface.hs index f8917a0..8d2b52a 100644 --- a/lib/Numeric/LinearAlgebra/Interface.hs +++ b/lib/Numeric/LinearAlgebra/Interface.hs | |||
@@ -28,7 +28,7 @@ import Numeric.LinearAlgebra.Instances() | |||
28 | import Data.Packed.Vector | 28 | import Data.Packed.Vector |
29 | import Data.Packed.Matrix | 29 | import Data.Packed.Matrix |
30 | import Numeric.LinearAlgebra.Algorithms | 30 | import Numeric.LinearAlgebra.Algorithms |
31 | import Numeric.LinearAlgebra.Linear | 31 | import Numeric.LinearAlgebra.Linear() |
32 | 32 | ||
33 | --import Numeric.GSL.Vector | 33 | --import Numeric.GSL.Vector |
34 | 34 | ||
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs index 1651247..e718e83 100644 --- a/lib/Numeric/LinearAlgebra/Linear.hs +++ b/lib/Numeric/LinearAlgebra/Linear.hs | |||
@@ -16,7 +16,7 @@ Basic optimized operations on vectors and matrices. | |||
16 | ----------------------------------------------------------------------------- | 16 | ----------------------------------------------------------------------------- |
17 | 17 | ||
18 | module Numeric.LinearAlgebra.Linear ( | 18 | module Numeric.LinearAlgebra.Linear ( |
19 | Vectors(..), normalise, | 19 | Vectors(..), |
20 | Linear(..) | 20 | Linear(..) |
21 | ) where | 21 | ) where |
22 | 22 | ||
@@ -25,21 +25,18 @@ import Data.Packed.Matrix | |||
25 | import Data.Complex | 25 | import Data.Complex |
26 | import Numeric.GSL.Vector | 26 | import Numeric.GSL.Vector |
27 | 27 | ||
28 | -- | normalise a vector to unit length | 28 | import Control.Monad(ap) |
29 | normalise :: (Floating a, Vectors Vector a, | ||
30 | Linear Vector a, Fractional (Vector a)) => Vector a -> Vector a | ||
31 | normalise v = scaleRecip (vectorSum v) v | ||
32 | 29 | ||
33 | -- | basic Vector functions | 30 | -- | basic Vector functions |
34 | class (Num b) => Vectors a b where | 31 | class Num e => Vectors a e where |
35 | vectorSum :: a b -> b | 32 | vectorSum :: a e -> e |
36 | euclidean :: a b -> b | 33 | euclidean :: a e -> e |
37 | absSum :: a b -> b | 34 | absSum :: a e -> e |
38 | vectorMin :: a b -> b | 35 | vectorMin :: a e -> e |
39 | vectorMax :: a b -> b | 36 | vectorMax :: a e -> e |
40 | minIdx :: a b -> Int | 37 | minIdx :: a e -> Int |
41 | maxIdx :: a b -> Int | 38 | maxIdx :: a e -> Int |
42 | dot :: a b -> a b -> b | 39 | dot :: a e -> a e -> e |
43 | 40 | ||
44 | instance Vectors Vector Float where | 41 | instance Vectors Vector Float where |
45 | vectorSum = sumF | 42 | vectorSum = sumF |
@@ -63,22 +60,22 @@ instance Vectors Vector Double where | |||
63 | 60 | ||
64 | instance Vectors Vector (Complex Float) where | 61 | instance Vectors Vector (Complex Float) where |
65 | vectorSum = sumQ | 62 | vectorSum = sumQ |
66 | euclidean = undefined | 63 | euclidean = (:+ 0) . toScalarQ Norm2 |
67 | absSum = undefined | 64 | absSum = (:+ 0) . toScalarQ AbsSum |
68 | vectorMin = undefined | 65 | vectorMin = ap (@>) minIdx |
69 | vectorMax = undefined | 66 | vectorMax = ap (@>) maxIdx |
70 | minIdx = undefined | 67 | minIdx = minIdx . (zipVector (*) `ap` mapVector conjugate) |
71 | maxIdx = undefined | 68 | maxIdx = maxIdx . (zipVector (*) `ap` mapVector conjugate) |
72 | dot = dotQ | 69 | dot = dotQ |
73 | 70 | ||
74 | instance Vectors Vector (Complex Double) where | 71 | instance Vectors Vector (Complex Double) where |
75 | vectorSum = sumC | 72 | vectorSum = sumC |
76 | euclidean = undefined | 73 | euclidean = (:+ 0) . toScalarC Norm2 |
77 | absSum = undefined | 74 | absSum = (:+ 0) . toScalarC AbsSum |
78 | vectorMin = undefined | 75 | vectorMin = ap (@>) minIdx |
79 | vectorMax = undefined | 76 | vectorMax = ap (@>) maxIdx |
80 | minIdx = undefined | 77 | minIdx = minIdx . (zipVector (*) `ap` mapVector conjugate) |
81 | maxIdx = undefined | 78 | maxIdx = maxIdx . (zipVector (*) `ap` mapVector conjugate) |
82 | dot = dotC | 79 | dot = dotC |
83 | 80 | ||
84 | ---------------------------------------------------- | 81 | ---------------------------------------------------- |