diff options
author | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-06-29 09:03:19 +0000 |
---|---|---|
committer | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-06-29 09:03:19 +0000 |
commit | 4957cff8af91cbb23c12382e25f5373fe96acb95 (patch) | |
tree | 2f2968d5ca88f7d76e208982b8938c4dfc46ce8a | |
parent | d18a86d37d55a39d4ec9b16397dd59f35aa13688 (diff) |
add-vector-float
-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 | ||||
-rw-r--r-- | lib/Numeric/GSL/Vector.hs | 32 | ||||
-rw-r--r-- | lib/Numeric/GSL/gsl-aux.c | 114 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Instances.hs | 29 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c | 22 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h | 6 | ||||
-rw-r--r-- | lib/Numeric/LinearAlgebra/Linear.hs | 11 |
11 files changed, 251 insertions, 9 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 | ||
diff --git a/lib/Numeric/GSL/Vector.hs b/lib/Numeric/GSL/Vector.hs index 28c3b06..d09323b 100644 --- a/lib/Numeric/GSL/Vector.hs +++ b/lib/Numeric/GSL/Vector.hs | |||
@@ -14,10 +14,10 @@ | |||
14 | ----------------------------------------------------------------------------- | 14 | ----------------------------------------------------------------------------- |
15 | 15 | ||
16 | module Numeric.GSL.Vector ( | 16 | module Numeric.GSL.Vector ( |
17 | FunCodeS(..), toScalarR, | 17 | FunCodeS(..), toScalarR, toScalarF, |
18 | FunCodeV(..), vectorMapR, vectorMapC, | 18 | FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, |
19 | FunCodeSV(..), vectorMapValR, vectorMapValC, | 19 | FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, |
20 | FunCodeVV(..), vectorZipR, vectorZipC, | 20 | FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, |
21 | RandDist(..), randomVector | 21 | RandDist(..), randomVector |
22 | ) where | 22 | ) where |
23 | 23 | ||
@@ -106,6 +106,12 @@ toScalarR oper = toScalarAux c_toScalarR (fromei oper) | |||
106 | 106 | ||
107 | foreign import ccall safe "gsl-aux.h toScalarR" c_toScalarR :: CInt -> TVV | 107 | foreign import ccall safe "gsl-aux.h toScalarR" c_toScalarR :: CInt -> TVV |
108 | 108 | ||
109 | -- | obtains different functions of a vector: norm1, norm2, max, min, posmax, posmin, etc. | ||
110 | toScalarF :: FunCodeS -> Vector Float -> Float | ||
111 | toScalarF oper = toScalarAux c_toScalarF (fromei oper) | ||
112 | |||
113 | foreign import ccall safe "gsl-aux.h toScalarF" c_toScalarF :: CInt -> TFF | ||
114 | |||
109 | ------------------------------------------------------------------ | 115 | ------------------------------------------------------------------ |
110 | 116 | ||
111 | -- | map of real vectors with given function | 117 | -- | map of real vectors with given function |
@@ -120,6 +126,12 @@ vectorMapC oper = vectorMapAux c_vectorMapC (fromei oper) | |||
120 | 126 | ||
121 | foreign import ccall safe "gsl-aux.h mapC" c_vectorMapC :: CInt -> TCVCV | 127 | foreign import ccall safe "gsl-aux.h mapC" c_vectorMapC :: CInt -> TCVCV |
122 | 128 | ||
129 | -- | map of real vectors with given function | ||
130 | vectorMapF :: FunCodeV -> Vector Float -> Vector Float | ||
131 | vectorMapF = vectorMapAux c_vectorMapF | ||
132 | |||
133 | foreign import ccall safe "gsl-aux.h mapF" c_vectorMapF :: CInt -> TFF | ||
134 | |||
123 | ------------------------------------------------------------------- | 135 | ------------------------------------------------------------------- |
124 | 136 | ||
125 | -- | map of real vectors with given function | 137 | -- | map of real vectors with given function |
@@ -134,6 +146,12 @@ vectorMapValC = vectorMapValAux c_vectorMapValC | |||
134 | 146 | ||
135 | foreign import ccall safe "gsl-aux.h mapValC" c_vectorMapValC :: CInt -> Ptr (Complex Double) -> TCVCV | 147 | foreign import ccall safe "gsl-aux.h mapValC" c_vectorMapValC :: CInt -> Ptr (Complex Double) -> TCVCV |
136 | 148 | ||
149 | -- | map of real vectors with given function | ||
150 | vectorMapValF :: FunCodeSV -> Float -> Vector Float -> Vector Float | ||
151 | vectorMapValF oper = vectorMapValAux c_vectorMapValF (fromei oper) | ||
152 | |||
153 | foreign import ccall safe "gsl-aux.h mapValF" c_vectorMapValF :: CInt -> Ptr Float -> TFF | ||
154 | |||
137 | ------------------------------------------------------------------- | 155 | ------------------------------------------------------------------- |
138 | 156 | ||
139 | -- | elementwise operation on real vectors | 157 | -- | elementwise operation on real vectors |
@@ -148,6 +166,12 @@ vectorZipC = vectorZipAux c_vectorZipC | |||
148 | 166 | ||
149 | foreign import ccall safe "gsl-aux.h zipC" c_vectorZipC :: CInt -> TCVCVCV | 167 | foreign import ccall safe "gsl-aux.h zipC" c_vectorZipC :: CInt -> TCVCVCV |
150 | 168 | ||
169 | -- | elementwise operation on real vectors | ||
170 | vectorZipF :: FunCodeVV -> Vector Float -> Vector Float -> Vector Float | ||
171 | vectorZipF = vectorZipAux c_vectorZipF | ||
172 | |||
173 | foreign import ccall safe "gsl-aux.h zipF" c_vectorZipF :: CInt -> TFFF | ||
174 | |||
151 | ----------------------------------------------------------------------- | 175 | ----------------------------------------------------------------------- |
152 | 176 | ||
153 | data RandDist = Uniform -- ^ uniform distribution in [0,1) | 177 | data RandDist = Uniform -- ^ uniform distribution in [0,1) |
diff --git a/lib/Numeric/GSL/gsl-aux.c b/lib/Numeric/GSL/gsl-aux.c index f5a95f4..6bb16f0 100644 --- a/lib/Numeric/GSL/gsl-aux.c +++ b/lib/Numeric/GSL/gsl-aux.c | |||
@@ -10,6 +10,16 @@ | |||
10 | #define KCVEC(A) int A##n, const gsl_complex*A##p | 10 | #define KCVEC(A) int A##n, const gsl_complex*A##p |
11 | #define KCMAT(A) int A##r, int A##c, const gsl_complex* A##p | 11 | #define KCMAT(A) int A##r, int A##c, const gsl_complex* A##p |
12 | 12 | ||
13 | #define FVEC(A) int A##n, float*A##p | ||
14 | #define FMAT(A) int A##r, int A##c, float* A##p | ||
15 | #define KFVEC(A) int A##n, const float*A##p | ||
16 | #define KFMAT(A) int A##r, int A##c, const float* A##p | ||
17 | |||
18 | #define QVEC(A) int A##n, gsl_complex_float*A##p | ||
19 | #define QMAT(A) int A##r, int A##c, gsl_complex_float* A##p | ||
20 | #define KQVEC(A) int A##n, const gsl_complex_float*A##p | ||
21 | #define KQMAT(A) int A##r, int A##c, const gsl_complex_float* A##p | ||
22 | |||
13 | #include <gsl/gsl_blas.h> | 23 | #include <gsl/gsl_blas.h> |
14 | #include <gsl/gsl_math.h> | 24 | #include <gsl/gsl_math.h> |
15 | #include <gsl/gsl_errno.h> | 25 | #include <gsl/gsl_errno.h> |
@@ -64,12 +74,24 @@ | |||
64 | #define KCVVIEW(A) gsl_vector_complex_const_view A = gsl_vector_complex_const_view_array((double*)A##p,A##n) | 74 | #define KCVVIEW(A) gsl_vector_complex_const_view A = gsl_vector_complex_const_view_array((double*)A##p,A##n) |
65 | #define KCMVIEW(A) gsl_matrix_complex_const_view A = gsl_matrix_complex_const_view_array((double*)A##p,A##r,A##c) | 75 | #define KCMVIEW(A) gsl_matrix_complex_const_view A = gsl_matrix_complex_const_view_array((double*)A##p,A##r,A##c) |
66 | 76 | ||
77 | #define FVVIEW(A) gsl_vector_float_view A = gsl_vector_float_view_array(A##p,A##n) | ||
78 | #define FMVIEW(A) gsl_matrix_float_view A = gsl_matrix_float_view_array(A##p,A##r,A##c) | ||
79 | #define QVVIEW(A) gsl_vector_float_complex_view A = gsl_vector_float_complex_view_array((float*)A##p,A##n) | ||
80 | #define QMVIEW(A) gsl_matrix_float_complex_view A = gsl_matrix_float_complex_view_array((float*)A##p,A##r,A##c) | ||
81 | #define KFVVIEW(A) gsl_vector_float_const_view A = gsl_vector_float_const_view_array(A##p,A##n) | ||
82 | #define KFMVIEW(A) gsl_matrix_float_const_view A = gsl_matrix_float_const_view_array(A##p,A##r,A##c) | ||
83 | #define KQVVIEW(A) gsl_vector_float_complex_const_view A = gsl_vector_float_complex_const_view_array((float*)A##p,A##n) | ||
84 | #define KQMVIEW(A) gsl_matrix_float_complex_const_view A = gsl_matrix_float_complex_const_view_array((float*)A##p,A##r,A##c) | ||
85 | |||
67 | #define V(a) (&a.vector) | 86 | #define V(a) (&a.vector) |
68 | #define M(a) (&a.matrix) | 87 | #define M(a) (&a.matrix) |
69 | 88 | ||
70 | #define GCVEC(A) int A##n, gsl_complex*A##p | 89 | #define GCVEC(A) int A##n, gsl_complex*A##p |
71 | #define KGCVEC(A) int A##n, const gsl_complex*A##p | 90 | #define KGCVEC(A) int A##n, const gsl_complex*A##p |
72 | 91 | ||
92 | #define GQVEC(A) int A##n, gsl_complex_float*A##p | ||
93 | #define KGQVEC(A) int A##n, const gsl_complex_float*A##p | ||
94 | |||
73 | #define BAD_SIZE 2000 | 95 | #define BAD_SIZE 2000 |
74 | #define BAD_CODE 2001 | 96 | #define BAD_CODE 2001 |
75 | #define MEM 2002 | 97 | #define MEM 2002 |
@@ -99,6 +121,24 @@ int toScalarR(int code, KRVEC(x), RVEC(r)) { | |||
99 | OK | 121 | OK |
100 | } | 122 | } |
101 | 123 | ||
124 | int toScalarF(int code, KFVEC(x), FVEC(r)) { | ||
125 | REQUIRES(rn==1,BAD_SIZE); | ||
126 | DEBUGMSG("toScalarF"); | ||
127 | KFVVIEW(x); | ||
128 | float res; | ||
129 | switch(code) { | ||
130 | case 0: { res = gsl_blas_snrm2(V(x)); break; } | ||
131 | case 1: { res = gsl_blas_sasum(V(x)); break; } | ||
132 | case 2: { res = gsl_vector_float_max_index(V(x)); break; } | ||
133 | case 3: { res = gsl_vector_float_max(V(x)); break; } | ||
134 | case 4: { res = gsl_vector_float_min_index(V(x)); break; } | ||
135 | case 5: { res = gsl_vector_float_min(V(x)); break; } | ||
136 | default: ERROR(BAD_CODE); | ||
137 | } | ||
138 | rp[0] = res; | ||
139 | OK | ||
140 | } | ||
141 | |||
102 | 142 | ||
103 | inline double sign(double x) { | 143 | inline double sign(double x) { |
104 | if(x>0) { | 144 | if(x>0) { |
@@ -110,6 +150,16 @@ inline double sign(double x) { | |||
110 | } | 150 | } |
111 | } | 151 | } |
112 | 152 | ||
153 | inline float float_sign(float x) { | ||
154 | if(x>0) { | ||
155 | return +1.0; | ||
156 | } else if (x<0) { | ||
157 | return -1.0; | ||
158 | } else { | ||
159 | return 0.0; | ||
160 | } | ||
161 | } | ||
162 | |||
113 | inline gsl_complex complex_abs(gsl_complex z) { | 163 | inline gsl_complex complex_abs(gsl_complex z) { |
114 | gsl_complex r; | 164 | gsl_complex r; |
115 | r.dat[0] = gsl_complex_abs(z); | 165 | r.dat[0] = gsl_complex_abs(z); |
@@ -159,6 +209,32 @@ int mapR(int code, KRVEC(x), RVEC(r)) { | |||
159 | } | 209 | } |
160 | } | 210 | } |
161 | 211 | ||
212 | int mapF(int code, KFVEC(x), FVEC(r)) { | ||
213 | int k; | ||
214 | REQUIRES(xn == rn,BAD_SIZE); | ||
215 | DEBUGMSG("mapF"); | ||
216 | switch (code) { | ||
217 | OP(0,sin) | ||
218 | OP(1,cos) | ||
219 | OP(2,tan) | ||
220 | OP(3,fabs) | ||
221 | OP(4,asin) | ||
222 | OP(5,acos) | ||
223 | OP(6,atan) /* atan2 mediante vectorZip */ | ||
224 | OP(7,sinh) | ||
225 | OP(8,cosh) | ||
226 | OP(9,tanh) | ||
227 | OP(10,gsl_asinh) | ||
228 | OP(11,gsl_acosh) | ||
229 | OP(12,gsl_atanh) | ||
230 | OP(13,exp) | ||
231 | OP(14,log) | ||
232 | OP(15,sign) | ||
233 | OP(16,sqrt) | ||
234 | default: ERROR(BAD_CODE); | ||
235 | } | ||
236 | } | ||
237 | |||
162 | 238 | ||
163 | int mapCAux(int code, KGCVEC(x), GCVEC(r)) { | 239 | int mapCAux(int code, KGCVEC(x), GCVEC(r)) { |
164 | int k; | 240 | int k; |
@@ -210,6 +286,22 @@ int mapValR(int code, double* pval, KRVEC(x), RVEC(r)) { | |||
210 | } | 286 | } |
211 | } | 287 | } |
212 | 288 | ||
289 | int mapValF(int code, float* pval, KFVEC(x), FVEC(r)) { | ||
290 | int k; | ||
291 | float val = *pval; | ||
292 | REQUIRES(xn == rn,BAD_SIZE); | ||
293 | DEBUGMSG("mapValF"); | ||
294 | switch (code) { | ||
295 | OPV(0,val*xp[k]) | ||
296 | OPV(1,val/xp[k]) | ||
297 | OPV(2,val+xp[k]) | ||
298 | OPV(3,val-xp[k]) | ||
299 | OPV(4,pow(val,xp[k])) | ||
300 | OPV(5,pow(xp[k],val)) | ||
301 | default: ERROR(BAD_CODE); | ||
302 | } | ||
303 | } | ||
304 | |||
213 | int mapValCAux(int code, gsl_complex* pval, KGCVEC(x), GCVEC(r)) { | 305 | int mapValCAux(int code, gsl_complex* pval, KGCVEC(x), GCVEC(r)) { |
214 | int k; | 306 | int k; |
215 | gsl_complex val = *pval; | 307 | gsl_complex val = *pval; |
@@ -255,6 +347,28 @@ int zipR(int code, KRVEC(a), KRVEC(b), RVEC(r)) { | |||
255 | } | 347 | } |
256 | 348 | ||
257 | 349 | ||
350 | int zipF(int code, KFVEC(a), KFVEC(b), FVEC(r)) { | ||
351 | REQUIRES(an == bn && an == rn, BAD_SIZE); | ||
352 | int k; | ||
353 | switch(code) { | ||
354 | OPZE(4,"zipF Pow",pow) | ||
355 | OPZE(5,"zipF ATan2",atan2) | ||
356 | } | ||
357 | KFVVIEW(a); | ||
358 | KFVVIEW(b); | ||
359 | FVVIEW(r); | ||
360 | gsl_vector_float_memcpy(V(r),V(a)); | ||
361 | int res; | ||
362 | switch(code) { | ||
363 | OPZV(0,"zipF Add",gsl_vector_float_add) | ||
364 | OPZV(1,"zipF Sub",gsl_vector_float_sub) | ||
365 | OPZV(2,"zipF Mul",gsl_vector_float_mul) | ||
366 | OPZV(3,"zipF Div",gsl_vector_float_div) | ||
367 | default: ERROR(BAD_CODE); | ||
368 | } | ||
369 | } | ||
370 | |||
371 | |||
258 | int zipCAux(int code, KGCVEC(a), KGCVEC(b), GCVEC(r)) { | 372 | int zipCAux(int code, KGCVEC(a), KGCVEC(b), GCVEC(r)) { |
259 | REQUIRES(an == bn && an == rn, BAD_SIZE); | 373 | REQUIRES(an == bn && an == rn, BAD_SIZE); |
260 | int k; | 374 | int k; |
diff --git a/lib/Numeric/LinearAlgebra/Instances.hs b/lib/Numeric/LinearAlgebra/Instances.hs index 1992db0..bba89c8 100644 --- a/lib/Numeric/LinearAlgebra/Instances.hs +++ b/lib/Numeric/LinearAlgebra/Instances.hs | |||
@@ -94,6 +94,14 @@ instance Linear Vector a => Eq (Vector a) where | |||
94 | 94 | ||
95 | #endif | 95 | #endif |
96 | 96 | ||
97 | instance Num (Vector Float) where | ||
98 | (+) = adaptScalar addConstant add (flip addConstant) | ||
99 | negate = scale (-1) | ||
100 | (*) = adaptScalar scale mul (flip scale) | ||
101 | signum = vectorMapF Sign | ||
102 | abs = vectorMapF Abs | ||
103 | fromInteger = fromList . return . fromInteger | ||
104 | |||
97 | instance Num (Vector Double) where | 105 | instance Num (Vector Double) where |
98 | (+) = adaptScalar addConstant add (flip addConstant) | 106 | (+) = adaptScalar addConstant add (flip addConstant) |
99 | negate = scale (-1) | 107 | negate = scale (-1) |
@@ -138,6 +146,27 @@ instance (Linear Vector a, Fractional (Vector a), Num (Matrix a)) => Fractional | |||
138 | 146 | ||
139 | --------------------------------------------------------- | 147 | --------------------------------------------------------- |
140 | 148 | ||
149 | instance Floating (Vector Float) where | ||
150 | sin = vectorMapF Sin | ||
151 | cos = vectorMapF Cos | ||
152 | tan = vectorMapF Tan | ||
153 | asin = vectorMapF ASin | ||
154 | acos = vectorMapF ACos | ||
155 | atan = vectorMapF ATan | ||
156 | sinh = vectorMapF Sinh | ||
157 | cosh = vectorMapF Cosh | ||
158 | tanh = vectorMapF Tanh | ||
159 | asinh = vectorMapF ASinh | ||
160 | acosh = vectorMapF ACosh | ||
161 | atanh = vectorMapF ATanh | ||
162 | exp = vectorMapF Exp | ||
163 | log = vectorMapF Log | ||
164 | sqrt = vectorMapF Sqrt | ||
165 | (**) = adaptScalar (vectorMapValF PowSV) (vectorZipF Pow) (flip (vectorMapValF PowVS)) | ||
166 | pi = fromList [pi] | ||
167 | |||
168 | ------------------------------------------------------------- | ||
169 | |||
141 | instance Floating (Vector Double) where | 170 | instance Floating (Vector Double) where |
142 | sin = vectorMapR Sin | 171 | sin = vectorMapR Sin |
143 | cos = vectorMapR Cos | 172 | cos = vectorMapR Cos |
diff --git a/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c b/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c index fd840e3..b9c2572 100644 --- a/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c +++ b/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c | |||
@@ -1039,6 +1039,18 @@ int multiplyC(int ta, int tb, KCMAT(a),KCMAT(b),CMAT(r)) { | |||
1039 | 1039 | ||
1040 | //////////////////// transpose ///////////////////////// | 1040 | //////////////////// transpose ///////////////////////// |
1041 | 1041 | ||
1042 | int transF(KFMAT(x),FMAT(t)) { | ||
1043 | REQUIRES(xr==tc && xc==tr,BAD_SIZE); | ||
1044 | DEBUGMSG("transF"); | ||
1045 | int i,j; | ||
1046 | for (i=0; i<tr; i++) { | ||
1047 | for (j=0; j<tc; j++) { | ||
1048 | tp[i*tc+j] = xp[j*xc+i]; | ||
1049 | } | ||
1050 | } | ||
1051 | OK | ||
1052 | } | ||
1053 | |||
1042 | int transR(KDMAT(x),DMAT(t)) { | 1054 | int transR(KDMAT(x),DMAT(t)) { |
1043 | REQUIRES(xr==tc && xc==tr,BAD_SIZE); | 1055 | REQUIRES(xr==tc && xc==tr,BAD_SIZE); |
1044 | DEBUGMSG("transR"); | 1056 | DEBUGMSG("transR"); |
@@ -1065,6 +1077,16 @@ int transC(KCMAT(x),CMAT(t)) { | |||
1065 | 1077 | ||
1066 | //////////////////// constant ///////////////////////// | 1078 | //////////////////// constant ///////////////////////// |
1067 | 1079 | ||
1080 | int constantF(float * pval, FVEC(r)) { | ||
1081 | DEBUGMSG("constantF") | ||
1082 | int k; | ||
1083 | double val = *pval; | ||
1084 | for(k=0;k<rn;k++) { | ||
1085 | rp[k]=val; | ||
1086 | } | ||
1087 | OK | ||
1088 | } | ||
1089 | |||
1068 | int constantR(double * pval, DVEC(r)) { | 1090 | int constantR(double * pval, DVEC(r)) { |
1069 | DEBUGMSG("constantR") | 1091 | DEBUGMSG("constantR") |
1070 | int k; | 1092 | int k; |
diff --git a/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h b/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h index adf096e..415a6ab 100644 --- a/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h +++ b/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h | |||
@@ -40,13 +40,17 @@ typedef short ftnlen; | |||
40 | 40 | ||
41 | /********************************************************/ | 41 | /********************************************************/ |
42 | 42 | ||
43 | #define FVEC(A) int A##n, float*A##p | ||
43 | #define DVEC(A) int A##n, double*A##p | 44 | #define DVEC(A) int A##n, double*A##p |
44 | #define CVEC(A) int A##n, double*A##p | 45 | #define CVEC(A) int A##n, double*A##p |
46 | #define FMAT(A) int A##r, int A##c, float* A##p | ||
45 | #define DMAT(A) int A##r, int A##c, double* A##p | 47 | #define DMAT(A) int A##r, int A##c, double* A##p |
46 | #define CMAT(A) int A##r, int A##c, double* A##p | 48 | #define CMAT(A) int A##r, int A##c, double* A##p |
47 | 49 | ||
50 | #define KFVEC(A) int A##n, const float*A##p | ||
48 | #define KDVEC(A) int A##n, const double*A##p | 51 | #define KDVEC(A) int A##n, const double*A##p |
49 | #define KCVEC(A) int A##n, const double*A##p | 52 | #define KCVEC(A) int A##n, const double*A##p |
53 | #define KFMAT(A) int A##r, int A##c, const float* A##p | ||
50 | #define KDMAT(A) int A##r, int A##c, const double* A##p | 54 | #define KDMAT(A) int A##r, int A##c, const double* A##p |
51 | #define KCMAT(A) int A##r, int A##c, const double* A##p | 55 | #define KCMAT(A) int A##r, int A##c, const double* A##p |
52 | 56 | ||
@@ -55,9 +59,11 @@ typedef short ftnlen; | |||
55 | int multiplyR(int ta, int tb, KDMAT(a),KDMAT(b),DMAT(r)); | 59 | int multiplyR(int ta, int tb, KDMAT(a),KDMAT(b),DMAT(r)); |
56 | int multiplyC(int ta, int tb, KCMAT(a),KCMAT(b),CMAT(r)); | 60 | int multiplyC(int ta, int tb, KCMAT(a),KCMAT(b),CMAT(r)); |
57 | 61 | ||
62 | int transF(KFMAT(x),FMAT(t)); | ||
58 | int transR(KDMAT(x),DMAT(t)); | 63 | int transR(KDMAT(x),DMAT(t)); |
59 | int transC(KCMAT(x),CMAT(t)); | 64 | int transC(KCMAT(x),CMAT(t)); |
60 | 65 | ||
66 | int constantF(float * pval, FVEC(r)); | ||
61 | int constantR(double * pval, DVEC(r)); | 67 | int constantR(double * pval, DVEC(r)); |
62 | int constantC(doublecomplex* pval, CVEC(r)); | 68 | int constantC(doublecomplex* pval, CVEC(r)); |
63 | 69 | ||
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs index c802712..481d72a 100644 --- a/lib/Numeric/LinearAlgebra/Linear.hs +++ b/lib/Numeric/LinearAlgebra/Linear.hs | |||
@@ -42,6 +42,17 @@ class (Container c e) => Linear c e where | |||
42 | equal :: c e -> c e -> Bool | 42 | equal :: c e -> c e -> Bool |
43 | 43 | ||
44 | 44 | ||
45 | instance Linear Vector Float where | ||
46 | scale = vectorMapValF Scale | ||
47 | scaleRecip = vectorMapValF Recip | ||
48 | addConstant = vectorMapValF AddConstant | ||
49 | add = vectorZipF Add | ||
50 | sub = vectorZipF Sub | ||
51 | mul = vectorZipF Mul | ||
52 | divide = vectorZipF Div | ||
53 | equal u v = dim u == dim v && vectorFMax (vectorMapF Abs (sub u v)) == 0.0 | ||
54 | scalar x = fromList [x] | ||
55 | |||
45 | instance Linear Vector Double where | 56 | instance Linear Vector Double where |
46 | scale = vectorMapValR Scale | 57 | scale = vectorMapValR Scale |
47 | scaleRecip = vectorMapValR Recip | 58 | scaleRecip = vectorMapValR Recip |