Go to the documentation of this file.
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;
77 #ifndef DOXYGEN_NO_O2NS
191 template<
class vec_t=std::vector<
double> >
class table {
227 for(
size_t i=0;i<t.get_ncolumns();i++) {
230 std::string cname=t.get_column_name(i);
236 atree.insert(make_pair(cname,s));
243 for(
size_t j=0;j<t.get_nlines();j++) {
244 it->second.dat[j]=t.get(cname,j);
273 for(
size_t i=0;i<t.get_ncolumns();i++) {
276 std::string cname=t.get_column_name(i);
282 atree.insert(make_pair(cname,s));
289 for(
size_t j=0;j<t.get_nlines();j++) {
290 it->second.dat[j]=t.get(cname,j);
317 void set(std::string scol,
size_t row,
double val) {
322 "table::set(string,size_t,double).";
328 if (it==
atree.end()) {
329 O2SCL_ERR((((std::string)
"Column '")+scol+
330 "' not found in table::set(string,size_t,double).").c_str(),
340 #if !O2SCL_NO_RANGE_CHECK
341 if (row>=it->second.dat.size()) {
342 O2SCL_ERR(
"Vector size failure in table::set(string,size_t,double).",
346 it->second.dat[row]=val;
354 void set(
size_t icol,
size_t row,
double val) {
357 O2SCL_ERR2(
"Specified row beyond end of table in ",
358 "table::set(size_t,size_t,double).",
exc_einval);
362 if (icol>=
atree.size()) {
363 std::string err=((std::string)
"Column index ")+
szttos(icol)+
364 ">="+
szttos(
atree.size())+
", in table::set(size_t,size_t,double).";
374 #if !O2SCL_NO_RANGE_CHECK
375 if (row>=
alist[icol]->second.dat.size()) {
376 O2SCL_ERR(
"Vector size failure in table::set(size_t,size_t,double).",
380 alist[icol]->second.dat[row]=val;
393 template<
class size_vec_t>
void set_row(
size_t row, size_vec_t &v) {
395 std::string err=((std::string)
"Row out of range, ")+
400 alist[i]->second.dat[row]=v[i];
408 double get(std::string scol,
size_t row)
const {
411 if (it==
atree.end()) {
412 O2SCL_ERR((((std::string)
"Column '")+scol+
413 "' not found in table::get(string,size_t).").c_str(),
418 std::string err=((std::string)
"Row out of range, ")+
422 #if !O2SCL_NO_RANGE_CHECK
423 if (row>=it->second.dat.size()) {
424 O2SCL_ERR(
"Vector size failure in table::get().",
428 tmp=it->second.dat[row];
436 double get(
size_t icol,
size_t row)
const {
437 if (icol>=
atree.size()) {
438 std::string err=((std::string)
"Column out of range, ")+
443 std::string err=((std::string)
"Row out of range, ")+
447 return alist[icol]->second.dat[row];
507 template<
class resize_vec_t>
508 void get_row(std::string scol,
double val, resize_vec_t &row)
const {
510 int irow=
lookup(scol,val);
512 O2SCL_ERR((((std::string)
"Column '")+scol+
"' not found in "+
513 "table::get_row(string,double,vec_t) const.").c_str(),
533 template<
class resize_vec_t>
534 void get_row(
size_t irow, resize_vec_t &row)
const {
538 "' not found in table::get_row(size_t,vec_t).").c_str(),
545 row.resize(
atree.size());
546 for(i=0,it=
atree.begin();it!=
atree.end();it++,i++) {
547 row[i]=(it->second.dat)[irow];
597 temp_col[j]=it->second.dat[j];
601 it->second.dat.resize(
maxlines+llines);
605 it->second.dat[j]=temp_col[j];
625 O2SCL_ERR2(
"Cannot set maximum number of lines to be smaller ",
626 "than current size in table::set_maxlines().",
639 temp_col.resize(llines);
640 for(
size_t j=0;j<
nlines;j++) {
641 temp_col[j]=it->second.dat[j];
645 it->second.dat.resize(llines);
648 for(
size_t j=0;j<
nlines;j++) {
649 it->second.dat[j]=temp_col[j];
674 if (it==
atree.end()) {
675 O2SCL_ERR((((std::string)
"Column '")+scol+
676 "' not found in table::get_column() const.").c_str(),
680 return it->second.dat;
704 #if !O2SCL_NO_RANGE_CHECK
705 if (icol>=
atree.size()) {
708 " in table::operator[size_t] const. Size: "+
710 " (index should be less than size).").c_str(),
exc_eindex);
714 return (
alist[icol]->second.dat);
735 #if !O2SCL_NO_RANGE_CHECK
736 if (it==
atree.end()) {
737 O2SCL_ERR((((std::string)
"Column '")+scol+
"' not found in table::"+
742 return (it->second.dat);
752 if (head.length()==0) {
753 O2SCL_ERR2(
"Cannot add column with empty name in ",
757 O2SCL_ERR((((std::string)
"Column '")+head+
758 "' already present in table::new_column().").c_str(),
761 for(
int i=0;i<((int)head.size());i++) {
762 if (head[i]==
' ' || head[i]==
'\t' || head[i]==
'\n' || head[i]==
'\r'
763 || head[i]==
'\v' || head[i]==
'\f') {
764 O2SCL_ERR((((std::string)
"Invalid column name '")+head+
765 "' in table::new_column().").c_str(),
772 atree.insert(make_pair(head,s));
792 size_t sz, vec2_t &v) {
796 "table::new_column(string,size_t,vec2_t)",
802 if (ret!=0)
return ret;
807 for(
size_t i=0;i<mxl;i++) {
820 if (icol+1>
atree.size()) {
822 " larger than number of "+
823 "columns in table::get_column_name().").c_str(),
826 return alist[icol]->first;
840 if (its==
atree.end()) {
841 O2SCL_ERR((((std::string)
"Column '")+scol+
842 " not found in table::swap_column_data().").c_str(),
846 if (v.size()!=its->second.dat.size()) {
847 O2SCL_ERR2(
"Vector sizes not commensurate in ",
850 std::swap(its->second.dat,v);
859 if (its==
atree.end()) {
860 O2SCL_ERR((((std::string)
"Column '")+src+
861 " not found in table::rename_column().").c_str(),
867 std::swap(its->second.dat,itd->second.dat);
881 if (it==
atree.end()) {
882 O2SCL_ERR((((std::string)
"Column '")+scol+
883 " not found in table::delete_column().").c_str(),
890 vit+=it->second.index;
895 alist[
alist.size()-1]->second.index=it->second.index;
916 if (icol+1>
atree.size()) {
920 for(
size_t i=0;i<icol;i++) it++;
941 if (it==
atree.end()) {
942 O2SCL_ERR((((std::string)
"Column '")+scol+
943 "' not found in table::init_column()").c_str(),
947 for(
size_t i=0;i<
nlines;i++) {
948 it->second.dat[i]=val;
964 if (it==
atree.end())
return false;
976 if (it==
atree.end()) {
977 O2SCL_ERR(
"Column not found in table::lookup_column().",
980 return it->second.index;
990 if (its==
atree.end()) {
991 O2SCL_ERR((((std::string)
"Column '")+src+
992 " not found in table::copy_column().").c_str(),
997 if (itd==
atree.end()) {
998 O2SCL_ERR((((std::string)
"Destination column '")+dest+
999 " not found in table::copy_column().").c_str(),
1003 for(
size_t i=0;i<
nlines;i++) {
1004 itd->second.dat[i]=its->second.dat[i];
1014 template<
class resize_vec_t>
1017 for(
size_t i=0;i<
nlines;i++) {
1018 v[i]=this->
get(scol,i);
1028 template<
class vec2_t>
1030 for(
size_t i=0;i<
nlines;i++) {
1031 this->
set(scol,i,v[i]);
1046 template<
class vec2_t>
1048 std::string src_index, std::string src_col,
1049 std::string dest_index, std::string dest_col=
"") {
1051 if (dest_col==
"") dest_col=src_col;
1057 for(
size_t i=0;i<
nlines;i++) {
1058 set(dest_col,i,source.interp(src_index,
get(dest_index,i),src_col));
1098 template<
class vec2_t>
1100 bool allow_extrap=
true, std::string dest_index=
"") {
1102 if (dest_index==
"") dest_index=src_index;
1104 if (!source.is_column(src_index)) {
1112 double min=source.min(src_index);
1113 double max=source.max(src_index);
1114 if (allow_extrap==
false) {
1115 if (!std::isfinite(
min) || !std::isfinite(
max)) {
1116 O2SCL_ERR2(
"Minimum or maximum of source index not finite ",
1122 std::vector<std::string> col_list;
1123 for(
size_t i=0;i<source.get_ncolumns();i++) {
1124 std::string col_name=source.get_column_name(i);
1125 if (col_name!=src_index && col_name!=dest_index) {
1126 col_list.push_back(col_name);
1131 for(
size_t i=0;i<col_list.size();i++) {
1134 double val=
get(dest_index,j);
1135 if (allow_extrap || (val>=
min && val<=
max)) {
1136 set(col_list[i],j,source.interp(src_index,val,col_list[i]));
1159 for(
int i=((
int)
nlines)-2;i>=((int)n);i--) {
1179 for(
int i=0;i<((int)
atree.size());i++) {
1195 size_t irow=
lookup(scol,val);
1208 std::string str=((std::string)
"Cannot delete row ")+
1211 "table::delete_row(size_t).";
1216 for(
int i=((
int)irow);i<((int)
nlines)-1;i++) {
1217 it->second.dat[i]=it->second.dat[i+1];
1235 size_t new_nlines=0;
1236 for(
size_t i=0;i<
nlines;i++) {
1243 if (i!=new_nlines) {
1245 it->second.dat[new_nlines]=it->second.dat[i];
1268 template<
class vec2_t>
1274 if (dest.is_column(cname)==
false) {
1275 dest.new_column(cname);
1279 size_t new_lines=dest.get_nlines();
1280 for(
size_t i=0;i<
nlines;i++) {
1283 dest.set_nlines_auto(new_lines+1);
1286 dest.set(cname,new_lines);
1312 O2SCL_ERR2(
"Row specifications beyond end of table in ",
1313 "table::delete_rows_ends(size_t,size_t).",
exc_einval);
1315 size_t new_nlines=0;
1316 for(
size_t i=0;i<
nlines;i++) {
1317 if ((row_start<=row_end && (i<row_start || i>row_end)) ||
1318 (row_start>row_end && (i<row_start && i>row_end))) {
1320 it->second.dat[new_nlines]=it->second.dat[i];
1342 template<
class vec_
size_t>
1346 for(
size_t j=0;j<row_list.size();j++) {
1347 if (row_list[j]>
nlines) {
1348 O2SCL_ERR(
"Invalid row in table<>::delete_rows_list(vec_size_t &)",
1355 size_t new_nlines=0;
1356 for(
size_t i=0;i<
nlines;i++) {
1358 for(
size_t j=0;j<row_list.size();j++) {
1359 if (row_list[j]==i) found=
true;
1363 it->second.dat[new_nlines]=it->second.dat[i];
1385 double tol_abs=1.0e-20,
1387 std::vector<size_t> list;
1388 for(
size_t i=0;i<
nlines;i++) {
1389 for(
size_t j=i+1;j<
nlines;j++) {
1391 if (i<
nlines && j<nlines && j>i) {
1393 if (fabs(it->second.dat[i])>tol_abs ||
1394 fabs(it->second.dat[j])>tol_abs) {
1395 if (fabs(it->second.dat[i]-it->second.dat[j])/
1396 fabs(it->second.dat[i]+it->second.dat[j])>tol_rel) {
1405 std::cout <<
"Match between rows " << i <<
" and " << j
1409 std::cout << k <<
" " <<
get(k,i) <<
" " <<
get(k,j)
1417 if (list.size()>0) {
1419 }
else if (verbose>0) {
1420 std::cout <<
"No matches found." << std::endl;
1434 std::vector<size_t> row_list;
1435 for(
size_t i=0;i<
nlines-1;i++) {
1438 if (it->second.dat[i+1]!=it->second.dat[i]) match=
false;
1440 if (match) row_list.push_back(i+1);
1466 std::istringstream is(newheads);
1472 O2SCL_ERR2(
"At least one new column failed in ",
1504 for(
size_t i=0;i<nv;i++) {
1505 (*this).set(i,
nlines-1,v[i]);
1544 if (!std::isfinite(val)) {
1546 "' not finite for column '"+
1547 scol+
"' in table::ordered_lookup()").c_str(),
exc_einval);
1551 if (it==
atree.end()) {
1552 O2SCL_ERR((((std::string)
"Column '")+scol+
1553 " not found in table::ordered_lookup().").c_str(),
1566 size_t lookup(std::string scol,
double val)
const {
1567 if (!std::isfinite(val)) {
1569 "' not finite for column '"+
1570 scol+
"' in table::lookup()").c_str(),
exc_einval);
1574 if (it==
atree.end()) {
1575 O2SCL_ERR((((std::string)
"Column '")+scol+
" not found in "+
1584 const vec_t &ov=it->second.dat;
1588 while(!std::isfinite(ov[i]) && i<
nlines-1) i++;
1596 double bdiff=fabs(ov[i]-val);
1598 if (std::isfinite(ov[i]) && fabs(ov[i]-val)<bdiff) {
1600 bdiff=fabs(ov[i]-val);
1608 double lookup_val(std::string scol,
double val, std::string scol2)
const {
1610 if (!std::isfinite(val)) {
1612 "' not finite for column '"+
1613 scol+
"' in table::lookup_val()").c_str(),
exc_einval);
1617 if (it==
atree.end()) {
1618 O2SCL_ERR((((std::string)
"Column '")+scol+
" not found in "+
1622 return get(scol2,it->second.dat->lookup(val));
1635 size_t mlookup(std::string scol,
double val, std::vector<size_t> &results,
1636 double threshold=0.0)
const {
1638 if (!std::isfinite(val)) {
1640 "' not finite for column '"+
1641 scol+
"' in table::mlookup()").c_str(),
exc_einval);
1645 if (it==
atree.end()) {
1646 O2SCL_ERR((((std::string)
"Column '")+scol+
" not found in "+
1650 if (threshold==0.0) {
1652 if (it->second.dat[i]==val) {
1653 results.push_back(i);
1658 if (fabs(it->second.dat[i]-val)<threshold) {
1659 results.push_back(i);
1663 return results.size();
1694 double interp(std::string sx,
double x0, std::string sy) {
1698 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1699 "' not found in table::interp().").c_str(),
1703 if (!std::isfinite(x0)) {
1714 ity->second.dat,
itype);
1733 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1734 "' not found in table::interp_const().").c_str(),
1738 if (!std::isfinite(x0)) {
1752 double interp(
size_t ix,
double x0,
size_t iy) {
1770 void deriv(std::string x, std::string y, std::string yp) {
1772 aiter itx, ity, ityp;
1776 ityp=
atree.find(yp);
1778 if (ityp==
atree.end()) {
1780 ityp=
atree.find(yp);
1784 O2SCL_ERR(
"Column not found in table::deriv(string,string,string).",
1791 for(
int i=0;i<((int)
nlines);i++) {
1792 ityp->second.dat[i]=
deriv(ix,(itx->second.dat)[i],iy);
1805 double deriv(std::string sx,
double x0, std::string sy) {
1809 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1810 "' not found in table::deriv(string,double,string).").c_str(),
1814 if (!std::isfinite(x0)) {
1815 O2SCL_ERR(
"x0 not finite in table::deriv(string,double,string).",
1846 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1847 "' not found in table::deriv_const().").c_str(),
1851 if (!std::isfinite(x0)) {
1868 double deriv(
size_t ix,
double x0,
size_t iy) {
1892 void deriv2(std::string x, std::string y, std::string yp) {
1893 aiter itx, ity, ityp;
1897 ityp=
atree.find(yp);
1899 if (ityp==
atree.end()) {
1901 ityp=
atree.find(yp);
1905 O2SCL_ERR(
"Column not found in table::deriv2(string,string,string).",
1912 for(
int i=0;i<((int)
nlines);i++) {
1913 ityp->second.dat[i]=
deriv2(ix,itx->second.dat[i],iy);
1926 double deriv2(std::string sx,
double x0, std::string sy) {
1930 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1931 "' not found in table::deriv2(string,double,string).").c_str(),
1935 if (!std::isfinite(x0)) {
1936 O2SCL_ERR(
"x0 not finite in table::deriv2(string,double,string).",
1967 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
1968 "' not found in table::deriv2_const().").c_str(),
1972 if (!std::isfinite(x0)) {
1989 double deriv2(
size_t ix,
double x0,
size_t iy) {
2011 double integ(std::string sx,
double x1,
double x2, std::string sy) {
2015 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
2016 "' not found in table::integ"+
2017 "(string,double,double,string).").c_str(),
2021 if (!std::isfinite(
x1) || !std::isfinite(
x2)) {
2022 std::string msg=((std::string)
"Value x1=")+
dtos(
x1)+
" or x2="+
2023 dtos(
x2)+
" not finite in table.integ(string,double,double,string).";
2051 std::string sy)
const {
2055 O2SCL_ERR((((std::string)
"Columns '")+sx+
"' or '"+sy+
2056 "' not found in table::integ_const().").c_str(),
2060 if (!std::isfinite(
x1) || !std::isfinite(
x2)) {
2106 void integ(std::string x, std::string y, std::string ynew) {
2107 aiter itx, ity, itynew;
2111 itynew=
atree.find(ynew);
2113 if (itynew==
atree.end()) {
2115 itynew=
atree.find(ynew);
2119 O2SCL_ERR(
"Column not found in table::integ(string,string,string).",
2126 for(
size_t i=0;i<
nlines;i++) {
2127 itynew->second.dat[i]=
integ(ix,(itx->second.dat)[0],
2128 (itx->second.dat)[i],iy);
2137 double max(std::string scol)
const {
2141 O2SCL_ERR((((std::string)
"Column '")+scol+
2147 for(i=0;i<((int)
nlines);i++) {
2148 if (std::isfinite(dcol[i])) {
2152 }
else if (dcol[i]>ret) {
2158 O2SCL_ERR((((std::string)
"No finite values in column '")+scol+
2168 double min(std::string scol)
const {
2172 O2SCL_ERR((((std::string)
"Column '")+scol+
2178 for(i=0;i<((int)
nlines);i++) {
2179 if (std::isfinite(dcol[i])) {
2183 }
else if (dcol[i]<ret) {
2189 O2SCL_ERR((((std::string)
"No finite values in column '")+scol+
2220 sublines=bottom-top+1;
2222 O2SCL_ERR2(
"Can't make a subtable of an empty table. ",
2223 "Returning 0 in table::subtable().",
2228 O2SCL_ERR2(
"Requested row beyond nlines. Adjusting ",
2229 "and continuing in table::subtable().",
exc_einval);
2233 std::istringstream is(list);
2235 tnew.set_nlines(sublines);
2237 it=
atree.find(head);
2238 if (it==
atree.end()) {
2240 ((((std::string)
"Couldn't find column named ")+head+
2241 " in table::subtable(). Returning 0.").c_str(),
2244 tnew.new_column(head);
2245 vec_t &dcol=tnew.get_column(head);
2246 for(i=0;i<sublines;i++) {
2247 dcol[i]=it->second.dat[i+top];
2250 if (tnew.get_ncolumns()==0) {
2251 O2SCL_ERR(
"Subtable has no columns in table::subtable().",
2255 tnew.nlines=sublines;
2271 for(
int j=0;j<((int)
nlines);j++) {
2272 it->second.dat[j]=0.0;
2342 for(
size_t i=0;i<ncols;i++) {
2343 for(
size_t j=0;j<nlins;j++) {
2344 data_copy(i,j)=
get(i,j);
2350 vec_t &data=it->second.dat;
2352 for(
size_t i=0;i<ncols;i++) {
2353 for(
size_t j=0;j<nlins;j++) {
2354 set(i,j,data_copy(i,order[j]));
2371 if (it==
atree.end()) {
2372 O2SCL_ERR((((std::string)
"Column '")+scol+
2373 " not found in table::sort_column().").c_str(),
2398 virtual void summary(std::ostream *out,
size_t ncol=79)
const {
2401 (*out) <<
"1 constant:" << std::endl;
2403 (*out) <<
constants.size() <<
" constants:" << std::endl;
2405 std::map<std::string,double>::const_iterator mit;
2407 (*out) << mit->first <<
" " << mit->second << std::endl;
2415 (*out) <<
"No columns." << std::endl;
2420 (*out) <<
"1 column: " << std::endl;
2422 (*out) << nh <<
" columns: " << std::endl;
2424 std::vector<std::string> h(nh);
2425 for(
size_t i=0;i<nh;i++) {
2429 std::vector<std::string> h2;
2435 for(
size_t i=0;i<nh2;i++) {
2436 (*out) << h2[i] << std::endl;
2441 if (
get_nlines()==0) (*out) <<
"No lines of data." << std::endl;
2442 else if (
get_nlines()==1) (*out) <<
"One line of data." << std::endl;
2443 (*out) <<
get_nlines() <<
" lines of data." << std::endl;
2474 bool err_on_notfound=
true) {
2479 if (err_on_notfound) {
2480 std::string err=((std::string)
"No constant with name '")+name+
2481 "' in table::set_constant().";
2498 std::string err=((std::string)
"No constant with name '")+name+
2499 "' in table::get_constant(string).";
2511 virtual void get_constant(
size_t ix, std::string &name,
double &val)
const {
2513 std::map<std::string,double>::const_iterator cit=
constants.begin();
2514 for(
size_t i=0;i<ix;i++) cit++;
2519 O2SCL_ERR(
"Index too large in table::get_constant(size_t,string,double).",
2545 std::vector<std::string> onames, nnames;
2547 std::istringstream is(line);
2548 while (is >> cname) {
2549 onames.push_back(cname);
2551 std::cout <<
"Read possible column name: " << cname << std::endl;
2557 for(
size_t i=0;i<onames.size();i++) {
2563 if (n_nums==onames.size()) {
2566 std::cout <<
"First row looks like it contains numerical values."
2568 std::cout <<
"Creating generic column names: ";
2571 for(
size_t i=0;i<onames.size();i++) {
2572 nnames.push_back(((std::string)
"c")+
szttos(i+1));
2573 if (verbose>0) std::cout << nnames[i] <<
" ";
2576 if (verbose>0) std::cout << std::endl;
2579 for(
size_t i=0;i<nnames.size();i++) {
2585 for(
size_t i=0;i<onames.size();i++) {
2593 for(
size_t i=0;i<onames.size();i++) {
2594 std::string temps=onames[i];
2597 nnames.push_back(temps);
2598 if (temps!=onames[i] && verbose>0) {
2599 std::cout <<
"Converted column named '" << onames[i] <<
"' to '"
2600 << temps <<
"'." << std::endl;
2605 for(
size_t i=0;i<nnames.size();i++) {
2612 while ((fin) >> data) {
2634 O2SCL_ERR2(
"Size of table and list do not match in ",
2639 if (it->second.index!=
alist[it->second.index]->second.index) {
2640 O2SCL_ERR((((std::string)
"Problem with iterator for entry '")+
2641 it->first+
"' in list in table::check_synchro().").c_str(),
2645 for(
int i=0;i<((int)
atree.size());i++) {
2646 if (
alist[i]->second.index!=i) {
2647 O2SCL_ERR((((std::string)
"Problem with index of entry ")+
2648 itos(i)+
" in list in table::check_synchro().").c_str(),
2660 O2SCL_ERR2(
"Value of maxlines smaller than nlines ",
2664 O2SCL_ERR2(
"Size of table and list do not match in ",
2669 if (it->second.dat.size()!=
maxlines) {
2670 O2SCL_ERR2(
"Vector with size different than maxlines ",
2673 if (it->second.index!=
alist[it->second.index]->second.index) {
2674 O2SCL_ERR((((std::string)
"Problem with iterator for entry '")+
2675 it->first+
"' in list in table::is_valid().").c_str(),
2679 for(
int i=0;i<((int)
atree.size());i++) {
2680 if (
alist[i]->second.index!=i) {
2681 O2SCL_ERR((((std::string)
"Problem with index of entry ")+
2682 itos(i)+
" in list in table::is_valid().").c_str(),
2691 virtual const char *
type() {
return "table"; }
2728 std::vector<std::string> funcs, names;
2731 std::istringstream is(list);
2732 while(is >> stemp) funcs.push_back(stemp);
2733 for(
size_t i=0;i<(funcs.size());i++) {
2734 names.push_back(funcs[i].substr(0,funcs[i].find(
"=")));
2735 funcs[i]=funcs[i].substr(funcs[i].find(
"=")+1,
2736 funcs[i].length()-funcs[i].find(
"=")-1);
2737 if (names[i].length()==0 || funcs[i].length()==0) {
2744 std::map<std::string,double> vars;
2745 std::map<std::string,double>::const_iterator mit;
2747 vars[mit->first]=mit->second;
2750 std::vector<calculator> calcs(funcs.size());
2751 std::vector<vec_t> newcols(funcs.size());
2753 for(
size_t j=0;j<funcs.size();j++) {
2754 calcs[j].compile(funcs[j].c_str(),&vars);
2759 for(
size_t i=0;i<
nlines;i++) {
2762 for(
size_t j=0;j<
atree.size();j++) {
2767 for(
size_t j=0;j<funcs.size();j++) {
2768 newcols[j][i]=calcs[j].eval(&vars);
2772 for(
size_t j=0;j<funcs.size();j++) {
2801 vec_t &colp=it2->second.dat;
2822 template<
class resize_vec_t>
2824 bool throw_on_err=
true) {
2828 std::map<std::string,double> vars;
2829 std::map<std::string,double>::const_iterator mit;
2831 vars[mit->first]=mit->second;
2833 calc.
compile(
function.c_str(),&vars);
2839 std::vector<double> vals(
atree.size());
2842 for(
size_t j=0;j<
nlines;j++) {
2844 vars[it->first]=it->second.dat[j];
2846 vec[j]=calc.
eval(&vars);
2858 std::map<std::string,double> vars;
2859 std::map<std::string,double>::const_iterator mit;
2861 vars[mit->first]=mit->second;
2863 calc.
compile(
function.c_str(),&vars);
2866 vars[it->first]=it->second.dat[row];
2869 double dret=calc.
eval(&vars);
2879 std::map<std::string,double> vars;
2880 std::map<std::string,double>::const_iterator mit;
2882 vars[mit->first]=mit->second;
2884 calc.
compile(
function.c_str(),&vars);
2886 double best_val=0.0;
2888 for(
size_t row=0;row<
nlines-1;row++) {
2890 vars[it->first]=it->second.dat[row];
2892 double dtemp=calc.
eval(&vars);
2896 if (dtemp>best_val) {
2910 friend void o2scl_hdf::hdf_output
2916 friend void o2scl_hdf::hdf_output_data
2930 #ifndef DOXYGEN_INTERNAL
2939 if (it==
atree.end()) {
2940 O2SCL_ERR((((std::string)
"Column '")+scol+
2941 "' not found in table::get_column() const.").c_str(),
2945 return it->second.dat;
2957 alist[it->second.index]=it;
2966 if (s==
"abs" || s==
"acos" || s==
"acosh" || s==
"asin" ||
2967 s==
"asinh" || s==
"atan" || s==
"atan2" || s==
"atanh" ||
2968 s==
"ceil" || s==
"cos" || s==
"cosh" || s==
"cot" || s==
"csc" ||
2969 s==
"eval" || s==
"exp" || s==
"floor" || s==
"if" || s==
"int" ||
2970 s==
"log" || s==
"log10" || s==
"max" || s==
"min" || s==
"sec" ||
2971 s==
"sin" || s==
"sinh" || s==
"sqrt" || s==
"tan" || s==
"tanh") {
2972 s=((std::string)
"v_")+s;
2973 }
else if (s[0]>=
'0' && s[0]<=
'9') {
2974 s=((std::string)
"v_")+s;
2977 for(
size_t i=0;i<s.length();i++) {
2978 if (!isalpha(s[i]) && !isdigit(s[i]) && s[i]!=
'_') s[i]=
'_';
2990 for(
size_t i=0;i<cnames.size();i++) {
2991 if (colx==cnames[i]) {
2999 }
while (done==
false);
3041 typedef typename std::map<std::string,col,
3042 std::greater<std::string> >::iterator
aiter;
3044 typedef typename std::map<std::string,
col,
3045 std::greater<std::string> >::const_iterator
3048 typedef typename std::vector<aiter>::iterator
aviter;
3058 std::map<std::string,col,std::greater<std::string> >
atree;
3068 if (it==
atree.end()) {
3069 O2SCL_ERR((((std::string)
"Column '")+lname+
3070 " not found in table::get_iterator().").c_str(),
3078 if (it==
atree.end()) {
3079 O2SCL_ERR((((std::string)
"Column '")+lname+
3080 " not found in table::get_col_struct().").c_str(),
3084 return &(it->second);
3122 template<
class vec_t=std::vector<
double> >
3148 std::vector<std::string> cols) {
3156 std::vector<std::string> cols) {
3159 for(
size_t i=0;i<
nc;i++) {
3189 std::string str=((std::string)
"Column ")+
o2scl::szttos(col)+
3216 template<
class vec_t=std::vector<
double> >
3217 class matrix_view_table :
public matrix_view {
3242 std::vector<std::string> cols) {
3250 std::vector<std::string> cols) {
3253 for(
size_t i=0;i<
nc;i++) {
3283 std::string str=((std::string)
"Column ")+
o2scl::szttos(col)+
3303 std::string str=((std::string)
"Column ")+
o2scl::szttos(col)+
3318 swap(t1.nlines,t2.nlines);
3319 swap(t1.col_ptrs,t2.col_ptrs);
3330 template<
class vec_t=std::vector<
double> >
3331 class matrix_view_table_transpose :
public matrix_view {
3356 std::vector<std::string> rows) {
3364 std::vector<std::string> rows) {
3367 for(
size_t i=0;i<
nr;i++) {
3397 std::string str=((std::string)
"Column ")+
o2scl::szttos(col)+
3417 std::string str=((std::string)
"Column ")+
o2scl::szttos(col)+
3432 swap(t1.nlines,t2.nlines);
3433 swap(t1.col_ptrs,t2.col_ptrs);
3450 std::vector<std::vector<double> >
cols;
3466 const std::vector<std::string> &col_names) {
3474 const std::vector<std::string> &col_names) {
3478 for(
size_t i=0;i<
nc;i++) {
3482 t.swap_column_data(col_names[i],
cols[i]);
3510 std::string str=((std::string)
"Column ")+
o2scl::szttos(col)+
3515 return cols[col][row];
3529 std::string str=((std::string)
"Column ")+
o2scl::szttos(col)+
3534 return cols[col][row];
3555 template<
class vec_t=std::vector<
double> >
3584 std::vector<std::string> rows) {
3592 std::vector<std::string> rows) {
3595 for(
size_t i=0;i<
nr;i++) {
3630 std::string str=((std::string)
"Column ")+
o2scl::szttos(col)+
3647 #ifndef DOXYGEN_NO_O2NS
void delete_idadj_rows()
Delete all rows which are identical to adjacent rows.
std::string intp_coly
The last y-column interpolated.
double integ_const(std::string sx, double x1, double x2, std::string sy) const
Compute the integral of the function defined by x-values stored in column named sx and y-values store...
const_matrix_view_table(o2scl::table< vec_t > &t, std::vector< std::string > cols)
Create a matrix view object from the specified table and list of columns.
void function_column(std::string function, std::string scol)
Make a column from the function specified in function and add it to the table.
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 integ(std::string x, std::string y, std::string ynew)
Create a new column named ynew which is equal to the integral of the function defined by x-values sto...
View a o2scl::table object as a matrix.
bool is_number(std::string s)
Return true if the string s is likely a integral or floating point number.
size_t nc
The number of columns.
size_t lookup(int icol, double val) const
Exhaustively search column col for the value val .
void functions_columns(std::string list)
Create new columns or recompute from a list of functions.
size_t get_nlines() const
Return the number of lines.
matrix_swap_table()
Create a matrix view object from the specified table and list of columns.
interp_vec< vec_t > * si
Interpolation object.
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.
const_matrix_view_table_transpose(o2scl::table< vec_t > &t, std::vector< std::string > rows)
Create a matrix view object from the specified table and list of rows.
size_t lookup_column(std::string lname) const
Find the index for column named name .
size_t size2()
Return the number of columns.
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. .
void set_row(size_t row, size_vec_t &v)
Set an entire row of data.
A class for representing permutations.
table(size_t cmaxlines=0)
Create a new table with space for nlines<=cmaxlines.
void copy_to_column(vec2_t &v, std::string scol)
Copy to a column from a generic vector object.
void set_nlines(size_t il)
Set the number of lines.
size_t nr
The number of rows.
std::map< std::string, double > constants
The list of constants.
virtual void clear()
Clear everything.
void copy_rows(std::string func, table< vec2_t > &dest)
Copy all rows matching a particular condition to a new table.
col * get_col_struct(std::string lname)
Return the column structure for a column.
std::string dtos(const fp_t &x, int prec=6, bool auto_prec=false)
Convert a double to a string.
virtual double integ(const double x1, const double x2) const
Give the value of the integral .
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.
double row_function(std::string function, size_t row) const
Compute a value by applying a function to a row.
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 .
View a o2scl::table object as a matrix.
size_t size1() const
Return the number of rows.
const_matrix_view_table_transpose()
Create a matrix view object from the specified table and list of rows.
void new_column(std::string head)
Add a new column owned by the table table .
virtual void remove_constant(std::string name)
Remove a constant.
@ exc_efailed
generic failure
size_t itype
Current interpolation type.
void copy_row(size_t src, size_t dest)
Copy the data in row src to row dest.
const double & operator()(size_t row, size_t col) const
Return a reference to the element at row row and column col.
size_t function_find_row(std::string function) const
Find a row which maximizes a function.
const_matrix_view_table()
Create a matrix view object from the specified table and list of columns.
void deriv(std::string x, std::string y, std::string yp)
Make a new column named yp which is the derivative formed from columns named x and y .
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
aiter get_iterator(std::string lname)
Return the iterator for a column.
size_t size2()
Return the number of columns.
View a o2scl::table object as a matrix.
int new_column(std::string name, size_t sz, vec2_t &v)
Add a new column by copying data from another vector.
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
vec_t dat
Pointer to column.
void delete_row(std::string scol, double val)
Delete the row with the entry closest to the value val in column scol .
std::string get_sorted_name(size_t icol) const
Returns the name of column col in sorted order. .
size_t nlines
Number of lines in the table.
void line_of_data(const vec2_t &v)
Read a line of data and store in a new row of the table.
friend void swap(matrix_swap_table &t1, matrix_swap_table &t2)
Swap method.
void sort_table(std::string scol)
Sort the entire table by the column scol.
virtual int read_generic(std::istream &fin, int verbose=0)
Clear the current table and read from a generic data file.
A simple matrix view object.
@ exc_enotfound
Generic "not found" result.
size_t maxlines
The size of allocated memory.
void new_row(size_t n)
Insert a row before row n.
virtual size_t get_nconsts() const
Get the number of constants.
aiter begin()
Return the beginning of the column tree.
void delete_row(size_t irow)
Delete the row of index irow .
vec_t & get_column_no_const(std::string scol)
Returns a non-const reference to the column named col. .
double integ(std::string sx, double x1, double x2, std::string sy)
Compute the integral of the function defined by x-values stored in column named sx and y-values store...
double stod(std::string s)
Convert a string to a double.
double deriv2(std::string sx, double x0, std::string sy)
Compute the second derivative of the function defined by x-values stored in column named sx and y-val...
size_t size1()
Return the number of rows.
size_t nlines
The number of lines in the table.
void set(std::string scol, size_t row, double val)
Set row row of column named col to value val . .
matrix_view_table_transpose()
Create a matrix view object from the specified table and list of columns.
void column_to_vector(std::string scol, resize_vec_t &v) const
Copy a column to a generic vector object.
double interp(std::string sx, double x0, std::string sy)
Interpolate value x0 from column named sx into column named sy.
friend void swap(const_matrix_view_table_transpose &t1, const_matrix_view_table_transpose &t2)
Swap method.
void make_fp_varname(std::string &s)
Ensure a variable name does not match a function or contain non-alphanumeric characters.
std::vector< aiter > alist
The list of tree iterators.
size_t nlines
The size of presently used memory.
col(const col &c)
Copy constructor.
void hdf_input(hdf_file &hf, o2scl::table< vec_t > &t, std::string name)
Input a o2scl::table object from a hdf_file.
void is_valid() const
Check if the table object appears to be valid.
void subtable(std::string list, size_t top, size_t bottom, table< vec_t > &tnew) const
Make a subtable.
void set(o2scl::table< vec_t > &t, std::vector< std::string > cols)
Create a matrix view object from the specified table and list of columns.
const double & operator()(size_t row, size_t col) const
Return a reference to the element at row row and column col.
size_t size2()
Return the number of columns.
size_t nlines
The number of lines in the table.
Column structure for table [protected].
double interp_const(std::string sx, double x0, std::string sy) const
Interpolate value x0 from column named sx into column named sy (const version)
static const double x2[5]
double min(std::string scol) const
Return column minimum. Makes no assumptions about ordering, .
void delete_rows_list(vec_size_t &row_list)
Delete all rows in a specified list.
Interpolation class for pre-specified vector.
double & operator()(size_t row, size_t col)
Return a reference to the element at row row and column col.
void set_maxlines(size_t llines)
Manually set the maximum number of lines.
std::vector< const vec_t * > col_ptrs
Pointers to each row.
virtual void get_constant(size_t ix, std::string &name, double &val) const
Get a constant by index.
virtual void swap_column_data(std::string scol, vec_t &v)
Swap the data in column scol with that in vector v.
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 and return an integer to indicate success o...
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 deriv2(size_t ix, double x0, size_t iy)
Compute the second derivative of the function defined by x-values stored in column with index ix and ...
aiter end()
Return the end of the column tree.
size_t nr
The number of rows in the matrix (equal to the number of pointers to table columns)
virtual double get_constant(std::string name) const
Get a constant.
const double & operator()(size_t row, size_t col) const
Return a reference to the element at row row and column col.
void vector_sort_index(size_t n, const vec_t &data, vec_size_t &order)
Create a permutation which sorts the first n elements of a vector (in increasing order)
size_t size1()
Return the number of rows.
double integ(size_t ix, double x1, double x2, size_t iy)
Compute the integral of the function defined by x-values stored in column with index ix and y-values ...
void set(o2scl::table< vec_t > &t, std::vector< std::string > rows)
Create a matrix view object from the specified table and list of columns.
View a o2scl::table object as a matrix.
void set(o2scl::table< vec_t > &t, std::vector< std::string > cols)
Create a matrix view object from the specified table and list of columns.
const double & operator()(size_t row, size_t col) const
Return a reference to the element at row row and column col.
static const double x1[5]
void inc_maxlines(size_t llines)
Manually increase the maximum number of lines.
size_t size2()
Return the number of columns.
col & operator=(const col &c)
Copy constructor for assignment operator.
friend void swap(matrix_view_table_transpose &t1, matrix_view_table_transpose &t2)
Swap method.
@ exc_einval
invalid argument supplied by user
Evaluate a mathematical expression in a string.
virtual void add_constant(std::string name, double val)
Add a constant, or if the constant already exists, change its value.
const vec_t & operator[](std::string scol) const
Returns the column named scol (const version). .
std::vector< vec_t * > col_ptrs
Pointers to each column.
friend void swap(matrix_view_table &t1, matrix_view_table &t2)
Swap method.
void set(o2scl::table< vec_t > &t, std::vector< std::string > rows)
Create a matrix view object from the specified table and list of columns.
size_t nc
The number of columns.
void sort_column(std::string scol)
Individually sort the column scol.
void set(size_t icol, size_t row, double val)
Set row row of column number icol to value val . .
virtual void delete_column(std::string scol)
Delete column named scol .
size_t nlines
The number of lines in the table.
size_t size2() const
Return the number of columns.
The O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl namespace ...
friend void swap(const_matrix_view_table &t1, const_matrix_view_table &t2)
Swap method.
void deriv2(std::string x, std::string y, std::string yp)
Create a new column named yp which is equal to the second derivative of the function defined by x-val...
void reset_list()
Set the elements of alist with the appropriate iterators from atree. .
std::map< std::string, col, std::greater< std::string > >::iterator aiter
Map iterator type.
void make_unique_name(std::string &colx, std::vector< std::string > &cnames)
Make sure a name is unique.
Searching class for monotonic data with caching.
double deriv(std::string sx, double x0, std::string sy)
Compute the first derivative of the function defined by x-values stored in column named sx and y-valu...
std::map< std::string, col, std::greater< std::string > >::const_iterator aciter
Const map iterator type.
size_t get_maxlines()
Return the maximum number of lines before a reallocation is required.
virtual void summary(std::ostream *out, size_t ncol=79) const
Output a summary of the information stored.
size_t delete_rows_tolerance(double tol_rel=1.0e-12, double tol_abs=1.0e-20, int verbose=0)
Exaustively search for groups of rows which match within a specified tolerance and remove all but one...
void line_of_names(std::string newheads)
Read a new set of names from newheads.
std::vector< aiter >::iterator aviter
Vector iterator type.
size_t get_ncolumns() const
Return the number of columns.
table & operator=(const table &t)
Copy constructor.
double deriv2_const(size_t ix, double x0, size_t iy) const
Compute the second derivative of the function defined by x-values stored in column with index ix and ...
std::string intp_colx
The last x-column interpolated.
double deriv_const(size_t ix, double x0, size_t iy) const
Compute the first derivative of the function defined by x-values stored in column with index ix and y...
const vec_t & get_column(std::string scol) const
Returns a reference to the column named col. .
std::string itos(int x)
Convert an integer to a string.
double interp(size_t ix, double x0, size_t iy)
Interpolate value x0 from column with index ix into column with index iy .
void hdf_input_data(hdf_file &hf, o2scl::table< vec_t > &t)
Internal function for inputting a o2scl::table object.
std::vector< vec_t * > col_ptrs
Pointers to each column.
table(const table &t)
Copy constructor.
void clear_data()
Remove all of the data by setting the number of lines to zero.
@ exc_eindex
Invalid index for array or matrix.
void check_synchro() const
Check if the tree and list are properly synchronized.
size_t lookup(std::string scol, double val) const
Exhaustively search column col for the value val .
const vec_t & operator[](size_t icol) const
Returns the column of index icol (const version). .
double interp_const(size_t ix, double x0, size_t iy) const
Interpolate value x0 from column with index ix into column with index iy .
bool intp_set
True if the interpolation object is up-to-date.
const double & operator()(size_t row, size_t col) const
Return a reference to the element at row row and column col.
void vector_sort_double(size_t n, vec_t &data)
Sort a vector of doubles (in increasing order)
void set(o2scl::table< std::vector< double > > &t, const std::vector< std::string > &col_names)
Create a matrix view object from the specified table and list of columns.
double & operator()(size_t row, size_t col)
Return a reference to the element at row row and column col.
void set_nlines_auto(size_t il)
Set the number of lines, increasing the size more agressively.
Store data in an O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$sc...
void get_row(size_t irow, resize_vec_t &row) const
Returns a copy of row number irow. .
@ exc_esanity
sanity check failed - shouldn't happen
virtual bool is_constant(std::string name) const
Test if name is a constant.
std::vector< const vec_t * > col_ptrs
Pointers to each column.
size_t ordered_lookup(const double x0) const
Find the index of x0 in the ordered array x.
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)
matrix_view_table()
Create a matrix view object from the specified table and list of columns.
void set_interp_type(size_t interp_type)
Set the base interpolation objects.
double & operator()(size_t row, size_t col)
Return a reference to the element at row row and column col.
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
@ itp_cspline
Cubic spline for natural boundary conditions.
Swap part of a o2scl::table object into a matrix.
bool is_column(std::string scol) const
Return true if scol is a column in the current table table .
void init_column(std::string scol, double val)
Initialize all values of column named scol to val .
double deriv2_const(std::string sx, double x0, std::string sy) const
The Compute the second derivative of the function defined by x-values stored in column named sx and y...
double eval(std::map< std::string, double > *vars=0)
Evalate the previously compiled expression using variables specified in vars.
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.
size_t nlines
The number of lines in the table.
size_t size1()
Return the number of rows.
size_t ordered_lookup(std::string scol, double val) const
Look for a value in an ordered column .
void delete_rows_func(std::string func)
Delete all rows where func evaluates to a number greater than 0.5 .
std::vector< std::vector< double > > cols
Array of columns.
A simple matrix view object.
double max(std::string scol) const
Return column maximum. Makes no assumptions about ordering, .
double integ_const(size_t ix, double x1, double x2, size_t iy) const
Compute the integral of the function defined by x-values stored in column with index ix and y-values ...
size_t size1()
Return the number of rows.
std::map< std::string, col, std::greater< std::string > > atree
The tree of columns.
virtual double deriv2(const double x0) const
Give the value of the second derivative .
virtual const char * type()
Return the type, "table".
matrix_view_table(o2scl::table< vec_t > &t, std::vector< std::string > cols)
Create a matrix view object from the specified table and list of columns.
virtual ~table()
Table destructor.
double get(std::string scol, size_t row) const
Get value from row row of column named col. .
virtual void rename_column(std::string src, std::string dest)
Rename column named src to dest .
virtual double deriv(const double x0) const
Give the value of the derivative .
size_t get_interp_type() const
Get the interpolation type.
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 .
double deriv(size_t ix, double x0, size_t iy)
Compute the first derivative of the function defined by x-values stored in column with index ix and y...
double get(size_t icol, size_t row) const
Get value from row row of column number icol. .
virtual void clear_table()
Clear the table and the column names (but leave constants)
std::string szttos(size_t x)
Convert a size_t to a string.
matrix_swap_table(o2scl::table< std::vector< double > > &t, const std::vector< std::string > &col_names)
Create a matrix view object from the specified table and list of columns.
matrix_view_table_transpose(o2scl::table< vec_t > &t, std::vector< std::string > rows)
Create a matrix view object from the specified table and list of columns.
void clear_constants()
CLear all constants.
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.
double deriv_const(std::string sx, double x0, std::string sy) const
Compute the first derivative of the function defined by x-values stored in column named sx and y-valu...
void zero_table()
Zero the data entries but keep the column names and nlines fixed.
size_t nc
The number of columns.
std::string get_column_name(size_t icol) const
Returns the name of column col .
vec_t empty_col
An empty vector for get_column()
void delete_rows_ends(size_t row_start, size_t row_end)
Delete all rows between row_start and row_end .
Documentation generated with Doxygen. Provided under the
GNU Free Documentation License (see License Information).