summaryrefslogtreecommitdiff
path: root/packages/base/src/C/vector-aux.c
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src/C/vector-aux.c')
-rw-r--r--packages/base/src/C/vector-aux.c145
1 files changed, 132 insertions, 13 deletions
diff --git a/packages/base/src/C/vector-aux.c b/packages/base/src/C/vector-aux.c
index 58afc49..b67275f 100644
--- a/packages/base/src/C/vector-aux.c
+++ b/packages/base/src/C/vector-aux.c
@@ -46,7 +46,7 @@ int sumF(KFVEC(x),FVEC(r)) {
46 rp[0] = res; 46 rp[0] = res;
47 OK 47 OK
48} 48}
49 49
50int sumR(KDVEC(x),DVEC(r)) { 50int sumR(KDVEC(x),DVEC(r)) {
51 DEBUGMSG("sumR"); 51 DEBUGMSG("sumR");
52 REQUIRES(rn==1,BAD_SIZE); 52 REQUIRES(rn==1,BAD_SIZE);
@@ -57,6 +57,15 @@ int sumR(KDVEC(x),DVEC(r)) {
57 OK 57 OK
58} 58}
59 59
60int sumI(KIVEC(x),IVEC(r)) {
61 REQUIRES(rn==1,BAD_SIZE);
62 int i;
63 int res = 0;
64 for (i = 0; i < xn; i++) res += xp[i];
65 rp[0] = res;
66 OK
67}
68
60 69
61int sumQ(KQVEC(x),QVEC(r)) { 70int sumQ(KQVEC(x),QVEC(r)) {
62 DEBUGMSG("sumQ"); 71 DEBUGMSG("sumQ");
@@ -72,7 +81,7 @@ int sumQ(KQVEC(x),QVEC(r)) {
72 rp[0] = res; 81 rp[0] = res;
73 OK 82 OK
74} 83}
75 84
76int sumC(KCVEC(x),CVEC(r)) { 85int sumC(KCVEC(x),CVEC(r)) {
77 DEBUGMSG("sumC"); 86 DEBUGMSG("sumC");
78 REQUIRES(rn==1,BAD_SIZE); 87 REQUIRES(rn==1,BAD_SIZE);
@@ -98,7 +107,7 @@ int prodF(KFVEC(x),FVEC(r)) {
98 rp[0] = res; 107 rp[0] = res;
99 OK 108 OK
100} 109}
101 110
102int prodR(KDVEC(x),DVEC(r)) { 111int prodR(KDVEC(x),DVEC(r)) {
103 DEBUGMSG("prodR"); 112 DEBUGMSG("prodR");
104 REQUIRES(rn==1,BAD_SIZE); 113 REQUIRES(rn==1,BAD_SIZE);
@@ -109,6 +118,16 @@ int prodR(KDVEC(x),DVEC(r)) {
109 OK 118 OK
110} 119}
111 120
121int prodI(KIVEC(x),IVEC(r)) {
122 REQUIRES(rn==1,BAD_SIZE);
123 int i;
124 int res = 1;
125 for (i = 0; i < xn; i++) res *= xp[i];
126 rp[0] = res;
127 OK
128}
129
130
112 131
113int prodQ(KQVEC(x),QVEC(r)) { 132int prodQ(KQVEC(x),QVEC(r)) {
114 DEBUGMSG("prodQ"); 133 DEBUGMSG("prodQ");
@@ -126,7 +145,7 @@ int prodQ(KQVEC(x),QVEC(r)) {
126 rp[0] = res; 145 rp[0] = res;
127 OK 146 OK
128} 147}
129 148
130int prodC(KCVEC(x),CVEC(r)) { 149int prodC(KCVEC(x),CVEC(r)) {
131 DEBUGMSG("prodC"); 150 DEBUGMSG("prodC");
132 REQUIRES(rn==1,BAD_SIZE); 151 REQUIRES(rn==1,BAD_SIZE);
@@ -144,7 +163,7 @@ int prodC(KCVEC(x),CVEC(r)) {
144 OK 163 OK
145} 164}
146 165
147 166
148double dnrm2_(integer*, const double*, integer*); 167double dnrm2_(integer*, const double*, integer*);
149double dasum_(integer*, const double*, integer*); 168double dasum_(integer*, const double*, integer*);
150 169
@@ -189,8 +208,8 @@ double vector_min_index(KDVEC(x)) {
189 } 208 }
190 return r; 209 return r;
191} 210}
192 211
193int toScalarR(int code, KDVEC(x), DVEC(r)) { 212int toScalarR(int code, KDVEC(x), DVEC(r)) {
194 REQUIRES(rn==1,BAD_SIZE); 213 REQUIRES(rn==1,BAD_SIZE);
195 DEBUGMSG("toScalarR"); 214 DEBUGMSG("toScalarR");
196 double res; 215 double res;
@@ -256,7 +275,7 @@ float vector_min_index_f(KFVEC(x)) {
256} 275}
257 276
258 277
259int toScalarF(int code, KFVEC(x), FVEC(r)) { 278int toScalarF(int code, KFVEC(x), FVEC(r)) {
260 REQUIRES(rn==1,BAD_SIZE); 279 REQUIRES(rn==1,BAD_SIZE);
261 DEBUGMSG("toScalarF"); 280 DEBUGMSG("toScalarF");
262 float res; 281 float res;
@@ -275,10 +294,68 @@ int toScalarF(int code, KFVEC(x), FVEC(r)) {
275 OK 294 OK
276} 295}
277 296
297int vector_max_i(KIVEC(x)) {
298 int r = xp[0];
299 int k;
300 for (k = 1; k<xn; k++) {
301 if(xp[k]>r) {
302 r = xp[k];
303 }
304 }
305 return r;
306}
307
308int vector_min_i(KIVEC(x)) {
309 float r = xp[0];
310 int k;
311 for (k = 1; k<xn; k++) {
312 if(xp[k]<r) {
313 r = xp[k];
314 }
315 }
316 return r;
317}
318
319int vector_max_index_i(KIVEC(x)) {
320 int k, r = 0;
321 for (k = 1; k<xn; k++) {
322 if(xp[k]>xp[r]) {
323 r = k;
324 }
325 }
326 return r;
327}
328
329int vector_min_index_i(KIVEC(x)) {
330 int k, r = 0;
331 for (k = 1; k<xn; k++) {
332 if(xp[k]<xp[r]) {
333 r = k;
334 }
335 }
336 return r;
337}
338
339
340int toScalarI(int code, KIVEC(x), IVEC(r)) {
341 REQUIRES(rn==1,BAD_SIZE);
342 int res;
343 switch(code) {
344 case 2: { res = vector_max_index_i(V(x)); break; }
345 case 3: { res = vector_max_i(V(x)); break; }
346 case 4: { res = vector_min_index_i(V(x)); break; }
347 case 5: { res = vector_min_i(V(x)); break; }
348 default: ERROR(BAD_CODE);
349 }
350 rp[0] = res;
351 OK
352}
353
354
278double dznrm2_(integer*, const doublecomplex*, integer*); 355double dznrm2_(integer*, const doublecomplex*, integer*);
279double dzasum_(integer*, const doublecomplex*, integer*); 356double dzasum_(integer*, const doublecomplex*, integer*);
280 357
281int toScalarC(int code, KCVEC(x), DVEC(r)) { 358int toScalarC(int code, KCVEC(x), DVEC(r)) {
282 REQUIRES(rn==1,BAD_SIZE); 359 REQUIRES(rn==1,BAD_SIZE);
283 DEBUGMSG("toScalarC"); 360 DEBUGMSG("toScalarC");
284 double res; 361 double res;
@@ -297,7 +374,7 @@ int toScalarC(int code, KCVEC(x), DVEC(r)) {
297double scnrm2_(integer*, const complex*, integer*); 374double scnrm2_(integer*, const complex*, integer*);
298double scasum_(integer*, const complex*, integer*); 375double scasum_(integer*, const complex*, integer*);
299 376
300int toScalarQ(int code, KQVEC(x), FVEC(r)) { 377int toScalarQ(int code, KQVEC(x), FVEC(r)) {
301 REQUIRES(rn==1,BAD_SIZE); 378 REQUIRES(rn==1,BAD_SIZE);
302 DEBUGMSG("toScalarQ"); 379 DEBUGMSG("toScalarQ");
303 float res; 380 float res;
@@ -389,6 +466,18 @@ int mapF(int code, KFVEC(x), FVEC(r)) {
389} 466}
390 467
391 468
469int mapI(int code, KIVEC(x), IVEC(r)) {
470 int k;
471 REQUIRES(xn == rn,BAD_SIZE);
472 switch (code) {
473 OP(3,abs)
474 OP(15,sign)
475 default: ERROR(BAD_CODE);
476 }
477}
478
479
480
392inline double abs_complex(doublecomplex z) { 481inline double abs_complex(doublecomplex z) {
393 return sqrt(z.r*z.r + z.i*z.i); 482 return sqrt(z.r*z.r + z.i*z.i);
394} 483}
@@ -526,6 +615,22 @@ int mapValF(int code, float* pval, KFVEC(x), FVEC(r)) {
526 } 615 }
527} 616}
528 617
618int mapValI(int code, int* pval, KIVEC(x), IVEC(r)) {
619 int k;
620 int val = *pval;
621 REQUIRES(xn == rn,BAD_SIZE);
622 DEBUGMSG("mapValI");
623 switch (code) {
624 OPV(0,val*xp[k])
625 OPV(1,val/xp[k])
626 OPV(2,val+xp[k])
627 OPV(3,val-xp[k])
628 OPV(6,val%xp[k])
629 OPV(7,xp[k]%val)
630 default: ERROR(BAD_CODE);
631 }
632}
633
529 634
530 635
531inline doublecomplex complex_add(doublecomplex a, doublecomplex b) { 636inline doublecomplex complex_add(doublecomplex a, doublecomplex b) {
@@ -608,6 +713,20 @@ REQUIRES(an == bn && an == rn, BAD_SIZE);
608} 713}
609 714
610 715
716int zipI(int code, KIVEC(a), KIVEC(b), IVEC(r)) {
717REQUIRES(an == bn && an == rn, BAD_SIZE);
718 int k;
719 switch(code) {
720 OPZO(0,"zipI Add",+)
721 OPZO(1,"zipI Sub",-)
722 OPZO(2,"zipI Mul",*)
723 OPZO(3,"zipI Div",/)
724 OPZO(6,"zipI Mod",%)
725 default: ERROR(BAD_CODE);
726 }
727}
728
729
611 730
612#define OPZOb(C,msg,O) case C: {DEBUGMSG(msg) for(k=0;k<an;k++) r2p[k] = a2p[k] O b2p[k]; OK } 731#define OPZOb(C,msg,O) case C: {DEBUGMSG(msg) for(k=0;k<an;k++) r2p[k] = a2p[k] O b2p[k]; OK }
613#define OPZEb(C,msg,E) case C: {DEBUGMSG(msg) for(k=0;k<an;k++) r2p[k] = E(a2p[k],b2p[k]); OK } 732#define OPZEb(C,msg,E) case C: {DEBUGMSG(msg) for(k=0;k<an;k++) r2p[k] = E(a2p[k],b2p[k]); OK }
@@ -679,7 +798,7 @@ int vectorScan(char * file, int* n, double**pp){
679 *pp = p; 798 *pp = p;
680 fclose(fp); 799 fclose(fp);
681 OK 800 OK
682} 801}
683 802
684int saveMatrix(char * file, char * format, KDMAT(a)){ 803int saveMatrix(char * file, char * format, KDMAT(a)){
685 FILE * fp; 804 FILE * fp;
@@ -754,7 +873,7 @@ int random_vector(unsigned int seed, int code, DVEC(r)) {
754 double V1,V2,S; 873 double V1,V2,S;
755 874
756 srandom(seed); 875 srandom(seed);
757 876
758 int k; 877 int k;
759 switch (code) { 878 switch (code) {
760 case 0: { // uniform 879 case 0: { // uniform
@@ -816,7 +935,7 @@ int random_vector(unsigned int seed, int code, DVEC(r)) {
816 char random_state[128]; 935 char random_state[128];
817 memset(&buffer, 0, sizeof(struct random_data)); 936 memset(&buffer, 0, sizeof(struct random_data));
818 memset(random_state, 0, sizeof(random_state)); 937 memset(random_state, 0, sizeof(random_state));
819 938
820 initstate_r(seed,random_state,sizeof(random_state),&buffer); 939 initstate_r(seed,random_state,sizeof(random_state),&buffer);
821 // setstate_r(random_state,&buffer); 940 // setstate_r(random_state,&buffer);
822 // srandom_r(seed,&buffer); 941 // srandom_r(seed,&buffer);