23 #ifndef O2SCL_TABLE3D_H 24 #define O2SCL_TABLE3D_H 37 #include <boost/numeric/ublas/vector.hpp> 38 #include <boost/numeric/ublas/vector_proxy.hpp> 39 #include <boost/numeric/ublas/matrix.hpp> 40 #include <boost/numeric/ublas/matrix_proxy.hpp> 42 #include <o2scl/misc.h> 43 #include <o2scl/err_hnd.h> 44 #include <o2scl/search_vec.h> 45 #include <o2scl/uniform_grid.h> 46 #include <o2scl/interp.h> 47 #include <o2scl/table_units.h> 48 #include <o2scl/contour.h> 49 #include <o2scl/shunting_yard.h> 60 void hdf_output(hdf_file &hf,
o2scl::table3d &t, std::string name);
63 #ifndef DOXYGEN_NO_O2NS 85 typedef boost::numeric::ublas::matrix_row<const ubmatrix> ubmatrix_row;
86 typedef boost::numeric::ublas::matrix_column<const ubmatrix>
118 template<
class vec_t,
class vec2_t>
119 void set_xy(std::string x_name,
size_t nx,
const vec_t &x,
120 std::string y_name,
size_t ny,
const vec2_t &y) {
122 if (has_slice && (size_set || xy_set) && (nx!=numx || ny!=numy)) {
123 O2SCL_ERR(
"Size cannot be reset in table3d::set_xy().",
137 for(
size_t i=0;i<nx;i++) (xval)[i]=x[i];
138 for(
size_t i=0;i<ny;i++) (yval)[i]=y[i];
164 void set_size(
size_t nx,
size_t ny);
174 void set(
size_t ix,
size_t iy, std::string name,
double val);
179 void set(
size_t ix,
size_t iy,
size_t z,
double val);
184 double &
get(
size_t ix,
size_t iy, std::string name);
189 const double &
get(
size_t ix,
size_t iy, std::string name)
const;
194 double &
get(
size_t ix,
size_t iy,
size_t z);
199 const double &
get(
size_t ix,
size_t iy,
size_t z)
const;
215 void set_val(
double x,
double y, std::string name,
double val);
220 void set_val(
double x,
double y,
size_t z,
double val);
225 double &get_val(
double x,
double y, std::string name);
230 const double &get_val(
double x,
double y, std::string name)
const;
235 double &get_val(
double x,
double y,
size_t z);
240 const double &get_val(
double x,
double y,
size_t z)
const;
245 template<
class vec_t>
246 void set_slices(
double x,
double y,
size_t nv, vec_t &vals) {
251 for(
size_t i=0;i<nv && i<list.size();i++) {
252 list[i](ix,iy)=vals[i];
260 template<
class vec_t>
268 for(
size_t i=0;i<nv && i<list.size();i++) {
283 void set_val_ret(
double &x,
double &y, std::string name,
double val);
288 void set_val_ret(
double &x,
double &y,
size_t z,
double val);
294 double &get_val_ret(
double &x,
double &y, std::string name);
300 const double &get_val_ret(
double &x,
double &y, std::string name)
const;
306 double &get_val_ret(
double &x,
double &y,
size_t z);
312 const double &get_val_ret(
double &x,
double &y,
size_t z)
const;
318 void add_slice_from_table(
table3d &source, std::string slice,
319 std::string dest_slice=
"");
324 template<
class vec_t>
332 for(
size_t i=0;i<nv && i<list.size();i++) {
333 list[i](ix,iy)=vals[i];
341 template<
class vec_t>
350 for(
size_t i=0;i<nv && i<list.size();i++) {
351 vals[i]=list[i](ix,iy);
363 void set_grid_x(
size_t ix,
double val);
366 void set_grid_y(
size_t iy,
double val);
369 double get_grid_x(
size_t ix)
const;
372 double get_grid_y(
size_t iy)
const;
375 std::string get_x_name()
const;
378 std::string get_y_name()
const;
381 void set_x_name(std::string name);
384 void set_y_name(std::string name);
387 const ubvector &get_x_data()
const;
390 const ubvector &get_y_data()
const;
397 void get_size(
size_t &nx,
size_t &ny)
const;
400 size_t get_nx()
const;
403 size_t get_ny()
const;
406 size_t get_nslices()
const;
409 bool is_size_set()
const;
412 bool is_xy_set()
const;
420 void line_of_names(std::string names);
424 std::string get_slice_name(
size_t z)
const;
428 void new_slice(std::string name);
432 void set_slice_all(std::string name,
double val);
436 size_t lookup_slice(std::string name)
const;
440 bool is_slice(std::string name,
size_t &ix)
const;
448 void rename_slice(std::string olds, std::string news);
453 void copy_slice(std::string src, std::string dest);
460 void init_slice(std::string scol,
double val);
463 const ubmatrix &get_slice(std::string scol)
const;
466 const ubmatrix &get_slice(
size_t iz)
const;
469 ubmatrix &get_slice(std::string scol);
472 ubmatrix &get_slice(
size_t iz);
483 const std::vector<ubmatrix> &get_data();
490 template<
class mat_t>
492 for(
size_t i=0;i<numx;i++) {
493 for(
size_t j=0;j<numy;j++) {
494 this->
set(i,j,slice_name,m(i,j));
506 void lookup_x(
double val,
size_t &ix)
const;
510 void lookup_y(
double val,
size_t &iy)
const;
514 void lookup(
double val, std::string slice,
size_t &ix,
524 void set_interp_type(
size_t interp_type);
528 size_t get_interp_type()
const;
532 double interp(
double x,
double y, std::string name)
const;
537 double deriv_x(
double x,
double y, std::string name)
const;
542 double deriv_y(
double x,
double y, std::string name)
const;
547 double deriv_xy(
double x,
double y, std::string name)
const;
552 double integ_x(
double x1,
double x2,
double y, std::string name)
const;
557 double integ_y(
double x,
double y1,
double y2, std::string name)
const;
562 template<
class vec_t>
565 for (
size_t i=0;i<list.size();i++) {
566 std::string name=get_slice_name(i);
567 v[i]=interp(x,y,name);
576 void deriv_x(std::string fname, std::string fpname);
581 void deriv_y(std::string fname, std::string fpname);
592 void extract_x(
double x,
table<> &t);
599 void extract_y(
double y,
table<> &t);
631 void summary(std::ostream *out,
int ncol=79)
const;
654 template<
class vec_t>
656 std::vector<contour_line> &clines) {
658 size_t z=lookup_slice(name);
661 co.
set_data(numx,numy,xval,yval,list[z]);
675 virtual void add_constant(std::string name,
double val);
678 virtual void remove_constant(std::string name);
690 virtual int set_constant(std::string name,
double val,
691 bool err_on_notfound=
true);
694 virtual bool is_constant(std::string name)
const;
697 virtual double get_constant(std::string name);
700 virtual void get_constant(
size_t ix, std::string &name,
705 return constants.size();
724 int read_gen3_list(std::istream &fin,
int verbose=0);
727 virtual const char *
type() {
return "table3d"; }
745 template<
class resize_mat_t>
747 bool throw_on_err=
true) {
750 std::map<std::string,double> vars;
752 std::map<std::string,double>::const_iterator mit;
753 for(mit=constants.begin();mit!=constants.end();mit++) {
754 vars[mit->first]=mit->second;
757 calc.
compile(
function.c_str(),&vars);
759 if (mat.size1()!=numx || mat.size2()!=numy) {
760 mat.resize(numx,numy);
763 for(
size_t i=0;i<numx;i++) {
764 for(
size_t j=0;j<numy;j++) {
768 for(
size_t k=0;k<list.size();k++) {
769 vars[get_slice_name(k)]=list[k](i,j);
772 mat(i,j)=calc.
eval(&vars);
785 void function_slice(std::string
function, std::string col);
791 table3d slice_to_uniform_grid(std::string slice,
size_t xpts,
792 bool log_x,
size_t ypts,
bool log_y);
797 table3d table_to_uniform_grid(
size_t xpts,
bool log_x,
798 size_t ypts,
bool log_y);
800 #ifdef O2SCL_NEVER_DEFINED 804 template<
class vec_t>
805 void test_uniform_grid_log(std::string slice,
bool &log_x,
bool &log_y,
806 vec_t &x, vec_t &y, vec_t &s) {
807 vector<double> dev_x;
808 for(
size_t i=0;i<numx-1;i++) {
809 dev_x.push_back(xval[i+1]-xval[i]);
815 if (stddev<1.0e-8*fabs(avg)) {
817 for(
size_t i=0;i<numx;i++) {
823 for(
size_t i=0;i<numx-1;i++) {
824 dev_x.push_back(xval[i+1]/xval[i]);
828 if (stddev<1.0e-8*fabs(avg)) {
830 for(
size_t i=0;i<numx;i++) {
846 #ifndef DOXYGEN_INTERNAL 850 typedef std::map<std::string,size_t,
851 std::greater<std::string> >::iterator map_iter;
852 typedef std::map<std::string,size_t,
853 std::greater<std::string> >::const_iterator map_const_iter;
868 std::map<std::string,size_t,std::greater<std::string> >
tree;
901 map_iter
begin() {
return tree.begin();};
903 map_iter
end() {
return tree.end();};
914 #ifndef DOXYGEN_NO_O2NS bool size_set
True if the size of the grid has been set.
double vector_mean(size_t n, const vec_t &data)
Compute the mean of the first n elements of a vector.
map_const_iter const_begin() const
Return the beginning of the slice tree.
int function_matrix(std::string function, resize_mat_t &mat, bool throw_on_err=true)
Fill a matrix from the function specified in function.
void slice_contours(std::string name, size_t nlev, vec_t &levs, std::vector< contour_line > &clines)
Create contour lines from the slice named name.
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
size_t numy
The size of the y grid.
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 set_slices(double x, double y, size_t nv, vec_t &vals)
Set elements in the first nv slices at the nearest location to x,y to value val.
std::string xname
The name for the x grid.
invalid argument supplied by user
void set_slices_ret(double &x, double &y, size_t nv, vec_t &vals)
Set elements in the first nv slices at the nearest location to x,y to values vals.
map_iter begin()
Return the beginning of the slice tree.
void set_data(size_t sizex, size_t sizey, const vec_t &x_fun, const vec_t &y_fun, const mat_t &udata)
Set the data.
size_t numx
The size of the x grid.
bool has_slice
True if the table has at least one slice.
double vector_stddev(size_t n, const vec_t &data)
Standard deviation with specified mean.
size_t itype
The interpolation type.
Calculate contour lines from a two-dimensional data set.
std::vector< ubmatrix > list
The pointers to the matrices.
virtual const char * type()
Return the type, "table3d".
The O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl namespace ...
double eval(std::map< std::string, double > *vars=0)
Evalate the previously compiled expression using variables specified in vars.
map_const_iter const_end() const
Return the end of the slice tree.
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
Evaluate a mathematical expression in a string.
map_iter end()
Return the end of the slice tree.
void copy_to_slice(mat_t &m, std::string slice_name)
Copy to a slice from a generic matrix object.
Data table table class with units.
void get_slices_ret(double &x, double &y, size_t nv, vec_t &vals)
Get elements in the first nv slices at the nearest location to x,y to value val.
bool xy_set
True if the grid has been set.
A data structure containing one or more slices of two-dimensional data points defined on a grid...
Store data in an O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$sc...
std::string yname
The name for the y grid.
virtual size_t get_nconsts() const
Get the number of constants.
std::map< std::string, double > constants
The list of constants.
void interp_slices(double x, double y, size_t nv, vec_t &v)
Fill a vector of interpolated values from each slice at the point x,y
void get_slices(double x, double y, size_t nv, vec_t &v)
Get the data for every slice at the nearest location to x,y
void set_levels(size_t nlevels, vec_t &ulevels)
Set the contour levels.
void calc_contours(std::vector< contour_line > &clines)
Calculate the contours.
void hdf_input(hdf_file &hf, o2scl::table3d &t, std::string name)
Input a o2scl::table3d object from a hdf_file.
std::map< std::string, size_t, std::greater< std::string > > tree
A tree connecting column names to list indexes.
static const double x2[5]
void set_xy(std::string x_name, size_t nx, const vec_t &x, std::string y_name, size_t ny, const vec2_t &y)
Initialize the x-y grid.
static const double x1[5]
void hdf_input(hdf_file &hf, o2scl::table< vec_t > &t, std::string name)
Input a o2scl::table object from a hdf_file.
Interpolation class for general vectors.