summaryrefslogtreecommitdiff
path: root/packages/base/src/Internal/C/vector-aux.c
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src/Internal/C/vector-aux.c')
-rw-r--r--packages/base/src/Internal/C/vector-aux.c300
1 files changed, 230 insertions, 70 deletions
diff --git a/packages/base/src/Internal/C/vector-aux.c b/packages/base/src/Internal/C/vector-aux.c
index fcc9dbd..0921c18 100644
--- a/packages/base/src/Internal/C/vector-aux.c
+++ b/packages/base/src/Internal/C/vector-aux.c
@@ -1,4 +1,5 @@
1#include <complex.h> 1#include <complex.h>
2#include <inttypes.h>
2 3
3typedef double complex TCD; 4typedef double complex TCD;
4typedef float complex TCF; 5typedef float complex TCF;
@@ -66,6 +67,14 @@ int sumI(KIVEC(x),IVEC(r)) {
66 OK 67 OK
67} 68}
68 69
70int sumL(KLVEC(x),LVEC(r)) {
71 REQUIRES(rn==1,BAD_SIZE);
72 int i;
73 int res = 0;
74 for (i = 0; i < xn; i++) res += xp[i];
75 rp[0] = res;
76 OK
77}
69 78
70int sumQ(KQVEC(x),QVEC(r)) { 79int sumQ(KQVEC(x),QVEC(r)) {
71 DEBUGMSG("sumQ"); 80 DEBUGMSG("sumQ");
@@ -127,7 +136,14 @@ int prodI(KIVEC(x),IVEC(r)) {
127 OK 136 OK
128} 137}
129 138
130 139int prodL(KLVEC(x),LVEC(r)) {
140 REQUIRES(rn==1,BAD_SIZE);
141 int i;
142 int res = 1;
143 for (i = 0; i < xn; i++) res *= xp[i];
144 rp[0] = res;
145 OK
146}
131 147
132int prodQ(KQVEC(x),QVEC(r)) { 148int prodQ(KQVEC(x),QVEC(r)) {
133 DEBUGMSG("prodQ"); 149 DEBUGMSG("prodQ");
@@ -189,7 +205,7 @@ double vector_min(KDVEC(x)) {
189 return r; 205 return r;
190} 206}
191 207
192double vector_max_index(KDVEC(x)) { 208int vector_max_index(KDVEC(x)) {
193 int k, r = 0; 209 int k, r = 0;
194 for (k = 1; k<xn; k++) { 210 for (k = 1; k<xn; k++) {
195 if(xp[k]>xp[r]) { 211 if(xp[k]>xp[r]) {
@@ -199,7 +215,7 @@ double vector_max_index(KDVEC(x)) {
199 return r; 215 return r;
200} 216}
201 217
202double vector_min_index(KDVEC(x)) { 218int vector_min_index(KDVEC(x)) {
203 int k, r = 0; 219 int k, r = 0;
204 for (k = 1; k<xn; k++) { 220 for (k = 1; k<xn; k++) {
205 if(xp[k]<xp[r]) { 221 if(xp[k]<xp[r]) {
@@ -254,7 +270,7 @@ float vector_min_f(KFVEC(x)) {
254 return r; 270 return r;
255} 271}
256 272
257float vector_max_index_f(KFVEC(x)) { 273int vector_max_index_f(KFVEC(x)) {
258 int k, r = 0; 274 int k, r = 0;
259 for (k = 1; k<xn; k++) { 275 for (k = 1; k<xn; k++) {
260 if(xp[k]>xp[r]) { 276 if(xp[k]>xp[r]) {
@@ -264,7 +280,7 @@ float vector_max_index_f(KFVEC(x)) {
264 return r; 280 return r;
265} 281}
266 282
267float vector_min_index_f(KFVEC(x)) { 283int vector_min_index_f(KFVEC(x)) {
268 int k, r = 0; 284 int k, r = 0;
269 for (k = 1; k<xn; k++) { 285 for (k = 1; k<xn; k++) {
270 if(xp[k]<xp[r]) { 286 if(xp[k]<xp[r]) {
@@ -306,7 +322,7 @@ int vector_max_i(KIVEC(x)) {
306} 322}
307 323
308int vector_min_i(KIVEC(x)) { 324int vector_min_i(KIVEC(x)) {
309 float r = xp[0]; 325 int r = xp[0];
310 int k; 326 int k;
311 for (k = 1; k<xn; k++) { 327 for (k = 1; k<xn; k++) {
312 if(xp[k]<r) { 328 if(xp[k]<r) {
@@ -352,6 +368,64 @@ int toScalarI(int code, KIVEC(x), IVEC(r)) {
352} 368}
353 369
354 370
371int64_t vector_max_l(KLVEC(x)) {
372 int64_t r = xp[0];
373 int k;
374 for (k = 1; k<xn; k++) {
375 if(xp[k]>r) {
376 r = xp[k];
377 }
378 }
379 return r;
380}
381
382int64_t vector_min_l(KLVEC(x)) {
383 int64_t r = xp[0];
384 int k;
385 for (k = 1; k<xn; k++) {
386 if(xp[k]<r) {
387 r = xp[k];
388 }
389 }
390 return r;
391}
392
393int vector_max_index_l(KLVEC(x)) {
394 int k, r = 0;
395 for (k = 1; k<xn; k++) {
396 if(xp[k]>xp[r]) {
397 r = k;
398 }
399 }
400 return r;
401}
402
403int vector_min_index_l(KLVEC(x)) {
404 int k, r = 0;
405 for (k = 1; k<xn; k++) {
406 if(xp[k]<xp[r]) {
407 r = k;
408 }
409 }
410 return r;
411}
412
413
414int toScalarL(int code, KLVEC(x), LVEC(r)) {
415 REQUIRES(rn==1,BAD_SIZE);
416 int64_t res;
417 switch(code) {
418 case 2: { res = vector_max_index_l(V(x)); break; }
419 case 3: { res = vector_max_l(V(x)); break; }
420 case 4: { res = vector_min_index_l(V(x)); break; }
421 case 5: { res = vector_min_l(V(x)); break; }
422 default: ERROR(BAD_CODE);
423 }
424 rp[0] = res;
425 OK
426}
427
428
355double dznrm2_(integer*, const doublecomplex*, integer*); 429double dznrm2_(integer*, const doublecomplex*, integer*);
356double dzasum_(integer*, const doublecomplex*, integer*); 430double dzasum_(integer*, const doublecomplex*, integer*);
357 431
@@ -477,6 +551,17 @@ int mapI(int code, KIVEC(x), IVEC(r)) {
477} 551}
478 552
479 553
554int mapL(int code, KLVEC(x), LVEC(r)) {
555 int k;
556 REQUIRES(xn == rn,BAD_SIZE);
557 switch (code) {
558 OP(3,abs)
559 OP(15,sign)
560 default: ERROR(BAD_CODE);
561 }
562}
563
564
480 565
481inline double abs_complex(doublecomplex z) { 566inline double abs_complex(doublecomplex z) {
482 return sqrt(z.r*z.r + z.i*z.i); 567 return sqrt(z.r*z.r + z.i*z.i);
@@ -641,6 +726,32 @@ int mapValI(int code, int* pval, KIVEC(x), IVEC(r)) {
641} 726}
642 727
643 728
729int64_t mod_l (int64_t a, int64_t b) {
730 int64_t m = a % b;
731 if (b>0) {
732 return m >=0 ? m : m+b;
733 } else {
734 return m <=0 ? m : m+b;
735 }
736}
737
738int mapValL(int code, int64_t* pval, KLVEC(x), LVEC(r)) {
739 int k;
740 int64_t val = *pval;
741 REQUIRES(xn == rn,BAD_SIZE);
742 DEBUGMSG("mapValL");
743 switch (code) {
744 OPV(0,val*xp[k])
745 OPV(1,val/xp[k])
746 OPV(2,val+xp[k])
747 OPV(3,val-xp[k])
748 OPV(6,mod(val,xp[k]))
749 OPV(7,mod(xp[k],val))
750 default: ERROR(BAD_CODE);
751 }
752}
753
754
644 755
645inline doublecomplex complex_add(doublecomplex a, doublecomplex b) { 756inline doublecomplex complex_add(doublecomplex a, doublecomplex b) {
646 doublecomplex r; 757 doublecomplex r;
@@ -736,6 +847,19 @@ REQUIRES(an == bn && an == rn, BAD_SIZE);
736} 847}
737 848
738 849
850int zipL(int code, KLVEC(a), KLVEC(b), LVEC(r)) {
851REQUIRES(an == bn && an == rn, BAD_SIZE);
852 int k;
853 switch(code) {
854 OPZO(0,"zipI Add",+)
855 OPZO(1,"zipI Sub",-)
856 OPZO(2,"zipI Mul",*)
857 OPZO(3,"zipI Div",/)
858 OPZO(6,"zipI Mod",%)
859 default: ERROR(BAD_CODE);
860 }
861}
862
739 863
740#define OPZOb(C,msg,O) case C: {DEBUGMSG(msg) for(k=0;k<an;k++) r2p[k] = a2p[k] O b2p[k]; OK } 864#define OPZOb(C,msg,O) case C: {DEBUGMSG(msg) for(k=0;k<an;k++) r2p[k] = a2p[k] O b2p[k]; OK }
741#define OPZEb(C,msg,E) case C: {DEBUGMSG(msg) for(k=0;k<an;k++) r2p[k] = E(a2p[k],b2p[k]); OK } 865#define OPZEb(C,msg,E) case C: {DEBUGMSG(msg) for(k=0;k<an;k++) r2p[k] = E(a2p[k],b2p[k]); OK }
@@ -990,6 +1114,18 @@ int sort_valuesI(KIVEC(v),IVEC(r)) {
990 OK 1114 OK
991} 1115}
992 1116
1117int
1118compare_longs(const void *a, const void *b) {
1119 return *(int64_t*)a > *(int64_t*)b;
1120}
1121
1122int sort_valuesL(KLVEC(v),LVEC(r)) {
1123 memcpy(rp,vp,vn*sizeof(int64_t));
1124 qsort(rp,rn,sizeof(int64_t),compare_ints);
1125 OK
1126}
1127
1128
993//////////////////////////////////////// 1129////////////////////////////////////////
994 1130
995 1131
@@ -1010,7 +1146,7 @@ int sort_valuesI(KIVEC(v),IVEC(r)) {
1010 OK 1146 OK
1011 1147
1012 1148
1013typedef struct SDI { int pos; double val;} DI; 1149typedef struct DI { int pos; double val;} DI;
1014 1150
1015int compare_doubles_i (const void *a, const void *b) { 1151int compare_doubles_i (const void *a, const void *b) {
1016 return ((DI*)a)->val > ((DI*)b)->val; 1152 return ((DI*)a)->val > ((DI*)b)->val;
@@ -1043,6 +1179,17 @@ int sort_indexI(KIVEC(v),IVEC(r)) {
1043} 1179}
1044 1180
1045 1181
1182typedef struct LI { int pos; int64_t val;} LI;
1183
1184int compare_longs_i (const void *a, const void *b) {
1185 return ((II*)a)->val > ((II*)b)->val;
1186}
1187
1188int sort_indexL(KLVEC(v),LVEC(r)) {
1189 SORTIDX_IMP(II,compare_longs_i)
1190}
1191
1192
1046//////////////////////////////////////////////////////////////////////////////// 1193////////////////////////////////////////////////////////////////////////////////
1047 1194
1048int round_vector(KDVEC(v),DVEC(r)) { 1195int round_vector(KDVEC(v),DVEC(r)) {
@@ -1088,6 +1235,44 @@ int range_vector(IVEC(r)) {
1088 OK 1235 OK
1089} 1236}
1090 1237
1238///////////////////////////
1239
1240
1241int round_vector_l(KDVEC(v),LVEC(r)) {
1242 int k;
1243 for(k=0; k<vn; k++) {
1244 rp[k] = round(vp[k]);
1245 }
1246 OK
1247}
1248
1249
1250int mod_vector_l(int64_t m, KLVEC(v), LVEC(r)) {
1251 int k;
1252 for(k=0; k<vn; k++) {
1253 rp[k] = vp[k] % m;
1254 }
1255 OK
1256}
1257
1258int div_vector_l(int64_t m, KLVEC(v), LVEC(r)) {
1259 int k;
1260 for(k=0; k<vn; k++) {
1261 rp[k] = vp[k] / m;
1262 }
1263 OK
1264}
1265
1266int range_vector_l(LVEC(r)) {
1267 int k;
1268 for(k=0; k<rn; k++) {
1269 rp[k] = k;
1270 }
1271 OK
1272}
1273
1274
1275
1091//////////////////// constant ///////////////////////// 1276//////////////////// constant /////////////////////////
1092 1277
1093int constantF(float * pval, FVEC(r)) { 1278int constantF(float * pval, FVEC(r)) {
@@ -1143,65 +1328,46 @@ int constantI(int * pval, IVEC(r)) {
1143} 1328}
1144 1329
1145 1330
1146//////////////////// float-double conversion /////////////////////////
1147 1331
1148int float2double(FVEC(x),DVEC(y)) { 1332int constantL(int64_t * pval, LVEC(r)) {
1149 DEBUGMSG("float2double") 1333 DEBUGMSG("constantL")
1150 int k; 1334 int k;
1151 for(k=0;k<xn;k++) { 1335 int64_t val = *pval;
1152 yp[k]=xp[k]; 1336 for(k=0;k<rn;k++) {
1337 rp[k]=val;
1153 } 1338 }
1154 OK 1339 OK
1155} 1340}
1156 1341
1157int float2int(KFVEC(x),IVEC(y)) {
1158 DEBUGMSG("float2int")
1159 int k;
1160 for(k=0;k<xn;k++) {
1161 yp[k]=xp[k];
1162 }
1163 OK
1164}
1165 1342
1343//////////////////// type conversions /////////////////////////
1166 1344
1167int double2float(DVEC(x),FVEC(y)) { 1345#define CONVERT_IMP { \
1168 DEBUGMSG("double2float") 1346 int k; \
1169 int k; 1347 for(k=0;k<xn;k++) { \
1170 for(k=0;k<xn;k++) { 1348 yp[k]=xp[k]; \
1171 yp[k]=xp[k]; 1349 } \
1172 } 1350 OK }
1173 OK
1174}
1175 1351
1352int float2double(FVEC(x),DVEC(y)) CONVERT_IMP
1176 1353
1177int double2int(KDVEC(x),IVEC(y)) { 1354int float2int(KFVEC(x),IVEC(y)) CONVERT_IMP
1178 DEBUGMSG("double2int")
1179 int k;
1180 for(k=0;k<xn;k++) {
1181 yp[k]=xp[k];
1182 }
1183 OK
1184}
1185 1355
1356int double2float(DVEC(x),FVEC(y)) CONVERT_IMP
1186 1357
1187int int2float(KIVEC(x),FVEC(y)) { 1358int double2int(KDVEC(x),IVEC(y)) CONVERT_IMP
1188 DEBUGMSG("int2float")
1189 int k;
1190 for(k=0;k<xn;k++) {
1191 yp[k]=xp[k];
1192 }
1193 OK
1194}
1195 1359
1360int double2long(KDVEC(x),LVEC(y)) CONVERT_IMP
1196 1361
1197int int2double(KIVEC(x),DVEC(y)) { 1362int int2float(KIVEC(x),FVEC(y)) CONVERT_IMP
1198 DEBUGMSG("int2double") 1363
1199 int k; 1364int int2double(KIVEC(x),DVEC(y)) CONVERT_IMP
1200 for(k=0;k<xn;k++) { 1365
1201 yp[k]=xp[k]; 1366int int2long(KIVEC(x),LVEC(y)) CONVERT_IMP
1202 } 1367
1203 OK 1368int lont2int(KLVEC(x),IVEC(y)) CONVERT_IMP
1204} 1369
1370int long2double(KLVEC(x),DVEC(y)) CONVERT_IMP
1205 1371
1206 1372
1207//////////////////// conjugate ///////////////////////// 1373//////////////////// conjugate /////////////////////////
@@ -1249,6 +1415,11 @@ int stepI(KIVEC(x),IVEC(y)) {
1249 STEP_IMP 1415 STEP_IMP
1250} 1416}
1251 1417
1418int stepL(KLVEC(x),LVEC(y)) {
1419 STEP_IMP
1420}
1421
1422
1252//////////////////// cond ///////////////////////// 1423//////////////////// cond /////////////////////////
1253 1424
1254#define COMPARE_IMP \ 1425#define COMPARE_IMP \
@@ -1272,26 +1443,10 @@ int compareI(KIVEC(x),KIVEC(y),IVEC(r)) {
1272 COMPARE_IMP 1443 COMPARE_IMP
1273} 1444}
1274 1445
1275 1446int compareL(KLVEC(x),KLVEC(y),IVEC(r)) {
1276#define COND_IMP \ 1447 COMPARE_IMP
1277 REQUIRES(xn==yn && xn==ltn && xn==eqn && xn==gtn && xn==rn ,BAD_SIZE); \
1278 int k; \
1279 for(k=0;k<xn;k++) { \
1280 rp[k] = xp[k]<yp[k]?ltp[k]:(xp[k]>yp[k]?gtp[k]:eqp[k]); \
1281 } \
1282 OK
1283
1284int condF(FVEC(x),FVEC(y),FVEC(lt),FVEC(eq),FVEC(gt),FVEC(r)) {
1285 COND_IMP
1286}
1287
1288int condD(DVEC(x),DVEC(y),DVEC(lt),DVEC(eq),DVEC(gt),DVEC(r)) {
1289 COND_IMP
1290} 1448}
1291 1449
1292int condI(KIVEC(x),KIVEC(y),KIVEC(lt),KIVEC(eq),KIVEC(gt),IVEC(r)) {
1293 COND_IMP
1294}
1295 1450
1296 1451
1297#define CHOOSE_IMP \ 1452#define CHOOSE_IMP \
@@ -1314,6 +1469,11 @@ int chooseI(KIVEC(cond),KIVEC(lt),KIVEC(eq),KIVEC(gt),IVEC(r)) {
1314 CHOOSE_IMP 1469 CHOOSE_IMP
1315} 1470}
1316 1471
1472int chooseL(KIVEC(cond),KLVEC(lt),KLVEC(eq),KLVEC(gt),LVEC(r)) {
1473 CHOOSE_IMP
1474}
1475
1476
1317int chooseC(KIVEC(cond),KCVEC(lt),KCVEC(eq),KCVEC(gt),CVEC(r)) { 1477int chooseC(KIVEC(cond),KCVEC(lt),KCVEC(eq),KCVEC(gt),CVEC(r)) {
1318 CHOOSE_IMP 1478 CHOOSE_IMP
1319} 1479}