1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659
|
/*
gbget (ver. 5.6) -- Basic data extraction and manipulation tool
Copyright (C) 2011-2018 Giulio Bottazzi
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
(version 2) as published by the Free Software Foundation;
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "tools.h"
#if defined HAVE_LIBGSL
#include "assert.h"
#endif
#if defined HAVE_LIBGSL
#include "matheval.h"
#endif
/* input management */
/*
parse_spec: parse the input line identifying the different pieces:
file name, block spec, slice spec and transformation
parse_slice: used to extract information about which blocks, columns
or rows to pick from the command line specification
new_matrix: build the new matrix from the read-in block, according
to columns and rows specs.
add_matrix: add the matrix to the output data
*/
void parse_spec(const char* specstring,char ** filename,char **blockslice,
char **columnslice,char **rowslice,char **transform){
char *piece=strdup (specstring);
size_t idx1,idx2;
char *pointer=piece;
/* initial checks */
if( (index(specstring,'[')!=NULL && index(specstring,']')==NULL )
|| (index(specstring,']')!=NULL && index(specstring,'[')==NULL ) ){
fprintf(stderr,"ERROR (%s): error in block specification\n",GB_PROGNAME);
exit(-1);
}
if( (index(specstring,'(')!=NULL && index(specstring,')')==NULL)
|| (index(specstring,')')!=NULL && index(specstring,'(')==NULL) ){
fprintf(stderr,"ERROR (%s): error in slice specification\n",GB_PROGNAME);
exit(-1);
}
/* filename */
free(*filename);
idx1=strcspn(pointer,"[(");
if(idx1==0)
*filename = strdup("");
else if(idx1<strlen(pointer))
*filename = my_strndup(pointer,idx1);
else
*filename = strdup(pointer);
/* fprintf(stderr,"file:-%s-\n",*filename); */
/* point after the filename */
pointer=piece+idx1;
/* blocks */
if(index(pointer,'[')!=NULL){
idx1=strcspn(pointer,"[");
idx2=strcspn(pointer+idx1,"]");
*blockslice = my_strndup(pointer+idx1+1,idx2-1);
}
else
*blockslice = strdup("");
/* fprintf(stderr,"block:-%s-\n",*blockslice); */
/* define slice specification */
if(index(pointer,'(')!=NULL){
char *stmp3;
idx1=strcspn(pointer,"(");
idx2=strcspn(pointer+idx1,")");
stmp3= my_strndup(pointer+idx1+1,idx2-1);
if(index(stmp3,',')!=NULL){
idx1=strcspn(stmp3,",");
*columnslice=my_strndup(stmp3,idx1);
*rowslice=strdup (stmp3+idx1+1);
}
else{
*columnslice = strdup(stmp3);
*rowslice=strdup ("");
}
free(stmp3);
}
else{
*columnslice=strdup ("");
*rowslice=strdup ("");
}
/* fprintf(stderr,"col:-%s-\n",*columnslice); */
/* fprintf(stderr,"row:-%s-\n",*rowslice); */
/* define trasformations */
idx1=0;
if(index(pointer,'(')!=NULL)
idx1=strcspn(pointer,")");
idx2=0;
if(index(pointer,'[')!=NULL)
idx2=strcspn(pointer,"]");
if(idx1==0 && idx2==0)
*transform = strdup("");
else{
if(idx2>idx1)
idx1=idx2;
*transform = strdup(pointer+idx1+1);
}
/* fprintf(stderr,"pointer:%s len:%d idx1:%d idx2:%d trasf:%s\n", */
/* pointer,strlen(pointer),idx1,idx2,*transform); */
free(piece);
/* exit(0); */
/* final checks */
if(strspn(*blockslice,"-1234567890:;")<strlen(*blockslice)){
fprintf(stderr,"ERROR (%s): wrong blocks specification: %s\n",GB_PROGNAME,*blockslice);
exit(-1);
}
if(strspn(*columnslice,"-1234567890:;")<strlen(*columnslice)){
fprintf(stderr,"ERROR (%s): wrong columns specification: %s\n",GB_PROGNAME,*columnslice);
exit(-1);
}
if(strspn(*rowslice,"-1234567890:;")<strlen(*rowslice)){
fprintf(stderr,"ERROR (%s): wrong rows specification: %s\n",GB_PROGNAME,*rowslice);
exit(-1);
}
}
/* split the spec according to ';' and build arrays of initial, final
and skip entries */
void parse_slice(const char * slice,const char * typename,const int max,
size_t * piecenum, size_t **indexini, size_t **indexfin,int **indexskip){
char *pieces=strdup (slice);
char *stmp1=pieces;
char *stmp2;
*piecenum=0;
/* split the ';' pieces */
while( (stmp2=strsep (&stmp1,";")) != NULL)
{
/* default values */
int ini=1;
int fin=max;
int skip=1;
/* increase the number of pieces */
(*piecenum)++;
if(strcmp(stmp2,"")!=0){
char *stmp3 = strdup(stmp2);
char *stmp4 = stmp3;
char *stmp5;
/* read ini and fin */
if( (stmp5=strsep (&stmp4,":")) != NULL && strlen(stmp5)>0 )
fin = ini = atoi(stmp5);
if( (stmp5=strsep (&stmp4,":")) != NULL){
if(strlen(stmp5)>0) fin = atoi(stmp5);
else fin=max;
}
/* treat negative elements */
if(ini<0) ini=max+ini+1;
if(fin<0) fin=max+fin+1;
/* eventually set skip */
if( (stmp5=strsep (&stmp4,",")) != NULL && strlen(stmp5)>0 )
skip= atoi(stmp5);
else if(ini>fin) skip=-1;
free(stmp3);
/*check the boundaries and skip*/
if(ini<1){
fprintf(stderr,
"WARNING (%s): requested %s %d out of range: set to %d\n",GB_PROGNAME,typename,ini,1);
ini=1;
}
else if(ini>max){
fprintf(stderr,"WARNING (%s): requested %s %d out of range: set to %d\n",GB_PROGNAME,typename,ini,max);
ini=max;
}
if(fin<1){
fprintf(stderr,"WARNING (%s): requested %s %d out of range: set to %d\n",GB_PROGNAME,typename,fin,1);
fin=1;
}
else if(fin>max){
fprintf(stderr,"WARNING (%s): requested %s %d out of range: set to %d\n",GB_PROGNAME,typename,fin,max);
fin=max;
}
if( ( (ini > fin) && (skip>0) ) || ((ini < fin) && (skip<0)) ){
fprintf(stderr,"WARNING (%s): requested %s out of range: increment reversed\n",
GB_PROGNAME,typename);
skip=-skip;
}
}
/* assign values */
*indexini = (size_t *) my_realloc((void *) *indexini,(*piecenum)*sizeof(size_t));
*indexfin = (size_t *) my_realloc((void *) *indexfin,(*piecenum)*sizeof(size_t));
*indexskip = (int *) my_realloc((void *) *indexskip,(*piecenum)*sizeof(int));
(*indexini)[(*piecenum)-1]=ini;
(*indexfin)[(*piecenum)-1]=fin;
(*indexskip)[(*piecenum)-1]=skip;
}
free(pieces);
}
void new_matrix(double ***newdata,size_t *columnlength,size_t *rowlength,
double ** const vals,const size_t columns,const size_t rows,
const char *columnslice,const char *rowslice){
size_t i;
size_t cslicenum=0;
size_t *cini=NULL;
size_t *cfin=NULL;
int *cskip=NULL;
size_t rslicenum=0;
size_t *rini=NULL;
size_t *rfin=NULL;
int *rskip=NULL;
/* read the specs */
parse_slice(columnslice,"column",columns,&cslicenum,&cini,&cfin,&cskip);
parse_slice(rowslice,"row",rows,&rslicenum,&rini,&rfin,&rskip);
/* decide new matrix dimensions */
*columnlength=0;
for(i=0;i<cslicenum;i++)
*columnlength +=
(cfin[i]>cini[i] ? cfin[i]-cini[i] : cini[i]-cfin[i])/(cskip[i]>0?cskip[i]:-cskip[i])+1;
*rowlength=0;
for(i=0;i<rslicenum;i++)
*rowlength +=
(rfin[i]>rini[i] ? rfin[i]-rini[i] : rini[i]-rfin[i])/(rskip[i]>0?rskip[i]:-rskip[i])+1;
/* for(i=0;i<cslicenum;i++) */
/* fprintf(stderr,"col[%zd] %zd:%zd:%d\n",i,cini[i],cfin[i],cskip[i]); */
/* for(i=0;i<rslicenum;i++) */
/* fprintf(stderr,"row[%zd] %zd:%zd:%d\n",i,rini[i],rfin[i],rskip[i]); */
/* fprintf(stderr,"clen=%zd rlen=%zd\n",*columnlength,*rowlength); */
/* build the new matrix */
{
size_t cs,rs;
int c,r;
size_t column=0,row=0;
/* allocate the new matrix */
/* if(*newdata != NULL) free(*newdata); */
double **data = (double **) my_alloc((*columnlength)*sizeof(double *));
for(i=0;i<*columnlength;i++)
data[i] = (double *) my_alloc((*rowlength)*sizeof(double));
column=0;
for(cs=0;cs<cslicenum;cs++){
for(c=cini[cs]-1; ( cskip[cs]>0 ? c < cfin[cs] : c+2 > cfin[cs] ) ;c+=cskip[cs]){
row=0;
for(rs=0;rs<rslicenum;rs++){
for(r=rini[rs]-1; ( rskip[rs]>0 ? r < rfin[rs] : r+2 > rfin[rs] ) ;r+=rskip[rs]){
/* fprintf(stderr,"val[%zd][%zd]=%f -> ", */
/* c,r,vals[c][r]); */
data[column][row]=vals[c][r];
/* fprintf(stderr,"data[%zd][%zd]=%f\n", */
/* column,row,data[column][row]); */
row++;
}
}
column++;
}
}
*newdata=data;
}
free(cini);
free(cfin);
free(cskip);
free(rini);
free(rfin);
free(rskip);
}
void data_transpose(double *** vals,size_t *rows,size_t *columns){
size_t i,j;
/* allocate space */
double **newvals = (double **) my_alloc((*rows)*sizeof(double *));
for(i=0;i<*rows;i++){
newvals[i] = (double *) my_alloc((*columns)*sizeof(double));
}
/* fill space */
for(i=0;i<*rows;i++)
for(j=0;j<*columns;j++)
newvals[i][j] = (*vals)[j][i];
/* deallocate old space */
if(*columns>0)
for(i=0;i<*columns;i++)
free((*vals)[i]);
free(*vals);
*vals = newvals;
/* swap axes */
j=*columns;
*columns=*rows;
*rows=j;
}
void data_flat(double *** vals,size_t *rows,size_t *columns){
size_t i,j,index;
/* allocate space */
double **newvals = (double **) my_alloc(sizeof(double *));
newvals[0] = (double *) my_alloc((*columns)*(*rows)*sizeof(double));
/* fill space */
index=0;
for(i=0;i<*columns;i++)
for(j=0;j<*rows;j++)
newvals[0][index++] = (*vals)[i][j];
/* deallocate old space */
for(i=0;i<*columns;i++)
free((*vals)[i]);
free(*vals);
*vals = newvals;
/* new axes */
*rows=(*columns)*(*rows);
*columns=1;
}
void data_diff(double *** vals,size_t *rows,size_t *columns){
size_t i,j;
/* allocate space */
double **newvals = (double **) my_alloc((*columns-1)*sizeof(double *));
for(i=0;i<*columns-1;i++)
newvals[i] = (double *) my_alloc((*rows)*sizeof(double));
/* fill space */
for(i=0;i<*columns-1;i++)
for(j=0;j<*rows;j++)
newvals[i][j] = (*vals)[i+1][j]-(*vals)[i][j];
/* deallocate old space */
if(*columns>0)
for(i=0;i<*columns;i++)
free((*vals)[i]);
free(*vals);
*vals = newvals;
/* new dimensions */
*columns=*columns-1;
}
#if defined HAVE_LIBGSL
/* handy structure used in computation */
typedef struct function {
void *f;
size_t argnum;
/* char *args[] = { "x" }; */
char **args; /* list of arguments names */
size_t *indeces; /* the index (shift) for each variable */
size_t *lags; /* the lag for each variable */
double *values; /* values used in computation */
size_t maxindex; /* the largest index */
size_t maxlag; /* the largest lag */
} Function;
void data_applyfuns(double *** myvals,size_t *myrows,size_t *mycolumns,const char *funslist){
Function *F=NULL;
size_t Fnum=0;
char *funspecs=strdup(funslist);
char *specstart=funspecs,*funspec;
/* traslate to more handy names */
double **data=*myvals;
size_t columns=*mycolumns;
size_t rows=*myrows;
/* fprintf(stderr,"func spec=%s\n",funspecs); */
/* parse line for functions specification */
while( (funspec=strsep (&funspecs,";")) != NULL ){
char *piece=strdup (funspec);
char *stmp1=piece;
char *stmp2;
while( (stmp2=strsep (&stmp1,",")) != NULL ){/*take a piece*/
size_t j;
char *stmp3 = strdup(stmp2);
/* fprintf(stderr,"token:%s\n",stmp3); */
/* allocate new function */
Fnum++;
F=(Function *) my_realloc((void *) F,Fnum*sizeof(Function));
/* generate the formal expression */
F[Fnum-1].f = evaluator_create (stmp3);
assert(F[Fnum-1].f);
free(stmp3);
/* retrive list of argument */
{
/* hack necessary due to libmatheval propotypes */
int argnum;
evaluator_get_variables (F[Fnum-1].f, &(F[Fnum-1].args), &argnum);
F[Fnum-1].argnum = (size_t) argnum;
}
/* compute list of indeces */
F[Fnum-1].indeces = (size_t *) my_alloc(F[Fnum-1].argnum*sizeof(size_t));
F[Fnum-1].lags = (size_t *) my_alloc(F[Fnum-1].argnum*sizeof(size_t));
F[Fnum-1].maxindex=0;
F[Fnum-1].maxlag=0;
for(j=0;j<F[Fnum-1].argnum;j++){
stmp3 = (F[Fnum-1].args)[j];
if(*stmp3 != 'x'){
fprintf(stderr,"ERROR (%s): variable name %s should begin with x\n",
GB_PROGNAME,stmp3);
exit(-1);
}
if( *(stmp3+1) == '\0' ){
(F[Fnum-1].indeces)[j] = 1;
(F[Fnum-1].lags)[j] = 0;
}
else {
char *endptr;
if( strchr(stmp3,'l') == NULL ){
(F[Fnum-1].indeces)[j] = strtol(stmp3+1,&endptr,10);
(F[Fnum-1].lags)[j] = 0;
if(*endptr != '\0'){
fprintf(stderr,"ERROR (%s): variable name %s must be of the form x#l#\n",
GB_PROGNAME,stmp3);
exit(-1);
}
}
else{
(F[Fnum-1].indeces)[j] = strtol(stmp3+1,&endptr,10);
(F[Fnum-1].lags)[j] = strtol(endptr+1,&endptr,10);
if(*endptr != '\0'){
fprintf(stderr,"ERROR (%s): variable name %s must be of the form x#l#\n",
GB_PROGNAME,stmp3);
exit(-1);
}
}
}
/* possibly update the value of the maximum index */
if( (F[Fnum-1].indeces)[j] > F[Fnum-1].maxindex)
F[Fnum-1].maxindex = (F[Fnum-1].indeces)[j];
/* possibly update the value of the maximum lag */
if( (F[Fnum-1].lags)[j] > F[Fnum-1].maxlag)
F[Fnum-1].maxlag = (F[Fnum-1].lags)[j];
/* check index */
if((F[Fnum-1].indeces)[j]> columns){
fprintf(stderr,"ERROR (%s): column %zd not present in data\n",
GB_PROGNAME,(F[Fnum-1].indeces)[j]);
exit(-1);
}
/* check lag */
if((F[Fnum-1].lags)[j]> rows){
fprintf(stderr,"ERROR (%s): lag %zd is longer then the data length\n",
GB_PROGNAME,(F[Fnum-1].lags)[j]);
exit(-1);
}
}
/* allocate list of values */
F[Fnum-1].values = (double *) my_alloc(F[Fnum-1].argnum*sizeof(double));
}
free(piece);
}
free(specstart);
/* generate new matrix */
{
/* placeholder for new data */
double **newvals;
size_t i,j,h;
/* double **output = (double **) my_alloc(Fnum*sizeof(double *)); */
/* for(i=0;i<Fnum;i++) output[i] = (double *) my_alloc(rows*sizeof(double)); */
size_t firstrow=0;
/* compute the first row to print */
for(j=0;j<Fnum;j++)
if(F[j].maxlag>firstrow) firstrow=F[j].maxlag;
/* allocate space */
newvals = (double **) my_alloc(Fnum*sizeof(double *));
for(i=0;i<Fnum;i++)
newvals[i] = (double *) my_alloc((rows-firstrow)*sizeof(double));
/* fill with new values */
for(i=firstrow;i<rows;i++){
for(j=0;j<Fnum;j++){
for(h=0;h<F[j].argnum;h++)
(F[j].values)[h] = (F[j].indeces[h]>0 ? data[F[j].indeces[h]-1][i-F[j].lags[h]] : i+1-F[j].lags[h]);
newvals[j][i-firstrow]=evaluator_evaluate (F[j].f,F[j].argnum,F[j].args,F[j].values);
}
}
/* free previous allocated space */
for(i=0;i<*mycolumns;i++)
free( (*myvals)[i]);
free((*myvals));
/* return newly allocated matrix */
*mycolumns=Fnum;
*myrows=rows-firstrow;
*myvals = newvals;
}
}
void data_applyfuns_recursive(double *** myvals,size_t *myrows,size_t *mycolumns,const char *funslist){
Function *F=NULL;
size_t Fnum=0;
char *funspecs=strdup(funslist);
char *specstart=funspecs,*funspec;
/* traslate to more handy names */
double **data=*myvals;
size_t columns=*mycolumns;
size_t rows=*myrows;
/* fprintf(stderr,"func spec=%s\n",funspecs); */
/* parse line for functions specification */
while( (funspec=strsep (&funspecs,";")) != NULL ){
char *piece=strdup (funspec);
char *stmp1=piece;
char *stmp2;
while( (stmp2=strsep (&stmp1,",")) != NULL ){/*take a piece*/
size_t j;
char *stmp3 = strdup(stmp2);
/* fprintf(stderr,"token:%s\n",stmp3); */
/* allocate new function */
Fnum++;
F=(Function *) my_realloc((void *) F,Fnum*sizeof(Function));
/* generate the formal expression */
F[Fnum-1].f = evaluator_create (stmp3);
assert(F[Fnum-1].f);
free(stmp3);
/* retrive list of argument */
{
/* hack necessary due to libmatheval propotypes */
int argnum;
evaluator_get_variables (F[Fnum-1].f, &(F[Fnum-1].args), &argnum);
F[Fnum-1].argnum = (size_t) argnum;
}
/* compute list of indeces */
F[Fnum-1].indeces = (size_t *) my_alloc(F[Fnum-1].argnum*sizeof(size_t));
F[Fnum-1].lags = (size_t *) my_alloc(F[Fnum-1].argnum*sizeof(size_t));
F[Fnum-1].maxindex=0;
F[Fnum-1].maxlag=0;
for(j=0;j<F[Fnum-1].argnum;j++){
stmp3 = (F[Fnum-1].args)[j];
if(*stmp3 != 'x'){
fprintf(stderr,"ERROR (%s): variable name %s should begin with x\n",
GB_PROGNAME,stmp3);
exit(-1);
}
if( *(stmp3+1) == '\0' ){
(F[Fnum-1].indeces)[j] = 1;
(F[Fnum-1].lags)[j] = 0;
}
else {
char *endptr;
if( strchr(stmp3,'l') == NULL ){
(F[Fnum-1].indeces)[j] = strtol(stmp3+1,&endptr,10);
(F[Fnum-1].lags)[j] = 0;
if(*endptr != '\0'){
fprintf(stderr,"ERROR (%s): variable name %s must be of the form x#l#\n",
GB_PROGNAME,stmp3);
exit(-1);
}
}
else{
(F[Fnum-1].indeces)[j] = strtol(stmp3+1,&endptr,10);
(F[Fnum-1].lags)[j] = strtol(endptr+1,&endptr,10);
if(*endptr != '\0'){
fprintf(stderr,"ERROR (%s): variable name %s must be of the form x#l#\n",
GB_PROGNAME,stmp3);
exit(-1);
}
}
}
/* possibly update the value of the maximum index */
if( (F[Fnum-1].indeces)[j] > F[Fnum-1].maxindex)
F[Fnum-1].maxindex = (F[Fnum-1].indeces)[j];
/* possibly update the value of the maximum lag */
if( (F[Fnum-1].lags)[j] > F[Fnum-1].maxlag)
F[Fnum-1].maxlag = (F[Fnum-1].lags)[j];
/* check index */
if((F[Fnum-1].indeces)[j]> columns){
fprintf(stderr,"ERROR (%s): column %zd not present in data\n",
GB_PROGNAME,(F[Fnum-1].indeces)[j]);
exit(-1);
}
/* check lag */
if((F[Fnum-1].lags)[j]> rows){
fprintf(stderr,"ERROR (%s): lag %zd is longer then the data length\n",
GB_PROGNAME,(F[Fnum-1].lags)[j]);
exit(-1);
}
}
/* allocate list of values */
F[Fnum-1].values = (double *) my_alloc(F[Fnum-1].argnum*sizeof(double));
}
free(piece);
}
free(specstart);
/* generate new matrix */
{
/* placeholder for new data */
double **newvals;
size_t i,j,l,h;
/* double **output = (double **) my_alloc(Fnum*sizeof(double *)); */
/* for(i=0;i<Fnum;i++) output[i] = (double *) my_alloc(rows*sizeof(double)); */
size_t firstrow=0;
size_t totalcols=0;
/* compute the first row to print */
for(j=0;j<Fnum;j++)
if(F[j].maxlag>firstrow) firstrow=F[j].maxlag;
/* compute the total number of columns */
for(j=0;j<Fnum;j++)
totalcols += columns-F[j].maxindex+1;
/* allocate space */
newvals = (double **) my_alloc(totalcols*sizeof(double *));
for(i=0;i<totalcols;i++)
newvals[i] = (double *) my_alloc((rows-firstrow)*sizeof(double));
/* fill with new values */
for(i=firstrow;i<rows;i++){
size_t col=0;
for(j=0;j<Fnum;j++){
for(l=0;l<columns-F[j].maxindex+1;l++){
for(h=0;h<F[j].argnum;h++)
(F[j].values)[h] = (F[j].indeces[h]>0 ? data[l+F[j].indeces[h]-1][i-F[j].lags[h]] : i+1-F[j].lags[h]);
newvals[col+l][i-firstrow]=evaluator_evaluate (F[j].f,F[j].argnum,F[j].args,F[j].values);
}
col+=columns-F[j].maxindex+1;
}
}
/* free previous allocated space */
for(i=0;i<*mycolumns;i++)
free( (*myvals)[i]);
free((*myvals));
/* allocate new space */
*mycolumns=totalcols;
*myrows=rows-firstrow;
*myvals=newvals;
}
}
void data_applyselection(double *** myvals,size_t *myrows,size_t *mycolumns,const char *funslist){
Function *F=NULL;
size_t Fnum=0;
char *funspecs=strdup(funslist);
char *specstart=funspecs,*funspec;
/* traslate to more handy names */
double **data=*myvals;
size_t columns=*mycolumns;
size_t rows=*myrows;
/* fprintf(stderr,"func spec=%s\n",funspecs); */
/* parse line for functions specification */
while( (funspec=strsep (&funspecs,";")) != NULL ){
char *piece=strdup (funspec);
char *stmp1=piece;
char *stmp2;
while( (stmp2=strsep (&stmp1,",")) != NULL ){/*take a piece*/
size_t j;
char *stmp3 = strdup(stmp2);
/* fprintf(stderr,"token:%s\n",stmp3); */
/* allocate new function */
Fnum++;
F=(Function *) my_realloc((void *) F,Fnum*sizeof(Function));
/* generate the formal expression */
F[Fnum-1].f = evaluator_create (stmp3);
assert(F[Fnum-1].f);
free(stmp3);
/* retrive list of argument */
{
/* hack necessary due to libmatheval propotypes */
int argnum;
evaluator_get_variables (F[Fnum-1].f, &(F[Fnum-1].args), &argnum);
F[Fnum-1].argnum = (size_t) argnum;
}
/* compute list of indeces */
F[Fnum-1].indeces = (size_t *) my_alloc(F[Fnum-1].argnum*sizeof(size_t));
F[Fnum-1].lags = (size_t *) my_alloc(F[Fnum-1].argnum*sizeof(size_t));
F[Fnum-1].maxindex=0;
F[Fnum-1].maxlag=0;
for(j=0;j<F[Fnum-1].argnum;j++){
stmp3 = (F[Fnum-1].args)[j];
if(*stmp3 != 'x'){
fprintf(stderr,"ERROR (%s): variable name %s should begin with x\n",
GB_PROGNAME,stmp3);
exit(-1);
}
if( *(stmp3+1) == '\0' ){
(F[Fnum-1].indeces)[j] = 1;
(F[Fnum-1].lags)[j] = 0;
}
else {
char *endptr;
if( strchr(stmp3,'l') == NULL ){
(F[Fnum-1].indeces)[j] = strtol(stmp3+1,&endptr,10);
(F[Fnum-1].lags)[j] = 0;
if(*endptr != '\0'){
fprintf(stderr,"ERROR (%s): variable name %s must be of the form x#l#\n",
GB_PROGNAME,stmp3);
exit(-1);
}
}
else{
(F[Fnum-1].indeces)[j] = strtol(stmp3+1,&endptr,10);
(F[Fnum-1].lags)[j] = strtol(endptr+1,&endptr,10);
if(*endptr != '\0'){
fprintf(stderr,"ERROR (%s): variable name %s must be of the form x#l#\n",
GB_PROGNAME,stmp3);
exit(-1);
}
}
}
/* possibly update the value of the maximum index */
if( (F[Fnum-1].indeces)[j] > F[Fnum-1].maxindex)
F[Fnum-1].maxindex = (F[Fnum-1].indeces)[j];
/* possibly update the value of the maximum lag */
if( (F[Fnum-1].lags)[j] > F[Fnum-1].maxlag)
F[Fnum-1].maxlag = (F[Fnum-1].lags)[j];
/* check index */
if((F[Fnum-1].indeces)[j]> columns){
fprintf(stderr,"ERROR (%s): column %zd not present in data\n",
GB_PROGNAME,(F[Fnum-1].indeces)[j]);
exit(-1);
}
/* check lag */
if((F[Fnum-1].lags)[j]> rows){
fprintf(stderr,"ERROR (%s): lag %zd is longer then the data length\n",
GB_PROGNAME,(F[Fnum-1].lags)[j]);
exit(-1);
}
}
/* allocate list of values */
F[Fnum-1].values = (double *) my_alloc(F[Fnum-1].argnum*sizeof(double));
}
free(piece);
}
free(specstart);
/* generate new matrix */
{
/* placeholder for new data */
double **newvals;
size_t newrows;
size_t i,j,h;
/* double **output = (double **) my_alloc(Fnum*sizeof(double *)); */
/* for(i=0;i<Fnum;i++) output[i] = (double *) my_alloc(rows*sizeof(double)); */
size_t firstrow=0;
/* compute the first row to print */
for(j=0;j<Fnum;j++)
if(F[j].maxlag>firstrow) firstrow=F[j].maxlag;
/* allocate space */
newvals = (double **) my_alloc(columns*sizeof(double *));
for(i=0;i<columns;i++)
newvals[i] = (double *) my_alloc((rows-firstrow)*sizeof(double));
/* fill with new values */
newrows=0;
for(i=firstrow;i<rows;i++){
char valid=0;
for(j=0;j<Fnum;j++){
for(h=0;h<F[j].argnum;h++)
(F[j].values)[h] = (F[j].indeces[h]>0 ? data[F[j].indeces[h]-1][i-F[j].lags[h]] : i+1);
if(evaluator_evaluate (F[j].f,F[j].argnum,F[j].args,F[j].values) >= 0)
valid=1;
}
/* if passed all checks, add it */
if(valid==1){
for(j=0;j<columns;j++)
newvals[j][newrows]=data[j][i];
newrows++;
}
}
/* fprintf(stderr,"newrows %zd\n",newrows); */
/* free previous allocated space */
for(i=0;i<*mycolumns;i++)
free( (*myvals)[i]);
free((*myvals));
/* return newly allocated matrix */
*mycolumns=columns;
*myrows=newrows;
*myvals = newvals;
}
}
#endif
void matrix_transform(double ***matrix,size_t *columnlength,size_t *rowlength,
const char *transform,const int o_verbose){
size_t itmp1;
for(itmp1=0;itmp1<strlen(transform);itmp1++){
switch(transform[itmp1]){
case 't':
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr," %zdx%zd\t[->tran]\t",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
data_transpose(matrix,rowlength,columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr,"%zdx%zd\n",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
break;
case 'f':
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr," %zdx%zd\t[->flat]\t",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
data_flat(matrix,rowlength,columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr,"%zdx%zd\n",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
break;
case 'l':
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr," %zdx%zd\t[-> log]\t",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
{
size_t i,j;
for(i=0;i<*columnlength;i++)
for(j=0;j<*rowlength;j++)
(*matrix)[i][j]=log((*matrix)[i][j]);
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr,"%zdx%zd\n",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
break;
case 'd':
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr," %zdx%zd\t[->diff]\t",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
data_diff(matrix,rowlength,columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr,"%zdx%zd\n",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
break;
case 'D':
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr," %zdx%zd\t[->denan]\t",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
denan_data(matrix,rowlength,columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr,"%zdx%zd\n",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
break;
case 'w':
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr," %zdx%zd\t[->norm]\t",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
{
size_t i,j;
for(i=0;i<*columnlength;i++){
double sum=0;
for(j=0;j<*rowlength;j++)
if(finite( (*matrix)[i][j] ))
sum += (*matrix)[i][j];
for(j=0;j<*rowlength;j++)
(*matrix)[i][j]/=sum;
}
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr,"%zdx%zd\n",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
break;
case 'z':
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr," %zdx%zd\t[->detr]\t",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
{
size_t i,j;
for(i=0;i<*columnlength;i++){
double mean=0;
size_t num=0;
for(j=0;j<*rowlength;j++)
if(finite( (*matrix)[i][j] )){
mean += (*matrix)[i][j];
num++;
}
mean /= num;
for(j=0;j<*rowlength;j++)
(*matrix)[i][j]-=mean;
}
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr,"%zdx%zd\n",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
break;
case 'Z':
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr," %zdx%zd\t[->zscr]\t",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
{
size_t i,j;
for(i=0;i<*columnlength;i++){
double mean=0,stdev=0,error=0,dtmp1;
size_t num=0;
for(j=0;j<*rowlength;j++)
if(finite( (*matrix)[i][j] ))
{
mean += (*matrix)[i][j];
num++;
}
mean /= num;
for(j=0;j<*rowlength;j++)
if(finite( (*matrix)[i][j] ))
{
error += ( dtmp1= (*matrix)[i][j] - mean);
stdev += dtmp1*dtmp1;
}
stdev = sqrt( (stdev-error*error/num)/(num-1));
for(j=0;j<*rowlength;j++)
(*matrix)[i][j]=((*matrix)[i][j]-mean)/stdev;
}
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr,"%zdx%zd\n",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
break;
case 'P':
/* ignored; the data will be printed at the end of this slice */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr," %zdx%zd\t[print ]\t",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr,"%zdx%zd\n",*rowlength,*columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
break;
#if defined HAVE_LIBGSL
case '<':
{
size_t i;
char *funslist;
/* select the list of functions specification */
for (i=itmp1;i<strlen(transform) && transform[i] != '>';i++);
funslist=my_strndup(transform+itmp1+1,i-itmp1-1);
/* apply functions */
if(funslist[0]=='@'){ /* recursive application */
free(funslist);
funslist=my_strndup(transform+itmp1+2,i-itmp1-2);
data_applyfuns_recursive(matrix,rowlength,columnlength,funslist);
}
else /* non-recursive application */
data_applyfuns(matrix,rowlength,columnlength,funslist);
/* update the index */
itmp1=i;
}
break;
case '{':
{
size_t i;
char *funslist;
/* select the list of functions specification */
for (i=itmp1;i<strlen(transform) && transform[i] != '}';i++);
funslist=my_strndup(transform+itmp1+1,i-itmp1-1);
/* apply selection */
data_applyselection(matrix,rowlength,columnlength,funslist);
/* update the index */
itmp1=i;
}
break;
#endif
default:
fprintf(stderr,
"WARNING (%s): unknown transformation \"%c\"; request ignored\n",
GB_PROGNAME,transform[itmp1]);
}
}
}
void add_matrix(double ***data, size_t **length, size_t *datanum,
double **newdata,size_t columnlength,size_t rowlength){
(*data)= (double **) my_realloc((void **) (*data),(*datanum+columnlength)*sizeof(double *));
(*length)= (size_t *) my_realloc((void *) (*length),(*datanum+columnlength)*sizeof(size_t));
{
size_t i;
for(i=0;i<columnlength;i++){
(*data)[*datanum]=newdata[i];
(*length)[*datanum]=rowlength;
(*datanum)++;
}
}
}
/* check if the data set is a matrix */
int isamatrix(size_t datanum,size_t *length){
size_t i,min,max;
if(datanum==0)
return 1;
min=max=length[0];
for(i=1;i<datanum;i++){
if(length[i]>max) max = length[i];
if(length[i]<min) min = length[i];
}
if(max==min)
return 1; /* true */
else
return 0; /* false */
}
void print_data(size_t *mydatanum, size_t **mylength, double ***mydata,
const char* finaltransform, const int o_verbose){
size_t i;
size_t datanum=*mydatanum;
double **data=*mydata;
size_t *length=*mylength;
if(datanum==0) return ;
/*OUTPUT*/
if(isamatrix(datanum,length)==1){/* final output has equally long columns */
size_t rowlength = length[0];
/* apply final transformations */
if(strlen(finaltransform)>0)
matrix_transform(&data,&datanum,&rowlength,
finaltransform,o_verbose);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr,"output => %zdx%zd\n",rowlength,datanum);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* print the result */
PRINTMATRIXBYCOLS(stdout,data,rowlength,datanum);
}
else{/* final output has columns of different lengths */
/* can't apply final transformations */
if(strlen(finaltransform)>0)
fprintf(stderr,
"WARNING (%s): cannot transform final output; request ignored\n",GB_PROGNAME);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1){
fprintf(stderr,"output (%zd columns of size) =>",datanum);
for(i=0;i<datanum;i++)
fprintf(stderr," %zd;",length[i]);
fprintf(stderr,"\n");
}
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
PRINTNOMATRIXBYCOLS(stdout,data,length,datanum);
}
/* free printed data */
free(*mylength);
*mylength=NULL;
if(datanum>0) for(i=0;i<datanum;i++) free(data[i]);
*mydatanum = 0;
free(data);
*mydata=NULL;
}
int main(int argc, char * argv[]){
/* actual input data array and its dimensions */
double ***vals=NULL;
size_t blocks=0;
size_t *rows=NULL,*columns=NULL;
/* actual input specification */
char *filename=strdup("");
char *old_filename=strdup("");
char *splitstring=strdup(" \t");
/* list of specs */
char **specs=NULL;
size_t specsnum;
size_t spec;
/* output data arrays */
double **data=NULL;
size_t datanum = 0;
size_t *length=NULL;
/* final transformations */
char *finaltransform=strdup("");
/* OPTIONS */
int o_binary_in=0;
int o_binary_out=0;
int o_verbose=0;
/* ------------------------------------------ */
int opt;
/* ------------------------------------------ */
/* COMMAND LINE PROCESSING */
while((opt=getopt_long(argc,argv,"vhF:e:o:s:t:b:",gb_long_options, &gb_option_index))!=EOF){
if(opt==0){
gbutils_header(argv[0],stdout);
exit(0);
}
else if(opt=='?'){
fprintf(stderr,"option %c not recognized\n",optopt);
exit(-1);
}
else if(opt=='v'){
/*increase verbosity*/
o_verbose=1;
}
else if(opt=='b'){
/*set binary input or output*/
switch(atoi(optarg)){
case 0 :
break;
case 1 :
o_binary_in=1;
break;
case 2 :
o_binary_out=1;
PRINTMATRIXBYCOLS=printmatrixbycols_bin;
PRINTNOMATRIXBYCOLS=printnomatrixbycols_bin;
break;
case 3 :
o_binary_in=1;
o_binary_out=1;
PRINTMATRIXBYCOLS=printmatrixbycols_bin;
PRINTNOMATRIXBYCOLS=printnomatrixbycols_bin;
break;
default:
fprintf(stderr,"unclear binary specification %d, option ignored.\n",atoi(optarg));
break;
}
}
else if(opt=='h'){
/*print help*/
fprintf(stdout,"Print slices of tabular data from files and apply transformations. Data\n");
fprintf(stdout,"are read from text files with fields separated by space (use option -F\n");
fprintf(stdout,"to specify a different separator). Inside data file, data-blocks are \n");
fprintf(stdout,"separated by two empty lines. File can be compressed with zlib (.gz).\n");
fprintf(stdout,"Usage: %s [options] 'filename[index](C,R)trans' \n\n",argv[0]);
fprintf(stdout," filename is the input file. If not specified it default to stdin or \n");
fprintf(stdout," the last specified filename if any.\n");
fprintf(stdout," index stands for a data-block index. \n");
fprintf(stdout," index stands for a data-block index. \n");
fprintf(stdout," C,R stands for columns and rows spec given as \"min:max:skip\" to\n");
fprintf(stdout," select from \"min\" to \"max\" every \"skip\" steps. If negative \n");
fprintf(stdout," min and max are counted from the end. By default all data \n");
fprintf(stdout," are printed (\"1:-1:1\"). If min>max then count is reversed \n");
fprintf(stdout," and skip must be negative (-1 by default). Different specs \n");
fprintf(stdout," are separated by semicolon ';' and considered sequentially. \n");
fprintf(stdout," trans is a list of transformations applied to selected data: 'd' \n");
fprintf(stdout," take the diff of subsequent columns; 'D' remove all rows with\n");
fprintf(stdout," at least one Not-A-Number (NAN) entry; 'f' flatten the output\n");
fprintf(stdout," piling all columns; 'l' take log of all entries, 'P' print all\n");
fprintf(stdout," entries collected as a data-block; 't' transpose the matrix \n");
fprintf(stdout," of data; 'z' subtract from the entries in each column their \n");
fprintf(stdout," mean; 'Z' replace the entry in each column with their zscore;\n");
fprintf(stdout," 'w' divide the entry in each columns by their mean. \n\n");
fprintf(stdout," '<..;..>' functions separated by semicolons in angle brackets\n");
fprintf(stdout," can be used for generic data transformation; the function is \n");
fprintf(stdout," computed for each row of data. Variables names are 'x' followed\n");
fprintf(stdout," by the number of the column and optionally by 'l' and the number\n");
fprintf(stdout," of lags. For instance 'x2+x3l1' means the sum of the entries in\n");
fprintf(stdout," the 2nd column plus the entries in the 3rd column in the previous\n");
fprintf(stdout," row. 'x0' stands for the row number and 'x' is equal to 'x1'\n\n");
fprintf(stdout," '<@..;..>' if the functions specification starts with a '@' the\n");
fprintf(stdout," functions are computed recursively along the columns. In this\n");
fprintf(stdout," case the number after the 'x' is the relative column counted \n");
fprintf(stdout," starting from the one considered at each step. \n\n");
fprintf(stdout," '{...}' a function in curly brackets can be use to select data:\n");
fprintf(stdout," only rows that return a non-negative value are retained \n");
fprintf(stdout,"Options: \n");
fprintf(stdout," -F set the input fields separators (default ' \\t') \n");
fprintf(stdout," -o set the output format (default '%%12.6e') \n");
fprintf(stdout," -e set the output format for empty fields (default '%%13s') \n");
fprintf(stdout," -s set the output separation string (default ' ') \n");
fprintf(stdout," -t define global transformations applied before each output (default '')\n");
fprintf(stdout," -v verbose mode\n");
fprintf(stdout,"Examples:\n");
fprintf(stdout," gbget 'file(1:3)ld' select the first three columns in 'file', take the\n");
fprintf(stdout," log and the difference of successive columns;\n");
fprintf(stdout," gbget 'file(2,-10:-1) <x^2> select the last ten elements of the second'\n");
fprintf(stdout," of 'file' and print their squares\n");
fprintf(stdout," gbget '[2]()' '[1]()' < ... select the second and first data block from the\n");
fprintf(stdout," standard input.\n");
fprintf(stdout," gbget 'file(1:3)<x1*x2-x3>' select the first three columns in 'file' and\n");
fprintf(stdout," in each row multiply the first two entries and.\n");
fprintf(stdout," subtract the third.\n");
fprintf(stdout," gbget 'file()<@x1+x2>' print the sum of two subsequent columns\n");
fprintf(stdout," gbget 'file(1:3){x2-2}' select the first three columns in 'file' for the\n");
fprintf(stdout," rows whose second field is not lower then 2 \n");
exit(0);
}
else if(opt=='F'){
/*set the input fields separator string*/
free(splitstring);
splitstring = strdup(optarg);
}
else if(opt=='e'){
/*set the format for empty fields*/
EMPTY = strdup(optarg);
}
else if(opt=='o'){
/*set the output string */
FLOAT = strdup(optarg);
}
else if(opt=='s'){
/*set the separator*/
SEP = strdup(optarg);
}
else if(opt=='t'){
/* set the final transformations */
size_t len1 = strlen(optarg);
size_t len2 = strlen(finaltransform);
finaltransform = (char *) my_realloc((void *) finaltransform ,(len1+len2+1)*sizeof(char));
strcpy(finaltransform+len2,optarg);
}
}
/* END OF COMMAND LINE PROCESSING */
/* initialize global variables */
initialize_program(argv[0]);
/* build the list of space and tabs separated specs */
specsnum=0;
for(spec=optind;spec<(size_t) argc;spec++){
char *piece=strdup (argv[spec]);
char *stmp1=piece,*stmp2;
while( (stmp2=strsep (&stmp1," \t")) != NULL)
if(strlen(stmp2) >0){
specsnum++;
specs = (char **) my_realloc((void *) specs,specsnum*sizeof(char *));
specs[specsnum-1] = strdup(stmp2);
}
free(piece);
}
/* +++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose){
fprintf(stderr,"Output format='%s' Separator='%s' Empty format='%s' Empty string='%s' ",
FLOAT,SEP,EMPTY," ");
if(o_binary_in || o_binary_out){
fprintf(stderr,"Binary:");
if(o_binary_in)
fprintf(stderr," in");
if(o_binary_out)
fprintf(stderr," out");
}
fprintf(stderr,"\n");
}
/* +++++++++++++++++++++++++++++++++++++++++++++ */
/* parse line for data specification */
/* --------------------------------- */
for(spec=0;spec<specsnum;spec++){
/* added matrix */
size_t rowlength,columnlength;
double **newdata=NULL;
/* different pieces of the spec */
char *blockslice=NULL, *columnslice=NULL, *rowslice=NULL, *transform=NULL;
parse_spec(specs[spec],&filename,&blockslice,&columnslice,&rowslice,&transform);
/* set proper filename */
if(strcmp(filename,"")==0 && strcmp(old_filename,"")!=0){
free(filename);filename=strdup(old_filename);
}
/* fprintf(stderr,"old filename:%s, filename:%s, blockslice:%s, columnslice:%s, rowslice:%s, transform:%s,\n",old_filename,filename,blockslice,columnslice,rowslice,transform); */
/* return 0; */
/* load new data if the file changed */
if( spec==0 || ( strcmp(filename,old_filename) !=0 )){
int file;
/*open file*/
if(strcmp(filename,"")==0) file=0;
else if( (file = open(filename,O_RDONLY)) == -1){
fprintf(stderr,"WARNING (%s): failed to open file \"%s\"; request ignored\n",GB_PROGNAME,filename);
continue;
}
/* deallocate if necessary */
if(vals != NULL){
size_t i,j ;
for(i=0;i<blocks;i++){
for(j=0;j<columns[i];j++)
free(vals[i][j]);
free(vals[i]);
}
free(vals);
free(rows);
free(columns);
blocks=0;
rows=NULL;
columns=NULL;
vals=NULL;
}
/*read from file*/
if(o_binary_in==1)
loadblocks_bin(&vals,&blocks,&rows,&columns,file);
else
loadblocks(&vals,&blocks,&rows,&columns,file,splitstring);
/* store values */
free(old_filename);old_filename=strdup(filename);
/* close file */
if(file==0)
lseek(file,0,SEEK_SET);
close(file);
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
if(o_verbose){
size_t b;
fprintf(stderr,"<- source:%s",(strcmp(filename,"")==0?"(stdin)":filename));
fprintf(stderr,"\n");
for(b=0;b<blocks;b++)
fprintf(stderr," block:%zd rows:%zd cols:%zd\n",b,rows[b],columns[b]);
fprintf(stderr,"\n");
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
}
/* insert in turn any matrix specified in the block spec */
{
size_t bslicenum=0;
size_t b,bs;
size_t *bini=NULL;
size_t *bfin=NULL;
int *bskip=NULL;
/* build array of block slice */
parse_slice(blockslice,"block",blocks,&bslicenum,&bini,&bfin,&bskip);
for(bs=0;bs<bslicenum;bs++){
/* for(b=bini[bs]-1; bskipsign*(b- bfin[bs]+1) <= 0;b+=bskip[bs]){ */
for(b=bini[bs]-1; ( bskip[bs]>0 ? b < bfin[bs] : b+2 > bfin[bs] );b+=bskip[bs]){
/* create the new matrix */
new_matrix(&newdata,&columnlength,&rowlength,
vals[b],columns[b],rows[b],columnslice,rowslice);
/* apply transformations to the new matrix */
if(strlen(transform)>0 && strcmp(transform,"P") )
matrix_transform(&newdata,&columnlength,&rowlength,
transform,o_verbose);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
if(o_verbose==1)
fprintf(stderr,"-> %zdx%zd\n",rowlength,columnlength);
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* add the new matrix to the data */
add_matrix(&data,&length,&datanum,
newdata,columnlength,rowlength);
/* if P transformation was specified then print data collected so
far and restart to collect them */
if(index(transform,'P') != NULL){
/* final transformation and print */
print_data(&datanum, &length, &data, finaltransform,o_verbose);
/* add two empty lines in ASCII output */
if(o_binary_out==0)
printf("\n\n");
}
}
}
free(bini);
free(bfin);
free(bskip);
}
/* free everything */
free(columnslice);
free(rowslice);
free(blockslice);
free(transform);
free(newdata);
}/* END OF DATA CREATION */
/* free some space */
free(old_filename);
free(filename);
free(splitstring);
if(specsnum>0){
size_t i;
for(i=0;i<specsnum;i++) free(specs[i]);
free(specs);
}
if(vals != NULL){
size_t i,j ;
for(i=0;i<blocks;i++){
for(j=0;j<columns[i];j++)
free(vals[i][j]);
free(vals[i]);
}
free(vals);
free(rows);
free(columns);
blocks=0;
}
/* final transformation and print*/
/* print */
print_data(&datanum, &length, &data, finaltransform,o_verbose);
{
size_t i;
for(i=0;i<datanum;i++) free(data[i]);
free(length);
free(data);
}
free(finaltransform);
finalize_program();
return 0 ;
}
|