Public Member Functions | Protected Attributes | Friends | List of all members
o2scl::tensor_grid< vec_t, vec_size_t > Class Template Reference

Tensor class with arbitrary dimensions with a grid. More...

#include <tensor_grid.h>

Inheritance diagram for o2scl::tensor_grid< vec_t, vec_size_t >:
o2scl::tensor< vec_t, vec_size_t > o2scl::tensor_grid1< vec_t, vec_size_t > o2scl::tensor_grid2< vec_t, vec_size_t > o2scl::tensor_grid3< vec_t, vec_size_t > o2scl::tensor_grid4< vec_t, vec_size_t >

Public Member Functions

 tensor_grid ()
 Create an empty tensor with zero rank.
 
template<class size_vec_t >
 tensor_grid (size_t rank, const size_vec_t &dim)
 Create a tensor of rank rank with sizes given in dim. More...
 
vec_t & get_data ()
 Return a reference to the data (for HDF I/O)
 
Set functions
template<class vec2_t >
void set_val (const vec2_t &grdp, double val)
 Set the element closest to grid point grdp to value val.
 
template<class vec2_t , class vec3_t >
void set_val (const vec2_t &grdp, double val, vec3_t &closest)
 Set the element closest to grid point grdp to value val. More...
 
Get functions
template<class vec2_t >
double get_val (const vec2_t &gridp)
 Get the element closest to grid point gridp.
 
template<class vec2_t , class vec3_t >
double get_val (const vec2_t &gridp, vec3_t &closest)
 Get the element closest to grid point gridp, store grid values in closest and return value. More...
 
Resize method
template<class size_vec2_t >
void resize (size_t rank, const size_vec2_t &dim)
 Resize the tensor to rank rank with sizes given in dim. More...
 
Grid manipulation
bool is_grid_set () const
 Return true if the grid has been set.
 
template<class vec2_t >
void set_grid_packed (const vec2_t &grid_vec)
 Set the grid. More...
 
template<class vec_vec_t >
void set_grid (const vec_vec_t &grid_vecs)
 Set grid from a vector of vectors of grid points.
 
template<class rvec_t >
void copy_grid (size_t i, rvec_t &v)
 Copy grid for index i to vector v. More...
 
double get_grid (size_t i, size_t j) const
 Lookup jth value on the ith grid.
 
void set_grid (size_t i, size_t j, double val)
 Set the jth value on the ith grid.
 
size_t lookup_grid (size_t i, double val)
 Lookup index for grid closest to val.
 
template<class vec2_t , class size_vec2_t >
void lookup_grid_vec (const vec2_t &vals, size_vec2_t &indices) const
 Lookup indices for grid closest point to vals. More...
 
size_t lookup_grid_val (size_t i, const double &val, double &val2)
 Lookup index for grid closest to val, returning the grid point. More...
 
size_t lookup_grid_packed (size_t i, double val)
 Lookup index for grid closest to val.
 
size_t lookup_grid_packed_val (size_t i, double val, double &val2)
 Lookup index for grid closest to val.
 
Slicing
template<class size_vec2_t >
void copy_slice_align (size_t ix_x, size_t ix_y, size_vec2_t &index, table3d &tab, std::string slice_name)
 Create a slice in a table3d object with an aligned grid. More...
 
template<class size_vec2_t >
void copy_slice_interp (size_t ix_x, size_t ix_y, size_vec2_t &index, table3d &tab, std::string slice_name)
 Copy to a slice in a table3d object using interpolation. More...
 
Clear method
void clear ()
 Clear the tensor of all data and free allocated memory.
 
Interpolation
void set_interp_type (size_t interp_type)
 Set interpolation type for interpolate()
 
template<class range_t = ub_range, class data_range_t = ubvector_range, class index_range_t = ubvector_size_t_range>
double interpolate (double *vals)
 Interpolate values vals into the tensor, returning the result. More...
 
template<class vec2_size_t >
double interp_linear (vec2_size_t &v)
 Perform a linear interpolation of v into the function implied by the tensor and grid. More...
 
template<class vec2_size_t >
double interp_linear_power_two (vec2_size_t &v)
 Perform linear interpolation assuming that all indices can take only two values. More...
 
template<class vec2_size_t , class vec2_t >
void interp_linear_vec0 (vec2_size_t &v, vec2_t &res)
 Perform a linear interpolation of v[1] to v[n-1] resulting in a vector. More...
 
template<class vec2_size_t , class vec2_t >
void interp_linear_power_two_vec0 (vec2_size_t &v, vec2_t &res)
 Perform linear interpolation assuming that the last n-1 indices can take only two values. More...
 
