table3d.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2018, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_TABLE3D_H
24 #define O2SCL_TABLE3D_H
25 
26 /** \file table3d.h
27  \brief File defining \ref o2scl::table3d
28 */
29 
30 #include <iostream>
31 #include <fstream>
32 #include <vector>
33 #include <string>
34 #include <cmath>
35 #include <sstream>
36 
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>
41 
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>
50 
51 // Forward definition of the table3d class for HDF I/O
52 namespace o2scl {
53  class table3d;
54 }
55 
56 // Forward definition of HDF I/O to extend friendship
57 namespace o2scl_hdf {
58  class hdf_file;
59  void hdf_input(hdf_file &hf, o2scl::table3d &t, std::string name);
60  void hdf_output(hdf_file &hf, o2scl::table3d &t, std::string name);
61 }
62 
63 #ifndef DOXYGEN_NO_O2NS
64 namespace o2scl {
65 #endif
66 
67  /** \brief A data structure containing one or more slices of
68  two-dimensional data points defined on a grid
69 
70  \future Improve interpolation and derivative caching, possibly
71  through non-const versions of the interpolation functions.
72  \future Should there be a clear_grid() function separate from
73  clear_data() and clear()?
74  \future Allow the user to more clearly probe 'size_set' vs.
75  'xy_set'? (AWS 07/18: This is apparently resolved.)
76  */
77  class table3d {
78 
79  public:
80 
83 
84  // This is used for the interpolation classes
85  typedef boost::numeric::ublas::matrix_row<const ubmatrix> ubmatrix_row;
86  typedef boost::numeric::ublas::matrix_column<const ubmatrix>
87  ubmatrix_column;
88 
89  /** \brief Create a new 3D \table
90  */
91  table3d();
92 
93  virtual ~table3d();
94 
95  /** \brief Create a table3d object from a table, assuming \c scolx
96  and \c scoly store the x- and y-grid data, respectively.
97  */
98  table3d(o2scl::table_units<> &t, std::string colx, std::string coly);
99 
100  /// Copy constructor
101  table3d(const table3d &t);
102 
103  /// Copy constructor
104  table3d &operator=(const table3d &t);
105 
106  /// \name Initialization
107  //@{
108  /** \brief Initialize the x-y grid
109 
110  This function will not allow you to redefine the grid when
111  there is data in the \table if a grid of a different size was
112  already set from a previous call to either set_xy() or
113  set_size(). However, you may freely redefine the grid after a
114  call to clear_data() or clear_table(). You may change
115  individual grid points at any time with set_grid_x() and
116  set_grid_y().
117  */
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) {
121 
122  if (has_slice && (size_set || xy_set) && (nx!=numx || ny!=numy)) {
123  O2SCL_ERR("Size cannot be reset in table3d::set_xy().",
125  return;
126  }
127  if (xy_set) {
128  xval.clear();
129  yval.clear();
130  }
131  numx=nx;
132  numy=ny;
133  xname=x_name;
134  yname=y_name;
135  xval.resize(nx);
136  yval.resize(ny);
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];
139  size_set=true;
140  xy_set=true;
141  return;
142  }
143 
144  /** \brief Initialize the x-y grid with \ref uniform_grid objects
145 
146  This function will not allow you to redefine the grid when
147  there is data in the \table if a grid of a different size was
148  already set from a previous call to either set_xy() or
149  set_size(). However, you may freely redefine the grid after a
150  call to clear_data() or clear_table(). You may change
151  individual grid points at any time with set_grid_x() and
152  set_grid_y().
153  */
154  void set_xy(std::string x_name, uniform_grid<double> gx,
155  std::string y_name, uniform_grid<double> gy);
156 
157  /** \brief Initialize \table size
158 
159  This function will not allow you to resize the \table if it
160  already has data or if the size has already been set with the
161  set_xy() function, unless you clear the data with clear_data()
162  or the \table with clear_table() first.
163  */
164  void set_size(size_t nx, size_t ny);
165  //@}
166 
167  // --------------------------------------------------------
168  /// \name On-grid get and set methods
169  //@{
170 
171  /** \brief Set element in slice \c name at location <tt>ix,iy</tt>
172  to value \c val
173  */
174  void set(size_t ix, size_t iy, std::string name, double val);
175 
176  /** \brief Set element in slice of index \c z at location
177  <tt>ix,iy</tt> to value \c val .
178  */
179  void set(size_t ix, size_t iy, size_t z, double val);
180 
181  /** \brief Get element in slice \c name at location
182  <tt>ix,iy</tt>
183  */
184  double &get(size_t ix, size_t iy, std::string name);
185 
186  /** \brief Get element in slice \c name at location
187  <tt>ix,iy</tt> (const version)
188  */
189  const double &get(size_t ix, size_t iy, std::string name) const;
190 
191  /** \brief Get element in slice of index \c z at location
192  <tt>ix,iy</tt>
193  */
194  double &get(size_t ix, size_t iy, size_t z);
195 
196  /** \brief Get element in slice of index \c z at location
197  <tt>ix,iy</tt> (const version)
198  */
199  const double &get(size_t ix, size_t iy, size_t z) const;
200  //@}
201 
202  // --------------------------------------------------------
203  /** \name Off-grid get and set methods
204 
205  These methods return the value of a slice on the grid
206  point nearest to a user-specified location. For
207  interpolation into a point off the grid, use
208  \ref table3d::interp().
209  */
210  //@{
211 
212  /** \brief Set element in slice \c name at the nearest location to
213  <tt>x,y</tt> to value \c val
214  */
215  void set_val(double x, double y, std::string name, double val);
216 
217  /** \brief Set element in slice of index \c z at the nearest
218  location to <tt>x,y</tt> to value \c val
219  */
220  void set_val(double x, double y, size_t z, double val);
221 
222  /** \brief Get element in slice \c name at location closest to
223  <tt>x,y</tt>
224  */
225  double &get_val(double x, double y, std::string name);
226 
227  /** \brief Get element in slice \c name at location closest to
228  <tt>x,y</tt>
229  */
230  const double &get_val(double x, double y, std::string name) const;
231 
232  /** \brief Get element in slice of index \c z at location closest
233  to <tt>x,y</tt>
234  */
235  double &get_val(double x, double y, size_t z);
236 
237  /** \brief Get element in slice of index \c z at location closest
238  to <tt>x,y</tt>
239  */
240  const double &get_val(double x, double y, size_t z) const;
241 
242  /** \brief Set elements in the first <tt>nv</tt> slices at the
243  nearest location to <tt>x,y</tt> to value \c val
244  */
245  template<class vec_t>
246  void set_slices(double x, double y, size_t nv, vec_t &vals) {
247  size_t ix, iy;
248  lookup_x(x,ix);
249  lookup_y(y,iy);
250 
251  for(size_t i=0;i<nv && i<list.size();i++) {
252  list[i](ix,iy)=vals[i];
253  }
254  return;
255  }
256 
257  /** \brief Get the data for every slice at the nearest location to
258  <tt>x,y</tt>
259  */
260  template<class vec_t>
261  void get_slices(double x, double y, size_t nv, vec_t &v) {
262 
263  size_t ix, iy;
264 
265  lookup_x(x,ix);
266  lookup_y(y,iy);
267 
268  for(size_t i=0;i<nv && i<list.size();i++) {
269  v[i]=list[i](ix,iy);
270  }
271 
272  return;
273  }
274  //@}
275 
276  // --------------------------------------------------------
277  /// \name Off-grid get and set methods returning nearest point
278  //@{
279 
280  /** \brief Set element in slice \c name at the nearest location to
281  <tt>x,y</tt> to value \c val
282  */
283  void set_val_ret(double &x, double &y, std::string name, double val);
284 
285  /** \brief Set element in slice of index \c z at the nearest
286  location to <tt>x,y</tt> to value \c val
287  */
288  void set_val_ret(double &x, double &y, size_t z, double val);
289 
290  /** \brief Get element in slice \c name at location closest to
291  <tt>x,y</tt>, and also return the corresponding values of \c x
292  and \c y
293  */
294  double &get_val_ret(double &x, double &y, std::string name);
295 
296  /** \brief Get element in slice \c name at location closest to
297  <tt>x,y</tt>, and also return the corresponding values of \c x
298  and \c y
299  */
300  const double &get_val_ret(double &x, double &y, std::string name) const;
301 
302  /** \brief Get element in slice of index \c z at location closest
303  to <tt>x,y</tt>, and also return the corresponding values of
304  \c x and \c y
305  */
306  double &get_val_ret(double &x, double &y, size_t z);
307 
308  /** \brief Get element in slice of index \c z at location closest
309  to <tt>x,y</tt>, and also return the corresponding values of
310  \c x and \c y
311  */
312  const double &get_val_ret(double &x, double &y, size_t z) const;
313 
314  /** \brief This function adds a slice from a different table3d
315  object, interpolating the results into the current
316  table3d object
317  */
318  void add_slice_from_table(table3d &source, std::string slice,
319  std::string dest_slice="");
320 
321  /** \brief Set elements in the first <tt>nv</tt> slices at the
322  nearest location to <tt>x,y</tt> to values \c vals
323  */
324  template<class vec_t>
325  void set_slices_ret(double &x, double &y, size_t nv, vec_t &vals) {
326  size_t ix, iy;
327  lookup_x(x,ix);
328  lookup_y(y,iy);
329  x=xval[ix];
330  y=yval[iy];
331 
332  for(size_t i=0;i<nv && i<list.size();i++) {
333  list[i](ix,iy)=vals[i];
334  }
335  return;
336  }
337 
338  /** \brief Get elements in the first <tt>nv</tt> slices at the
339  nearest location to <tt>x,y</tt> to value \c val
340  */
341  template<class vec_t>
342  void get_slices_ret(double &x, double &y, size_t nv, vec_t &vals) {
343 
344  size_t ix, iy;
345  lookup_x(x,ix);
346  lookup_y(y,iy);
347  x=xval[ix];
348  y=yval[iy];
349 
350  for(size_t i=0;i<nv && i<list.size();i++) {
351  vals[i]=list[i](ix,iy);
352  }
353  return;
354  }
355 
356  //@}
357 
358  // --------------------------------------------------------
359  /// \name Grid information get and set methods
360  //@{
361 
362  /// Set x grid point at index \c ix
363  void set_grid_x(size_t ix, double val);
364 
365  /// Set y grid point at index \c iy
366  void set_grid_y(size_t iy, double val);
367 
368  /// Get x grid point at index \c ix
369  double get_grid_x(size_t ix) const;
370 
371  /// Get y grid point at index \c iy
372  double get_grid_y(size_t iy) const;
373 
374  /// Get the name of the x grid variable
375  std::string get_x_name() const;
376 
377  /// Get the name of the y grid variable
378  std::string get_y_name() const;
379 
380  /// Set the name of the x grid variable
381  void set_x_name(std::string name);
382 
383  /// Set the name of the y grid variable
384  void set_y_name(std::string name);
385 
386  /// Get a const reference to the full x grid
387  const ubvector &get_x_data() const;
388 
389  /// Get a const reference to the full y grid
390  const ubvector &get_y_data() const;
391  //@}
392 
393  // --------------------------------------------------------
394  /// \name Size get methods
395  //@{
396  /// Get the size of the slices
397  void get_size(size_t &nx, size_t &ny) const;
398 
399  /// Get the x size
400  size_t get_nx() const;
401 
402  /// Get the y size
403  size_t get_ny() const;
404 
405  /// Get the number of slices
406  size_t get_nslices() const;
407 
408  /// True if the size of the table has been set
409  bool is_size_set() const;
410 
411  /// True if the grid has been set
412  bool is_xy_set() const;
413  //@}
414 
415  // --------------------------------------------------------
416  /// \name Slice manipulation
417  //@{
418 
419  /// Create a set of new slices specified in the string \c names
420  void line_of_names(std::string names);
421 
422  /** \brief Returns the name of slice with index \c z
423  */
424  std::string get_slice_name(size_t z) const;
425 
426  /** \brief Add a new slice
427  */
428  void new_slice(std::string name);
429 
430  /** \brief Set all of the values in slice \c name to \c val
431  */
432  void set_slice_all(std::string name, double val);
433 
434  /** \brief Find the index for slice named \c name
435  */
436  size_t lookup_slice(std::string name) const;
437 
438  /** \brief Return true if slice is already present
439  */
440  bool is_slice(std::string name, size_t &ix) const;
441 
442  /** \brief Rename slice named \c olds to \c news
443 
444  This is slow since we have to delete the column and re-insert
445  it. This process in turn mangles all of the iterators in the
446  list.
447  */
448  void rename_slice(std::string olds, std::string news);
449 
450  /** \brief Make a new slice named \c dest which is a copy
451  of the slice with name given in \c src.
452  */
453  void copy_slice(std::string src, std::string dest);
454 
455  /** \brief Initialize all values of slice named \c scol to \c val
456 
457  \note This will call the error handler if the value \c val is
458  not finite (i.e. either <tt>Inf</tt> or <tt>NaN</tt>).
459  */
460  void init_slice(std::string scol, double val);
461 
462  /// Return a constant reference to a slice
463  const ubmatrix &get_slice(std::string scol) const;
464 
465  /// Return a constant reference to a slice
466  const ubmatrix &get_slice(size_t iz) const;
467 
468  /// Return a constant reference to a slice
469  ubmatrix &get_slice(std::string scol);
470 
471  /// Return a constant reference to a slice
472  ubmatrix &get_slice(size_t iz);
473 
474  /** \brief Return a constant reference to all the slice data
475 
476  \comment
477  This isn't designated const, i.e. as
478  const std::vector<ubmatrix> &get_data() const;
479  because it would then have to be
480  const std::vector<const ubmatrix> &get_data() const;
481  \endcomment
482  */
483  const std::vector<ubmatrix> &get_data();
484 
485  /** \brief Copy to a slice from a generic matrix object
486 
487  The type <tt>mat_t</tt> can be any type with an
488  <tt>operator(,)</tt> method.
489  */
490  template<class mat_t>
491  void copy_to_slice(mat_t &m, std::string slice_name) {
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));
495  }
496  }
497  return;
498  }
499  //@}
500 
501  // --------------------------------------------------------
502  /// \name Lookup and search methods
503  //@{
504  /** \brief Look for a value in the x grid
505  */
506  void lookup_x(double val, size_t &ix) const;
507 
508  /** \brief Look for a value in the y grid
509  */
510  void lookup_y(double val, size_t &iy) const;
511 
512  /** \brief Look for a value in a specified slice
513  */
514  void lookup(double val, std::string slice, size_t &ix,
515  size_t &iy) const;
516  //@}
517 
518  // --------------------------------------------------------
519  /// \name Interpolation, differentiation, and integration
520  //@{
521 
522  /** \brief Specify the interpolation type
523  */
524  void set_interp_type(size_t interp_type);
525 
526  /** \brief Get the interpolation type
527  */
528  size_t get_interp_type() const;
529 
530  /** \brief Interpolate \c x and \c y in slice named \c name
531  */
532  double interp(double x, double y, std::string name) const;
533 
534  /** \brief Interpolate the derivative of the data with respect to
535  the x grid at point \c x and \c y in slice named \c name
536  */
537  double deriv_x(double x, double y, std::string name) const;
538 
539  /** \brief Interpolate the derivative of the data with respect to
540  the y grid at point \c x and \c y in slice named \c name
541  */
542  double deriv_y(double x, double y, std::string name) const;
543 
544  /** \brief Interpolate the mixed second derivative of the data at
545  point \c x and \c y in slice named \c name
546  */
547  double deriv_xy(double x, double y, std::string name) const;
548 
549  /** \brief Interpolate the integral of the data
550  respect to the x grid
551  */
552  double integ_x(double x1, double x2, double y, std::string name) const;
553 
554  /** \brief Interpolate the integral of the data
555  respect to the y grid
556  */
557  double integ_y(double x, double y1, double y2, std::string name) const;
558 
559  /** \brief Fill a vector of interpolated values from each slice at the
560  point <tt>x,y</tt>
561  */
562  template<class vec_t>
563  void interp_slices(double x, double y, size_t nv, vec_t &v) {
564 
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);
568  }
569 
570  return;
571  }
572 
573  /** \brief Create a new slice, named \c fpname, containing the
574  derivative of \c fname with respect to the x coordinate
575  */
576  void deriv_x(std::string fname, std::string fpname);
577 
578  /** \brief Create a new slice, named \c fpname, containing the
579  derivative of \c fname with respect to the y coordinate
580  */
581  void deriv_y(std::string fname, std::string fpname);
582  //@}
583 
584  // --------------------------------------------------------
585  /// \name Extract 2-dimensional tables
586  //@{
587  /** \brief Extract a table at a fixed x grid point
588 
589  \note All of the information previously stored in \c t will
590  be lost.
591  */
592  void extract_x(double x, table<> &t);
593 
594  /** \brief Extract a table at a fixed y grid point
595 
596  \note All of the information previously stored in \c t will
597  be lost.
598  */
599  void extract_y(double y, table<> &t);
600  //@}
601 
602  // --------------------------------------------------------
603  /// \name Clear methods
604  //@{
605  /** \brief Zero the data entries but keep the slice names
606  and grid
607  */
608  void zero_table();
609 
610  /** \brief Clear everything
611  */
612  void clear();
613 
614  /** \brief Remove all of the data by setting the number
615  of lines to zero
616 
617  This leaves the column names intact and does not remove
618  the constants.
619  */
620  void clear_data();
621  //@}
622 
623  // --------------------------------------------------------
624  /// \name Summary method
625  //@{
626  /** \brief Output a summary of the information stored
627 
628  Outputs the number of constants, the grid information,
629  and a list of the slice names
630  */
631  void summary(std::ostream *out, int ncol=79) const;
632  //@}
633 
634  // ---------
635  // Allow HDF I/O functions to access table3d data
636 
637  friend void o2scl_hdf::hdf_output(o2scl_hdf::hdf_file &hf,
638  table3d &t,
639  std::string name);
640 
641  friend void o2scl_hdf::hdf_input(o2scl_hdf::hdf_file &hf, table3d &t,
642  std::string name);
643 
644  // --------------------------------------------------------
645  /// \name Contour lines method
646  //@{
647 
648  /** \brief Create contour lines from the slice named \c name
649 
650  This uses \ref contour to compute contour lines (stored in \c
651  clines) from slice \c name given \c nlev contour levels in \c
652  levs .
653  */
654  template<class vec_t>
655  void slice_contours(std::string name, size_t nlev, vec_t &levs,
656  std::vector<contour_line> &clines) {
657 
658  size_t z=lookup_slice(name);
659 
660  contour co;
661  co.set_data(numx,numy,xval,yval,list[z]);
662  co.set_levels(nlev,levs);
663  co.calc_contours(clines);
664 
665  return;
666  }
667  //@}
668 
669  // --------------------------------------------------------
670  /// \name Manipulating constants
671  //@{
672  /** \brief Add a constant, or if the constant already exists, change
673  its value
674  */
675  virtual void add_constant(std::string name, double val);
676 
677  /// Remove a constant
678  virtual void remove_constant(std::string name);
679 
680  /** \brief Set a constant equal to a value, but don't add it if
681  not already present
682 
683  If \c err_on_notfound is <tt>true</tt> (the default), then
684  this function throws an exception if a constant with
685  name \c name is not found. If \c err_on_notfound is
686  <tt>false</tt>, then if a constant with name \c name
687  is not found this function just silently returns
688  \ref o2scl::exc_enotfound.
689  */
690  virtual int set_constant(std::string name, double val,
691  bool err_on_notfound=true);
692 
693  /// Test if \c name is a constant
694  virtual bool is_constant(std::string name) const;
695 
696  /// Get a constant
697  virtual double get_constant(std::string name);
698 
699  /// Get a constant by index
700  virtual void get_constant(size_t ix, std::string &name,
701  double &val) const;
702 
703  /// Get the number of constants
704  virtual size_t get_nconsts() const {
705  return constants.size();
706  }
707  //@}
708 
709  /// \name Miscellaneous methods
710  //@{
711  /** \brief Read a generic table3d object specified as a
712  text file
713 
714  This function reads a set of columns of numerical values,
715  presuming that the first column is the x-grid value, the
716  second column is the y-grid value, and the remaining columns
717  are slices to be added.
718 
719  \todo A bit more documentation needs to be added here.
720 
721  \future It would be great to add a function which generates
722  a text file in this format as well.
723  */
724  int read_gen3_list(std::istream &fin, int verbose=0);
725 
726  /// Return the type, \c "table3d".
727  virtual const char *type() { return "table3d"; }
728  //@}
729 
730  /** \name Parsing mathematical functions specified as strings
731 
732  \comment
733  Note that doxygen doesn't format the documentation propertly
734  if the \name specification covers more than one line
735  \endcomment
736  */
737  //@{
738  /** \brief Fill a matrix from the function specified in \c function
739 
740  \comment
741  This function must return an int rather than void because
742  of the presence of the 'throw_on_err' mechanism
743  \endcomment
744  */
745  template<class resize_mat_t>
746  int function_matrix(std::string function, resize_mat_t &mat,
747  bool throw_on_err=true) {
748 
749  calculator calc;
750  std::map<std::string,double> vars;
751 
752  std::map<std::string,double>::const_iterator mit;
753  for(mit=constants.begin();mit!=constants.end();mit++) {
754  vars[mit->first]=mit->second;
755  }
756 
757  calc.compile(function.c_str(),&vars);
758 
759  if (mat.size1()!=numx || mat.size2()!=numy) {
760  mat.resize(numx,numy);
761  }
762 
763  for(size_t i=0;i<numx;i++) {
764  for(size_t j=0;j<numy;j++) {
765  vars[xname]=xval[i];
766  vars[yname]=yval[j];
767 
768  for(size_t k=0;k<list.size();k++) {
769  vars[get_slice_name(k)]=list[k](i,j);
770  }
771 
772  mat(i,j)=calc.eval(&vars);
773  }
774  }
775 
776 
777  return 0;
778  }
779 
780  /** \brief Make a column from <tt>function</tt> and add it to the table.
781 
782  If the column already exists, the data already present is
783  overwritten with the result.
784  */
785  void function_slice(std::string function, std::string col);
786  //@}
787 
788  /** \brief Copy slice named \c slice to a new \ref o2scl::table3d
789  object with a uniform grid using the current interpolation type
790  */
791  table3d slice_to_uniform_grid(std::string slice, size_t xpts,
792  bool log_x, size_t ypts, bool log_y);
793 
794  /** \brief Copy entire table to a new \ref o2scl::table3d
795  object with a uniform grid using the current interpolation type
796  */
797  table3d table_to_uniform_grid(size_t xpts, bool log_x,
798  size_t ypts, bool log_y);
799 
800 #ifdef O2SCL_NEVER_DEFINED
801 
802  /** \brief Desc
803  */
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]);
810  }
811  double avg=vector_mean(dev_x.size(),dev_x);
812  double stddev=vector_stddev(dev_x.size(),dev_x);
813  bool x_set=false;
814  log_x=false;
815  if (stddev<1.0e-8*fabs(avg)) {
816  x.resize(numx);
817  for(size_t i=0;i<numx;i++) {
818  x[i]=xval[i];
819  }
820  x_set=true;
821  } else {
822  dev_x.clear();
823  for(size_t i=0;i<numx-1;i++) {
824  dev_x.push_back(xval[i+1]/xval[i]);
825  }
826  avg=vector_mean(dev_x.size(),dev_x);
827  stddev=vector_stddev(dev_x.size(),dev_x);
828  if (stddev<1.0e-8*fabs(avg)) {
829  x.resize(numx);
830  for(size_t i=0;i<numx;i++) {
831  x[i]=xval[i];
832  }
833  x_set=true;
834  log_x=true;
835  }
836  }
837  if (x_set==false) {
839  }
840  return;
841  }
842 #endif
843 
844  protected:
845 
846 #ifndef DOXYGEN_INTERNAL
847 
848  /// \name Iterator types
849  //@{
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;
854  //@}
855 
856  /// \name Data storage
857  //@{
858  /// The list of constants
859  std::map<std::string,double> constants;
860 
861  /// The size of the x grid
862  size_t numx;
863 
864  /// The size of the y grid
865  size_t numy;
866 
867  /// A tree connecting column names to list indexes
868  std::map<std::string,size_t,std::greater<std::string> > tree;
869 
870  /// The name for the x grid
871  std::string xname;
872 
873  /// The name for the y grid
874  std::string yname;
875 
876  /// The pointers to the matrices
877  std::vector<ubmatrix> list;
878 
879  /// The x grid
880  ubvector xval;
881 
882  /// The y grid
883  ubvector yval;
884 
885  /// True if the grid has been set
886  bool xy_set;
887 
888  /// True if the size of the grid has been set
889  bool size_set;
890 
891  /// True if the table has at least one slice
892  bool has_slice;
893 
894  /// The interpolation type
895  size_t itype;
896  //@}
897 
898  /// \name Tree iterator boundaries
899  //@{
900  /// Return the beginning of the slice tree
901  map_iter begin() {return tree.begin();};
902  /// Return the end of the slice tree
903  map_iter end() {return tree.end();};
904  /// Return the beginning of the slice tree
905  map_const_iter const_begin() const {return tree.begin();};
906  /// Return the end of the slice tree
907  map_const_iter const_end() const {return tree.end();};
908  //@}
909 
910 #endif
911 
912  };
913 
914 #ifndef DOXYGEN_NO_O2NS
915 }
916 #endif
917 
918 #endif
bool size_set
True if the size of the grid has been set.
Definition: table3d.h:889
double vector_mean(size_t n, const vec_t &data)
Compute the mean of the first n elements of a vector.
Definition: vec_stats.h:55
map_const_iter const_begin() const
Return the beginning of the slice tree.
Definition: table3d.h:905
int function_matrix(std::string function, resize_mat_t &mat, bool throw_on_err=true)
Fill a matrix from the function specified in function.
Definition: table3d.h:746
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.
Definition: table3d.h:655
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
size_t numy
The size of the y grid.
Definition: table3d.h:865
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.
Definition: table3d.h:246
Data table table class.
Definition: table.h:49
std::string xname
The name for the x grid.
Definition: table3d.h:871
invalid argument supplied by user
Definition: err_hnd.h:59
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.
Definition: table3d.h:325
map_iter begin()
Return the beginning of the slice tree.
Definition: table3d.h:901
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.
Definition: contour.h:266
size_t numx
The size of the x grid.
Definition: table3d.h:862
bool has_slice
True if the table has at least one slice.
Definition: table3d.h:892
ubvector yval
The y grid.
Definition: table3d.h:883
double vector_stddev(size_t n, const vec_t &data)
Standard deviation with specified mean.
Definition: vec_stats.h:253
size_t itype
The interpolation type.
Definition: table3d.h:895
Calculate contour lines from a two-dimensional data set.
Definition: contour.h:240
std::vector< ubmatrix > list
The pointers to the matrices.
Definition: table3d.h:877
virtual const char * type()
Return the type, "table3d".
Definition: table3d.h:727
The O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl namespace ...
Definition: table.h:53
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.
Definition: table3d.h:907
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
Definition: err_hnd.h:273
Evaluate a mathematical expression in a string.
map_iter end()
Return the end of the slice tree.
Definition: table3d.h:903
void copy_to_slice(mat_t &m, std::string slice_name)
Copy to a slice from a generic matrix object.
Definition: table3d.h:491
Data table table class with units.
Definition: table_units.h:42
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.
Definition: table3d.h:342
bool xy_set
True if the grid has been set.
Definition: table3d.h:886
A data structure containing one or more slices of two-dimensional data points defined on a grid...
Definition: table3d.h:77
Store data in an O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$sc...
Definition: hdf_file.h:101
std::string yname
The name for the y grid.
Definition: table3d.h:874
virtual size_t get_nconsts() const
Get the number of constants.
Definition: table3d.h:704
ubvector xval
The x grid.
Definition: table3d.h:880
std::map< std::string, double > constants
The list of constants.
Definition: table3d.h:859
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
Definition: table3d.h:563
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
Definition: table3d.h:261
void set_levels(size_t nlevels, vec_t &ulevels)
Set the contour levels.
Definition: contour.h:337
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.
Definition: table3d.h:868
static const double x2[5]
Definition: inte_qng_gsl.h:66
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.
Definition: table3d.h:119
static const double x1[5]
Definition: inte_qng_gsl.h:48
void hdf_input(hdf_file &hf, o2scl::table< vec_t > &t, std::string name)
Input a o2scl::table object from a hdf_file.
Definition: hdf_io.h:146
Interpolation class for general vectors.
Definition: interp.h:1654
Linear grid with fixed number of bins and fixed endpoint.
Definition: uniform_grid.h:333

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).