37 #include <boost/numeric/ublas/vector.hpp> 38 #include <boost/numeric/ublas/matrix.hpp> 40 #include <o2scl/misc.h> 41 #include <o2scl/interp.h> 43 #include <o2scl/shunting_yard.h> 45 #ifndef DOXYGEN_NO_O2NS 49 template<
class vec_t>
class table;
68 #ifndef DOXYGEN_NO_O2NS 192 template<
class vec_t=std::vector<
double> >
class table {
208 if (intp_set==
true) {
217 size_t nc=t.get_nconsts();
218 for(
size_t i=0;i<nc;i++) {
221 t.get_constant(i,name,val);
222 constants.insert(make_pair(name,val));
229 nlines=t.get_nlines();
232 for(
size_t i=0;i<t.get_ncolumns();i++) {
235 std::string cname=t.get_column_name(i);
239 s.
dat.resize(nlines);
240 s.
index=atree.size();
241 atree.insert(make_pair(cname,s));
244 aiter it=atree.find(cname);
248 for(
size_t j=0;j<t.get_nlines();j++) {
249 it->second.dat[j]=t.get(cname,j);
268 size_t nc=t.get_nconsts();
269 for(
size_t i=0;i<nc;i++) {
272 t.get_constant(i,name,val);
273 constants.insert(make_pair(name,val));
277 nlines=t.get_nlines();
283 for(
size_t i=0;i<t.get_ncolumns();i++) {
286 std::string cname=t.get_column_name(i);
290 s.
dat.resize(nlines);
291 s.
index=atree.size();
292 atree.insert(make_pair(cname,s));
295 aiter it=atree.find(cname);
299 for(
size_t j=0;j<t.get_nlines();j++) {
300 it->second.dat[j]=t.get(cname,j);
327 void set(std::string scol,
size_t row,
double val) {
328 if (!std::isfinite(val)) {
330 "' not finite for column '"+
331 scol+
"' in table::set(string,size_t,double)").c_str(),
336 if (maxlines==0) inc_maxlines(row+1);
337 while(row>maxlines-1) inc_maxlines(maxlines);
338 if (row>=nlines) set_nlines_auto(row+1);
340 if ((intp_colx==scol || intp_coly==scol) && intp_set==
true) {
345 aiter it=atree.find(scol);
346 if (it==atree.end()) {
350 if (it==atree.end()) {
351 O2SCL_ERR((((std::string)
"Refused to add column '")+scol+
352 "' in table::set(string,size_t,double).").c_str(),
357 it->second.dat[row]=val;
364 void set(
size_t icol,
size_t row,
double val) {
365 if (!std::isfinite(val)) {
366 if (icol>=get_ncolumns()) {
368 "' not finite and index "+
szttos(icol)+
369 " too large in table::set(size_t,size_t,double)").c_str(),
373 "' not finite for column '"+get_column_name(icol)+
374 "' in table::set(size_t,size_t,double)").c_str(),
377 if (icol>=atree.size()) {
378 std::string err=((std::string)
"Column out of range, ")+
szttos(icol)+
379 ">="+
szttos(atree.size())+
", in table::set(size_t,size_t,double).";
383 while(row>maxlines-1) inc_maxlines(maxlines);
384 if (row>=nlines) set_nlines_auto(row+1);
386 std::string scol=get_column_name(icol);
387 if ((intp_colx==scol || intp_coly==scol) && intp_set==
true) {
392 alist[icol]->second.dat[row]=val;
401 template<
class size_vec_t>
void set_row(
size_t row, size_vec_t &v) {
402 if (row>=get_nlines()) {
405 for(
size_t i=0;i<get_ncolumns() && i<v.size();i++) {
406 alist[i]->second.dat[row]=v[i];
414 double get(std::string scol,
size_t row)
const {
416 aciter it=atree.find(scol);
417 if (it==atree.end()) {
418 O2SCL_ERR((((std::string)
"Column '")+scol+
419 "' not found in table::get().").c_str(),
423 tmp=it->second.dat[row];
431 double get(
size_t icol,
size_t row)
const {
432 if (icol>=atree.size()) {
433 std::string err=((std::string)
"Column out of range, ")+
434 itos(icol)+
">="+
itos(atree.size())+
", in table::get(size_t,size_t).";
439 std::string err=((std::string)
"Column out of range, ")+
440 itos(row)+
">="+
itos(nlines)+
", in table::get(size_t,size_t).";
444 return alist[icol]->second.dat[row];
471 inc_maxlines(il-maxlines);
504 template<
class resize_vec_t>
505 void get_row(std::string scol,
double val, resize_vec_t &row)
const {
507 int irow=lookup(scol,val);
509 O2SCL_ERR((((std::string)
"Column '")+scol+
510 "' not found in table::get_row() const.").c_str(),
530 template<
class resize_vec_t>
531 void get_row(
size_t irow, resize_vec_t &row)
const {
535 "' not found in table::get_row().").c_str(),
542 row.allocate(atree.size());
543 for(i=0,it=atree.begin();it!=atree.end();it++,i++) {
544 row[i]=(it->second.dat)[irow];
563 size_t inc=il-maxlines;
564 if (inc<maxlines) inc=maxlines;
589 for(
aiter it=atree.begin();it!=atree.end();it++) {
592 temp_col.resize(maxlines+llines);
593 for(
size_t j=0;j<maxlines;j++) {
594 temp_col[j]=it->second.dat[j];
598 it->second.dat.resize(maxlines+llines);
601 for(
size_t j=0;j<maxlines;j++) {
602 it->second.dat[j]=temp_col[j];
620 aciter it=atree.find(scol);
621 if (it==atree.end()) {
622 O2SCL_ERR((((std::string)
"Column '")+scol+
623 "' not found in table::get_column() const.").c_str(),
627 return it->second.dat;
644 const vec_t &operator[] (
size_t icol)
const {
645 #if !O2SCL_NO_RANGE_CHECK 646 if (icol>=atree.size()) {
649 " in table::operator[size_t] const. Size: "+
651 " (index should be less than size).").c_str(),
exc_eindex);
655 return (alist[icol]->second.dat);
673 aciter it=atree.find(scol);
674 #if !O2SCL_NO_RANGE_CHECK 675 if (it==atree.end()) {
676 O2SCL_ERR((((std::string)
"Column '")+scol+
"' not found in table::"+
681 return (it->second.dat);
691 if (head.length()==0) {
692 O2SCL_ERR2(
"Cannot add column with empty name in ",
695 if (is_column(head)==
true) {
696 O2SCL_ERR((((std::string)
"Column '")+head+
697 "' already present in table::new_column().").c_str(),
700 for(
int i=0;i<((int)head.size());i++) {
701 if (head[i]==
' ' || head[i]==
'\t' || head[i]==
'\n' || head[i]==
'\r' 702 || head[i]==
'\v' || head[i]==
'\f') {
703 O2SCL_ERR((((std::string)
"Invalid column name '")+head+
704 "' in table::new_column().").c_str(),
709 s.
dat.resize(maxlines);
710 s.
index=((int)atree.size());
711 atree.insert(make_pair(head,s));
712 aiter it=atree.find(head);
731 size_t sz, vec2_t &v) {
735 "table::new_column(string,size_t,vec2_t)",
740 int ret=new_column(name);
741 if (ret!=0)
return ret;
745 if (sz>get_nlines()) mxl=get_nlines();
746 for(
size_t i=0;i<mxl;i++) {
759 if (icol+1>atree.size()) {
761 " larger than number of "+
762 "columns in table::get_column_name().").c_str(),
765 return alist[icol]->first;
778 aiter its=atree.find(scol);
779 if (its==atree.end()) {
780 O2SCL_ERR((((std::string)
"Column '")+scol+
781 " not found in table::swap_column_data().").c_str(),
785 if (v.size()!=its->second.dat.size()) {
786 O2SCL_ERR2(
"Vector sizes not commensurate in ",
789 std::swap(its->second.dat,v);
797 aiter its=atree.find(src);
798 if (its==atree.end()) {
799 O2SCL_ERR((((std::string)
"Column '")+src+
800 " not found in table::rename_column().").c_str(),
805 aiter itd=atree.find(dest);
806 std::swap(its->second.dat,itd->second.dat);
819 aiter it=atree.find(scol);
820 if (it==atree.end()) {
821 O2SCL_ERR((((std::string)
"Column '")+scol+
822 " not found in table::delete_column().").c_str(),
829 vit+=it->second.index;
834 alist[alist.size()-1]->second.index=it->second.index;
843 if ((intp_colx==scol || intp_coly==scol) && intp_set==
true) {
855 if (icol+1>atree.size()) {
859 for(
size_t i=0;i<icol;i++) it++;
871 if (!std::isfinite(val)) {
873 "' not finite for column '"+
874 scol+
"' in table::init_column()").c_str(),
exc_einval);
877 aiter it=atree.find(scol);
878 if (it==atree.end()) {
879 O2SCL_ERR((((std::string)
"Column '")+scol+
880 "' not found in table::init_column()").c_str(),
884 for(
size_t i=0;i<nlines;i++) {
885 it->second.dat[i]=val;
887 if (intp_set && (scol==intp_colx || scol==intp_coly)) {
900 aciter it=atree.find(scol);
901 if (it==atree.end())
return false;
912 aciter it=atree.find(lname);
913 if (it==atree.end()) {
914 O2SCL_ERR(
"Column not found in table::lookup_column().",
917 return it->second.index;
925 if (!is_column(dest)) new_column(dest);
926 aiter its=atree.find(src);
927 if (its==atree.end()) {
928 O2SCL_ERR((((std::string)
"Column '")+src+
929 " not found in table::copy_column().").c_str(),
933 aiter itd=atree.find(dest);
934 if (itd==atree.end()) {
935 O2SCL_ERR((((std::string)
"Destination column '")+dest+
936 " not found in table::copy_column().").c_str(),
940 for(
size_t i=0;i<nlines;i++) {
941 itd->second.dat[i]=its->second.dat[i];
951 template<
class resize_vec_t>
954 for(
size_t i=0;i<nlines;i++) {
955 v[i]=this->
get(scol,i);
965 template<
class vec2_t>
967 for(
size_t i=0;i<nlines;i++) {
968 this->
set(scol,i,v[i]);
983 template<
class vec2_t>
985 std::string src_index, std::string src_col,
986 std::string dest_index, std::string dest_col=
"") {
988 if (dest_col==
"") dest_col=src_col;
991 if (!is_column(dest_col)) new_column(dest_col);
994 for(
size_t i=0;i<nlines;i++) {
995 set(dest_col,i,source.interp(src_index,
get(dest_index,i),src_col));
1005 template<
class vec2_t>
1007 bool allow_extrap=
true, std::string dest_index=
"") {
1009 if (dest_index==
"") dest_index=src_index;
1012 double min=source.min(src_index);
1013 double max=source.max(src_index);
1014 if (allow_extrap==
false) {
1015 if (!std::isfinite(min) || !std::isfinite(max)) {
1016 O2SCL_ERR2(
"Minimum or maximum of source index not finite ",
1022 std::vector<std::string> col_list;
1023 for(
size_t i=0;i<source.get_ncolumns();i++) {
1024 std::string
col=source.get_column_name(i);
1025 if (col!=src_index && col!=dest_index && is_column(col)==
false) {
1026 col_list.push_back(col);
1031 for(
size_t i=0;i<col_list.size();i++) {
1032 new_column(col_list[i]);
1033 for(
size_t j=0;j<get_nlines();j++) {
1034 double val=
get(dest_index,j);
1035 if (allow_extrap || (val>=min && val<=max)) {
1036 set(col_list[i],j,source.interp(src_index,val,col_list[i]));
1056 if (nlines>=maxlines) inc_maxlines(maxlines);
1059 for(
int i=((
int)nlines)-2;i>=((int)n);i--) {
1074 for(
int i=0;i<((int)atree.size());i++) {
1075 set(i,dest,
get(i,src));
1090 size_t irow=lookup(scol,val);
1098 for(
aiter it=atree.begin();it!=atree.end();it++) {
1100 for(
int i=((
int)irow);i<((int)nlines)-1;i++) {
1101 it->second.dat[i]=it->second.dat[i+1];
1105 if (intp_set==
true) {
1119 size_t new_nlines=0;
1120 for(
size_t i=0;i<nlines;i++) {
1121 double val=row_function(func,i);
1127 if (i!=new_nlines) {
1128 for(
aiter it=atree.begin();it!=atree.end();it++) {
1129 it->second.dat[new_nlines]=it->second.dat[i];
1136 if (intp_set==
true) {
1152 template<
class vec2_t>
1156 for(
size_t i=0;i<get_ncolumns();i++) {
1157 std::string cname=get_column_name(i);
1158 if (dest.is_column(cname)==
false) {
1159 dest.new_column(cname);
1163 size_t new_lines=dest.get_nlines();
1164 for(
size_t i=0;i<nlines;i++) {
1165 double val=row_function(func,i);
1167 dest.set_nlines_auto(new_lines+1);
1168 for(
size_t j=0;j<get_ncolumns();j++) {
1169 std::string cname=get_column_name(j);
1170 dest.set(cname,new_lines);
1195 if (row_start>=nlines || row_end>=nlines) {
1196 O2SCL_ERR2(
"Row specifications beyond end of table in ",
1197 "table::delete_rows(size_t,size_t).",
exc_einval);
1199 size_t new_nlines=0;
1200 for(
size_t i=0;i<nlines;i++) {
1201 if ((row_start<=row_end && (i<row_start || i>row_end)) ||
1202 (row_start>row_end && (i<row_start && i>row_end))) {
1203 for(
aiter it=atree.begin();it!=atree.end();it++) {
1204 it->second.dat[new_nlines]=it->second.dat[i];
1210 if (intp_set==
true) {
1235 std::istringstream is(newheads);
1241 O2SCL_ERR2(
"At least one new column failed in ",
1255 if (maxlines==0) inc_maxlines(1);
1256 if (nlines>=maxlines) inc_maxlines(maxlines);
1263 if (nlines<maxlines && nv<=(atree.size())) {
1265 set_nlines(nlines+1);
1266 for(
size_t i=0;i<nv;i++) {
1267 (*this).set(i,nlines-1,v[i]);
1284 line_of_data(v.size(),v);
1303 if (!std::isfinite(val)) {
1305 "' not finite for column '"+
1306 scol+
"' in table::ordered_lookup()").c_str(),
exc_einval);
1309 aciter it=atree.find(scol);
1310 if (it==atree.end()) {
1311 O2SCL_ERR((((std::string)
"Column '")+scol+
1312 " not found in table::ordered_lookup().").c_str(),
1325 size_t lookup(std::string scol,
double val)
const {
1326 if (!std::isfinite(val)) {
1328 "' not finite for column '"+
1329 scol+
"' in table::lookup()").c_str(),
exc_einval);
1332 aciter it=atree.find(scol);
1333 if (it==atree.end()) {
1334 O2SCL_ERR((((std::string)
"Column '")+scol+
" not found in "+
1343 const vec_t &ov=it->second.dat;
1347 while(!std::isfinite(ov[i]) && i<nlines-1) i++;
1355 double best=ov[i], bdiff=fabs(ov[i]-val);
1356 for(;i<nlines;i++) {
1357 if (std::isfinite(ov[i]) && fabs(ov[i]-val)<bdiff) {
1360 bdiff=fabs(ov[i]-val);
1368 double lookup_val(std::string scol,
double val, std::string scol2)
const {
1370 if (!std::isfinite(val)) {
1372 "' not finite for column '"+
1373 scol+
"' in table::lookup_val()").c_str(),
exc_einval);
1376 aciter it=atree.find(scol);
1377 if (it==atree.end()) {
1378 O2SCL_ERR((((std::string)
"Column '")+scol+
" not found in "+
1382 return get(scol2,it->second.dat->lookup(val));
1389 return lookup(get_column_name(icol),val);
1395 size_t mlookup(std::string scol,
double val, std::vector<size_t> &results,
1396 double threshold=0.0)
const {
1398 if (!std::isfinite(val)) {
1400 "' not finite for column '"+
1401 scol+
"' in table::mlookup()").c_str(),
exc_einval);
1404 aciter it=atree.find(scol);
1405 if (it==atree.end()) {
1406 O2SCL_ERR((((std::string)
"Column '")+scol+
" not found in "+
1410 if (threshold==0.0) {
1411 for(i=0;i<nlines;i++) {
1412 if (it->second.dat[i]==val) {
1413 results.push_back(i);
1417 for(i=0;i<nlines;i++) {
1418 if (fabs(it->second.dat[i]-val)<threshold) {
1419 results.push_back(i);
1423 return results.size();
1453 double interp(std::string sx,
double x0, std::string sy) {
1455 aiter itx=atree.find(sx), ity=atree.find(sy);
1456 if (itx==atree.end() || ity==atree.end()) {
1457 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1458 "' not found in table::interp().").c_str(),
1462 if (!std::isfinite(x0)) {
1466 if (intp_set==
false || sx!=intp_colx || sy!=intp_coly) {
1467 if (intp_set==
true) {
1473 ity->second.dat,itype);
1489 aciter itx=atree.find(sx), ity=atree.find(sy);
1490 if (itx==atree.end() || ity==atree.end()) {
1491 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1492 "' not found in table::interp_const().").c_str(),
1496 if (!std::isfinite(x0)) {
1509 double interp(
size_t ix,
double x0,
size_t iy) {
1510 return interp(get_column_name(ix),x0,get_column_name(iy));
1517 return interp_const(get_column_name(ix),x0,get_column_name(iy));
1524 void deriv(std::string x, std::string y, std::string yp) {
1525 aiter itx, ity, ityp;
1530 ityp=atree.find(yp);
1532 if (itx==atree.end() || ity==atree.end() || ityp==atree.end()) {
1537 size_t ix=lookup_column(x);
1538 size_t iy=lookup_column(y);
1539 for(
int i=0;i<((int)nlines);i++) {
1540 ityp->second.dat[i]=deriv(ix,(itx->second.dat)[i],iy);
1551 double deriv(std::string sx,
double x0, std::string sy) {
1553 aiter itx=atree.find(sx), ity=atree.find(sy);
1554 if (itx==atree.end() || ity==atree.end()) {
1555 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1556 "' not found in table::deriv().").c_str(),
1560 if (!std::isfinite(x0)) {
1564 if (intp_set==
false || sx!=intp_colx || sy!=intp_coly) {
1565 if (intp_set==
true) {
1571 (nlines,itx->second.dat,ity->second.dat,itype);
1587 aciter itx=atree.find(sx), ity=atree.find(sy);
1588 if (itx==atree.end() || ity==atree.end()) {
1589 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1590 "' not found in table::deriv_const().").c_str(),
1594 if (!std::isfinite(x0)) {
1599 (nlines,itx->second.dat,ity->second.dat,itype);
1609 double deriv(
size_t ix,
double x0,
size_t iy) {
1610 return deriv(get_column_name(ix),x0,get_column_name(iy));
1619 return deriv_const(get_column_name(ix),x0,get_column_name(iy));
1625 void deriv2(std::string x, std::string y, std::string yp) {
1626 aiter itx, ity, ityp;
1631 ityp=atree.find(yp);
1633 if (itx==atree.end() || ity==atree.end() || ityp==atree.end()) {
1638 size_t ix=lookup_column(x);
1639 size_t iy=lookup_column(y);
1640 for(
int i=0;i<((int)nlines);i++) {
1641 ityp->second.dat[i]=deriv2(ix,itx->second.dat[i],iy);
1652 double deriv2(std::string sx,
double x0, std::string sy) {
1654 aiter itx=atree.find(sx), ity=atree.find(sy);
1655 if (itx==atree.end() || ity==atree.end()) {
1656 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1657 "' not found in table::deriv2().").c_str(),
1661 if (!std::isfinite(x0)) {
1665 if (intp_set==
false || sx!=intp_colx || sy!=intp_coly) {
1666 if (intp_set==
true) {
1672 (nlines,itx->second.dat,ity->second.dat,itype);
1688 aciter itx=atree.find(sx), ity=atree.find(sy);
1689 if (itx==atree.end() || ity==atree.end()) {
1690 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1691 "' not found in table::deriv2_const().").c_str(),
1695 if (!std::isfinite(x0)) {
1700 (nlines,itx->second.dat,ity->second.dat,itype);
1710 double deriv2(
size_t ix,
double x0,
size_t iy) {
1711 return deriv2(get_column_name(ix),x0,get_column_name(iy));
1720 return deriv2_const(get_column_name(ix),x0,get_column_name(iy));
1728 double integ(std::string sx,
double x1,
double x2, std::string sy) {
1730 aiter itx=atree.find(sx), ity=atree.find(sy);
1731 if (itx==atree.end() || ity==atree.end()) {
1732 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1733 "' not found in table::integ().").c_str(),
1737 if (!std::isfinite(x1) || !std::isfinite(x2)) {
1738 std::string msg=((std::string)
"Value x1=")+
dtos(x1)+
" or x2="+
1739 dtos(x2)+
" not finite in table.integ().";
1742 if (intp_set==
false || sx!=intp_colx || sy!=intp_coly) {
1743 if (intp_set==
true) {
1749 (nlines,itx->second.dat,ity->second.dat,itype);
1754 ret=si->
integ(x1,x2);
1764 std::string sy)
const {
1766 aciter itx=atree.find(sx), ity=atree.find(sy);
1767 if (itx==atree.end() || ity==atree.end()) {
1768 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1769 "' not found in table::integ_const().").c_str(),
1773 if (!std::isfinite(x1) || !std::isfinite(x2)) {
1778 (nlines,itx->second.dat,ity->second.dat,itype);
1779 ret=sic.
integ(x1,x2);
1789 return integ(get_column_name(ix),x1,x2,
1790 get_column_name(iy));
1799 return integ_const(get_column_name(ix),x1,x2,
1800 get_column_name(iy));
1808 void integ(std::string x, std::string y, std::string ynew) {
1809 aiter itx, ity, itynew;
1814 itynew=atree.find(ynew);
1816 if (itx==atree.end() || ity==atree.end() || itynew==atree.end()) {
1821 size_t ix=lookup_column(x);
1822 size_t iy=lookup_column(y);
1823 for(
size_t i=0;i<nlines;i++) {
1824 itynew->second.dat[i]=integ(ix,(itx->second.dat)[0],
1825 (itx->second.dat)[i],iy);
1834 double max(std::string scol)
const {
1837 if (is_column(scol)==
false) {
1838 O2SCL_ERR((((std::string)
"Column '")+scol+
1842 const vec_t &dcol=get_column(scol);
1844 for(i=0;i<((int)nlines);i++) {
1845 if (std::isfinite(dcol[i])) {
1849 }
else if (dcol[i]>ret) {
1855 O2SCL_ERR((((std::string)
"No finite values in column '")+scol+
1865 double min(std::string scol)
const {
1868 if (is_column(scol)==
false) {
1869 O2SCL_ERR((((std::string)
"Column '")+scol+
1873 const vec_t &dcol=get_column(scol);
1875 for(i=0;i<((int)nlines);i++) {
1876 if (std::isfinite(dcol[i])) {
1880 }
else if (dcol[i]<ret) {
1886 O2SCL_ERR((((std::string)
"No finite values in column '")+scol+
1917 sublines=bottom-top+1;
1919 O2SCL_ERR2(
"Can't make a subtable of an empty table. ",
1920 "Returning 0 in table::subtable().",
1924 if (bottom+1>nlines) {
1925 O2SCL_ERR2(
"Requested row beyond nlines. Adjusting ",
1926 "and continuing in table::subtable().",
exc_einval);
1930 std::istringstream is(list);
1932 tnew.set_nlines(sublines);
1934 it=atree.find(head);
1935 if (it==atree.end()) {
1937 ((((std::string)
"Couldn't find column named ")+head+
1938 " in table::subtable(). Returning 0.").c_str(),
1941 tnew.new_column(head);
1942 vec_t &dcol=tnew.get_column(head);
1943 for(i=0;i<sublines;i++) {
1944 dcol[i]=it->second.dat[i+top];
1947 if (tnew.get_ncolumns()==0) {
1948 O2SCL_ERR(
"Subtable has no columns in table::subtable().",
1952 tnew.nlines=sublines;
1967 for(it=atree.begin();it!=atree.end();it++) {
1968 for(
int j=0;j<((int)nlines);j++) {
1969 it->second.dat[j]=0.0;
1995 if (intp_set==
true) {
2010 if (intp_set==
true) {
2035 size_t ncols=get_ncolumns(), nlins=get_nlines();
2039 for(
size_t i=0;i<ncols;i++) {
2040 for(
size_t j=0;j<nlins;j++) {
2041 data_copy(i,j)=
get(i,j);
2046 aiter it=atree.find(scol);
2047 vec_t &data=it->second.dat;
2049 for(
size_t i=0;i<ncols;i++) {
2050 for(
size_t j=0;j<nlins;j++) {
2051 set(i,j,data_copy(i,order[j]));
2067 aiter it=atree.find(scol);
2068 if (it==atree.end()) {
2069 O2SCL_ERR((((std::string)
"Column '")+scol+
2070 " not found in table::sort_column().").c_str(),
2077 if (intp_set && (scol==intp_colx || scol==intp_coly)) {
2095 virtual void summary(std::ostream *out,
size_t ncol=79)
const {
2097 if (constants.size()==1) {
2098 (*out) <<
"1 constant:" << std::endl;
2100 (*out) << constants.size() <<
" constants:" << std::endl;
2102 std::map<std::string,double>::const_iterator mit;
2103 for(mit=constants.begin();mit!=constants.end();mit++) {
2104 (*out) << mit->first <<
" " << mit->second << std::endl;
2108 size_t nh=get_ncolumns(), nh2;
2112 (*out) <<
"No columns." << std::endl;
2117 (*out) <<
"1 column: " << std::endl;
2119 (*out) << nh <<
" columns: " << std::endl;
2121 std::vector<std::string> h(nh);
2122 for(
size_t i=0;i<nh;i++) {
2123 h[i]=
szttos(i)+
". "+get_column_name(i);
2126 std::vector<std::string> h2;
2132 for(
size_t i=0;i<nh2;i++) {
2133 (*out) << h2[i] << std::endl;
2138 if (get_nlines()==0) (*out) <<
"No lines of data." << std::endl;
2139 else if (get_nlines()==1) (*out) <<
"One line of data." << std::endl;
2140 (*out) << get_nlines() <<
" lines of data." << std::endl;
2152 if (constants.find(name)!=constants.end()) {
2153 constants.find(name)->second=val;
2156 constants.insert(make_pair(name,val));
2171 bool err_on_notfound=
true) {
2172 if (constants.find(name)!=constants.end()) {
2173 constants.find(name)->second=val;
2176 if (err_on_notfound) {
2177 std::string err=((std::string)
"No constant with name '")+name+
2178 "' in table::set_constant().";
2186 if (constants.find(name)==constants.end()) {
2194 if (constants.find(name)==constants.end()) {
2195 std::string err=((std::string)
"No constant with name '")+name+
2196 "' in table::get_constant().";
2199 return constants.find(name)->second;
2204 return constants.size();
2208 virtual void get_constant(
size_t ix, std::string &name,
double &val)
const {
2209 if (ix<constants.size()) {
2210 std::map<std::string,double>::const_iterator cit=constants.begin();
2211 for(
size_t i=0;i<ix;i++) cit++;
2222 constants.erase(name);
2237 std::vector<std::string> onames, nnames;
2239 std::istringstream is(line);
2240 while (is >> cname) {
2241 onames.push_back(cname);
2243 std::cout <<
"Read possible column name: " << cname << std::endl;
2249 for(
size_t i=0;i<onames.size();i++) {
2255 if (n_nums==onames.size()) {
2258 std::cout <<
"First row looks like it contains numerical values." 2260 std::cout <<
"Creating generic column names: ";
2263 for(
size_t i=0;i<onames.size();i++) {
2264 nnames.push_back(((std::string)
"c")+
szttos(i+1));
2265 if (verbose>0) std::cout << nnames[i] <<
" ";
2268 if (verbose>0) std::cout << std::endl;
2271 for(
size_t i=0;i<nnames.size();i++) {
2272 new_column(nnames[i]);
2276 for(
size_t i=0;i<onames.size();i++) {
2284 for(
size_t i=0;i<onames.size();i++) {
2285 std::string temps=onames[i];
2286 make_fp_varname(temps);
2287 make_unique_name(temps,nnames);
2288 nnames.push_back(temps);
2289 if (temps!=onames[i] && verbose>0) {
2290 std::cout <<
"Converted column named '" << onames[i] <<
"' to '" 2291 << temps <<
"'." << std::endl;
2296 for(
size_t i=0;i<nnames.size();i++) {
2297 new_column(nnames[i]);
2303 while ((fin) >> data) {
2305 for(
size_t i=1;i<get_ncolumns();i++) {
2323 if (atree.size()!=alist.size()) {
2324 O2SCL_ERR2(
"Size of table and list do not match in ",
2328 for(
aciter it=atree.begin();it!=atree.end();it++) {
2329 if (it->second.index!=alist[it->second.index]->second.index) {
2330 O2SCL_ERR((((std::string)
"Problem with iterator for entry '")+
2331 it->first+
"' in list in table::check_synchro().").c_str(),
2335 for(
int i=0;i<((int)atree.size());i++) {
2336 if (alist[i]->second.index!=i) {
2337 O2SCL_ERR((((std::string)
"Problem with index of entry ")+
2338 itos(i)+
" in list in table::check_synchro().").c_str(),
2347 virtual const char *
type() {
return "table"; }
2384 std::vector<std::string> funcs, names;
2387 std::istringstream is(list);
2388 while(is >> stemp) funcs.push_back(stemp);
2389 for(
size_t i=0;i<(funcs.size());i++) {
2390 names.push_back(funcs[i].substr(0,funcs[i].find(
"=")));
2391 funcs[i]=funcs[i].substr(funcs[i].find(
"=")+1,
2392 funcs[i].length()-funcs[i].find(
"=")-1);
2393 if (names[i].length()==0 || funcs[i].length()==0) {
2400 std::map<std::string,double> vars;
2401 std::map<std::string,double>::const_iterator mit;
2402 for(mit=constants.begin();mit!=constants.end();mit++) {
2403 vars[mit->first]=mit->second;
2406 std::vector<calculator> calcs(funcs.size());
2407 std::vector<vec_t> newcols(funcs.size());
2409 for(
size_t j=0;j<funcs.size();j++) {
2410 calcs[j].compile(funcs[j].c_str(),&vars);
2411 newcols[j].resize(maxlines);
2415 for(
size_t i=0;i<nlines;i++) {
2418 for(
size_t j=0;j<atree.size();j++) {
2419 vars[get_column_name(j)]=(*this)[j][i];
2423 for(
size_t j=0;j<funcs.size();j++) {
2424 newcols[j][i]=calcs[j].eval(&vars);
2428 for(
size_t j=0;j<funcs.size();j++) {
2429 if (!is_column(names[j])) {
2430 new_column(names[j]);
2432 swap_column_data(names[j],newcols[j]);
2451 if (!is_column(scol)) {
2456 aiter it2=atree.find(scol);
2457 vec_t &colp=it2->second.dat;
2460 function_vector(
function,colp);
2478 template<
class resize_vec_t>
2480 bool throw_on_err=
true) {
2484 std::map<std::string,double> vars;
2485 std::map<std::string,double>::const_iterator mit;
2486 for(mit=constants.begin();mit!=constants.end();mit++) {
2487 vars[mit->first]=mit->second;
2489 calc.
compile(
function.c_str(),&vars);
2492 if (vec.size()<nlines) vec.resize(nlines);
2495 std::vector<double> vals(atree.size());
2498 for(
size_t j=0;j<nlines;j++) {
2499 for(
aciter it=atree.begin();it!=atree.end();it++) {
2500 vars[it->first]=it->second.dat[j];
2502 vec[j]=calc.
eval(&vars);
2514 std::map<std::string,double> vars;
2515 std::map<std::string,double>::const_iterator mit;
2516 for(mit=constants.begin();mit!=constants.end();mit++) {
2517 vars[mit->first]=mit->second;
2519 calc.
compile(
function.c_str(),&vars);
2521 for(
aciter it=atree.begin();it!=atree.end();it++) {
2522 vars[it->first]=it->second.dat[row];
2525 double dret=calc.
eval(&vars);
2535 std::map<std::string,double> vars;
2536 std::map<std::string,double>::const_iterator mit;
2537 for(mit=constants.begin();mit!=constants.end();mit++) {
2538 vars[mit->first]=mit->second;
2540 calc.
compile(
function.c_str(),&vars);
2542 double best_val=0.0;
2544 for(
size_t row=0;row<nlines-1;row++) {
2545 for(
aciter it=atree.begin();it!=atree.end();it++) {
2546 vars[it->first]=it->second.dat[row];
2548 double dtemp=calc.
eval(&vars);
2552 if (dtemp>best_val) {
2565 friend void o2scl_hdf::hdf_output
2571 friend void o2scl_hdf::hdf_output_data
2579 #ifndef DOXYGEN_INTERNAL 2596 for(it=atree.begin();it!=atree.end();it++) {
2597 alist[it->second.index]=it;
2606 if (s==
"abs" || s==
"acos" || s==
"acosh" || s==
"asin" ||
2607 s==
"asinh" || s==
"atan" || s==
"atan2" || s==
"atanh" ||
2608 s==
"ceil" || s==
"cos" || s==
"cosh" || s==
"cot" || s==
"csc" ||
2609 s==
"eval" || s==
"exp" || s==
"floor" || s==
"if" || s==
"int" ||
2610 s==
"log" || s==
"log10" || s==
"max" || s==
"min" || s==
"sec" ||
2611 s==
"sin" || s==
"sinh" || s==
"sqrt" || s==
"tan" || s==
"tanh") {
2612 s=((std::string)
"v_")+s;
2613 }
else if (s[0]>=
'0' && s[0]<=
'9') {
2614 s=((std::string)
"v_")+s;
2617 for(
size_t i=0;i<s.length();i++) {
2618 if (!isalpha(s[i]) && !isdigit(s[i]) && s[i]!=
'_') s[i]=
'_';
2630 for(
size_t i=0;i<cnames.size();i++) {
2631 if (colx==cnames[i]) {
2639 }
while (done==
false);
2660 typedef typename std::map<std::string,
col,
2661 std::greater<std::string> >::iterator
aiter;
2663 typedef typename std::map<std::string,col,
2664 std::greater<std::string> >::const_iterator
2667 typedef typename std::vector<aiter>::iterator
aviter;
2677 std::map<std::string,col,std::greater<std::string> >
atree;
2686 aiter it=atree.find(lname);
2687 if (it==atree.end()) {
2688 O2SCL_ERR((((std::string)
"Column '")+lname+
2689 " not found in table::get_iterator().").c_str(),
2696 aiter it=atree.find(lname);
2697 if (it==atree.end()) {
2698 O2SCL_ERR((((std::string)
"Column '")+lname+
2699 " not found in table::get_col_struct().").c_str(),
2703 return &(it->second);
2708 aiter
end() {
return atree.end(); }
2736 #ifndef DOXYGEN_NO_O2NS void delete_rows(size_t row_start, size_t row_end)
Delete all rows between row_start and row_end .
size_t mlookup(std::string scol, double val, std::vector< size_t > &results, double threshold=0.0) const
Exhaustively search column col for many occurences of val .
virtual void delete_column(std::string scol)
Delete column named scol .
size_t nlines
The size of presently used memory.
aiter begin()
Return the beginning of the column tree.
void deriv(std::string x, std::string y, std::string yp)
Make a new column yp which is the derivative . .
double max(std::string scol) const
Return column maximum. Makes no assumptions about ordering .
void clear_table()
Clear the table and the column names (but leave constants)
const vec_t & operator[](std::string scol) const
Returns the column named scol (const version). .
void line_of_data(const vec2_t &v)
Read a line of data and store in a new row of the table.
void copy_to_column(vec2_t &v, std::string scol)
Copy to a column from a generic vector object.
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
std::string intp_coly
The last y-column interpolated.
size_t get_nlines() const
Return the number of lines.
double integ(size_t ix, double x1, double x2, size_t iy)
The integral of the function iy(ix) from ix=x1 to ix=x2.
interp_vec< vec_t > * si
Interpolation object.
double min(std::string scol) const
Return column minimum. Makes no assumptions about ordering .
double deriv(std::string sx, double x0, std::string sy)
The first derivative of the function sy(sx) at sx=x0.
double integ_const(size_t ix, double x1, double x2, size_t iy) const
The integral of the function iy(ix) from ix=x1 to ix=x2.
void hdf_input_data(hdf_file &hf, o2scl::table< vec_t > &t)
Internal function for inputting a o2scl::table object.
void compile(const char *expr, std::map< std::string, double > *vars=0, bool debug=false, std::map< std::string, int > opPrec=opPrecedence)
Compile expression expr using variables specified in vars.
void copy_rows(std::string func, table< vec2_t > &dest)
Copy all rows matching a particular condition to a new table.
virtual double deriv2(const double x0) const
Give the value of the second derivative .
void subtable(std::string list, size_t top, size_t bottom, table< vec_t > &tnew) const
Make a subtable.
void zero_table()
Zero the data entries but keep the column names and nlines fixed.
void set_nlines(size_t il)
Set the number of lines.
void functions_columns(std::string list)
Create new columns or recompute from a list of functions.
void new_row(size_t n)
Insert a row before row n.
sanity check failed - shouldn't happen
int function_vector(std::string function, resize_vec_t &vec, bool throw_on_err=true)
Compute a column from a function specified in a string.
aiter get_iterator(std::string lname)
Return the iterator for a column.
double deriv_const(std::string sx, double x0, std::string sy) const
The first derivative of the function sy(sx) at sx=x0.
invalid argument supplied by user
void deriv2(std::string x, std::string y, std::string yp)
Make a new column yp which is - O(log(C)*R).
virtual int set_constant(std::string name, double val, bool err_on_notfound=true)
Set a constant equal to a value, but don't add it if not already present.
void set_nlines_auto(size_t il)
Set the number of lines, increasing the size more agressively.
double deriv(size_t ix, double x0, size_t iy)
The first derivative of the function iy(ix) at ix=x0.
std::string intp_colx
The last x-column interpolated.
virtual void remove_constant(std::string name)
Remove a constant.
double integ_const(std::string sx, double x1, double x2, std::string sy) const
The integral of the function sy(sx) from sx=x1 to sx=x2.
A class for representing permutations.
void make_fp_varname(std::string &s)
Ensure a variable name does not match a function or contain non-alphanumeric characters.
double interp_const(size_t ix, double x0, size_t iy) const
Interpolate x0 from ix into iy .
virtual double deriv(const double x0) const
Give the value of the derivative .
int new_column(std::string name, size_t sz, vec2_t &v)
Add a new column by copying data from another vector.
virtual size_t get_nconsts() const
Get the number of constants.
table & operator=(const table &t)
Copy constructor.
double interp_const(std::string sx, double x0, std::string sy) const
Interpolate x0 from sx into sy.
std::map< std::string, col, std::greater< std::string > >::const_iterator aciter
Const map iterator type.
void check_synchro() const
Return 0 if the tree and list are properly synchronized.
Generic "not found" result.
void insert_table(table< vec2_t > &source, std::string src_index, bool allow_extrap=true, std::string dest_index="")
Insert columns from a source table into the new table by interpolation (or extrapolation) ...
vec_t empty_col
An empty vector for get_column()
std::vector< aiter > alist
The list of tree iterators.
const vec_t & get_column(std::string scol) const
Returns a reference to the column named col. .
size_t get_interp_type() const
Get the interpolation type.
double integ(std::string sx, double x1, double x2, std::string sy)
The integral of the function sy(sx) from sx=x1 to sx=x2.
virtual double get_constant(std::string name) const
Get a constant.
void clear_data()
Remove all of the data by setting the number of lines to zero.
void reset_list()
Set the elements of alist with the appropriate iterators from atree. .
std::map< std::string, double > constants
The list of constants.
void vector_sort_index(size_t n, const vec_t &data, vec_size_t &order)
Create a permutation which sorts a vector (in increasing order)
size_t get_maxlines()
Return the maximum number of lines before a reallocation is required.
Cubic spline for natural boundary conditions.
double interp(std::string sx, double x0, std::string sy)
Interpolate x0 from sx into sy.
size_t maxlines
The size of allocated memory.
void sort_column(std::string scol)
Individually sort the column scol.
double lookup_val(std::string scol, double val, std::string scol2) const
Search column col for the value val and return value in col2.
void line_of_names(std::string newheads)
Read a new set of names from newheads.
void init_column(std::string scol, double val)
Initialize all values of column named scol to val .
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
Column structure for table [protected].
The O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl namespace ...
std::string dtos(double x, int prec=6, bool auto_prec=false)
Convert a double to a string.
double deriv2(std::string sx, double x0, std::string sy)
The second derivative of the function sy(sx) at sx=x0.
size_t ordered_lookup(std::string scol, double val) const
Look for a value in an ordered column .
virtual double integ(const double x1, const double x2) const
Give the value of the integral .
virtual void copy_column(std::string src, std::string dest)
Copy data from column named src to column named dest, creating a new column if necessary ...
col * get_col_struct(std::string lname)
Return the column structure for a column.
double interp(size_t ix, double x0, size_t iy)
Interpolate x0 from ix into iy .
void get_row(std::string scol, double val, resize_vec_t &row) const
Returns a copy of the row with value val in column col. .
table(const table &t)
Copy constructor.
virtual const char * type()
Return the type, "table".
void screenify(size_t nin, const string_arr_t &in_cols, std::vector< std::string > &out_cols, size_t max_size=80)
Reformat the columns for output of width size.
double eval(std::map< std::string, double > *vars=0)
Evalate the previously compiled expression using variables specified in vars.
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
void make_unique_name(std::string &colx, std::vector< std::string > &cnames)
Make sure a name is unique.
void line_of_data(size_t nv, const vec2_t &v)
Read a line of data from the first nv entries in a vector and store as a new row in the table...
void copy_row(size_t src, size_t dest)
Copy the data in row src to row dest.
virtual void add_constant(std::string name, double val)
Add a constant, or if the constant already exists, change its value.
void delete_row(std::string scol, double val)
Delete the row with the entry closest to the value val in column scol .
void set_interp_type(size_t interp_type)
Set the base interpolation objects.
bool is_number(std::string s)
Return true if the string s is likely a integral or floating point number.
size_t ordered_lookup(const double x0) const
Find the index of x0 in the ordered array x.
Evaluate a mathematical expression in a string.
double deriv2(size_t ix, double x0, size_t iy)
The second derivative of the function iy(ix) at ix=x0.
void delete_rows(std::string func)
Delete all rows where func evaluates to a number greater than 0.5 .
double deriv2_const(size_t ix, double x0, size_t iy) const
The second derivative of the function iy(ix) at ix=x0.
void delete_row(size_t irow)
Delete the row of index irow .
size_t get_ncolumns() const
Return the number of columns.
virtual bool is_constant(std::string name) const
Test if name is a constant.
void new_column(std::string head)
Add a new column owned by the table table .
void clear()
Clear everything.
virtual void summary(std::ostream *out, size_t ncol=79) const
Output a summary of the information stored.
aiter end()
Return the end of the column tree.
void column_to_vector(std::string scol, resize_vec_t &v) const
Copy a column to a generic vector object.
void function_column(std::string function, std::string scol)
Make a column from the function specified in function and add it to the table.
void integ(std::string x, std::string y, std::string ynew)
The integral of the function iy(ix)
bool is_column(std::string scol) const
Return true if scol is a column in the current table table .
Searching class for monotonic data with caching.
size_t lookup(int icol, double val) const
Exhaustively search column col for the value val .
std::map< std::string, col, std::greater< std::string > >::iterator aiter
Map iterator type.
virtual void get_constant(size_t ix, std::string &name, double &val) const
Get a constant by index.
Store data in an O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$sc...
void set_row(size_t row, size_vec_t &v)
Set an entire row of data.
table(size_t cmaxlines=0)
Create a new table with space for nlines<=cmaxlines.
bool intp_set
True if the interpolation object is up-to-date.
double row_function(std::string function, size_t row) const
Compute a value by applying a function to a row.
std::map< std::string, col, std::greater< std::string > > atree
The tree of columns.
void get_row(size_t irow, resize_vec_t &row) const
Returns a copy of row number irow. .
size_t function_find_row(std::string function) const
Find a row which maximizes a function.
Invalid index for array or matrix.
size_t itype
Current interpolation type.
double stod(std::string s, bool err_on_fail=true)
Convert a string to a double.
virtual int read_generic(std::istream &fin, int verbose=0)
Clear the current table and read from a generic data file.
void add_col_from_table(table< vec2_t > &source, std::string src_index, std::string src_col, std::string dest_index, std::string dest_col="")
Insert a column from a separate table, interpolating it into a new column.
std::string itos(int x)
Convert an integer to a string.
static const double x2[5]
virtual double eval(const double x0) const
Give the value of the function .
std::string get_column_name(size_t icol) const
Returns the name of column col .
static const double x1[5]
std::string szttos(size_t x)
Convert a size_t to a string.
void hdf_input(hdf_file &hf, o2scl::table< vec_t > &t, std::string name)
Input a o2scl::table object from a hdf_file.
vec_t dat
Pointer to column.
std::vector< aiter >::iterator aviter
Vector iterator type.
void clear_constants()
CLear all constants.
void vector_sort_double(size_t n, vec_t &data)
Sort a vector of doubles (in increasing order)
double deriv2_const(std::string sx, double x0, std::string sy) const
The second derivative of the function sy(sx) at sx=x0.
double deriv_const(size_t ix, double x0, size_t iy) const
The first derivative of the function iy(ix) at ix=x0.
size_t lookup_column(std::string lname) const
Find the index for column named name .
size_t lookup(std::string scol, double val) const
Exhaustively search column col for the value val .
Interpolation class for general vectors.
void inc_maxlines(size_t llines)
Manually increase the maximum number of lines.
virtual void swap_column_data(std::string scol, vec_t &v)
Swap the data in column scol with that in vector v.
std::string get_sorted_name(size_t icol) const
Returns the name of column col in sorted order. .
void sort_table(std::string scol)
Sort the entire table by the column scol.
virtual void rename_column(std::string src, std::string dest)
Rename column named src to dest .