template<class vec2_size_t , class vec2_t >
void interp_linear_vec (vec2_size_t &v, size_t ifree, vec2_t &res)
 Perform a linear interpolation of v into the tensor leaving one index free resulting in a vector. More...
 
- Public Member Functions inherited from o2scl::tensor< vec_t, vec_size_t >
 tensor ()
 Create an empty tensor with zero rank.
 
template<class size_vec_t >
 tensor (size_t rank, const size_vec_t &dim)
 Create a tensor of rank rank with sizes given in dim. More...
 
void clear ()
 Clear the tensor of all data and free allocated memory.
 
template<class size_vec_t >
void set (const size_vec_t &index, double val)
 Set the element indexed by index to value val.
 
void set_all (double x)
 Set all elements in a tensor to some fixed value.
 
template<class size_vec_t >
double & get (const size_vec_t &index)
 Get the element indexed by index.
 
template<class size_vec_t >
double const & get (const size_vec_t &index) const
 Get a const reference to the element indexed by index.
 
template<class size_vec_t >
ubvector_slice vector_slice (size_t ix, const size_vec_t &index)
 Fix all but one index to create a vector. More...
 
template<class size_vec_t >
void resize (size_t rank, const size_vec_t &dim)
 Resize the tensor to rank rank with sizes given in dim. More...
 
size_t get_rank () const
 Return the rank of the tensor.
 
size_t get_size (size_t i) const
 Returns the size of the ith index.
 
const vec_size_t & get_size_arr () const
 Return the full vector of sizes.
 
const vec_t & get_data () const
 Return the full data vector.
 
size_t total_size () const
 Returns the size of the tensor (the product of the sizes over every index)
 
template<class size_vec_t >
size_t pack_indices (const size_vec_t &index)
 Pack the indices into a single vector index.
 
template<class size_vec_t >
void unpack_indices (size_t ix, size_vec_t &index)
 Unpack the single vector index into indices.
 
double min_value ()
 Compute the minimum value in the tensor.
 
void min_index (vec_size_t &index)
 Compute the index of the minimum value in the tensor.
 
void min (vec_size_t &index, double &val)
 Compute the index of the minimum value in the tensor and return the minimum.
 
double max_value ()
 Compute the maximum value in the tensor.
 
void max_index (vec_size_t &index)
 Compute the index of the maximum value in the tensor.
 
void max (vec_size_t &index, double &val)
 Compute the index and value of the maximum value in the tensor and return the maximum.
 
void minmax_value (double &min, double &max)
 Compute the minimum and maximum values in the tensor.
 
void minmax_index (vec_size_t &index_min, vec_size_t &index_max)
 Compute the indices of the minimum and maximum values in the tensor.
 
void minmax (vec_size_t &index, size_t &index_min, double &min, size_t &index_max, double &max)
 Compute the indices and values of the maximum and minimum in the tensor.
 

Protected Attributes

vec_t grid
 A rank-sized set of arrays for the grid points.
 
bool grid_set
 If true, the grid has been set by the user.
 
size_t itype
 Interpolation type.
 
- Protected Attributes inherited from o2scl::tensor< vec_t, vec_size_t >
vec_t data
 The data.
 
vec_size_t size
 A rank-sized vector of the sizes of each dimension.
 
size_t rk
 Rank.
 

Friends

template<class vecf_t , class vecf_size_t >
void o2scl_hdf::hdf_output (o2scl_hdf::hdf_file &hf, tensor_grid< vecf_t, vecf_size_t > &t, std::string name)
 
template<class vecf_t , class vecf_size_t >
void o2scl_hdf::hdf_input (o2scl_hdf::hdf_file &hf, tensor_grid< vecf_t, vecf_size_t > &t, std::string name)
 

Additional Inherited Members

- Public Types inherited from o2scl::tensor< vec_t, vec_size_t >
typedef boost::numeric::ublas::vector_slice< boost::numeric::ublas::vector< double > > ubvector_slice
 
typedef boost::numeric::ublas::slice slice
 

Detailed Description

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
class o2scl::tensor_grid< vec_t, vec_size_t >

This tensor class allows one to assign the indexes to numerical scales, effectively defining a data set on an n-dimensional grid. To set the grid, use set_grid() or set_grid_packed().

By convention, member functions ending in the _val suffix return the closest grid-point to some user-specified values.

Note
Currently, HDF5 I/O is only allowed if the tensor is allocated with std::vector-based types, and the interpolate() function only works with ublas-based vector types.
Todo:
It is possible for the user to create a tensor_grid object, upcast it to a tensor object, and then use tensor::resize() to resize the tensor, failing to resize the grid. This probably needs fixing.
Idea for Future:

