summaryrefslogtreecommitdiff
path: root/lib/Numeric
diff options
context:
space:
mode:
authorVivian McPhail <haskell.vivian.mcphail@gmail.com>2010-07-08 23:03:48 +0000
committerVivian McPhail <haskell.vivian.mcphail@gmail.com>2010-07-08 23:03:48 +0000
commit97e8a48be58fd53afccc7ae01ee6ec5805d5c1cd (patch)
tree837f4a6b21e0317da834c8ac42c8adfce9a22d24 /lib/Numeric
parentb8699c4f1acff1e3f31cdbac1a7a4a8864b1eeba (diff)
Linear and Floating (Complex Float)
Diffstat (limited to 'lib/Numeric')
-rw-r--r--lib/Numeric/GSL/Vector.hs24
-rw-r--r--lib/Numeric/GSL/gsl-aux.c126
-rw-r--r--lib/Numeric/LinearAlgebra/Instances.hs29
-rw-r--r--lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c22
-rw-r--r--lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h6
-rw-r--r--lib/Numeric/LinearAlgebra/Linear.hs11
6 files changed, 215 insertions, 3 deletions
diff --git a/lib/Numeric/GSL/Vector.hs b/lib/Numeric/GSL/Vector.hs
index 0148c4f..14ba0ff 100644
--- a/lib/Numeric/GSL/Vector.hs
+++ b/lib/Numeric/GSL/Vector.hs
@@ -17,9 +17,9 @@ 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, toScalarC, toScalarQ, 19 FunCodeS(..), toScalarR, toScalarF, toScalarC, toScalarQ,
20 FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, 20 FunCodeV(..), vectorMapR, vectorMapC, vectorMapF, vectorMapQ,
21 FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, 21 FunCodeSV(..), vectorMapValR, vectorMapValC, vectorMapValF, vectorMapValQ,
22 FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, 22 FunCodeVV(..), vectorZipR, vectorZipC, vectorZipF, vectorZipQ,
23 RandDist(..), randomVector 23 RandDist(..), randomVector
24) where 24) where
25 25
@@ -214,6 +214,12 @@ vectorMapF = vectorMapAux c_vectorMapF
214 214
215foreign import ccall safe "gsl-aux.h mapF" c_vectorMapF :: CInt -> TFF 215foreign import ccall safe "gsl-aux.h mapF" c_vectorMapF :: CInt -> TFF
216 216
217-- | map of real vectors with given function
218vectorMapQ :: FunCodeV -> Vector (Complex Float) -> Vector (Complex Float)
219vectorMapQ = vectorMapAux c_vectorMapQ
220
221foreign import ccall safe "gsl-aux.h mapQ" c_vectorMapQ :: CInt -> TQVQV
222
217------------------------------------------------------------------- 223-------------------------------------------------------------------
218 224
219-- | map of real vectors with given function 225-- | map of real vectors with given function
@@ -234,6 +240,12 @@ vectorMapValF oper = vectorMapValAux c_vectorMapValF (fromei oper)
234 240
235foreign import ccall safe "gsl-aux.h mapValF" c_vectorMapValF :: CInt -> Ptr Float -> TFF 241foreign import ccall safe "gsl-aux.h mapValF" c_vectorMapValF :: CInt -> Ptr Float -> TFF
236 242
243-- | map of complex vectors with given function
244vectorMapValQ :: FunCodeSV -> Complex Float -> Vector (Complex Float) -> Vector (Complex Float)
245vectorMapValQ oper = vectorMapValAux c_vectorMapValQ (fromei oper)
246
247foreign import ccall safe "gsl-aux.h mapValQ" c_vectorMapValQ :: CInt -> Ptr (Complex Float) -> TQVQV
248
237------------------------------------------------------------------- 249-------------------------------------------------------------------
238 250
239-- | elementwise operation on real vectors 251-- | elementwise operation on real vectors
@@ -254,6 +266,12 @@ vectorZipF = vectorZipAux c_vectorZipF
254 266
255foreign import ccall safe "gsl-aux.h zipF" c_vectorZipF :: CInt -> TFFF 267foreign import ccall safe "gsl-aux.h zipF" c_vectorZipF :: CInt -> TFFF
256 268
269-- | elementwise operation on complex vectors
270vectorZipQ :: FunCodeVV -> Vector (Complex Float) -> Vector (Complex Float) -> Vector (Complex Float)
271vectorZipQ = vectorZipAux c_vectorZipQ
272
273foreign import ccall safe "gsl-aux.h zipQ" c_vectorZipQ :: CInt -> TQVQVQV
274
257----------------------------------------------------------------------- 275-----------------------------------------------------------------------
258 276
259data RandDist = Uniform -- ^ uniform distribution in [0,1) 277data RandDist = Uniform -- ^ uniform distribution in [0,1)
diff --git a/lib/Numeric/GSL/gsl-aux.c b/lib/Numeric/GSL/gsl-aux.c
index 3f9eeba..689989d 100644
--- a/lib/Numeric/GSL/gsl-aux.c
+++ b/lib/Numeric/GSL/gsl-aux.c
@@ -393,6 +393,83 @@ int mapC(int code, KCVEC(x), CVEC(r)) {
393} 393}
394 394
395 395
396gsl_complex_float complex_float_math_fun(gsl_complex (*cf)(gsl_complex), gsl_complex_float a)
397{
398 gsl_complex c;
399 gsl_complex r;
400
401 gsl_complex_float float_r;
402
403 c.dat[0] = a.dat[0];
404 c.dat[1] = a.dat[1];
405
406 r = (*cf)(c);
407
408 float_r.dat[0] = r.dat[0];
409 float_r.dat[1] = r.dat[1];
410
411 return float_r;
412}
413
414gsl_complex_float complex_float_math_op(gsl_complex (*cf)(gsl_complex,gsl_complex),
415 gsl_complex_float a,gsl_complex_float b)
416{
417 gsl_complex c1;
418 gsl_complex c2;
419 gsl_complex r;
420
421 gsl_complex_float float_r;
422
423 c1.dat[0] = a.dat[0];
424 c1.dat[1] = a.dat[1];
425
426 c2.dat[0] = b.dat[0];
427 c2.dat[1] = b.dat[1];
428
429 r = (*cf)(c1,c2);
430
431 float_r.dat[0] = r.dat[0];
432 float_r.dat[1] = r.dat[1];
433
434 return float_r;
435}
436
437#define OPC(C,F) case C: { for(k=0;k<xn;k++) rp[k] = complex_float_math_fun(&F,xp[k]); OK }
438#define OPCA(C,F,A,B) case C: { for(k=0;k<xn;k++) rp[k] = complex_float_math_op(&F,A,B); OK }
439int mapQAux(int code, KGQVEC(x), GQVEC(r)) {
440 int k;
441 REQUIRES(xn == rn,BAD_SIZE);
442 DEBUGMSG("mapQ");
443 switch (code) {
444 OPC(0,gsl_complex_sin)
445 OPC(1,gsl_complex_cos)
446 OPC(2,gsl_complex_tan)
447 OPC(3,complex_abs)
448 OPC(4,gsl_complex_arcsin)
449 OPC(5,gsl_complex_arccos)
450 OPC(6,gsl_complex_arctan)
451 OPC(7,gsl_complex_sinh)
452 OPC(8,gsl_complex_cosh)
453 OPC(9,gsl_complex_tanh)
454 OPC(10,gsl_complex_arcsinh)
455 OPC(11,gsl_complex_arccosh)
456 OPC(12,gsl_complex_arctanh)
457 OPC(13,gsl_complex_exp)
458 OPC(14,gsl_complex_log)
459 OPC(15,complex_signum)
460 OPC(16,gsl_complex_sqrt)
461
462 // gsl_complex_arg
463 // gsl_complex_abs
464 default: ERROR(BAD_CODE);
465 }
466}
467
468int mapQ(int code, KQVEC(x), QVEC(r)) {
469 return mapQAux(code, xn, (gsl_complex_float*)xp, rn, (gsl_complex_float*)rp);
470}
471
472
396int mapValR(int code, double* pval, KRVEC(x), RVEC(r)) { 473int mapValR(int code, double* pval, KRVEC(x), RVEC(r)) {
397 int k; 474 int k;
398 double val = *pval; 475 double val = *pval;
@@ -446,6 +523,27 @@ int mapValC(int code, gsl_complex* val, KCVEC(x), CVEC(r)) {
446} 523}
447 524
448 525
526int mapValQAux(int code, gsl_complex_float* pval, KQVEC(x), GQVEC(r)) {
527 int k;
528 gsl_complex_float val = *pval;
529 REQUIRES(xn == rn,BAD_SIZE);
530 DEBUGMSG("mapValQ");
531 switch (code) {
532 OPCA(0,gsl_complex_mul,val,xp[k])
533 OPCA(1,gsl_complex_div,val,xp[k])
534 OPCA(2,gsl_complex_add,val,xp[k])
535 OPCA(3,gsl_complex_sub,val,xp[k])
536 OPCA(4,gsl_complex_pow,val,xp[k])
537 OPCA(5,gsl_complex_pow,xp[k],val)
538 default: ERROR(BAD_CODE);
539 }
540}
541
542int mapValQ(int code, gsl_complex_float* val, KQVEC(x), QVEC(r)) {
543 return mapValQAux(code, val, xn, (gsl_complex_float*)xp, rn, (gsl_complex_float*)rp);
544}
545
546
449#define OPZE(C,msg,E) case C: {DEBUGMSG(msg) for(k=0;k<an;k++) rp[k] = E(ap[k],bp[k]); OK } 547#define OPZE(C,msg,E) case C: {DEBUGMSG(msg) for(k=0;k<an;k++) rp[k] = E(ap[k],bp[k]); OK }
450#define OPZV(C,msg,E) case C: {DEBUGMSG(msg) res = E(V(r),V(b)); CHECK(res,res); OK } 548#define OPZV(C,msg,E) case C: {DEBUGMSG(msg) res = E(V(r),V(b)); CHECK(res,res); OK }
451int zipR(int code, KRVEC(a), KRVEC(b), RVEC(r)) { 549int zipR(int code, KRVEC(a), KRVEC(b), RVEC(r)) {
@@ -519,6 +617,34 @@ int zipC(int code, KCVEC(a), KCVEC(b), CVEC(r)) {
519} 617}
520 618
521 619
620#define OPCZE(C,msg,E) case C: {DEBUGMSG(msg) for(k=0;k<an;k++) rp[k] = complex_float_math_op(&E,ap[k],bp[k]); OK }
621int zipQAux(int code, KGQVEC(a), KGQVEC(b), GQVEC(r)) {
622 REQUIRES(an == bn && an == rn, BAD_SIZE);
623 int k;
624 switch(code) {
625 OPCZE(0,"zipQ Add",gsl_complex_add)
626 OPCZE(1,"zipQ Sub",gsl_complex_sub)
627 OPCZE(2,"zipQ Mul",gsl_complex_mul)
628 OPCZE(3,"zipQ Div",gsl_complex_div)
629 OPCZE(4,"zipQ Pow",gsl_complex_pow)
630 //OPZE(5,"zipR ATan2",atan2)
631 }
632 //KCVVIEW(a);
633 //KCVVIEW(b);
634 //CVVIEW(r);
635 //gsl_vector_memcpy(V(r),V(a));
636 //int res;
637 switch(code) {
638 default: ERROR(BAD_CODE);
639 }
640}
641
642
643int zipQ(int code, KQVEC(a), KQVEC(b), QVEC(r)) {
644 return zipQAux(code, an, (gsl_complex_float*)ap, bn, (gsl_complex_float*)bp, rn, (gsl_complex_float*)rp);
645}
646
647
522 648
523int fft(int code, KCVEC(X), CVEC(R)) { 649int fft(int code, KCVEC(X), CVEC(R)) {
524 REQUIRES(Xn == Rn,BAD_SIZE); 650 REQUIRES(Xn == Rn,BAD_SIZE);
diff --git a/lib/Numeric/LinearAlgebra/Instances.hs b/lib/Numeric/LinearAlgebra/Instances.hs
index bba89c8..04a9d88 100644
--- a/lib/Numeric/LinearAlgebra/Instances.hs
+++ b/lib/Numeric/LinearAlgebra/Instances.hs
@@ -118,6 +118,14 @@ instance Num (Vector (Complex Double)) where
118 abs = vectorMapC Abs 118 abs = vectorMapC Abs
119 fromInteger = fromList . return . fromInteger 119 fromInteger = fromList . return . fromInteger
120 120
121instance Num (Vector (Complex Float)) where
122 (+) = adaptScalar addConstant add (flip addConstant)
123 negate = scale (-1)
124 (*) = adaptScalar scale mul (flip scale)
125 signum = vectorMapQ Sign
126 abs = vectorMapQ Abs
127 fromInteger = fromList . return . fromInteger
128
121instance Linear Matrix a => Eq (Matrix a) where 129instance Linear Matrix a => Eq (Matrix a) where
122 (==) = equal 130 (==) = equal
123 131
@@ -209,6 +217,27 @@ instance Floating (Vector (Complex Double)) where
209 217
210----------------------------------------------------------- 218-----------------------------------------------------------
211 219
220instance Floating (Vector (Complex Float)) where
221 sin = vectorMapQ Sin
222 cos = vectorMapQ Cos
223 tan = vectorMapQ Tan
224 asin = vectorMapQ ASin
225 acos = vectorMapQ ACos
226 atan = vectorMapQ ATan
227 sinh = vectorMapQ Sinh
228 cosh = vectorMapQ Cosh
229 tanh = vectorMapQ Tanh
230 asinh = vectorMapQ ASinh
231 acosh = vectorMapQ ACosh
232 atanh = vectorMapQ ATanh
233 exp = vectorMapQ Exp
234 log = vectorMapQ Log
235 sqrt = vectorMapQ Sqrt
236 (**) = adaptScalar (vectorMapValQ PowSV) (vectorZipQ Pow) (flip (vectorMapValQ PowVS))
237 pi = fromList [pi]
238
239-----------------------------------------------------------
240
212instance (Linear Vector a, Floating (Vector a), Fractional (Matrix a)) => Floating (Matrix a) where 241instance (Linear Vector a, Floating (Vector a), Fractional (Matrix a)) => Floating (Matrix a) where
213 sin = liftMatrix sin 242 sin = liftMatrix sin
214 cos = liftMatrix cos 243 cos = liftMatrix cos
diff --git a/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c b/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c
index b9c2572..7a40991 100644
--- a/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c
+++ b/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.c
@@ -1063,6 +1063,18 @@ int transR(KDMAT(x),DMAT(t)) {
1063 OK 1063 OK
1064} 1064}
1065 1065
1066int transQ(KQMAT(x),QMAT(t)) {
1067 REQUIRES(xr==tc && xc==tr,BAD_SIZE);
1068 DEBUGMSG("transQ");
1069 int i,j;
1070 for (i=0; i<tr; i++) {
1071 for (j=0; j<tc; j++) {
1072 ((complex*)tp)[i*tc+j] = ((complex*)xp)[j*xc+i];
1073 }
1074 }
1075 OK
1076}
1077
1066int transC(KCMAT(x),CMAT(t)) { 1078int transC(KCMAT(x),CMAT(t)) {
1067 REQUIRES(xr==tc && xc==tr,BAD_SIZE); 1079 REQUIRES(xr==tc && xc==tr,BAD_SIZE);
1068 DEBUGMSG("transC"); 1080 DEBUGMSG("transC");
@@ -1097,6 +1109,16 @@ int constantR(double * pval, DVEC(r)) {
1097 OK 1109 OK
1098} 1110}
1099 1111
1112int constantQ(complex* pval, QVEC(r)) {
1113 DEBUGMSG("constantQ")
1114 int k;
1115 complex val = *pval;
1116 for(k=0;k<rn;k++) {
1117 ((complex*)rp)[k]=val;
1118 }
1119 OK
1120}
1121
1100int constantC(doublecomplex* pval, CVEC(r)) { 1122int constantC(doublecomplex* pval, CVEC(r)) {
1101 DEBUGMSG("constantC") 1123 DEBUGMSG("constantC")
1102 int k; 1124 int k;
diff --git a/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h b/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h
index 415a6ab..d01d9e5 100644
--- a/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h
+++ b/lib/Numeric/LinearAlgebra/LAPACK/lapack-aux.h
@@ -42,16 +42,20 @@ typedef short ftnlen;
42 42
43#define FVEC(A) int A##n, float*A##p 43#define FVEC(A) int A##n, float*A##p
44#define DVEC(A) int A##n, double*A##p 44#define DVEC(A) int A##n, double*A##p
45#define QVEC(A) int A##n, float*A##p
45#define CVEC(A) int A##n, double*A##p 46#define CVEC(A) int A##n, double*A##p
46#define FMAT(A) int A##r, int A##c, float* A##p 47#define FMAT(A) int A##r, int A##c, float* A##p
47#define DMAT(A) int A##r, int A##c, double* A##p 48#define DMAT(A) int A##r, int A##c, double* A##p
49#define QMAT(A) int A##r, int A##c, float* A##p
48#define CMAT(A) int A##r, int A##c, double* A##p 50#define CMAT(A) int A##r, int A##c, double* A##p
49 51
50#define KFVEC(A) int A##n, const float*A##p 52#define KFVEC(A) int A##n, const float*A##p
51#define KDVEC(A) int A##n, const double*A##p 53#define KDVEC(A) int A##n, const double*A##p
54#define KQVEC(A) int A##n, const float*A##p
52#define KCVEC(A) int A##n, const double*A##p 55#define KCVEC(A) int A##n, const double*A##p
53#define KFMAT(A) int A##r, int A##c, const float* A##p 56#define KFMAT(A) int A##r, int A##c, const float* A##p
54#define KDMAT(A) int A##r, int A##c, const double* A##p 57#define KDMAT(A) int A##r, int A##c, const double* A##p
58#define KQMAT(A) int A##r, int A##c, const float* A##p
55#define KCMAT(A) int A##r, int A##c, const double* A##p 59#define KCMAT(A) int A##r, int A##c, const double* A##p
56 60
57/********************************************************/ 61/********************************************************/
@@ -61,10 +65,12 @@ int multiplyC(int ta, int tb, KCMAT(a),KCMAT(b),CMAT(r));
61 65
62int transF(KFMAT(x),FMAT(t)); 66int transF(KFMAT(x),FMAT(t));
63int transR(KDMAT(x),DMAT(t)); 67int transR(KDMAT(x),DMAT(t));
68int transQ(KQMAT(x),QMAT(t));
64int transC(KCMAT(x),CMAT(t)); 69int transC(KCMAT(x),CMAT(t));
65 70
66int constantF(float * pval, FVEC(r)); 71int constantF(float * pval, FVEC(r));
67int constantR(double * pval, DVEC(r)); 72int constantR(double * pval, DVEC(r));
73int constantQ(complex* pval, QVEC(r));
68int constantC(doublecomplex* pval, CVEC(r)); 74int constantC(doublecomplex* pval, CVEC(r));
69 75
70int svd_l_R(KDMAT(x),DMAT(u),DVEC(s),DMAT(v)); 76int svd_l_R(KDMAT(x),DMAT(u),DVEC(s),DMAT(v));
diff --git a/lib/Numeric/LinearAlgebra/Linear.hs b/lib/Numeric/LinearAlgebra/Linear.hs
index aed6a2b..2351ff1 100644
--- a/lib/Numeric/LinearAlgebra/Linear.hs
+++ b/lib/Numeric/LinearAlgebra/Linear.hs
@@ -132,6 +132,17 @@ instance Linear Vector (Complex Double) where
132 equal u v = dim u == dim v && vectorMax (mapVector magnitude (sub u v)) == 0.0 132 equal u v = dim u == dim v && vectorMax (mapVector magnitude (sub u v)) == 0.0
133 scalar x = fromList [x] 133 scalar x = fromList [x]
134 134
135instance Linear Vector (Complex Float) where
136 scale = vectorMapValQ Scale
137 scaleRecip = vectorMapValQ Recip
138 addConstant = vectorMapValQ AddConstant
139 add = vectorZipQ Add
140 sub = vectorZipQ Sub
141 mul = vectorZipQ Mul
142 divide = vectorZipQ Div
143 equal u v = dim u == dim v && vectorMax (mapVector magnitude (sub u v)) == 0.0
144 scalar x = fromList [x]
145
135instance (Linear Vector a, Container Matrix a) => (Linear Matrix a) where 146instance (Linear Vector a, Container Matrix a) => (Linear Matrix a) where
136 scale x = liftMatrix (scale x) 147 scale x = liftMatrix (scale x)
137 scaleRecip x = liftMatrix (scaleRecip x) 148 scaleRecip x = liftMatrix (scaleRecip x)