Data Tables

O2scl

The class table is a container to hold and perform operations on related columns of data. It supports column operations, interpolation, column reference by either name or index, binary searching (in the case of ordered columns), and sorting. It also supports creating new columns with functions which operate on column names using the calc_utf8 class (see also Algebraic Function Evaluation). A child class, table_units is similiar except that it additionally allows one to specify physical units for each column and convert units using convert_units .

The class table3d is a generalization of table which operates on two-dimensional slices of data rather than one-dimensional columns.

For higher-dimensional generalizations, the class tensor_grid provides a set of data defined on a grid in an arbitrary number of dimensions.

Table example

/* Example: ex_table.cpp
   -------------------------------------------------------------------
   See "License Information" section of the documentation for license
   information.
*/

#include <iostream>
#include <o2scl/table.h>
#include <o2scl/constants.h>
#include <o2scl/test_mgr.h>
#include <o2scl/hdf_file.h>
#include <o2scl/hdf_io.h>

using namespace std;
using namespace o2scl;
using namespace o2scl_hdf;

int main(void) {

  cout.setf(ios::scientific);
  
  test_mgr t;
  t.set_output_level(1);

  // Create a table with two columns. 
  table<> dat;
  dat.line_of_names("x y");

  // Increase the number of lines
  dat.set_nlines(11);
  
  for(int i=0;i<11;i++) {
    dat.set("x",i,((double)i)/10.0);
    dat.set("y",i,sin(dat.get("x",i)));
  }
  
  // We demonstrate interpolation and differentiation and 
  // compare with the exact answers
  cout << " Result        Expected" << endl;
  cout << "----------------------------" << endl;
  cout << " " << dat.interp("y",0.5,"x") << "  " << asin(0.5) << endl;
  cout << " " << dat.deriv("x",0.5,"y") << "  " << cos(0.5) << endl;
  cout << dat.deriv2("x",0.5,"y") << " " << -sin(0.5) << endl;
  cout << endl;

  dat.new_column("cx");
  dat.new_column("sx");
  for(size_t i=0;i<dat.get_nlines();i++) {
    dat.set("sx",i,dat["y"][i]);
    dat.set("cx",i,sqrt(1.0-pow(dat["sx"][i],2.0)));
  }

  dat.add_constant("pi",acos(-1.0));

  dat.new_column("s2");
  for(size_t i=0;i<dat.get_nlines();i++) {
    dat.set("s2",i,sin(dat["x"][i]*acos(-1.0)/2.0));
  }

  // We get approximate derivatives for an entire column
  dat.deriv("x","sx","cx2");

  hdf_file hf;
  hf.open_or_create("ex_table.o2");
  hdf_output(hf,dat,"table");
  hf.close();
  
  t.report();
  return 0;
}