Is it really necessary that get_data() is public?

Only allocate space for grid if it is set

Consider creating a new set_grid() function which takes grids from an object like uniform_grid. Maybe make a constructor for a tensor_grid object which just takes as input a set of grids?

Definition at line 46 of file tensor_grid.h.

Constructor & Destructor Documentation

◆ tensor_grid()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class size_vec_t >
o2scl::tensor_grid< vec_t, vec_size_t >::tensor_grid ( size_t  rank,
const size_vec_t &  dim 
)
inline

The parameter dim must be a vector of sizes with length rank. If the user requests any of the sizes to be zero, this constructor will call the error handler.

Definition at line 147 of file tensor_grid.h.

Member Function Documentation

◆ copy_grid()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class rvec_t >
void o2scl::tensor_grid< vec_t, vec_size_t >::copy_grid ( size_t  i,
rvec_t &  v 
)
inline

The type rvec_t must be a vector with a resize method.

Definition at line 386 of file tensor_grid.h.

◆ copy_slice_align()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class size_vec2_t >
void o2scl::tensor_grid< vec_t, vec_size_t >::copy_slice_align ( size_t  ix_x,
size_t  ix_y,
size_vec2_t &  index,
table3d tab,
std::string  slice_name 
)
inline

This function uses the grid associated with indices ix_x and ix_y, and the tensor interpolation function to copy to the slice named slice_name in the table3d object tab .

If the table3d object does not currently have a grid set, then the grid is automatically set to be the same as that stored in the tensor_grid object associated with ranks ix_x and iy_y. If the o2scl::table3d object does have a grid set, then the values returned by o2scl::table3d::get_nx() and o2scl::table3d::get_ny() must be equal to the size of the tensor in indices ix_x and ix_y, respectively.

This currently requires a copy of all the tensor data into the table3d object.

Definition at line 593 of file tensor_grid.h.

◆ copy_slice_interp()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class size_vec2_t >
void o2scl::tensor_grid< vec_t, vec_size_t >::copy_slice_interp ( size_t  ix_x,
size_t  ix_y,
size_vec2_t &  index,
table3d tab,
std::string  slice_name 
)
inline

This function uses the grid associated with indices ix_x and ix_y, and the tensor interpolation function to copy the tensor information to the slice named slice_name in the table3d object tab .

Note
This function uses the tensor_grid::interp_linear() for the interpolation.

Definition at line 655 of file tensor_grid.h.

◆ get_val()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class vec2_t , class vec3_t >
double o2scl::tensor_grid< vec_t, vec_size_t >::get_val ( const vec2_t &  gridp,
vec3_t &  closest 
)
inline

The parameters gridp and closest may refer to the same object.

Definition at line 249 of file tensor_grid.h.

◆ interp_linear()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class vec2_size_t >
double o2scl::tensor_grid< vec_t, vec_size_t >::interp_linear ( vec2_size_t &  v)
inline

This performs multi-dimensional linear interpolation (or extrapolation) It works by first using o2scl::search_vec to find the interval containing (or closest to) the specified point in each direction and constructing the corresponding hypercube of size $ 2^{\mathrm{rank}} $ containing v. It then calls interp_linear_power_two() to perform the interpolation in that hypercube.

Idea for Future:
This starts with a small copy, which can be eliminated by creating a new version of interp_linear_power_two which accepts an offset vector parameter so that the first interpolation is offset. Remaining interpolations don't need to be offset because the tensor has to be created from the previous interpolation round.

Definition at line 841 of file tensor_grid.h.

◆ interp_linear_power_two()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class vec2_size_t >
double o2scl::tensor_grid< vec_t, vec_size_t >::interp_linear_power_two ( vec2_size_t &  v)
inline

This function works by recursively slicing the hypercube of size $ 2^{\mathrm{rank}} $ into a hypercube of size $ 2^{\mathrm{rank-1}} $ performing linear interpolation for each pair of points.

Note
This is principally a function for internal use by interp_linear().

Definition at line 892 of file tensor_grid.h.

◆ interp_linear_power_two_vec0()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class vec2_size_t , class vec2_t >
void o2scl::tensor_grid< vec_t, vec_size_t >::interp_linear_power_two_vec0 ( vec2_size_t &  v,
vec2_t &  res 
)
inline

This function performs linear interpolation assuming that the last n-1 indices can take only two values and placing the result into res.

Note
The type vec2_t for the vector res must have a resize() method. This is principally a function for internal use by interp_linear_vec0().

Definition at line 996 of file tensor_grid.h.

