Perform binary trees search based a collection of data - c++
I was asked to write a BTS code for a collection of data with have the x, y and z coordinate, which has negative and positive number, and the requirement are below:
That program is capable to perform binary trees search based a collection of data.
Please use the data provided within this email. It is collection of coordinates in X,Y, and Z
That program must capable of:
FInding positive X, negative X, positive y, negative y, positive z, negative z.
User can search all positive numbers, all negative numbers, etc.
So the user will input a X coordinate, it will display the Y and Z together in the output, and another is when user want to search for all Positive/Negative of X, it will also show the Y and Z with the X.
So far as I only know binary search only can perform a search on a single number, but not for a string of element with 3 numbers inside.
This is the few of the collection of X, Y and Z data for example:
-0.090729 0.122568 0.030209 <--- one line of X Y Z coordinate
-0.179660 0.154953 0.033881
-0.335793 0.244996 0.269589
0.075957 0.149626 0.114472
also I will going to use this code as the base
#include <cstdlib>
#include <iostream>
using namespace std;
int binary_search(int array[],int first,int last, int value);
int main() {
int list[10];
for (int k=0; k<11; k++)
list[k]=2*k+1;
cout<< "binary search results: "<< binary_search(list,1,21,11)<<endl;
return 0;
}//end of main
int binary_search(int array[],int first,int last, int search_key)
{
int index;
if (first > last)
index = -1;
else
{
int mid = (first + last)/2;
if (search_key == array[mid])
index = mid;
else
if (search_key < array[mid])
index = binary_search(array,first, mid-1, search_key);
else
index = binary_search(array, mid+1, last, search_key);
} // end if
return index;
}// end binarySearch
I was also thinking that Using array is not enough for this, and I will have to make a separate file like a database that user can search the data with one input.
So I'm having an idea that I only do the binary tree for X coordinate but when the user input the X, it will show the Y and Z with the X from the data collection, also the data collection is a lot, any expert's help will be appreciated :) still wondering does binary search tree able to do that or not....
So this is the collections of data I need to used, can someone please tell me how to make that function that able to call the data from this huge collection?
0.075957 0.149626 0.114472
0.000905 0.131220 0.031000
0.334059 -0.004790 0.207368
0.380561 -0.016845 0.110792
-0.083847 -0.009495 0.260374
0.316664 0.071467 0.120474
-0.307909 0.341245 -0.115581
0.411077 -0.098945 0.029724
-0.093728 0.413972 -0.045792
-0.445182 0.173948 0.306924
-0.339452 0.094466 0.338246
0.154974 0.153764 0.113986
0.383043 -0.017294 0.038540
0.371618 -0.036996 -0.034430
-0.488796 0.174331 0.109211
-0.425485 -0.003917 0.201166
-0.328634 0.319858 0.114390
-0.410262 0.298180 0.193996
0.155009 0.097454 0.225033
-0.225622 0.246139 0.118317
-0.399749 0.163849 0.335552
0.076578 0.133341 0.177679
-0.201047 0.142376 0.174390
-0.429153 -0.098775 -0.040573
0.230882 0.131755 0.118676
-0.483743 0.082920 0.040329
-0.078715 0.471042 -0.055900
-0.411718 0.237937 0.278383
-0.005249 0.003769 0.270521
-0.018101 0.489653 -0.124951
-0.133572 0.398057 -0.090000
0.154095 0.134174 0.180583
-0.172136 0.147088 0.112057
0.240332 0.079992 0.210373
-0.175250 0.391765 0.011504
-0.477955 0.162864 0.042821
-0.263429 0.246060 0.204495
0.408153 -0.094551 0.112774
0.388142 -0.103007 0.184936
-0.257106 0.309349 0.107726
-0.020619 0.343770 -0.051245
-0.366778 -0.278556 0.116261
-0.329815 0.313916 0.034602
-0.482418 0.071724 0.268572
0.296142 0.067455 0.185325
0.392588 -0.106540 -0.040632
-0.348415 0.380318 -0.132477
0.074743 0.098941 0.226089
0.313984 -0.095701 0.274736
-0.000127 0.359321 -0.099978
-0.389304 0.419894 -0.275472
-0.448952 -0.015681 0.018614
0.360441 0.017741 0.133138
0.072016 -0.429793 0.306404
0.157903 -0.423369 0.305657
-0.253995 0.329448 0.039563
-0.486770 0.073079 0.193422
-0.277697 0.136104 0.260296
-0.000924 0.131167 0.114723
-0.324768 0.304498 0.200203
-0.172816 0.407647 -0.040188
-0.262926 -0.426393 0.279458
-0.389169 0.326903 -0.115444
-0.171523 0.319164 0.036876
-0.166670 -0.433521 0.278390
-0.052386 -0.126211 0.329163
0.021908 0.122344 0.172256
-0.467692 -0.118237 0.042218
-0.339549 0.330551 -0.049267
-0.333589 0.232102 -0.053896
0.240226 -0.009687 0.271713
-0.125529 0.419457 -0.000220
-0.323461 0.062386 -0.069065
-0.335525 0.413992 -0.292639
-0.485317 0.253072 0.122003
0.160470 0.053417 0.252663
0.320594 -0.097082 -0.121452
0.153275 -0.012350 0.284012
-0.351329 0.151160 0.328284
0.155106 0.152666 0.034476
0.237370 -0.432505 0.276914
0.213352 -0.110928 0.333017
0.076081 0.050562 0.261882
-0.420589 0.067501 -0.027579
-0.404542 0.241335 -0.029536
0.154083 -0.010130 -0.141933
-0.011780 0.079933 0.212997
0.076419 0.148417 0.035528
-0.474790 0.231229 0.188058
0.065221 0.102536 -0.068695
-0.341908 0.470328 -0.362529
-0.081834 0.109006 0.134754
-0.445012 0.272483 0.212757
-0.471614 -0.110944 0.114503
-0.086541 0.096661 -0.045842
-0.087413 0.067613 0.198527
-0.400299 0.085220 0.335886
0.335514 0.004886 -0.052942
-0.331894 -0.098257 -0.115394
-0.384468 -0.109560 -0.094346
-0.429120 -0.083583 0.201641
-0.450774 0.192619 0.008875
-0.077900 -0.436097 0.335867
-0.001575 -0.432054 0.337764
-0.318593 0.418017 -0.378191
-0.081679 0.417342 -0.113276
-0.256147 0.180127 0.214579
-0.324634 0.166962 0.291635
-0.165520 0.315139 -0.053349
-0.244783 -0.239901 0.258588
-0.157444 -0.112794 -0.153111
0.001362 -0.102979 0.350567
0.076088 -0.090730 0.354817
0.395664 -0.267686 0.191676
0.304469 0.060476 -0.029716
-0.103680 -0.414289 0.294346
-0.240880 0.071427 0.229963
0.395003 -0.186405 -0.042120
0.312090 -0.430972 0.197739
-0.092992 -0.078157 -0.160328
0.367958 -0.017433 0.172606
-0.072734 -0.114118 -0.196052
0.001156 -0.422750 0.282151
0.140519 -0.396572 0.327980
0.401578 -0.409301 0.180077
-0.038587 0.485641 -0.078435
0.078437 -0.338751 0.352289
0.150609 -0.344197 0.348366
-0.468113 -0.013229 0.110504
-0.258706 -0.401441 0.198291
-0.080173 -0.353192 0.259849
0.076135 -0.012304 0.287421
-0.025898 -0.350688 0.291216
0.008889 -0.332588 0.332609
-0.259982 0.255333 -0.021288
-0.500000 0.074738 0.115107
-0.293236 0.334808 -0.197488
-0.450030 -0.006072 0.139787
0.338110 -0.354940 0.228053
0.398298 -0.341848 0.204755
-0.171607 0.062339 0.205716
-0.334439 0.323599 -0.285487
0.295641 -0.030420 0.258598
-0.078645 0.467777 -0.100027
-0.388458 -0.109277 0.249584
-0.010804 -0.060563 0.298001
-0.235267 -0.337188 0.190461
-0.169882 -0.344871 0.254487
-0.434959 0.052108 0.305670
0.211018 0.127704 0.169978
-0.141419 0.105761 0.161259
-0.277201 0.080277 0.260381
-0.348562 0.307347 -0.210293
-0.264058 0.296204 0.184212
-0.201645 -0.339661 0.235866
0.044728 0.475219 -0.187208
-0.467501 -0.011187 0.052141
-0.406464 0.305509 0.039260
-0.079797 0.121035 0.092894
-0.424583 0.308193 0.113378
-0.410118 -0.023560 -0.044817
-0.048690 0.100157 0.158385
-0.323503 0.344986 -0.356561
-0.403118 -0.246061 0.111481
0.051702 0.432676 -0.178928
-0.258646 -0.350652 0.045387
-0.005999 0.095570 -0.055052
-0.344346 0.255465 -0.122999
-0.157020 0.102027 -0.067144
-0.337089 0.267242 -0.193677
-0.241299 -0.345101 0.113604
0.003790 -0.261338 0.347452
0.075487 -0.264686 0.369264
0.162459 -0.267253 0.361906
0.020993 0.060885 0.246241
0.471800 -0.343702 0.172142
0.149844 -0.096394 0.350343
-0.169260 -0.266480 0.265656
-0.078075 -0.262286 0.267258
-0.135235 0.358184 -0.088433
-0.255263 -0.279737 0.222196
-0.387804 0.315919 -0.042996
-0.249325 0.097467 -0.066377
0.232603 -0.011736 -0.126844
0.468932 -0.278385 0.170881
-0.458816 -0.088186 0.015550
-0.389489 0.356552 -0.200456
-0.175817 0.130162 -0.025492
-0.091194 -0.184586 0.283638
-0.247519 -0.009323 -0.126505
-0.362708 0.015190 -0.066164
-0.250302 -0.104264 -0.143793
-0.206967 -0.275905 0.243332
-0.197050 0.247164 0.041679
-0.415699 0.144577 -0.034869
-0.160657 -0.012060 -0.135141
0.073840 -0.096450 -0.221220
0.076158 -0.010711 -0.150732
-0.079237 0.333988 -0.044751
-0.001993 -0.009713 -0.147751
-0.469462 0.198805 0.256553
-0.330455 0.136368 -0.060891
0.422080 -0.239554 0.139433
-0.155069 0.062187 -0.100190
0.045128 0.395109 -0.135353
-0.236349 0.328456 -0.022454
0.150712 -0.095474 -0.203321
-0.395556 -0.231558 0.172341
-0.297510 0.278742 -0.103171
-0.339099 -0.258986 0.198846
-0.244746 -0.177858 -0.137134
-0.173047 -0.190625 0.279613
-0.007145 -0.190266 0.364539
0.075964 -0.187064 0.378191
0.162311 -0.175585 0.365091
-0.446336 0.252967 0.034904
0.360230 0.017437 0.013711
-0.335841 -0.181269 0.265241
-0.253792 -0.188356 0.283896
0.367728 -0.129890 -0.095475
-0.309241 -0.450194 0.258058
-0.346159 0.401686 -0.210900
-0.292800 0.317122 -0.141242
-0.296030 0.360867 -0.265735
-0.338339 -0.101703 0.275068
-0.210562 0.208448 0.093963
-0.402655 0.015646 0.272364
0.234197 -0.188255 0.339100
0.150828 0.105100 -0.069723
-0.252361 -0.094102 0.288347
-0.169846 -0.103389 0.282147
-0.081747 -0.090851 0.290155
0.251737 -0.136450 0.321562
0.229362 0.131550 0.030055
-0.076129 -0.006871 -0.140948
0.238180 -0.085572 0.311821
0.076712 0.058579 -0.109601
-0.233470 0.362778 0.015787
0.352277 -0.078644 -0.083349
-0.487177 0.156348 0.191350
-0.332970 -0.005332 0.263005
-0.176265 -0.018775 0.261670
0.165160 -0.062183 0.318872
-0.251163 -0.025516 0.268080
-0.326562 0.052598 0.309106
-0.215845 0.215242 0.013283
-0.250863 0.162990 -0.035906
0.018121 0.489990 -0.177498
-0.079734 0.357054 -0.098577
-0.255718 0.023253 0.243169
-0.155250 0.018091 0.241299
-0.095910 0.017906 0.241005
0.280869 0.018851 0.239697
-0.194925 0.262219 -0.018530
-0.005598 -0.479311 0.331880
-0.002292 0.414560 -0.123543
-0.260753 -0.481258 0.276218
0.154172 -0.480161 0.274365
-0.167268 -0.479832 0.271694
-0.323638 -0.019706 -0.102023
0.285772 -0.408742 0.248108
0.366403 -0.172115 -0.097373
-0.442366 -0.184972 0.119327
0.467558 -0.397663 0.116399
0.238829 -0.357919 0.301619
0.164031 0.069292 -0.104950
-0.092374 0.325879 0.002629
0.304791 -0.336291 0.265589
0.500000 -0.348679 0.115310
0.227075 -0.117799 -0.181677
0.211965 -0.325049 0.327752
0.358338 -0.105352 0.238236
-0.015772 0.387595 -0.078307
0.214950 -0.264517 0.340296
0.254757 -0.262003 0.316288
0.313084 -0.262813 0.273214
0.359071 -0.263765 0.238343
0.495477 -0.261305 0.109474
0.325024 -0.193317 -0.130771
-0.386855 0.449060 -0.338864
-0.317542 -0.310432 0.112304
-0.417095 -0.173898 0.198758
0.241026 0.090858 -0.049727
0.318413 -0.186461 0.281366
0.361404 -0.188428 0.239352
0.392783 -0.186163 0.191749
0.419850 -0.187629 0.113618
-0.405129 -0.244415 0.041488
-0.078681 0.053919 -0.101020
-0.459326 0.038064 0.023302
0.021646 0.437884 -0.175533
0.231143 -0.479464 -0.043005
0.222780 -0.475271 -0.110977
0.243339 -0.475372 0.203179
0.403476 -0.440490 0.034505
0.265111 -0.443827 -0.056269
0.242083 -0.429440 -0.130781
0.389655 -0.410616 -0.027253
0.404388 -0.439917 0.113291
0.454583 -0.405555 0.034545
0.304492 -0.017036 -0.097924
0.318352 -0.418003 -0.044667
-0.006068 -0.085607 -0.207010
-0.092889 -0.485483 -0.191576
0.287708 -0.397662 -0.113398
0.481895 -0.258555 0.035148
0.459541 -0.348207 -0.020224
0.490604 -0.353829 0.030048
0.398522 -0.346518 -0.043788
-0.294172 0.394323 -0.352233
0.341075 -0.354125 -0.066500
0.310707 -0.341708 -0.120061
-0.001949 0.053880 -0.108140
-0.160536 -0.475848 -0.178375
0.081321 -0.427695 -0.184472
-0.323267 -0.485070 -0.031361
0.429826 -0.241506 0.032040
0.280277 -0.166830 -0.166326
-0.082011 -0.430622 -0.190980
0.259778 -0.371250 -0.160124
-0.063844 -0.479809 0.332759
0.222145 0.052023 -0.099953
0.325379 -0.262782 -0.125122
0.392030 -0.265942 -0.040276
0.455904 -0.291006 -0.015375
-0.474778 0.135820 0.279064
0.419232 -0.185902 0.034486
-0.350612 0.278498 -0.259807
-0.168321 -0.427839 -0.120334
-0.360026 0.463230 -0.317495
-0.387025 0.461928 -0.315415
0.316651 0.070958 0.034101
-0.394244 0.396200 -0.201302
0.249565 -0.098461 -0.160699
-0.052792 -0.184322 0.333604
-0.314333 -0.478718 0.103402
-0.312439 -0.480349 0.190804
-0.310186 -0.438183 0.197410
-0.309481 -0.479378 0.252555
-0.331159 -0.487284 0.035204
-0.333495 -0.435541 0.040352
-0.263413 -0.426976 0.128511
-0.303818 -0.438361 0.090061
-0.277050 -0.400649 0.037718
-0.038925 -0.278358 0.294233
-0.304313 -0.238548 0.246970
-0.011962 -0.176767 -0.257824
-0.166728 -0.485130 -0.130257
0.070000 -0.483251 -0.137440
-0.318461 -0.442320 -0.030378
-0.138111 -0.435125 -0.178330
-0.252236 -0.426105 -0.053975
-0.159840 -0.434426 -0.069893
-0.097390 -0.417033 -0.114525
-0.087536 -0.339895 -0.126204
-0.167931 -0.340827 -0.078534
-0.248072 -0.337747 -0.038942
-0.057447 -0.336240 -0.190659
-0.249331 0.064124 -0.092216
-0.010390 -0.350874 -0.214420
-0.338845 -0.258346 -0.047109
-0.367182 -0.279822 0.032717
-0.021345 -0.267534 -0.245406
-0.076570 -0.266564 -0.195832
-0.101198 -0.272924 -0.132843
-0.001765 -0.485288 -0.175093
-0.395722 -0.228026 -0.030905
0.215824 0.121247 -0.021429
-0.443022 -0.183083 0.031252
-0.408828 -0.184413 -0.046159
-0.089085 -0.186963 -0.210342
-0.119101 -0.171558 -0.153775
-0.334735 -0.182650 -0.112396
-0.223385 -0.485981 -0.089089
0.222636 -0.180295 -0.196924
0.020131 -0.131068 -0.255166
0.001177 -0.426232 -0.190799
-0.012155 -0.064992 -0.167676
-0.251505 -0.489586 -0.044734
0.162056 -0.065542 -0.166444
0.017214 0.121009 -0.020486
0.231414 -0.347292 -0.184091
0.052590 -0.187358 -0.254877
0.150268 -0.453553 -0.148873
0.231710 -0.263207 -0.195083
0.077347 0.134286 -0.022666
0.157028 0.134088 -0.024777
0.152628 -0.483117 -0.122336
0.312729 -0.474830 0.028494
0.156938 -0.408421 -0.178160
0.210296 -0.399260 -0.170051
0.155543 -0.345337 -0.204948
0.075077 -0.351087 -0.219512
-0.113516 -0.334652 -0.079797
0.070376 -0.262941 -0.241359
0.152999 -0.265258 -0.221395
-0.260925 -0.281522 -0.067153
-0.163813 -0.249589 -0.110536
-0.000517 -0.245935 -0.256464
-0.251757 -0.234363 -0.110374
-0.305968 -0.241161 -0.095382
0.153196 -0.189709 -0.227997
0.093251 -0.180480 -0.246527
-0.048941 -0.173545 -0.249073
-0.157017 -0.189159 -0.134516
-0.370694 -0.198645 -0.084469
-0.003756 -0.479136 -0.132966
-0.093078 -0.475263 -0.122568
0.075829 -0.460227 -0.099777
-0.001899 -0.459362 -0.105936
0.165415 -0.477174 -0.046786
0.144067 -0.458390 -0.051246
0.076365 -0.454779 -0.042957
-0.000796 -0.457043 -0.042454
-0.168778 -0.486562 -0.041229
0.240678 -0.480161 0.029208
-0.082080 -0.458459 -0.042333
0.149462 -0.481931 0.033089
0.122461 -0.463743 0.002471
0.076974 -0.458785 0.035224
-0.165019 -0.485403 0.035211
-0.002507 -0.458952 0.035797
0.305050 -0.475313 0.120096
-0.248056 -0.489990 0.034763
-0.072565 -0.460061 0.025380
0.109360 -0.475999 0.064561
0.243323 -0.479673 0.126123
-0.110629 -0.489074 0.043599
0.151045 -0.483545 0.115056
0.113069 -0.479908 0.084853
0.336442 -0.463570 0.135023
0.076638 -0.459867 0.113200
-0.117317 -0.480495 0.113990
-0.002076 -0.459748 0.113895
-0.057473 -0.462636 0.113188
-0.256639 -0.489621 0.112012
0.123161 -0.463285 0.143835
0.181877 -0.477548 0.192178
0.151977 -0.457373 0.192210
0.076190 -0.455132 0.193957
-0.102975 -0.477424 0.189320
-0.067928 -0.459351 0.209563
-0.002745 -0.457144 0.191918
-0.161571 -0.485037 0.194232
0.216362 -0.476830 0.268931
-0.250727 -0.485793 0.193098
-0.320925 -0.308279 0.034877
0.072334 -0.480626 0.296205
-0.087828 -0.482461 0.282326
-0.003897 -0.482100 0.299386
does this code will work if i place all the data into a data.txt file and read it in the main.cpp?
int main ()
{
{
ifstream myReadFile;
myReadFile.open("Data.txt");
char output[100];
if (myReadFile.is_open())
{
while (!myReadFile.eof())
{
myReadFile >> output;
cout<<output;
}
}
myReadFile.close();
return 0;
}
and this is the bubble sort for the vertex X, I know it will take a really long time to sort it, but time doesn't matter as long as the code works.
for (i=0; i<500-1; i++)
for (int j=i+1; j<500; j++)
if (X[i] > X[j])
{
int temp = X[i];
X[i] = X [j];
X[j] = temp;
temp = Y[i];
Y[i] = Y [j];
Y[j] = temp;
temp = Z[i];
Z[i] = Z [j];
Z[j] = temp;
}
You talk about binary search trees but the code you have shown does a binary search on a sorted array. That is not the same thing.
One way of using your binary search would be to put the X,Y and Z values together in an array or custom struct. Something like:
struct Vertex {
float X;
float Y;
float Z;
};
Modify your binary search function so that it takes an array of these structs instead of an array of integers. You will have to make your search key an X float value:
int binary_search(Vertex array[],int first,int last, float x_value);
Load your X, Y, Z values into an array of Vertex and then sort by X value:
bool vertexXComparator(const Vertex& lhs, const Vertex& rhs) {
return lhs.X < rhs.X;
}
std::sort(list, list+number_of_vertexes, vertexXComparator);
Then you should be able to use your binary search (or std::binary_search) on the sorted list.
I would be a bit wary of using a float as a search key. It is generally a bad idea to test floating point numbers for equality.
Live demo.
Related
Memory Leak in Matlab when calling a C++ mexfile
Dear stackoverflow community, I am currently working on a MEX function that itself calls external C++ code. I am calling my MEX in a loop. Here is my mexfile: #include "mex.h" #include "OOMP.h" #include <iostream> using namespace std; /* function [h, Di, beta, c, Q, NorQ] = OOMP_v4(f, D, tol,ind,No) %OOMP Optimized Orthogonal Matching Pursuit % % It creates an atomic decomposition of a siganl % using OOMP criterion. You can choose a tolerance, % the number of atoms to take in or an initial subspace to influence the OOMP algorithm. % % % Usage: [h, Di, c, beta, Q, NorQ ] = OOMP_v2(f, D, tol,ind, No); % [h, Di, c] = OOMP_v2(f, D, tol,ind, No); */ void mexFunction(int nlhs, mxArray *plhs[] , int nrhs, const mxArray *prhs[]) { /* Begin input validation */ if(nrhs != 5) { mexErrMsgTxt("Wrong number of input arguments"); } // Check we are being passed real full and non string variables for ( int i = 0; i < nrhs; i++ ) { if ( mxIsComplex(prhs[i]) || mxIsClass(prhs[i],"sparse") || mxIsChar(prhs[i]) || !mxIsDouble(prhs[i]) ) { mexErrMsgTxt("Input must be double, real, full,and nonstring"); } } // First check f is a vector if ((mxGetM(prhs[0]) !=1) || (mxGetN(prhs[0]) == 1)) { mexErrMsgTxt("f should be a line vector"); } // Validate D if ( (mxGetM(prhs[1])) != (mxGetN(prhs[0])) ) { mexPrintf("The atoms in the dictionary D are of length %i and should be of length %i\n",mxGetM(prhs[1]),mxGetN(prhs[0])); mexErrMsgTxt("Error!!\n"); } if ((mxGetM(prhs[1])) == 1) { mexErrMsgTxt("The dictionary D should contain more than 1 atom!!\n"); } long L = mxGetM(prhs[1]); long N = mxGetN(prhs[1]); long numInd = mxGetN(prhs[3]); Matrix *f = new Matrix(mxGetPr(prhs[0]),1,L, 'T'); Matrix *D = new Matrix(mxGetPr(prhs[1]),L,N,'T'); double tol = mxGetScalar(prhs[2]); Matrix *mind = new Matrix(mxGetPr(prhs[3]),1,numInd); long No = mxGetScalar(prhs[4]); /* Begin Output validation */ if ( nlhs <3 || nlhs >6) { mexErrMsgTxt("Wrong number of output arguments"); } Matrix *h = new Matrix(1,L,0); Matrix *Di= new Matrix(1,0,0); Matrix *beta= new Matrix(L,0,0); Matrix *c= new Matrix(1,0,0); Matrix *Q= new Matrix(L,0,0); Matrix *NorQ= new Matrix((long)0,1,0); int fail =OOMP_v4(f,D,tol,mind,No ,L, N,h,Di,beta,c,Q,NorQ); if(!fail) { // Creation des variables de Sorties long K = (*Di).getNbre_C(); plhs[0] = mxCreateDoubleMatrix(1,L,mxREAL); plhs[1] = mxCreateDoubleMatrix(1,K,mxREAL); plhs[2] = mxCreateDoubleMatrix(1,K,mxREAL); if(nlhs>=4) plhs[3] = mxCreateDoubleMatrix(L,K,mxREAL); if(nlhs>=5) plhs[4] = mxCreateDoubleMatrix(L,K,mxREAL); if(nlhs==6) plhs[5] = mxCreateDoubleMatrix(K,1,mxREAL); // Transformation double *m_h= mxGetPr(plhs[0]); double *m_Di = mxGetPr(plhs[1]); double *m_c = mxGetPr(plhs[2]); ToArray(h,m_h,1,L); ToArray(Di,m_Di,1,K); ToArray(c,m_c,1,K); if(nlhs>=4){ double *m_beta = mxGetPr(plhs[3]); ToArray(beta,m_beta,L,K); } if(nlhs>=5){ double *m_Q = mxGetPr(plhs[4]); ToArray(Q,m_Q,L,K); } if(nlhs==6){ double *m_NorQ = mxGetPr(plhs[5]); ToArray(NorQ,m_NorQ,K,1); } } else { mexErrMsgTxt("Error OOMP exited early see message above!!\n"); } } I am using the previous mexfile in a matlab program that runs successfully Here is the loop using the mexfile: for i=1:Nframes f=F(:,i)'; %the signals are the colums of F tol=PRD0*norm(f)/100; [h, Di0, ~] = OOMP_for_new_Mex(f, D, tol,ind,No); error_norm(i)= norm(f-h); K(i)=numel(Di0); clear Di0 h f tol ; end I got good results but the program is using too much memory 10Gb! which awkward because the variable stocked in the Matlab use only a few Mb. The problem here that in each loop new memory is allocated but it is not released in the end of the loop: The pointers created in the mex file are not deleted after every iteration. So my questions are: 1)Do you see something else that could cause a memory leak in the code? 2)how can I fix it?
How does AudioKit's AKNodeOutputPlot pull it's data?
I'm very new to the AudioKit framework and I have been trying to understand a bit more about the DSP side to it. Whilst rummaging around in the source code I realised that AKNodeOutputPlot does not pull data from the node the same way others would. In the DSP code for the AKAmplitudeTracker an RMS value is calculated for each channel and the result is briefly written to the output buffer but at the end of the for loop the node is essentially bypassed by setting the output to the original input: void process(AUAudioFrameCount frameCount, AUAudioFrameCount bufferOffset) override { for (int frameIndex = 0; frameIndex < frameCount; ++frameIndex) { int frameOffset = int(frameIndex + bufferOffset); for (int channel = 0; channel < channels; ++channel) { float *in = (float *)inBufferListPtr->mBuffers[channel].mData + frameOffset; float temp = *in; float *out = (float *)outBufferListPtr->mBuffers[channel].mData + frameOffset; if (channel == 0) { if (started) { sp_rms_compute(sp, leftRMS, in, out); leftAmplitude = *out; } else { leftAmplitude = 0; } } else { if (started) { sp_rms_compute(sp, rightRMS, in, out); rightAmplitude = *out; } else { rightAmplitude = 0; } } *out = temp; } } } This makes sense since outputting the RMS value to the device speakers would sound terrible but when this node is used as the input to the AKNodeOutputPlot object RMS values are plotted. I assumed that the leftAmplitude and rightAmplitude variables were being referenced somewhere but even if they are zeroed out the plot works just fine. I'm interested in doing some work on the signal without effecting the output so I'd love it someone could help me figure how the AKPlot is grabbing this data. Cheers
AKNodeOutputPlot works with something called a "tap": https://github.com/AudioKit/AudioKit/blob/master/AudioKit/Common/User%20Interface/AKNodeOutputPlot.swift There are also a few other taps that are not necessarily just for user interface purposes: https://github.com/AudioKit/AudioKit/tree/master/AudioKit/Common/Taps Taps allow you to inspect the data being pulled through another node without being inserted into the signal chain itself.
How to do arithmetic operation on very large numbers [closed]
Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 8 years ago. Improve this question I am trying to add several 50-digit numbers from an array of strings and print first 10 digit of the summation,but I am getting the wrong answer.What is wrong with my code? The answer should be: 5537376230 My answer is :3737623039 p.s. i have used the array to store single digits of the summation #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string num[100] = { "37107287533902102798797998220837590246510135740250", "46376937677490009712648124896970078050417018260538", "74324986199524741059474233309513058123726617309629", "91942213363574161572522430563301811072406154908250", "23067588207539346171171980310421047513778063246676", "89261670696623633820136378418383684178734361726757", "28112879812849979408065481931592621691275889832738", "44274228917432520321923589422876796487670272189318", "47451445736001306439091167216856844588711603153276", "70386486105843025439939619828917593665686757934951", "62176457141856560629502157223196586755079324193331", "64906352462741904929101432445813822663347944758178", "92575867718337217661963751590579239728245598838407", "58203565325359399008402633568948830189458628227828", "80181199384826282014278194139940567587151170094390", "35398664372827112653829987240784473053190104293586", "86515506006295864861532075273371959191420517255829", "71693888707715466499115593487603532921714970056938", "54370070576826684624621495650076471787294438377604", "53282654108756828443191190634694037855217779295145", "36123272525000296071075082563815656710885258350721", "45876576172410976447339110607218265236877223636045", "17423706905851860660448207621209813287860733969412", "81142660418086830619328460811191061556940512689692", "51934325451728388641918047049293215058642563049483", "62467221648435076201727918039944693004732956340691", "15732444386908125794514089057706229429197107928209", "55037687525678773091862540744969844508330393682126", "18336384825330154686196124348767681297534375946515", "80386287592878490201521685554828717201219257766954", "78182833757993103614740356856449095527097864797581", "16726320100436897842553539920931837441497806860984", "48403098129077791799088218795327364475675590848030", "87086987551392711854517078544161852424320693150332", "59959406895756536782107074926966537676326235447210", "69793950679652694742597709739166693763042633987085", "41052684708299085211399427365734116182760315001271", "65378607361501080857009149939512557028198746004375", "35829035317434717326932123578154982629742552737307", "94953759765105305946966067683156574377167401875275", "88902802571733229619176668713819931811048770190271", "25267680276078003013678680992525463401061632866526", "36270218540497705585629946580636237993140746255962", "24074486908231174977792365466257246923322810917141", "91430288197103288597806669760892938638285025333403", "34413065578016127815921815005561868836468420090470", "23053081172816430487623791969842487255036638784583", "11487696932154902810424020138335124462181441773470", "63783299490636259666498587618221225225512486764533", "67720186971698544312419572409913959008952310058822", "95548255300263520781532296796249481641953868218774", "76085327132285723110424803456124867697064507995236", "37774242535411291684276865538926205024910326572967", "23701913275725675285653248258265463092207058596522", "29798860272258331913126375147341994889534765745501", "18495701454879288984856827726077713721403798879715", "38298203783031473527721580348144513491373226651381", "34829543829199918180278916522431027392251122869539", "40957953066405232632538044100059654939159879593635", "29746152185502371307642255121183693803580388584903", "41698116222072977186158236678424689157993532961922", "62467957194401269043877107275048102390895523597457", "23189706772547915061505504953922979530901129967519", "86188088225875314529584099251203829009407770775672", "11306739708304724483816533873502340845647058077308", "82959174767140363198008187129011875491310547126581", "97623331044818386269515456334926366572897563400500", "42846280183517070527831839425882145521227251250327", "55121603546981200581762165212827652751691296897789", "32238195734329339946437501907836945765883352399886", "75506164965184775180738168837861091527357929701337", "62177842752192623401942399639168044983993173312731", "32924185707147349566916674687634660915035914677504", "99518671430235219628894890102423325116913619626622", "73267460800591547471830798392868535206946944540724", "76841822524674417161514036427982273348055556214818", "97142617910342598647204516893989422179826088076852", "87783646182799346313767754307809363333018982642090", "10848802521674670883215120185883543223812876952786", "71329612474782464538636993009049310363619763878039", "62184073572399794223406235393808339651327408011116", "66627891981488087797941876876144230030984490851411", "60661826293682836764744779239180335110989069790714", "85786944089552990653640447425576083659976645795096", "66024396409905389607120198219976047599490197230297", "64913982680032973156037120041377903785566085089252", "16730939319872750275468906903707539413042652315011", "94809377245048795150954100921645863754710598436791", "78639167021187492431995700641917969777599028300699", "15368713711936614952811305876380278410754449733078", "40789923115535562561142322423255033685442488917353", "44889911501440648020369068063960672322193204149535", "41503128880339536053299340368006977710650566631954", "81234880673210146739058568557934581403627822703280", "82616570773948327592232845941706525094512325230608", "22918802058777319719839450180888072429661980811197", "77158542502016545090413245809786882778948721859617", "72107838435069186155435662884062257473692284509516", "20849603980134001723930671666823555245252804609722", "53503534226472524250874054075591789781264330331690", }; long long sum[100]; int runner=0; long long carry=0; for(int l=49;l>=0;l--) { int total=0; for(int k=0;k<100;k++ ) { total+=(int)num[k][l]-48; } total=total+carry; sum[runner]=total%10; if(total>9)carry=total/10; else carry=0; runner++; } //cout<<runner<<endl; for(int l=runner-1;l>runner-11;l--) { cout<<sum[l]; } }
After your last loop, the carry is 55, but you never insert it into the result.
Since you only need the first 10 digits int main(int argc, char **argv) { int i; char *num[100] = { "37107287533902102798797998220837590246510135740250", "46376937677490009712648124896970078050417018260538", "74324986199524741059474233309513058123726617309629", "91942213363574161572522430563301811072406154908250", "23067588207539346171171980310421047513778063246676", "89261670696623633820136378418383684178734361726757", "28112879812849979408065481931592621691275889832738", "44274228917432520321923589422876796487670272189318", "47451445736001306439091167216856844588711603153276", "70386486105843025439939619828917593665686757934951", "62176457141856560629502157223196586755079324193331", "64906352462741904929101432445813822663347944758178", "92575867718337217661963751590579239728245598838407", "58203565325359399008402633568948830189458628227828", "80181199384826282014278194139940567587151170094390", "35398664372827112653829987240784473053190104293586", "86515506006295864861532075273371959191420517255829", "71693888707715466499115593487603532921714970056938", "54370070576826684624621495650076471787294438377604", "53282654108756828443191190634694037855217779295145", "36123272525000296071075082563815656710885258350721", "45876576172410976447339110607218265236877223636045", "17423706905851860660448207621209813287860733969412", "81142660418086830619328460811191061556940512689692", "51934325451728388641918047049293215058642563049483", "62467221648435076201727918039944693004732956340691", "15732444386908125794514089057706229429197107928209", "55037687525678773091862540744969844508330393682126", "18336384825330154686196124348767681297534375946515", "80386287592878490201521685554828717201219257766954", "78182833757993103614740356856449095527097864797581", "16726320100436897842553539920931837441497806860984", "48403098129077791799088218795327364475675590848030", "87086987551392711854517078544161852424320693150332", "59959406895756536782107074926966537676326235447210", "69793950679652694742597709739166693763042633987085", "41052684708299085211399427365734116182760315001271", "65378607361501080857009149939512557028198746004375", "35829035317434717326932123578154982629742552737307", "94953759765105305946966067683156574377167401875275", "88902802571733229619176668713819931811048770190271", "25267680276078003013678680992525463401061632866526", "36270218540497705585629946580636237993140746255962", "24074486908231174977792365466257246923322810917141", "91430288197103288597806669760892938638285025333403", "34413065578016127815921815005561868836468420090470", "23053081172816430487623791969842487255036638784583", "11487696932154902810424020138335124462181441773470", "63783299490636259666498587618221225225512486764533", "67720186971698544312419572409913959008952310058822", "95548255300263520781532296796249481641953868218774", "76085327132285723110424803456124867697064507995236", "37774242535411291684276865538926205024910326572967", "23701913275725675285653248258265463092207058596522", "29798860272258331913126375147341994889534765745501", "18495701454879288984856827726077713721403798879715", "38298203783031473527721580348144513491373226651381", "34829543829199918180278916522431027392251122869539", "40957953066405232632538044100059654939159879593635", "29746152185502371307642255121183693803580388584903", "41698116222072977186158236678424689157993532961922", "62467957194401269043877107275048102390895523597457", "23189706772547915061505504953922979530901129967519", "86188088225875314529584099251203829009407770775672", "11306739708304724483816533873502340845647058077308", "82959174767140363198008187129011875491310547126581", "97623331044818386269515456334926366572897563400500", "42846280183517070527831839425882145521227251250327", "55121603546981200581762165212827652751691296897789", "32238195734329339946437501907836945765883352399886", "75506164965184775180738168837861091527357929701337", "62177842752192623401942399639168044983993173312731", "32924185707147349566916674687634660915035914677504", "99518671430235219628894890102423325116913619626622", "73267460800591547471830798392868535206946944540724", "76841822524674417161514036427982273348055556214818", "97142617910342598647204516893989422179826088076852", "87783646182799346313767754307809363333018982642090", "10848802521674670883215120185883543223812876952786", "71329612474782464538636993009049310363619763878039", "62184073572399794223406235393808339651327408011116", "66627891981488087797941876876144230030984490851411", "60661826293682836764744779239180335110989069790714", "85786944089552990653640447425576083659976645795096", "66024396409905389607120198219976047599490197230297", "64913982680032973156037120041377903785566085089252", "16730939319872750275468906903707539413042652315011", "94809377245048795150954100921645863754710598436791", "78639167021187492431995700641917969777599028300699", "15368713711936614952811305876380278410754449733078", "40789923115535562561142322423255033685442488917353", "44889911501440648020369068063960672322193204149535", "41503128880339536053299340368006977710650566631954", "81234880673210146739058568557934581403627822703280", "82616570773948327592232845941706525094512325230608", "22918802058777319719839450180888072429661980811197", "77158542502016545090413245809786882778948721859617", "72107838435069186155435662884062257473692284509516", "20849603980134001723930671666823555245252804609722", "53503534226472524250874054075591789781264330331690", }; long int sum = 0; for (i = 0 ; i < 100 ; i++) { char firstDigits[15]; memcpy(firstDigits, num[i], 14); firstDigits[14] = 0; sum += strtol(firstDigits, NULL, 10); } char string[11]; snprintf(string, 11, "%ld", sum); fprintf(stderr, "%s\n", string); return 0; }
I don't know if this is the only problem, but you are using 'int' which has the following range: –2,147,483,648 to 2,147,483,647 Your numbers are to big and will therefore not work as desired. Check http://msdn.microsoft.com/en-us/library/s3f49ktz.aspx to se the available data-types
You're implicitly assuming that the answer will have no more digits than each of the inputs, which clearly doesn't hold in this case. You do allocate 100 digits for the output, which is more than sufficient, but you never fill more than 50 of them. I've had a go at modifying your code to remedy this. #include <iostream> #include <fstream> #include <string> using namespace std; int main() { const int nnum = 100, idigits = 50, odigits = 100; string num[nnum] = { "37107287533902102798797998220837590246510135740250", "46376937677490009712648124896970078050417018260538", "74324986199524741059474233309513058123726617309629", "91942213363574161572522430563301811072406154908250", "23067588207539346171171980310421047513778063246676", "89261670696623633820136378418383684178734361726757", "28112879812849979408065481931592621691275889832738", "44274228917432520321923589422876796487670272189318", "47451445736001306439091167216856844588711603153276", "70386486105843025439939619828917593665686757934951", "62176457141856560629502157223196586755079324193331", "64906352462741904929101432445813822663347944758178", "92575867718337217661963751590579239728245598838407", "58203565325359399008402633568948830189458628227828", "80181199384826282014278194139940567587151170094390", "35398664372827112653829987240784473053190104293586", "86515506006295864861532075273371959191420517255829", "71693888707715466499115593487603532921714970056938", "54370070576826684624621495650076471787294438377604", "53282654108756828443191190634694037855217779295145", "36123272525000296071075082563815656710885258350721", "45876576172410976447339110607218265236877223636045", "17423706905851860660448207621209813287860733969412", "81142660418086830619328460811191061556940512689692", "51934325451728388641918047049293215058642563049483", "62467221648435076201727918039944693004732956340691", "15732444386908125794514089057706229429197107928209", "55037687525678773091862540744969844508330393682126", "18336384825330154686196124348767681297534375946515", "80386287592878490201521685554828717201219257766954", "78182833757993103614740356856449095527097864797581", "16726320100436897842553539920931837441497806860984", "48403098129077791799088218795327364475675590848030", "87086987551392711854517078544161852424320693150332", "59959406895756536782107074926966537676326235447210", "69793950679652694742597709739166693763042633987085", "41052684708299085211399427365734116182760315001271", "65378607361501080857009149939512557028198746004375", "35829035317434717326932123578154982629742552737307", "94953759765105305946966067683156574377167401875275", "88902802571733229619176668713819931811048770190271", "25267680276078003013678680992525463401061632866526", "36270218540497705585629946580636237993140746255962", "24074486908231174977792365466257246923322810917141", "91430288197103288597806669760892938638285025333403", "34413065578016127815921815005561868836468420090470", "23053081172816430487623791969842487255036638784583", "11487696932154902810424020138335124462181441773470", "63783299490636259666498587618221225225512486764533", "67720186971698544312419572409913959008952310058822", "95548255300263520781532296796249481641953868218774", "76085327132285723110424803456124867697064507995236", "37774242535411291684276865538926205024910326572967", "23701913275725675285653248258265463092207058596522", "29798860272258331913126375147341994889534765745501", "18495701454879288984856827726077713721403798879715", "38298203783031473527721580348144513491373226651381", "34829543829199918180278916522431027392251122869539", "40957953066405232632538044100059654939159879593635", "29746152185502371307642255121183693803580388584903", "41698116222072977186158236678424689157993532961922", "62467957194401269043877107275048102390895523597457", "23189706772547915061505504953922979530901129967519", "86188088225875314529584099251203829009407770775672", "11306739708304724483816533873502340845647058077308", "82959174767140363198008187129011875491310547126581", "97623331044818386269515456334926366572897563400500", "42846280183517070527831839425882145521227251250327", "55121603546981200581762165212827652751691296897789", "32238195734329339946437501907836945765883352399886", "75506164965184775180738168837861091527357929701337", "62177842752192623401942399639168044983993173312731", "32924185707147349566916674687634660915035914677504", "99518671430235219628894890102423325116913619626622", "73267460800591547471830798392868535206946944540724", "76841822524674417161514036427982273348055556214818", "97142617910342598647204516893989422179826088076852", "87783646182799346313767754307809363333018982642090", "10848802521674670883215120185883543223812876952786", "71329612474782464538636993009049310363619763878039", "62184073572399794223406235393808339651327408011116", "66627891981488087797941876876144230030984490851411", "60661826293682836764744779239180335110989069790714", "85786944089552990653640447425576083659976645795096", "66024396409905389607120198219976047599490197230297", "64913982680032973156037120041377903785566085089252", "16730939319872750275468906903707539413042652315011", "94809377245048795150954100921645863754710598436791", "78639167021187492431995700641917969777599028300699", "15368713711936614952811305876380278410754449733078", "40789923115535562561142322423255033685442488917353", "44889911501440648020369068063960672322193204149535", "41503128880339536053299340368006977710650566631954", "81234880673210146739058568557934581403627822703280", "82616570773948327592232845941706525094512325230608", "22918802058777319719839450180888072429661980811197", "77158542502016545090413245809786882778948721859617", "72107838435069186155435662884062257473692284509516", "20849603980134001723930671666823555245252804609722", "53503534226472524250874054075591789781264330331690", }; int sum[odigits]; int carry=0, d; for(d=0;d<odigits;d++) { int ipos = idigits-1-d; int opos = odigits-1-d; int total = carry; if(ipos >= 0) for(int k = 0; k < nnum; k++) total += num[k][ipos]-48; else if(carry == 0) break; sum[opos] = total%10; carry = total/10; } for(int i=0; i < 10; i++) cout << sum[odigits-d+i]; cout << "\n"; } This produces the desired result 5537376230. Since an int can hold much more than just 1 decimal digit, a more efficient approach would be to process larger groups of digits at once.
Gauss-Jordan elimination returning only zeros
My code is returning zero for all values and I'm not sure why. I'm supposed to solve using Gauss-Jordan elimination. Does anyone have any suggestions? #include <stdio.h> #define N 10 int main() { double a[N][N+1]={{3.55618, 5.87317, 7.84934, 5.6951, 3.84642, 9.15038, -1.68539, 5.03067, 7.63384, -1.75626}, {-4.82893, 8.38177, -0.301221, 5.10182, -4.1169, -6.09145, -3.95675, -2.33365, 1.3969, 6.54555}, {-7.64196, 5.66605, 3.20481, 1.55619, -1.19814, 9.79288, 5.35547, 5.86109, 4.95544, -9.35749}, {-2.95914, -9.16958, 7.3216, 2.39876, -8.1302, -7.55135, -2.37718, 7.29694, 5.9867, 8.5401}, {-8.42043, -0.369407, -5.4102, -8.00545, 9.22153, 3.96454, 5.38499, 0.438365, 0.419677, 4.17166}, {6.02952, 4.57728, 5.46424, 3.52915, -1.01135, -3.74686, 8.14264, -8.86961, -2.88114, 1.29821}, {0.519819, -6.16655, 1.13216, 2.75811, -1.05975, 4.20286, -3.45764, 0.763558, -0.281287, -9.76168}, {5.15737, -9.67481, 9.29904, -3.93334, 9.12785, -4.25208, -6.1652, 2.5375, 0.139195, 2.00106}, {-4.30784, 1.40711, -6.97966, -9.29715, 5.17234, 2.42634, 1.88818, -2.05526, -3.7679, 3.3708}, {-4.65418, 7.18118, 6.51338, 3.13249, 0.188456, -16.85599, 7.21435, -2.93417, 1.06061, 1.10807}}; double pivot,d; int i,j,k; for(k=0; k<N; k++) { pivot=a[k][k]; for(j=k; j<N+1; j++) a[k][j]=a[k][j]/pivot; for(i=0; i<N; i++) { if(i != k) { d=a[i][k]; for(j=k; j<N+1; j++) a[i][j]=a[i][j]-d*a[k][j]; } } } for(i=0; i<N; i++) printf("x[%d]=%lf\n", i+1, a[i][N]); return 0; }
Your input array is of size [10][11] but your input values are of size [10][10]. Therefore your rightmost column is full of zeroes, which means the output will be all zeroes.
Try this :- (N in place of N+1) :-) double a[N][N]={{3.55618, 5.87317, 7.84934, 5.6951, 3.84642, 9.15038, -1.68539, 5.03067, 7.63384, -1.75626}, {-4.82893, 8.38177, -0.301221, 5.10182, -4.1169, -6.09145, -3.95675, -2.33365, 1.3969, 6.54555}, {-7.64196, 5.66605, 3.20481, 1.55619, -1.19814, 9.79288, 5.35547, 5.86109, 4.95544, -9.35749}, {-2.95914, -9.16958, 7.3216, 2.39876, -8.1302, -7.55135, -2.37718, 7.29694, 5.9867, 8.5401}, {-8.42043, -0.369407, -5.4102, -8.00545, 9.22153, 3.96454, 5.38499, 0.438365, 0.419677, 4.17166}, {6.02952, 4.57728, 5.46424, 3.52915, -1.01135, -3.74686, 8.14264, -8.86961, -2.88114, 1.29821}, {0.519819, -6.16655, 1.13216, 2.75811, -1.05975, 4.20286, -3.45764, 0.763558, -0.281287, -9.76168}, {5.15737, -9.67481, 9.29904, -3.93334, 9.12785, -4.25208, -6.1652, 2.5375, 0.139195, 2.00106}, {-4.30784, 1.40711, -6.97966, -9.29715, 5.17234, 2.42634, 1.88818, -2.05526, -3.7679, 3.3708}, {-4.65418, 7.18118, 6.51338, 3.13249, 0.188456, -16.85599, 7.21435, -2.93417, 1.06061, 1.10807}};
Enormous Increase In the Use Of Memory
I've got a code where I need to create a map with key values as double (value of the f-test between two clusters. I need to calculate the residual sum of squares for this) and the mapped value of cluspair which is pair of the class Cluster that I created. Map aims to store the F-test values between the all clusters so that I would not need to do the calculation again and again in every step. BTW cluster is a tree structure where every cluster contains two subclusters and the stored values are 70-dimensional vectors. Problem is, in order to calculate the RSS, I need to implement a recursive code where I need to find the distance of every element of the cluster with the mean of the cluster and this seems to be consuming an enormous amount of memory. When I create the same map with the key values being the simple distance between the means of two clusters, the program uses minimal memory so I think the increase in the memory use is caused by the call of the recursive function RSS. What should I do to manage the memory use in the code below? In its current implementation the system runs out of memory and windows closes the application saying that the system ran out of virtual memory. The main code: map<double,cluspair> createRSSMap( list<Cluster*> cluslist ) { list<Cluster*>::iterator it1; list<Cluster*>::iterator it2; map<double,cluspair> rtrnmap; for(it1=cluslist.begin(); it1!= --cluslist.end() ;it1++) { it2=it1; ++it2; cout << "."; list<Cluster*>::iterator itc; double cFvalue=10000000000000000000; double rIt1 = (*it1)->rss(); for(int kk=0 ; it2!=cluslist.end(); it2++) { Cluster tclustr ((*it1) , (*it2)); double r1 = tclustr.rss(); double r2= rIt1 + (*it2)->rss(); int df2 = tclustr.getNumOfVecs() - 2; double fvalue = (r1 - r2) / (r2 / df2); if(fvalue<cFvalue) { cFvalue=fvalue; itc=it2; } } cluspair clp; clp.c1 = *it1; clp.c2 = *itc; bool doesexists = (rtrnmap.find(cFvalue) != rtrnmap.end()); while(rtrnmap) { cFvalue+= 0.000000001; rtrnmap= (rtrnmap.find(cFvalue) != rtrnmap.end()); } rtrnmap[cFvalue] = clp; } return rtrnmap; } and the imlementation of the function RSS: double Cluster::rss() { return rss(cnode->mean); } double Cluster::rss(vector<double> &cmean) { if(cnode->numOfVecs==1) { return vectorDist(cmean,cnode->mean); } else { return ( ec1->rss(cmean) + ec2->rss(cmean) ); } } Much thanks in advance. I really don't know what to do at this point. below is the code with that I use to create a map with keys being simple euclidian distance between two cluster means. As I've said above, it is quite similar and uses minimal memory. It only differs in the calculation of the fvalue. Instead of the recursive calculation, there is the calculation of simple distance of means of two clusters. Hope it helps to identify the problem map<double,cluspair> createDistMap( list<Cluster*> cluslist ) { list<Cluster*>::iterator it1; list<Cluster*>::iterator it2; map<double,cluspair> rtrnmap; for(it1=cluslist.begin(); it1!= --cluslist.end() ;it1++) { it2=it1; ++it2; cout << "."; list<Cluster*>::iterator itc; double cDist=1000000000000000; for(int kk=0 ; it2!=cluslist.end(); it2++) { double nDist = vectorDist( (*it1)->getMean(),(*it2)->getMean()); if (nDist<cDist) { cDist = nDist; itc=it2; } } cluspair clp; clp.c1 = *it1; clp.c2 = *itc; bool doesexists = (rtrnmap.find(cDist) != rtrnmap.end()); while(doesexists) { cDist+= 0.000000001; doesexists = (rtrnmap.find(cDist) != rtrnmap.end()); } rtrnmap[cDist] = clp; } return rtrnmap; } implementation of vectorDist() double vectorDist(vector<double> vec1, vector<double> vec2) { double sqrsum=0; double tempd=0; int vs = vec1.size(); for ( int i=0;i<vs;i++) { tempd = vec1[i] - vec2[i]; sqrsum += tempd*tempd; } return sqrsum; } Edit: BTW I've tried this alternative implementation which still fails to control the memory usage double Cluster::rss() { list<double> fvals; rss(cnode->mean , fvals); double sum=0; list<double>::iterator tpit; for(tpit=fvals.begin() ; tpit != fvals.end() ; ++tpit) { sum += *tpit; } return sum; } void Cluster::rss(vector<double> &cmean , list<double> &fvals) { if(cnode->numOfVecs==1) { fvals.push_back( vectorDist(cmean,cnode->mean) ); } else { ec1->rss(cmean , fvals); ec2->rss(cmean , fvals); } }
If you're running out of memory you have a very deep tree or your Cluster objects are large or both. Try creating another tree data structure of doubles with the same topology as your Cluster tree and call it RSS tree to hold the RSS values. Calculate the bottom nodes' rss values and then recursively fill out the rest of the values in the RSS tree. This way you aren't holding the cluster objects in memory while you do the rss calculation.