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

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