Tensor class with arbitrary dimensions with a grid. More...
#include <tensor_grid.h>
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 default_grid(), 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.
Slicing
New o2scl::tensor_grid objects can be obtained by fixing any set of indices using copy_slice_interp().
Fixing all but two indices also results in a o2scl::table3d object, and five functions perform this task in different ways. The function copy_table3d_align() copies a two-dimensional slice to a o2scl::table3d object presuming that the grid in the o2scl::table3d object has already been set and exactly matches the corresponding sizes for the selected tensor indices. This function does not check that the grids between the two objects match, it only ensures that they have the same size. In order to copy to a o2scl::table3d object and set its grid to match that from the unfixed indices in the o2scl::tensor_grid object, the function copy_table3d_align_setxy() can be used. The function copy_table3d_interp() uses interpolation to extract values from the o2scl::tensor_grid object. It allows the user to select indices to be fixed and then uses the values in the grid in the o2scl::table3d object for the indices which vary. Alternatively copy_table3d_interp_values() allows the user to specify values on the grid for the indices to be fixed and uses the grid in the o2scl::table3d object for the indices which vary. Finally, copy_table3d_interp_values_setxy() acts like copy_table3d_interp_values() except that it sets the o2scl::table3d grid to be the same as the grid in the o2scl::tensor_grid object which corresponds to the indices which are being varied.
Notes and Todos
Definition at line 46 of file tensor_grid.h.
Public Member Functions | |
Constructors and Destructors | |
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... | |
tensor_grid (std::vector< uniform_grid< double > > &ugs) | |
Create a tensor with a grid defined by a set of o2scl::uniform_grid objects. | |
virtual | ~tensor_grid () |
Destructor. | |
Method to check for valid object | |
void | is_valid () const |
Check that the o2scl::tensor_grid object is valid. | |
Copy constructors | |
tensor_grid (const tensor_grid< vec_t, vec_size_t > &t) | |
Copy using operator() | |
tensor_grid< vec_t, vec_size_t > & | operator= (const tensor_grid< vec_t, vec_size_t > &t) |
Copy using operator=() | |
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... | |
const vec_t & | get_grid () const |
Get grid. | |
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. | |
void | default_grid () |
Use a default grid which just uses the index. | |
template<class vec2_t > | |
void | set_grid_i_vec (size_t ix, const vec2_t &grid_vec) |
Set grid for one index from a vector. | |
template<class vec2_t > | |
void | set_grid_i_func (size_t ix, std::string func) |
Set grid for one index from a function. | |
void | set_grid (std::vector< uniform_grid< double > > &ugs) |
Set grid from a vector of uniform grid objects. More... | |
template<class rvec_t > | |
void | copy_grid (size_t i, rvec_t &v) const |
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_val (size_t i, const double &val, double &val2) const |
Lookup index for grid closest to val , returning the grid point. More... | |
size_t | lookup_grid (size_t i, double val) const |
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_packed_val (size_t i, double val, double &val2) const |
Lookup internal packed grid index for point closest to val and store closest value in val2 . More... | |
size_t | lookup_grid_packed (size_t i, double val) const |
Lookup internal packed grid index for point closest to val . | |
Slicing to tensor_grid objects | |
template<class size_vec2_t , class vec2_t > | |
tensor_grid | copy_slice_interp (size_vec2_t &ifix, vec2_t &vals) const |
Copy an abitrary slice by fixing 1 or more indices and use interpolation to return a new tensor_grid object. | |
Slicing and converting to table3d objects | |
void | convert_table3d_sum (size_t ix_x, size_t ix_y, table3d &tab, std::string x_name="x", std::string y_name="y", std::string slice_name="z") const |
Convert to a o2scl::table3d object by summing over all but two indices. | |
template<class size_vec2_t > | |
void | copy_table3d_align (size_t ix_x, size_t ix_y, size_vec2_t &index, table3d &tab, std::string slice_name="z") const |
Create a slice in a o2scl::table3d object with an aligned grid. More... | |
template<class size_vec2_t > | |
void | copy_table3d_align_setxy (size_t ix_x, size_t ix_y, size_vec2_t &index, table3d &tab, std::string x_name="x", std::string y_name="y", std::string slice_name="z") const |
Create a slice in a table3d object with a new aligned grid. | |
template<class size_vec2_t > | |
void | copy_table3d_interp (size_t ix_x, size_t ix_y, size_vec2_t &index, table3d &tab, std::string slice_name="z") const |
Copy to a slice in a table3d object using interpolation. More... | |
template<class vec2_t > | |
void | copy_table3d_interp_values (size_t ix_x, size_t ix_y, vec2_t &values, table3d &tab, std::string slice_name="z", int verbose=0) const |
Copy to a slice in a table3d object using interpolation. | |
template<class vec2_t > | |
void | copy_table3d_interp_values_setxy (size_t ix_x, size_t ix_y, vec2_t &values, table3d &tab, std::string x_name="x", std::string y_name="y", std::string slice_name="z") const |
Copy to a slice in a table3d object using interpolation creating a new table3d grid. | |
Clear method | |
void | clear () |
Clear the tensor of all data and free allocated memory. | |
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. | |
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 , class vec3_size_t , class vec2_t > | |
double | interp_linear_partial (const vec2_size_t &ix_to_interp, vec3_size_t &ix, const vec2_t &val) const |
Obtain a value by looking up some indices and interpolating the others. More... | |
template<class vec2_t > | |
double | interp_linear (vec2_t &v) const |
Perform a linear interpolation of v into the function implied by the tensor and grid. More... | |
template<class vec2_t > | |
double | interp_linear_power_two (vec2_t &v) const |
Perform linear interpolation assuming that all indices can take only two values. More... | |
template<class vec2_t , class vec3_t > | |
void | interp_linear_vec0 (vec2_t &v, vec3_t &res) const |
Perform a linear interpolation of v[1] to v[n-1] resulting in a vector. More... | |
template<class vec2_t , class vec3_t > | |
void | interp_linear_power_two_vec0 (vec2_t &v, vec3_t &res) const |
Perform linear interpolation assuming that the last n-1 indices can take only two values. More... | |
template<class vec2_t , class vec3_t > | |
void | interp_linear_vec (vec2_t &v, size_t ifree, vec3_t &res) const |
Perform a linear interpolation of v into the tensor leaving one index free resulting in a vector. More... | |
tensor_grid | rearrange_and_copy (std::vector< index_spec > spec, int verbose=0, bool err_on_fail=true) const |
Rearrange, sum and copy current tensor to a new tensor. More... | |
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) |
|
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 204 of file tensor_grid.h.
|
inline |
The type rvec_t
must be a vector with a resize method.
Definition at line 623 of file tensor_grid.h.
|
inline |
This function uses the grid associated with indices ix_x
and ix_y
, to copy data to a slice named slice_name
in the table3d object tab
. All other indices are fixed to values specified by the user in index
and the values of index[ix_x]
and index[ix_y]
are used for temporary storage.
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. If a slice named slice_name
is not already present in tab
, then a new slice with that name is created.
The error handler is called if ix_x
is the same as ix_y
, or if either of these two values is greater than or equal to the tensor rank.
Definition at line 921 of file tensor_grid.h.
|
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
. All other indices are fixed to values specified by the user in index
and the values of index[ix_x]
and index[ix_y]
are used for temporary storage.
If a slice named slice_name
is not already present in tab
, then a new slice with that name is created.
The error handler is called if ix_x
is the same as ix_y
, or if either of these two values is greater than or equal to the tensor rank.
Definition at line 1012 of file tensor_grid.h.
|
inline |
The parameters gridp
and closest
may refer to the same object.
Definition at line 386 of file tensor_grid.h.
|
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 containing
v
. It then calls interp_linear_power_two() to perform the interpolation in that hypercube.
Definition at line 1366 of file tensor_grid.h.
|
inline |
To call this function, the arguments should be of the following form
ix_to_interp
should be a list of indices to interpolate. The size of ix_to_interp
must be at least 1 or larger but smaller than or equal to the full tensor rank. All entries in ix_to_interp
should be smaller than the full tensor rank.ix
should have a size equal to the tensor rank, but values stored in entries corresponding to the indices in ix_to_interp
will be ignoredval
should be a list of values to be interpolated and should have a size equal to that of ix_to_interp
. Definition at line 1289 of file tensor_grid.h.
|
inline |
This function works by recursively slicing the hypercube of size into a hypercube of size
performing linear interpolation for each pair of points.
Definition at line 1417 of file tensor_grid.h.
|
inline |
This function performs linear interpolation assuming that the last n-1
indices can take only two values and placing the result into res
.
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 1521 of file tensor_grid.h.
|
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
.
Definition at line 1575 of file tensor_grid.h.
|
inline |
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 1460 of file tensor_grid.h.
|
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.
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.Definition at line 1191 of file tensor_grid.h.
|
inline |
This version, rather than o2scl::tensor_grid::lookup_grid_val() can be useful because it gives the index of the grid point in the internal grid vector object.
Definition at line 737 of file tensor_grid.h.
|
inline |
The parameters val
and val2
may refer to the same object.
Definition at line 674 of file tensor_grid.h.
|
inline |
The values in vals
are not modified by this function.
Definition at line 722 of file tensor_grid.h.
|
inline |
Definition at line 1672 of file tensor_grid.h.
|
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 424 of file tensor_grid.h.
|
inline |
Definition at line 599 of file tensor_grid.h.
|
inline |
The grid must be specified for all of the dimensions at once. Denote as the size of the first dimension,
as the size of the second dimesion, and so on. Then the first
entries in
grid
must be the grid for the first dimension, the next entries must be the grid for the second dimension, and so on. Thus
grid
must be a vector of size
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.
Definition at line 486 of file tensor_grid.h.
|
inline |
The parameters closest
and grdp
may be identical, allowing one to update a vector grdp
with the closest grid point.
Definition at line 335 of file tensor_grid.h.
Documentation generated with Doxygen. Provided under the
GNU Free Documentation License (see License Information).