00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <cmath>
00023 #include <iomanip>
00024 #include "field.h"
00025
00026 extern "C"{
00027 extern double erf(double x);
00028 extern double erfc(double x);
00029 }
00030
00031 namespace matvec {
00032
00033 Field::Field(const unsigned n, DataNode *a,FieldStruct st,HashTable* ht) {
00034 ne = n;
00035 dat_vec = a;
00036 col_struct = st;
00037 hashtable = ht;
00038 }
00039
00040 Field::Field(const Field& a)
00041 {
00042 ne = 0; dat_vec = 0; hashtable = 0;
00043 copyfrom(a);
00044 }
00045
00046 void Field::release(void)
00047 {
00048 if (dat_vec) { delete [] dat_vec; dat_vec = 0; }
00049 if (hashtable) { delete hashtable; hashtable = 0; }
00050 ne = 0;
00051 }
00052
00053 void Field::copyfrom(const Field& A)
00054 {
00055 if (this == &A) return;
00056 resize(A.ne);
00057 memcpy(dat_vec,A.dat_vec,sizeof(DataNode)*ne);
00058 col_struct = A.col_struct;
00059 if (A.hashtable) {
00060 if (hashtable) {
00061 delete hashtable;
00062 hashtable=0;
00063 }
00064 hashtable = new HashTable;
00065 *hashtable = *(A.hashtable);
00066 }
00067 }
00068
00069 const Field& Field::operator=(const Field& a)
00070 {
00071 copyfrom(a);
00072 return *this;
00073 }
00074
00075 Field& Field::resize(const unsigned n)
00076 {
00077 if (ne == n) return *this;
00078 if (dat_vec) {
00079 delete [] dat_vec;
00080 dat_vec=0;
00081 }
00082 ne = n;
00083 if (ne>0){
00084 dat_vec = new DataNode [ne];
00085 }
00086 else {
00087 dat_vec = 0;
00088 }
00089
00090 if (hashtable) {delete hashtable; hashtable = 0;}
00091 return *this;
00092 }
00093
00094 Field& Field::resize(const unsigned n, DataNode *A)
00095 {
00096 if (dat_vec) {
00097 delete [] dat_vec;
00098 dat_vec=0;
00099 }
00100 ne = n;
00101 dat_vec = A;
00102 return *this;
00103 }
00104
00105
00106 Field& Field::operator+=(const Field& a)
00107 {
00108 if (ne == a.ne ) {
00109 if (col_struct.type() == 'S') {
00110 warning("Field += Field: string column");
00111 }
00112 else {
00113 for (unsigned i=0; i<ne; i++) {
00114 if (!a.dat_vec[i].missing) {
00115 dat_vec[i].data.double_value += a.dat_vec[i].data.double_value;
00116 }
00117 }
00118 }
00119 }
00120 else {
00121 warning("operator +=, size not conformable");
00122 }
00123 return *this;
00124 }
00125
00126 Field& Field::operator-=(const Field& a)
00127 {
00128 if (ne == a.ne ) {
00129 if (col_struct.type() == 'S') {
00130 warning("Field -= Field: string column");
00131 }
00132 else {
00133 for (unsigned i=0; i<ne; i++) {
00134 if (!a.dat_vec[i].missing) {
00135 dat_vec[i].data.double_value -= a.dat_vec[i].data.double_value;
00136 }
00137 }
00138 }
00139 }
00140 else {
00141 warning("operator -=, Fields not conformable");
00142 }
00143 return *this;
00144 }
00145
00146 Field& Field::operator*=(const Field& a)
00147 {
00148 if (ne == a.ne ) {
00149 if (col_struct.type() == 'S') {
00150 warning("Field *= Field: string column");
00151 }
00152 else {
00153 for (unsigned i=0; i<ne; i++) {
00154 if (!a.dat_vec[i].missing) {
00155 dat_vec[i].data.double_value *= a.dat_vec[i].data.double_value;
00156 }
00157 }
00158 }
00159 }
00160 else {
00161 warning("operator *=, Fields not conformable");
00162 }
00163 return *this;
00164 }
00165
00166 Field& Field::operator/=(const Field& a)
00167 {
00168 if (ne != a.ne ) {
00169 if (col_struct.type() == 'S') {
00170 warning("Field /= Field: string column");
00171 }
00172 else {
00173 for (unsigned i=0; i<ne; i++) {
00174 if (!a.dat_vec[i].missing) {
00175 dat_vec[i].data.double_value /= a.dat_vec[i].data.double_value;
00176 }
00177 }
00178 }
00179 }
00180 else {
00181 warning("operator /=, Fields not conformable");
00182 }
00183 return *this;
00184 }
00185
00186 Field& Field::operator+=(const double s)
00187 {
00188 if (col_struct.type() == 'S') {
00189 warning("Field += s: failed for string column");
00190 }
00191 else {
00192 for (unsigned i=0; i<ne; i++) dat_vec[i].data.double_value += s;
00193 }
00194 return *this;
00195 }
00196 Field& Field::operator-=(const double s)
00197 {
00198 if (col_struct.type() == 'S') {
00199 warning("Field -= s: failed for string column");
00200 }
00201 else {
00202 for (unsigned i=0; i<ne; i++) dat_vec[i].data.double_value -= s;
00203 }
00204 return *this;
00205 }
00206
00207 Field& Field::operator*=(const double s)
00208 {
00209 if (col_struct.type() == 'S') {
00210 warning("Field *= s: failed for string column");
00211 }
00212 else {
00213 for (unsigned i=0; i<ne; i++) {
00214 dat_vec[i].data.double_value *= s;
00215 }
00216 }
00217 return *this;
00218 }
00219
00220 Field& Field::operator/=(const double s)
00221 {
00222 if (col_struct.type() == 'S') {
00223 warning("Field /= s: failed for string column");
00224 }
00225 else {
00226 for (unsigned i=0; i<ne; i++) dat_vec[i].data.double_value /= s;
00227 }
00228 return *this;
00229 }
00230
00231 Vector<bool> Field::operator==(const Field& a) const
00232 {
00233 Vector<bool> temp(ne);
00234 if (ne != a.len() ) {
00235 warning("Field::operator==(), size not conformable");
00236 return temp;
00237 }
00238
00239 if (col_struct.type() != a.col_struct.type() ) return temp;
00240 if (col_struct.nlevel() != a.col_struct.nlevel()) return temp;
00241
00242 if (col_struct.type() == 'S') {
00243 char *str, *a_str;
00244 for (unsigned i=0; i<ne; i++) {
00245 if (!dat_vec[i].missing && !a.dat_vec[i].missing) {
00246 str = (char *)hashtable->find(dat_vec[i].unsigned_val());
00247 a_str = (char *)a.hashtable->find(dat_vec[i].unsigned_val());
00248 if (strcmp(str,a_str)==0) temp[i] = true;
00249 }
00250 }
00251 }
00252 else if (col_struct.type() == 'F') {
00253 for (unsigned i=0; i<ne; i++) {
00254 if (dat_vec[i].double_val()==a.dat_vec[i].double_val()) temp[i]= true;
00255 }
00256 }
00257 return temp;
00258 }
00259
00260 Vector<bool> Field::operator==(const double x) const
00261 {
00262 Vector<bool> temp(ne);
00263
00264 if (col_struct.type() == 'S') return temp;
00265 for (unsigned i=0; i<ne; i++) {
00266 if (dat_vec[i].double_val()==x) temp[i] = true;
00267 }
00268 return temp;
00269 }
00270
00271 Vector<bool> Field::operator!=(const Field& a) const {return !(*this == a);}
00272 Vector<bool> Field::operator<=(const Field& a) const {return !(*this > a);}
00273 Vector<bool> Field::operator>=(const Field& a) const {return !(*this < a);}
00274
00275 Vector<bool> Field::operator<(const Field& a) const
00276 {
00277 Vector<bool> temp(ne);
00278 if (ne != a.len() ) {
00279 warning("A==B, two Fields are not conformable");
00280 return temp;
00281 }
00282
00283 if (col_struct.type() != a.col_struct.type() ) return temp;
00284 if (col_struct.nlevel() != a.col_struct.nlevel()) return temp;
00285
00286 if (col_struct.type() == 'S') {
00287 char *str, *a_str;
00288 for (unsigned i=0; i<ne; i++) {
00289 if (!dat_vec[i].missing && !a.dat_vec[i].missing) {
00290 str = (char *)hashtable->find(dat_vec[i].unsigned_val());
00291 a_str = (char *)a.hashtable->find(dat_vec[i].unsigned_val());
00292 if (strcmp(str,a_str) < 0) temp[i] = true;
00293 }
00294 }
00295 }
00296 else if (col_struct.type() == 'F') {
00297 for (unsigned i=0; i<ne; i++) {
00298 if (dat_vec[i].double_val()<a.dat_vec[i].double_val()) temp[i]= true;
00299 }
00300 }
00301 return temp;
00302
00303 }
00304
00305 Vector<bool> Field::operator > (const Field& a) const
00306 {
00307 Vector<bool> temp(ne);
00308 if (ne != a.len() ) {
00309 warning("A==B, two Fields are not conformable");
00310 return temp;
00311 }
00312
00313 if (col_struct.type() != a.col_struct.type() ) return temp;
00314 if (col_struct.nlevel() != a.col_struct.nlevel()) return temp;
00315
00316 if (col_struct.type() == 'S') {
00317 char *str, *a_str;
00318 for (unsigned i=0; i<ne; i++) {
00319 if (!dat_vec[i].missing && !a.dat_vec[i].missing) {
00320 str = (char *)hashtable->find(dat_vec[i].unsigned_val());
00321 a_str = (char *)a.hashtable->find(dat_vec[i].unsigned_val());
00322 if (strcmp(str,a_str) > 0) temp[i] = true;
00323 }
00324 }
00325 }
00326 else if (col_struct.type() == 'F') {
00327 for (unsigned i=0; i<ne; i++) {
00328 if (dat_vec[i].double_val()>a.dat_vec[i].double_val()) temp[i]= true;
00329 }
00330 }
00331 return temp;
00332 }
00333
00334 Vector<bool> operator==(const double a, const Field& b) {return (b==a);}
00335 Vector<bool> operator!=(const Field& a,const double b) {return !(a==b);}
00336 Vector<bool> operator!=(const double a,const Field& b) {return !(b==a); }
00337 Vector<bool> operator>(const Field& a,const double b) {return (b<a);}
00338 Vector<bool> operator>(const double a,const Field& b) {return (b<a);}
00339 Vector<bool> operator<=(const Field& a,const double b) {return !(b<a);}
00340 Vector<bool> operator<=(const double a,const Field& b) {return !(b<a);}
00341 Vector<bool> operator>=(const Field& a,const double b) {return !(a<b);}
00342 Vector<bool> operator>=(const double a,const Field& b) {return !(a<b);}
00343
00344 Vector<bool> operator<(const Field& A,const double x)
00345 {
00346 unsigned n = A.ne;
00347 Vector<bool> temp(n);
00348
00349 if (A.col_struct.type() == 'S') return temp;
00350 for (unsigned i=0; i<n; i++) {
00351 if (A.dat_vec[i].double_val() < x) temp[i] = true;
00352 }
00353 return temp;
00354 }
00355
00356 Vector<bool> operator<(const double x,const Field& A)
00357 {
00358 unsigned n = A.ne;
00359 Vector<bool> temp(n);
00360
00361 if (A.col_struct.type() == 'S') return temp;
00362 for (unsigned i=0; i<n; i++) {
00363 if (x < A.dat_vec[i].double_val()) temp[i] = true;
00364 }
00365 return temp;
00366 }
00367
00368 Field operator+(const Field& A,const Field& B)
00369 {
00370 unsigned n = A.ne;
00371 if (n != B.ne) {
00372 warning("Col + Col: size not conformable");
00373 return Field();
00374 }
00375 if (A.col_struct.type()=='S' || B.col_struct.type()=='S') {
00376 warning("Col + Col: failed for string column");
00377 return Field();
00378 }
00379 DataNode *temp;
00380 if(n>0){
00381 temp = new DataNode [n];
00382 }
00383 else {
00384 temp = 0;
00385 }
00386 for (unsigned i=0; i<n; i++) {
00387 if (A.dat_vec[i].missing || B.dat_vec[i].missing) {
00388 temp[i].missing = 1;
00389 }
00390 else {
00391 temp[i].double_val(A.dat_vec[i].double_val() +
00392 B.dat_vec[i].double_val());
00393 }
00394 }
00395 return Field(n,temp,A.col_struct);
00396 }
00397
00398 Field operator-(const Field& A,const Field& B)
00399 {
00400 unsigned n = A.ne;
00401 if (n != B.ne) {
00402 warning("Col - Col: size not conformable");
00403 return Field();
00404 }
00405 if (A.col_struct.type()=='S' || B.col_struct.type()=='S') {
00406 warning("Col - Field: failed for string column");
00407 return Field();
00408 }
00409 DataNode *temp;
00410 if(n>0){
00411 temp = new DataNode [n];
00412 }
00413 else {
00414 temp = 0;
00415 }
00416 for (unsigned i=0; i<n; i++) {
00417 if (A.dat_vec[i].missing || B.dat_vec[i].missing) {
00418 temp[i].missing = 1;
00419 }
00420 else {
00421 temp[i].double_val(A.dat_vec[i].double_val()-
00422 B.dat_vec[i].double_val());
00423 }
00424 }
00425 return Field(n,temp,A.col_struct);
00426 }
00427
00428 Field operator*(const Field& A,const Field& B)
00429 {
00430 unsigned n = A.ne;
00431 if (n != B.ne) {
00432 warning("Col * Col: size not conformable");
00433 return Field();
00434 }
00435 if (A.col_struct.type()=='S' || B.col_struct.type()=='S') {
00436 warning("Col * Field: failed for string column");
00437 return Field();
00438 }
00439 DataNode *temp;
00440 if(n>0){
00441 temp = new DataNode [n];
00442 }
00443 else {
00444 temp = 0;
00445 }
00446 for (unsigned i=0; i<n; i++) {
00447 if (A.dat_vec[i].missing || B.dat_vec[i].missing) {
00448 temp[i].missing = 1;
00449 }
00450 else {
00451 temp[i].double_val(A.dat_vec[i].double_val()*
00452 B.dat_vec[i].double_val());
00453 }
00454 }
00455 return Field(n,temp,A.col_struct);
00456 }
00457
00458 Field operator/(const Field& A,const Field& B)
00459 {
00460 unsigned n = A.ne;
00461 if (n != B.ne) {
00462 warning("Col / Col: size not conformable");
00463 return Field();
00464 }
00465 if (A.col_struct.type()=='S' || B.col_struct.type()=='S') {
00466 warning("Field / Field: failed for string column");
00467 return Field();
00468 }
00469 DataNode *temp;
00470 if(n>0){
00471 temp = new DataNode [n];
00472 }
00473 else {
00474 temp = 0;
00475 }
00476 for (unsigned i=0; i<n; i++) {
00477 if (A.dat_vec[i].missing || B.dat_vec[i].missing){
00478 temp[i].missing = 1;
00479 }
00480 else {
00481 temp[i].double_val(A.dat_vec[i].double_val()/
00482 B.dat_vec[i].double_val());
00483 }
00484 }
00485 return Field(n,temp,A.col_struct);
00486 }
00487
00488 Field operator+(const Field& A,const double s)
00489 {
00490 if (A.col_struct.type()=='S') {
00491 warning("Field + s: failed for string column");
00492 return Field();
00493 }
00494 DataNode *temp;
00495 if(A.ne>0){
00496 temp = new DataNode [A.ne];
00497 }
00498 else {
00499 temp = 0;
00500 }
00501 for (unsigned i=0; i<A.ne; i++) {
00502 if (A.dat_vec[i].missing) {
00503 temp[i].missing = 1;
00504 }
00505 else {
00506 temp[i].double_val(A.dat_vec[i].double_val() + s);
00507 }
00508 }
00509 return Field(A.ne,temp,A.col_struct);
00510 }
00511
00512 Field operator-(const Field& A,const double s)
00513 {
00514 if (A.col_struct.type()=='S') {
00515 warning("Field - s: failed for string column");
00516 return Field();
00517 }
00518 DataNode *temp;
00519 if(A.ne>0){
00520 temp = new DataNode [A.ne];
00521 }
00522 else {
00523 temp = 0;
00524 }
00525 for (unsigned i=0; i<A.ne; i++) {
00526 if (A.dat_vec[i].missing) {
00527 temp[i].missing = 1;
00528 }
00529 else {
00530 temp[i].double_val(A.dat_vec[i].double_val() - s);
00531 }
00532 }
00533 return Field(A.ne,temp,A.col_struct);
00534 }
00535
00536 Field operator*(const Field& A,const double s)
00537 {
00538 if (A.col_struct.type()=='S') {
00539 warning("Field * s: failed for string column");
00540 return Field();
00541 }
00542 DataNode *temp;
00543 if(A.ne>0){
00544 temp = new DataNode [A.ne];
00545 }
00546 else {
00547 temp = 0;
00548 }
00549 for (unsigned i=0; i<A.ne; i++) {
00550 if (A.dat_vec[i].missing) {
00551 temp[i].missing = 1;
00552 }
00553 else {
00554 temp[i].double_val(A.dat_vec[i].double_val() * s);
00555 }
00556 }
00557 return Field(A.ne,temp,A.col_struct);
00558 }
00559
00560 Field operator/(const Field& A,const double s)
00561 {
00562 if (A.col_struct.type()=='S') {
00563 warning("Field / s: failed for string column");
00564 return Field();
00565 }
00566 DataNode *temp;
00567 if(A.ne>0){
00568 temp = new DataNode [A.ne];
00569 }
00570 else {
00571 temp = 0;
00572 }
00573 for (unsigned i=0; i<A.ne; i++) {
00574 if (A.dat_vec[i].missing) {
00575 temp[i].missing = 1;
00576 }
00577 else {
00578 temp[i].double_val(A.dat_vec[i].double_val() / s);
00579 }
00580 }
00581 return Field(A.ne,temp,A.col_struct);
00582 }
00583
00584 Field operator+(const double s,const Field& B)
00585 {
00586 if (B.col_struct.type()=='S') {
00587 warning("s + Field: failed for string column");
00588 return Field();
00589 }
00590 DataNode *temp;
00591 if(B.ne>0){
00592 temp = new DataNode [B.ne];
00593 }
00594 else {
00595 temp = 0;
00596 }
00597 for (unsigned i=0; i<B.ne; i++) {
00598 if (B.dat_vec[i].missing) {
00599 temp[i].missing = 1;
00600 }
00601 else {
00602 temp[i].double_val(s + B.dat_vec[i].double_val());
00603 }
00604 }
00605 return Field(B.ne,temp,B.col_struct);
00606 }
00607
00608 Field operator-(const double s,const Field& B)
00609 {
00610 if (B.col_struct.type()=='S') {
00611 warning("s - Field: failed for string column");
00612 return Field();
00613 }
00614 DataNode *temp;
00615 if(B.ne>0){
00616 temp = new DataNode [B.ne];
00617 }
00618 else {
00619 temp = 0;
00620 }
00621 for (unsigned i=0; i<B.ne; i++) {
00622 if (B.dat_vec[i].missing) {
00623 temp[i].missing = 1;
00624 }
00625 else {
00626 temp[i].double_val(s - B.dat_vec[i].double_val());
00627 }
00628 }
00629 return Field(B.ne,temp,B.col_struct);
00630 }
00631
00632 Field operator*(const double s,const Field& B)
00633 {
00634 if (B.col_struct.type()=='S') {
00635 warning("s * Field: failed for string column");
00636 return Field();
00637 }
00638 DataNode *temp;
00639 if(B.ne>0){
00640 temp = new DataNode [B.ne];
00641 }
00642 else {
00643 temp = 0;
00644 }
00645 for (unsigned i=0; i<B.ne; i++) {
00646 if (B.dat_vec[i].missing) {
00647 temp[i].missing = 1;
00648 }
00649 else {
00650 temp[i].double_val( s * B.dat_vec[i].double_val());
00651 }
00652 }
00653 return Field(B.ne,temp,B.col_struct);
00654 }
00655 Field operator/(const double s,const Field& B)
00656 {
00657 if (B.col_struct.type()=='S') {
00658 warning("s / Col: failed for string column");
00659 return Field();
00660 }
00661 DataNode *temp;
00662 if(B.ne>0){
00663 temp = new DataNode [B.ne];
00664 }
00665 else {
00666 temp = 0;
00667 }
00668 for (unsigned i=0; i<B.ne; i++) {
00669 if (B.dat_vec[i].missing) {
00670 temp[i].missing = 1;
00671 }
00672 else {
00673 temp[i].double_val(s / B.dat_vec[i].double_val());
00674 }
00675 }
00676 return Field(B.ne,temp,B.col_struct);
00677 }
00678
00679 Field Field::operator!(void) const
00680 {
00681 if (col_struct.type()=='S') {
00682 warning("!Field: failed for string column");
00683 return Field();
00684 }
00685 DataNode *temp;
00686 if(ne>0){
00687 temp = new DataNode [ne];
00688 }
00689 else {
00690 temp = 0;
00691 }
00692 for (unsigned i=0; i<ne; i++) {
00693 if (dat_vec[i].missing) {
00694 temp[i].missing = 1;
00695 }
00696 else {
00697 temp[i].double_val(-(dat_vec[i].double_val()));
00698 }
00699 }
00700 return Field(ne,temp,col_struct);
00701 }
00702
00703 Field Field::map(double (*f)(double)) const
00704 {
00705 if (col_struct.type()=='S') {
00706 warning("Field:map(): failed for string column");
00707 return Field();
00708 }
00709 DataNode *temp;
00710 if(ne>0){
00711 temp = new DataNode [ne];
00712 }
00713 else {
00714 temp = 0;
00715 }
00716 for (unsigned i=0; i<ne; i++) {
00717 if (dat_vec[i].missing) {temp[i].missing = 1; }
00718 else { temp[i].double_val((*f)(dat_vec[i].double_val())); }
00719 }
00720 return Field(ne,temp,col_struct);
00721 }
00722
00723 Field Field::sub(const int i1,const int i2) const
00724 {
00725 if (ne == 0) return Field();
00726 int newne = i2 - i1 + 1;
00727 if ( newne >ne || i2 > ne) {
00728 warning("Field::s(), subscript out of range");
00729 return Field();
00730 }
00731 unsigned id,i,k;
00732 HashTable *tmp_hashtable = 0;
00733 FieldStruct tmp_struct = col_struct;
00734
00735 DataNode *temp;
00736 if(newne>0){
00737 temp = new DataNode [newne];
00738 }
00739 else {
00740 temp = 0;
00741 }
00742 if (col_struct.type()=='S') {
00743 tmp_hashtable = new HashTable;
00744 tmp_hashtable->resize(col_struct.nlevel());
00745 for (i=i1-1; i<i2; i++) {
00746 tmp_hashtable->insert((char *)hashtable->find(i+1));
00747 }
00748 id = tmp_hashtable->size();
00749 tmp_struct.nlevel(id);
00750 tmp_hashtable->resize(id);
00751 for (k=0,i=i1-1; i<i2; i++) {
00752 id = tmp_hashtable->insert((char *)hashtable->find(i+1));
00753 temp[k++].unsigned_val(id);
00754 }
00755 }
00756 else {
00757 memcpy(temp,&dat_vec[i1-1],sizeof(DataNode)*newne);
00758 }
00759 return Field(newne,temp,tmp_struct,tmp_hashtable);
00760 }
00761
00762 Field sin(Field& a){return a.map(std::sin);}
00763 Field asin(Field& a){return a.map(std::asin);}
00764 Field cos(Field& a){return a.map(std::cos);}
00765 Field acos(Field& a){return a.map(std::acos);}
00766 Field tan(Field& a){return a.map(std::tan);}
00767 Field atan(Field& a){return a.map(std::atan);}
00768 Field ceil(Field& a){return a.map(std::ceil);}
00769 Field floor(Field& a){return a.map(std::floor);}
00770 Field log(Field& a){return a.map(std::log);}
00771 Field log10(Field& a){return a.map(std::log10);}
00772 Field exp(Field& a){return a.map(std::exp);}
00773 Field sqrt(Field& a){return a.map(std::sqrt);}
00774 Field abs(Field& a){return a.map(std::fabs);}
00775 Field erf(Field& a){return a.map(::erf);}
00776 Field erfc(Field& a){return a.map(::erfc);}
00777
00778 DataNode Field::max(void) const
00779 {
00780 DataNode retval;
00781 retval.missing = 1;
00782
00783 if (col_struct.type() != 'S') {
00784 unsigned i,k=0;
00785 while (dat_vec[k].missing) k++;
00786 double y,mx = dat_vec[k].double_val();
00787 for (i=k; i<ne; i++) {
00788 if (!dat_vec[i].missing) {
00789 y = dat_vec[i].double_val();
00790 if ( y > mx) mx = y;
00791 }
00792 }
00793 if (k < ne) {
00794 retval.missing = 0;
00795 retval.double_val(mx);
00796 }
00797 }
00798 return retval;
00799 }
00800
00801 DataNode Field::min(void) const
00802 {
00803 DataNode retval;
00804 retval.missing = 1;
00805
00806 if (col_struct.type() != 'S') {
00807 unsigned i, k=0;
00808 while (dat_vec[k].missing) k++;
00809 double y,mx = dat_vec[k].double_val();
00810 for (i=k; i<ne; i++) {
00811 if (!dat_vec[i].missing) {
00812 y = dat_vec[i].double_val();
00813 if ( y < mx) mx = y;
00814 }
00815 }
00816 if (k < ne) {
00817 retval.missing = 0;
00818 retval.double_val(mx);
00819 }
00820 }
00821 return retval;
00822 }
00823
00824 DataNode Field::sum(void) const
00825 {
00826 DataNode retval;
00827 retval.missing = 1;
00828
00829 if (col_struct.type() != 'S') {
00830 unsigned i,k;
00831 double x;
00832 for (x=0.0, k=0,i=0; i<ne; i++) {
00833 if (!dat_vec[i].missing) {
00834 k++;
00835 x += dat_vec[i].double_val();
00836 }
00837 }
00838 if (k) {
00839 retval.missing = 0;
00840 retval.double_val(x);
00841 }
00842 }
00843 return retval;
00844 }
00845
00846 DataNode Field::sumsq(void) const
00847 {
00848 DataNode retval;
00849 retval.missing = 1;
00850 if (col_struct.type() !='S') {
00851 unsigned i,k;
00852 double y,x;
00853 for (x=0.0, k=0,i=0; i<ne; i++) {
00854 if (!dat_vec[i].missing) {
00855 k++;
00856 y = dat_vec[i].double_val();
00857 x += y*y;
00858 }
00859 }
00860 if (k) {
00861 retval.missing = 0;
00862 retval.double_val(x);
00863 }
00864 }
00865 return retval;
00866 }
00867
00868 DataNode Field::product(void) const
00869 {
00870 DataNode retval;
00871 retval.missing = 1;
00872 if (col_struct.type() != 'S') {
00873 unsigned i,k=0;
00874 double x;
00875 for (x=1.0,k=0,i=0; i<ne; i++) {
00876 if (!dat_vec[i].missing) {
00877 k++;
00878 x *= dat_vec[i].double_val();
00879 }
00880 }
00881 if (k) {
00882 retval.missing = 0;
00883 retval.double_val(x);
00884 }
00885 }
00886 return retval;
00887 }
00888
00889 DataNode Field::mean(const int flag)
00890 {
00891 if (flag == 0 || col_struct.type()=='S') return mean_value;
00892
00893 unsigned i,k;
00894 double x;
00895 for (x=0.0,k=0,i=0; i<ne; i++) {
00896 if (!dat_vec[i].missing) {
00897 k++;
00898 x += dat_vec[i].double_val();
00899 }
00900 }
00901 if (k >=1 ) {
00902 x /= k;
00903 mean_value.missing = 0;
00904 mean_value.double_val(x);
00905 }
00906 return mean_value;
00907 }
00908
00909
00910 DataNode Field::covariance(const Field *B) const
00911 {
00912 if (B) throw exception("Field::covariance(B): not yet implemented");
00913 DataNode retval;
00914 retval.missing = 1;
00915
00916 if (col_struct.type() != 'S') {
00917 unsigned i,k;
00918 for (k=0,i=0; i<ne; i++) if (!dat_vec[i].missing) k++;
00919 if (k >= 2) {
00920 Vector<double> tmp(k);
00921 for (k=0,i=0; i<ne; i++) {
00922 if (!dat_vec[i].missing) tmp[k++] = dat_vec[i].double_val();
00923 }
00924 retval.double_val(tmp.variance());
00925 retval.missing = 0;
00926 }
00927 }
00928 return retval;
00929 }
00930
00931 void Field::value_for_missing(const double vm)
00932 {
00933 if (col_struct.type() == 'F') {
00934 for (unsigned i=0; i<ne; i++) {
00935 if (dat_vec[i].missing) dat_vec[i].double_val(vm);
00936 }
00937 }
00938 else {
00939 warning("Field::value_for_missing(): won't work for string column");
00940 }
00941 }
00942
00943 void Field::set_missing(const unsigned k)
00944 {
00945 if (k >= ne) throw exception("Field::set_missing(): out of range");
00946 if (!dat_vec[k].missing) col_struct.count_miss(1);
00947 dat_vec[k].missing = 1;
00948 }
00949
00950 void Field::pretend_missing(const unsigned k)
00951 {
00952 if (k >= ne) throw exception("Field::pretend_missing(): out of range");
00953 dat_vec[k].missing++;
00954 col_struct.count_miss(1);
00955 }
00956
00957 void Field::recover_missing(const unsigned k)
00958 {
00959 if (k >= ne) throw exception("Field::recover_missing(): out of range");
00960 if (dat_vec[k].missing <= 0) {
00961 warning("recover::recover_missing(): can't recover from missing");
00962 }
00963 else {
00964 dat_vec[k].missing--;
00965 col_struct.count_miss(-1);
00966 }
00967 }
00968
00969 int compare_DataNode(const void* A,const void* B)
00970 {
00971 DataNode *x = (DataNode *)A;
00972 DataNode *y = (DataNode *)B;
00973 if (x->missing && y->missing) {
00974 return 0;
00975 }
00976 else if (x->missing && !y->missing) {
00977 return 1;
00978 }
00979 else if (!x->missing && y->missing) {
00980 return -1;
00981 }
00982 else {
00983 if (x->double_val() < y->double_val()) {
00984 return -1;
00985 }
00986 else if (x->double_val() == y->double_val()) {
00987 return 0;
00988 }
00989 else {
00990 return 1;
00991 }
00992 }
00993 }
00994
00995 Field& Field::sort(void)
00996 {
00997 qsort((DataNode*)dat_vec,ne,sizeof(DataNode),compare_DataNode);
00998 return *this;
00999 }
01000
01001 void Field::out_to_stream(std::ostream& stream,const int ic) const
01002 {
01003 stream << "\n";
01004 unsigned i,k;
01005 stream.precision(6);
01006 unsigned W = 6+6;
01007 char ch;
01008
01009 if (col_struct.type()=='S') {
01010 for (k=23,i=0; i<ne; i++) {
01011 if (ic && i>=k) {
01012 k += 23;
01013 stream << " more ... [q for quit] ";
01014 std::cin.get(ch);
01015 std::cin.seekg(0L,std::ios::beg);
01016 if (ch == 'q') break;
01017 }
01018 if (dat_vec[i].missing) {
01019 stream << " " << std::setw(W) << ".";
01020 }
01021 else {
01022 stream << " " << std::setw(W)
01023 << (char *)hashtable->find(dat_vec[i].unsigned_val());
01024 }
01025 stream << "\n";
01026 }
01027 }
01028 else {
01029 for (k=23,i=0; i<ne; i++) {
01030 if (ic && i>=k) {
01031 k += 23;
01032 stream << " more ... [q for quit] ";
01033 std::cin.get(ch);
01034 std::cin.seekg(0L,std::ios::beg);
01035 if (ch == 'q') break;
01036 }
01037 if (dat_vec[i].missing) {
01038 stream << " " << std::setw(W) << ".";
01039 }
01040 else {
01041 stream << " " << std::setw(W) << dat_vec[i].double_val();
01042 }
01043 stream << "\n";
01044 }
01045 }
01046 }
01047
01048 void Field::save(const std::string &fname,const int io_mode) const
01049 {
01050 std::ofstream ofs;
01051 ofs.open(fname.c_str(),(OpenModeType)io_mode);
01052 if (!ofs) throw exception("Field::save(): cannot open file");
01053 this->out_to_stream(ofs,0);
01054 ofs.close();
01055 }
01056
01057 void Field::display(const std::string &msg,const int ic) const
01058 {
01059 if (msg != "") std::cout <<"\n " << msg << "\n";
01060 this->out_to_stream(std::cout,ic);
01061 }
01062
01063 std::ostream& operator<<(std::ostream& stream, const Field& V)
01064 {
01065 V.out_to_stream(stream,1);
01066 return stream;
01067 }
01068 }
01069