◆ interp_linear_vec()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class vec2_size_t , class vec2_t >
void o2scl::tensor_grid< vec_t, vec_size_t >::interp_linear_vec ( vec2_size_t &  v,
size_t  ifree,
vec2_t &  res 
)
inline

This performs multi-dimensional linear interpolation (or extrapolation) in the last n-1 indices of the rank-n tensor leaving the first index free and places the results in the vector res.

Idea for Future:
This function could be more efficient.

Definition at line 1050 of file tensor_grid.h.

◆ interp_linear_vec0()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class vec2_size_t , class vec2_t >
void o2scl::tensor_grid< vec_t, vec_size_t >::interp_linear_vec0 ( vec2_size_t &  v,
vec2_t &  res 
)
inline
Note
The type vec2_t for the vector res must have a resize() method.

This performs multi-dimensional linear interpolation (or extrapolation) in the last n-1 indices of the rank-n tensor leaving the first index free and places the results in the vector res.

Definition at line 935 of file tensor_grid.h.

◆ interpolate()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class range_t = ub_range, class data_range_t = ubvector_range, class index_range_t = ubvector_size_t_range>
double o2scl::tensor_grid< vec_t, vec_size_t >::interpolate ( double *  vals)
inline

This is a quick and dirty implementation of n-dimensional interpolation by recursive application of the 1-dimensional routine from interp_vec, using the base interpolation object specified in the template parameter base_interp_t. This will be very slow for sufficiently large data sets.

Note
This function requires a range objects to obtain ranges of vector objects. In ublas, this is done with ublas::vector_range objects, so this function will certainly work for tensor_grid objects built on ublas vector types. There is no corresponding std::range, but you may be able to use either ublas::vector_range or Boost.Range in order to call this function with tensor_grid objects built on std::vector. However, this is not fully tested at the moment.
Idea for Future:
It should be straightforward to improve the scaling of this algorithm significantly by creating a "window" of local points around the point of interest. This could be done easily by constructing an initial subtensor. However, this should probably be superceded by a more generic alternative which avoids explicit use of the 1-d interpolation types.

Definition at line 744 of file tensor_grid.h.

◆ lookup_grid_val()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
size_t o2scl::tensor_grid< vec_t, vec_size_t >::lookup_grid_val ( size_t  i,
const double &  val,
double &  val2 
)
inline

The parameters val and val2 may refer to the same object.

Definition at line 483 of file tensor_grid.h.

◆ lookup_grid_vec()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class vec2_t , class size_vec2_t >
void o2scl::tensor_grid< vec_t, vec_size_t >::lookup_grid_vec ( const vec2_t &  vals,
size_vec2_t &  indices 
) const
inline

The values in vals are not modified by this function.

Definition at line 470 of file tensor_grid.h.

◆ resize()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class size_vec2_t >
void o2scl::tensor_grid< vec_t, vec_size_t >::resize ( size_t  rank,
const size_vec2_t &  dim 
)
inline

The parameter dim must be a vector of sizes with a length equal to rank. This resize method is always destructive, and the grid is always reset.

If the user requests any of the sizes to be zero, this function will call the error handler.

Definition at line 282 of file tensor_grid.h.

◆ set_grid_packed()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class vec2_t >
void o2scl::tensor_grid< vec_t, vec_size_t >::set_grid_packed ( const vec2_t &  grid_vec)
inline

The grid must be specified for all of the dimensions at once. Denote $ (\mathrm{size})_0 $ as the size of the first dimension, $ (\mathrm{size})_1 $ as the size of the second dimesion, and so on. Then the first $ (\mathrm{size})_0 $ entries in grid must be the grid for the first dimension, the next $ (\mathrm{size})_1 $ entries must be the grid for the second dimension, and so on. Thus grid must be a vector of size

\[ \sum_{i=0}^{\mathrm{rank}} (\mathrm{size})_i \]

Note that the grid is copied so the function argument may be destroyed by the user after calling set_grid_packed() without affecting the tensor grid.

Idea for Future:
Define a more generic interface for matrix types

Definition at line 344 of file tensor_grid.h.

◆ set_val()

template<class vec_t = std::vector<double>, class vec_size_t = std::vector<size_t>>
template<class vec2_t , class vec3_t >
void o2scl::tensor_grid< vec_t, vec_size_t >::set_val ( const vec2_t &  grdp,
double  val,
vec3_t &  closest 
)
inline

The parameters closest and grdp may be identical, allowing one to update a vector grdp with the closest grid point.

Definition at line 198 of file tensor_grid.h.


The documentation for this class was generated from the following file:

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