Go to the documentation of this file.
23 #ifndef O2SCL_TENSOR_H
24 #define O2SCL_TENSOR_H
35 #include <boost/numeric/ublas/vector.hpp>
36 #include <boost/numeric/ublas/vector_proxy.hpp>
37 #include <boost/numeric/ublas/matrix.hpp>
38 #include <boost/numeric/ublas/matrix_proxy.hpp>
40 #include <gsl/gsl_matrix.h>
41 #include <gsl/gsl_ieee_utils.h>
43 #include <o2scl/err_hnd.h>
44 #include <o2scl/interp.h>
45 #include <o2scl/table3d.h>
46 #include <o2scl/misc.h>
48 #ifndef DOXYGEN_NO_O2NS
82 static const size_t sum=3;
92 static const size_t grid=8;
110 double v1=0.0,
double v2=0.0,
double v3=0.0) {
130 index_spec
ix_fixed(
size_t ix,
size_t ix2);
134 index_spec
ix_sum(
size_t ix);
138 index_spec
ix_trace(
size_t ix,
size_t ix2);
146 index_spec
ix_range(
size_t ix,
size_t start,
size_t end);
151 index_spec
ix_interp(
size_t ix,
double v);
156 index_spec
ix_grid(
size_t ix,
double begin,
double end,
size_t n_bins,
162 index_spec
ix_gridw(
size_t ix,
double begin,
double end,
double width,
225 template<
class data_t=
double,
class vec_t=std::vector<data_t>,
226 class vec_
size_t=std::vector<
size_t> >
class tensor {
230 #ifndef DOXYGEN_INTERNAL
259 template<
class size_vec_t>
260 tensor(
size_t rank,
const size_vec_t &dim) {
265 for(
size_t i=0;i<
rk;i++) {
268 O2SCL_ERR((((std::string)
"Requested zero size with non-zero ")+
269 "rank for index "+
szttos(i)+
270 " in tensor::tensor(size_t,size_vec_t)").c_str(),
276 for(
size_t i=0;i<
rk;i++) {
293 if (
data.size()!=0) {
294 O2SCL_ERR2(
"Rank is zero but the data vector has non-zero size ",
300 O2SCL_ERR2(
"Rank does not match size vector size ",
306 for(
size_t i=0;i<
rk;i++) tot*=
size[i];
308 O2SCL_ERR2(
"One entry in the size vector is zero ",
311 if (tot!=
data.size()) {
312 O2SCL_ERR2(
"Product of size vector entries does not match data ",
359 template<
class size_vec_t>
360 void set(
const size_vec_t &index, data_t val) {
361 #if O2SCL_NO_RANGE_CHECK
366 if (index[0]>=
size[0]) {
368 " greater than or equal to size[0]="+
369 szttos(
size[0])+
" in tensor::set().").c_str(),
374 for(
size_t i=1;i<
rk;i++) {
375 #if O2SCL_NO_RANGE_CHECK
377 if (index[i]>=
size[i]) {
379 szttos(index[i])+
" greater than or equal to size "+
380 szttos(
size[i])+
" in tensor::set().").c_str(),
400 if (
data.size()!=dat.size()) {
401 O2SCL_ERR2(
"Size of new vector does not equal tensor size in ",
412 template<
class size_vec_t> data_t &
get(
const size_vec_t &index) {
413 #if O2SCL_NO_RANGE_CHECK
418 if (index[0]>=
size[0]) {
420 " greater than or equal to size[0]="+
421 szttos(
size[0])+
" in tensor::get().").c_str(),
426 for(
size_t i=1;i<
rk;i++) {
427 #if O2SCL_NO_RANGE_CHECK
429 if (index[i]>=
size[i]) {
431 szttos(index[i])+
" greater than or equal to size "+
432 szttos(
size[i])+
" in tensor::get().").c_str(),
443 template<
class size_vec_t>
444 data_t
const &
get(
const size_vec_t &index)
const {
445 #if O2SCL_NO_RANGE_CHECK
450 if (index[0]>=
size[0]) {
452 " greater than or equal to size[0]="+
453 szttos(
size[0])+
" in tensor::get() (const).").c_str(),
458 for(
size_t i=1;i<
rk;i++) {
459 #if O2SCL_NO_RANGE_CHECK
461 if (index[i]>=
size[i]) {
463 szttos(index[i])+
" greater than or equal to size "+
464 szttos(
size[i])+
" in tensor::get() (const).").c_str(),
475 typedef boost::numeric::ublas::vector_slice<
477 typedef boost::numeric::ublas::slice slice;
503 template<
class size_vec_t>
507 " greater than or equal to rank "+
szttos(
rk)+
508 " in tensor::vector_slice()").c_str(),
514 for(
size_t i=1;i<
rk;i++) {
516 if (i!=ix) start+=index[i];
519 for(
size_t i=ix+1;i<
rk;i++) stride*=
size[i];
520 return ubvector_slice(
data,slice(start,stride,
size[ix]));
524 #ifdef O2SCL_NEVER_DEFINED
532 template<
class size_vec_t> ubmatrix_array matrix_slice() {
548 template<
class size_vec_t>
549 void resize(
size_t rank,
const size_vec_t &dim) {
550 for(
size_t i=0;i<rank;i++) {
552 O2SCL_ERR((((std::string)
"Requested zero size with non-zero ")+
553 "rank for index "+
szttos(i)+
" in tensor::"+
563 for(
size_t i=0;i<
rk;i++) {
582 " greater than or equal to rank "+
szttos(
rk)+
583 " in tensor::get_size()").c_str(),
605 for(
size_t i=0;i<
rk;i++) tot*=
size[i];
613 template<
class size_vec_t>
619 if (index[0]>=
size[0]) {
621 " greater than or equal to size[0]="+
622 szttos(
size[0])+
" in tensor::pack_indices().").c_str(),
626 for(
size_t i=1;i<
rk;i++) {
627 if (index[i]>=
size[i]) {
629 szttos(index[i])+
" greater than or equal to size "+
630 szttos(
size[i])+
" in tensor::pack_indices().").c_str(),
640 template<
class size_vec_t>
644 " greater than or equal to total size"+
646 " in tensor::unpack_index().").c_str(),
650 size_t ix2, sub_size;
651 for(
size_t i=0;i<
rk;i++) {
656 for(
size_t j=i+1;j<
rk;j++) sub_size*=
size[j];
657 index[i]=ix/sub_size;
659 ix-=sub_size*(ix/sub_size);
677 size_t ix=o2scl::vector_min_index<vec_t,data_t>(
total_size(),
data);
685 void min(vec_size_t &index, data_t &val) {
701 size_t ix=o2scl::vector_max_index<vec_t,data_t>(
total_size(),
data);
709 void max(vec_size_t &index, data_t &val) {
725 size_t ix_min, ix_max;
726 o2scl::vector_minmax_index<vec_t,data_t>
736 void minmax(vec_size_t &index,
size_t &index_min, data_t &
min,
737 size_t &index_max, data_t &
max) {
738 size_t ix_min, ix_max;
749 if (
rk==0)
return 0.0;
751 for(
size_t i=0;i<
data.size();i++) {
764 (
size_t ix_x,
size_t ix_y,
table3d &tab, std::string x_name=
"x",
765 std::string y_name=
"y", std::string slice_name=
"z") {
771 if (nx==0 && ny==0) {
773 if (x_name.length()==0) x_name=
"x";
774 if (y_name.length()==0) y_name=
"y";
778 std::vector<double> grid_x(
size[ix_x]), grid_y(
size[ix_y]);
779 for(
size_t i=0;i<
size[ix_x];i++) {
780 grid_x[i]=((double)i);
782 for(
size_t i=0;i<
size[ix_y];i++) {
783 grid_y[i]=((double)i);
785 tab.
set_xy(
"x",grid_x.size(),grid_x,
786 "y",grid_y.size(),grid_y);
792 if (nx!=this->size[ix_x] || ny!=this->size[ix_y]) {
794 "tensor_grid::convert_table3d_sum().",
exc_einval);
799 std::vector<size_t> ix;
802 tab.
set(ix[ix_x],ix[ix_y],slice_name,
803 tab.
get(ix[ix_x],ix[ix_y],slice_name)+
817 int verbose=0,
bool err_on_fail=
true) {
820 size_t rank_old=this->
rk;
824 std::vector<size_t> size_new;
828 std::vector<index_spec> spec_old(rank_old);
829 std::vector<index_spec> spec_new;
835 std::vector<size_t> sum_sizes;
838 for(
size_t i=0;i<spec.size();i++) {
841 if (spec[i].ix1>=rank_old) {
843 O2SCL_ERR2(
"Index too large (index,reverse) in ",
847 std::cout <<
"Index " << spec[i].ix1
848 <<
" too large (index,reverse) in "
849 <<
"tensor in tensor::rearrange_and_copy()."
855 size_new.push_back(this->size[spec[i].ix1]);
858 spec_old[spec[i].ix1]=
index_spec(spec[i].type,rank_new);
859 spec_new.push_back(
index_spec(spec[i].type,spec[i].ix1));
863 std::cout <<
"In range " << spec[i].ix1 <<
" "
864 << spec[i].ix2 <<
" " << spec[i].ix3 << std::endl;
866 if (spec[i].ix1>=rank_old ||
867 spec[i].ix2>=this->size[spec[i].ix1] ||
868 spec[i].ix3>=this->size[spec[i].ix1]) {
874 std::cout <<
"Index " << spec[i].ix1 <<
" "
875 << spec[i].ix2 <<
" " << spec[i].ix3
876 <<
" too large (range) in "
877 <<
"tensor in tensor::rearrange_and_copy()."
883 if (spec[i].ix3>spec[i].ix2) {
884 size_new.push_back(spec[i].ix3-spec[i].ix2+1);
886 size_new.push_back(spec[i].ix2-spec[i].ix3+1);
890 spec_old[spec[i].ix1]=
891 index_spec(spec[i].type,rank_new,spec[i].ix2,spec[i].ix3);
894 spec[i].ix2,spec[i].ix3));
897 std::cout <<
"Out range " << size_new[size_new.size()-1]
901 if (spec[i].ix1>=rank_old || spec[i].ix2>=rank_old) {
907 std::cout <<
"Indices " << spec[i].ix1 <<
" or "
908 << spec[i].ix2 <<
" too large (trace) in "
909 <<
"tensor in tensor::rearrange_and_copy()."
915 if (
size[spec[i].ix1]<
size[spec[i].ix2]) {
916 n_sum_loop*=
size[spec[i].ix1];
917 sum_sizes.push_back(
size[spec[i].ix1]);
919 n_sum_loop*=
size[spec[i].ix2];
920 sum_sizes.push_back(
size[spec[i].ix2]);
924 spec_old[spec[i].ix1]=
index_spec(spec[i].type,
925 spec[i].ix1,spec[i].ix2);
926 spec_old[spec[i].ix2]=
index_spec(spec[i].type,
927 spec[i].ix2,spec[i].ix1);
929 if (spec[i].ix1>=rank_old) {
935 std::cout <<
"Index " << spec[i].ix1
936 <<
" too large (sum) in "
937 <<
"tensor in tensor::rearrange_and_copy()."
943 n_sum_loop*=
size[spec[i].ix1];
944 sum_sizes.push_back(
size[spec[i].ix1]);
945 spec_old[spec[i].ix1]=
index_spec(spec[i].type,
946 spec[i].ix1,spec[i].ix2,0);
948 if (spec[i].ix1>=rank_old ||
949 spec[i].ix2>=this->size[spec[i].ix1]) {
955 std::cout <<
"Index too large (fixed) in "
956 <<
"tensor in tensor::rearrange_and_copy()."
964 spec_old[spec[i].ix1]=
index_spec(spec[i].type,
965 rank_new,spec[i].ix2);
968 O2SCL_ERR2(
"Index specification type not allowed in ",
972 std::cout <<
"Index specification type not allowed in "
973 <<
"tensor::rearrange_and_copy()." << std::endl;
979 size_t n_sums=sum_sizes.size();
988 std::cout <<
"Zero new indices in "
989 <<
"tensor::rearrange_and_copy()." << std::endl;
995 for(
size_t i=0;i<rank_old;i++) {
998 O2SCL_ERR2(
"Not all indices accounted for in ",
1002 std::cout <<
"Index " << i <<
" not accounted for in "
1003 <<
"tensor::rearrange_and_copy()." << std::endl;
1012 std::cout <<
"Using a " << rank_old <<
" rank tensor to create a new "
1013 << rank_new <<
" rank tensor." << std::endl;
1016 for(
size_t i=0;i<rank_old;i++) {
1017 std::cout <<
"Old index " << i;
1019 std::cout <<
" is being remapped to new index " << spec_old[i].ix1
1020 <<
"." << std::endl;
1022 std::cout <<
" is being remapped to new index " << spec_old[i].ix1
1023 <<
" with a range from " << spec_old[i].ix2
1024 <<
" to " << spec_old[i].ix3 <<
"." << std::endl;
1026 std::cout <<
" is being reversed and remapped to new index "
1027 << spec_old[i].ix1 <<
"." << std::endl;
1029 std::cout <<
" is being traced with index "
1030 << spec_old[i].ix2 <<
"." << std::endl;
1032 std::cout <<
" is being summed." << std::endl;
1034 std::cout <<
" is being fixed to " << spec_old[i].ix2
1035 <<
"." << std::endl;
1038 for(
size_t i=0;i<rank_new;i++) {
1039 std::cout <<
"New index " << i;
1041 std::cout <<
" was remapped from old index " << spec_new[i].ix1
1042 <<
"." << std::endl;
1044 std::cout <<
" was remapped from old index " << spec_new[i].ix1
1045 <<
" using range from " << spec_new[i].ix2 <<
" to "
1046 << spec_new[i].ix3 <<
"." << std::endl;
1048 std::cout <<
" was reversed and remapped from old index "
1049 << spec_new[i].ix1 <<
"." << std::endl;
1058 std::vector<size_t> ix_new(rank_new);
1059 std::vector<size_t> ix_old(rank_old);
1060 std::vector<size_t> sum_ix(n_sums);
1069 for(
size_t j=0;j<rank_old;j++) {
1071 ix_old[j]=ix_new[spec_old[j].ix1];
1073 if (spec_old[j].ix2<spec_old[j].ix3) {
1074 ix_old[j]=ix_new[spec_old[j].ix1]+spec_old[j].ix2;
1076 ix_old[j]=spec_old[j].ix2-ix_new[spec_old[j].ix1];
1079 ix_old[j]=
get_size(j)-1-ix_new[spec_old[j].ix1];
1081 ix_old[j]=spec_old[j].ix2;
1087 for(
size_t j=0;j<n_sum_loop;j++) {
1091 size_t j2=j, sub_size;
1092 for(
size_t k=0;k<n_sums;k++) {
1097 for(
size_t kk=k+1;kk<n_sums;kk++) sub_size*=sum_sizes[kk];
1098 sum_ix[k]=j2/sub_size;
1099 j2-=sub_size*(j2/sub_size);
1103 std::cout <<
"n_sum_loop: " << n_sum_loop <<
" n_sums: "
1104 << n_sums <<
" sum_sizes: ";
1106 std::cout <<
"j: " << j <<
" sum_ix: ";
1112 for(
size_t k=0;k<rank_old;k++) {
1114 if (cnt>=sum_ix.size()) {
1115 std::cout <<
"X: " << cnt <<
" " << sum_ix.size() << std::endl;
1119 ix_old[k]=sum_ix[cnt];
1122 spec_old[k].ix1<spec_old[k].ix2) {
1123 if (cnt>=sum_ix.size()) {
1124 std::cout <<
"X: " << cnt <<
" " << sum_ix.size() << std::endl;
1128 ix_old[spec_old[k].ix1]=sum_ix[cnt];
1129 ix_old[spec_old[k].ix2]=sum_ix[cnt];
1135 std::cout <<
"Here old: ";
1137 std::cout <<
"Here new: ";
1145 t_new.
set(ix_new,val);
1155 template<
class data_t=
double,
class vec_t=std::vector<data_t>,
1156 class vec_
size_t=std::vector<
size_t> >
class tensor1 :
1157 public tensor<data_t,vec_t,vec_size_t> {
1166 vec_size_t sizex(1);
1179 "tensor1::is_valid().",
1195 const data_t &
get(
size_t ix)
const {
1200 void set(
size_t index, data_t val)
1208 template<
class size_vec_t>
1209 void set(
const size_vec_t &index, data_t val) {
1220 const data_t &
operator[](
size_t ix)
const {
return this->data[ix]; }
1226 const data_t &
operator()(
size_t ix)
const {
return this->data[ix]; }
1233 template<
class data_t=
double,
class vec_t=std::vector<data_t>,
1234 class vec_
size_t=std::vector<
size_t> >
class tensor2 :
1235 public tensor<data_t,vec_t,vec_size_t> {
1249 this->data.resize(tot);
1258 if (this->rk!=0 && this->rk!=2) {
1260 "tensor2::is_valid().",
1271 data_t &
get(
size_t ix1,
size_t ix2) {
1272 size_t sz[2]={ix1,ix2};
1277 const data_t &
get(
size_t ix1,
size_t ix2)
const {
1278 size_t sz[2]={ix1,ix2};
1283 void set(
size_t ix1,
size_t ix2, data_t val) {
1284 size_t sz[2]={ix1,ix2};
1294 template<
class size_vec_t>
1295 void set(
const size_vec_t &index, data_t val) {
1302 {
return this->data[ix*this->size[1]+iy]; }
1306 {
return this->data[ix*this->size[1]+iy]; }
1312 template<
class data_t=
double,
class vec_t=std::vector<data_t>,
1313 class vec_
size_t=std::vector<
size_t> >
class tensor3 :
1314 public tensor<data_t,vec_t,vec_size_t> {
1323 tensor<data_t,vec_t,vec_size_t>() {
1329 size_t tot=sz*sz2*sz3;
1330 this->data.resize(tot);
1339 if (this->rk!=0 && this->rk!=3) {
1341 "tensor3::is_valid().",
1352 data_t &
get(
size_t ix1,
size_t ix2,
size_t ix3) {
1353 size_t sz[3]={ix1,ix2,ix3};
1358 const data_t &
get(
size_t ix1,
size_t ix2,
size_t ix3)
const {
1359 size_t sz[3]={ix1,ix2,ix3};
1364 void set(
size_t ix1,
size_t ix2,
size_t ix3, data_t val) {
1365 size_t sz[3]={ix1,ix2,ix3};
1375 template<
class size_vec_t>
1376 void set(
const size_vec_t &index, data_t val) {
1386 template<
class data_t=
double,
class vec_t=std::vector<data_t>,
1387 class vec_
size_t=std::vector<
size_t> >
class tensor4 :
1388 public tensor<data_t,vec_t,vec_size_t> {
1396 tensor4(
size_t sz,
size_t sz2,
size_t sz3,
size_t sz4) :
1397 tensor<data_t,vec_t,vec_size_t>() {
1404 size_t tot=sz*sz2*sz3*sz4;
1405 this->data.resize(tot);
1414 if (this->rk!=0 && this->rk!=4) {
1416 "tensor4::is_valid().",
1427 data_t &
get(
size_t ix1,
size_t ix2,
size_t ix3,
size_t ix4) {
1428 size_t sz[4]={ix1,ix2,ix3,ix4};
1433 const data_t &
get(
size_t ix1,
size_t ix2,
size_t ix3,
1435 size_t sz[4]={ix1,ix2,ix3,ix4};
1440 void set(
size_t ix1,
size_t ix2,
size_t ix3,
size_t ix4,
1442 size_t sz[4]={ix1,ix2,ix3,ix4};
1452 template<
class size_vec_t>
1453 void set(
const size_vec_t &index, data_t val) {
1464 template<
class tensor_t>
1465 void tensor_out(std::ostream &os, tensor_t &t,
bool pretty=
true) {
1469 size_t rk=t.get_rank();
1470 os <<
"rank: " << rk <<
" sizes: ";
1471 for(
size_t i=0;i<rk;i++) {
1472 os << t.get_size(i) <<
" ";
1475 auto &data=t.get_data();
1476 std::vector<size_t> ix(rk);
1477 std::vector<std::string> sv, sv_out;
1478 for(
size_t i=0;i<t.total_size();i++) {
1479 t.unpack_index(i,ix);
1480 std::string tmp=
"(";
1481 for(
size_t j=0;j<rk;j++) {
1492 for(
size_t k=0;k<sv_out.size();k++) {
1493 os << sv_out[k] << std::endl;
1498 size_t rk=t.get_rank();
1500 for(
size_t i=0;i<rk;i++) {
1501 os << t.get_size(i) <<
" ";
1504 auto &data=t.get_data();
1505 for(
size_t i=0;i<t.total_size();i++) {
1506 os << data[i] <<
" ";
1507 if (i%10==9) os << std::endl;
1518 template<
class data_t,
class vec_t,
class vec_
size_t>
1522 for(
size_t i=0;i<t1.
get_rank();i++) {
1528 if (v1[i]!=v2[i])
return false;
1534 #ifndef DOXYGEN_NO_O2NS
Tensor class with arbitrary dimensions.
size_t get_rank() const
Return the rank of the tensor.
data_t & get(size_t ix1, size_t ix2, size_t ix3, size_t ix4)
Get the element indexed by (ix1,ix2,ix3,ix4)
index_spec(size_t typ, size_t i1, size_t i2=0, size_t i3=0, double v1=0.0, double v2=0.0, double v3=0.0)
Explicit full constructor.
const data_t & operator[](size_t ix) const
Get an element using array-like indexing (const version)
tensor(size_t rank, const size_vec_t &dim)
Create a tensor of rank rank with sizes given in dim.
tensor4()
Create an empty tensor.
static const size_t index
Retain an index.
tensor1()
Create an empty tensor.
void set(const size_vec_t &index, data_t val)
Set the element indexed by index to value val.
tensor1(size_t sz)
Create a rank 1 tensory of size sz.
void min(vec_size_t &index, data_t &val)
Compute the index of the minimum value in the tensor and return the minimum.
void set(size_t ix, size_t iy, std::string name, double val)
Set element in slice name at location ix,iy to value val.
data_t & operator()(size_t ix)
Get an element using operator()
data_t max_value()
Compute the maximum value in the tensor.
std::string dtos(const fp_t &x, int prec=6, bool auto_prec=false)
Convert a double to a string.
The default vector type from uBlas.
static const size_t sum
Sum over an index.
static const size_t reverse
Reverse an index.
size_t type
Type of specification.
void is_valid() const
Check that the o2scl::tensor object is valid.
void set(const size_vec_t &index, data_t val)
Set the element indexed by index to value val.
index_spec ix_range(size_t ix, size_t start, size_t end)
Index covers a range of values.
index_spec ix_grid(size_t ix, double begin, double end, size_t n_bins, bool log=false)
Interpolate grid with fixed number of bins into index ix (for o2scl::tensor_grid only)
void set(size_t ix1, size_t ix2, data_t val)
Set the element indexed by (ix1,ix2) to value val.
void set(const size_vec_t &index, data_t val)
Set the element indexed by index to value val.
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
bool operator==(const tensor< data_t, vec_t, vec_size_t > &t1, const tensor< data_t, vec_t, vec_size_t > &t2)
Compare two tensors for equality.
const data_t & get(size_t ix1, size_t ix2) const
Get the element indexed by (ix1,ix2)
const data_t & operator()(size_t ix) const
Get an element using operator() (const version)
static const size_t fixed
Fix the value of an index.
static const size_t grid
Interpolate a value to set a new grid (fixed bin number)
void min_index(vec_size_t &index)
Compute the index of the minimum value in the tensor.
data_t & operator[](size_t ix)
Get an element using array-like indexing.
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
void max_index(vec_size_t &index)
Compute the index of the maximum value in the tensor.
const data_t & get(const size_vec_t &index) const
Get a const reference to the element indexed by index.
data_t & get(const size_vec_t &index)
Get the element indexed by index.
const data_t & get(size_t ix1, size_t ix2, size_t ix3) const
Get the element indexed by (ix1,ix2,ix3)
ubvector_slice vector_slice(size_t ix, const size_vec_t &index)
Fix all but one index to create a vector.
double val1
First double argument.
vec_size_t size
A rank-sized vector of the sizes of each dimension.
index_spec ix_gridw(size_t ix, double begin, double end, double width, bool log=false)
Interpolate grid with fixed bin width into index ix (for o2scl::tensor_grid only)
size_t get_size(size_t i) const
Returns the size of the ith index.
tensor2()
Create an empty tensor.
size_t pack_indices(const size_vec_t &index)
Pack the indices into a single vector index.
tensor3()
Create an empty tensor.
const vec_size_t & get_size_arr() const
Return the full vector of sizes.
const data_t & operator()(size_t ix, size_t iy) const
Get the element indexed by (ix1,ix2) (const version)
static const size_t trace
Perform a trace (sum over two indices)
index_spec ix_index(size_t ix)
Choose an index.
void is_valid() const
Check that the o2scl::tensor4 object is valid.
index_spec ix_interp(size_t ix, double v)
Interpolate value v into index ix (for o2scl::tensor_grid only)
size_t ix2
Second argument.
const vec_t & get_data() const
Return the full data vector.
void screenify(size_t nin, const string_arr_t &in_cols, std::vector< std::string > &out_cols, size_t max_size=80)
Reformat the columns for output of width size.
size_t ix1
First argument.
double total_sum() const
Return the sum over every element in the tensor.
const data_t & get(size_t ix) const
Get the element indexed by ix.
data_t & get(size_t ix)
Get the element indexed by ix.
@ exc_einval
invalid argument supplied by user
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.
data_t & get(size_t ix1, size_t ix2)
Get the element indexed by (ix1,ix2)
tensor4(size_t sz, size_t sz2, size_t sz3, size_t sz4)
Create a rank 4 tensor of size (sz,sz2,sz3,sz4)
data_t & get(size_t ix1, size_t ix2, size_t ix3)
Get the element indexed by (ix1,ix2,ix3)
void set(size_t ix1, size_t ix2, size_t ix3, data_t val)
Set the element indexed by (ix1,ix2,ix3) to value val.
void set(const size_vec_t &index, data_t val)
Set the element indexed by index to value val.
void is_valid() const
Check that the o2scl::tensor1 object is valid.
tensor3(size_t sz, size_t sz2, size_t sz3)
Create a rank 3 tensor of size (sz,sz2,sz3)
index_spec ix_trace(size_t ix, size_t ix2)
Perform a trace over indices ix and ix2.
double & get(size_t ix, size_t iy, std::string name)
Get element in slice name at location ix,iy
size_t ix3
Third argument.
void clear()
Clear the tensor of all data and free allocated memory.
tensor2(size_t sz, size_t sz2)
Create a rank 2 tensor of size (sz,sz2)
data_t min_value()
Compute the minimum value in the tensor.
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.
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")
Convert to a o2scl::table3d object by summing over all but two indices.
tensor()
Create an empty tensor with zero rank.
void set(const size_vec_t &index, data_t val)
Set the element indexed by index to value val.
data_t & operator()(size_t ix, size_t iy)
Get the element indexed by (ix1,ix2)
@ exc_eindex
Invalid index for array or matrix.
static const size_t range
Choose a new range for an index.
void set(size_t ix1, size_t ix2, size_t ix3, size_t ix4, data_t val)
Set the element indexed by (ix1,ix2,ix3,ix4) to value val.
double val2
Second double argument.
double val3
Third double argument.
@ exc_esanity
sanity check failed - shouldn't happen
Interpolation class for general vectors.
void set_slice_all(std::string name, double val)
Set all of the values in slice name to val.
void minmax(vec_size_t &index, size_t &index_min, data_t &min, size_t &index_max, data_t &max)
Compute the indices and values of the maximum and minimum in the tensor.
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
tensor< data_t > rearrange_and_copy(std::vector< index_spec > spec, int verbose=0, bool err_on_fail=true)
Rearrange, sum and copy current tensor to a new tensor.
void get_size(size_t &nx, size_t &ny) const
Get the size of the slices.
index_spec ix_reverse(size_t ix)
Reverse index ix.
void tensor_out(std::ostream &os, tensor_t &t, bool pretty=true)
Output a tensor to a stream.
void is_valid() const
Check that the o2scl::tensor2 object is valid.
static const size_t gridw
Interpolate a value to set a new grid (fixed bin width)
void resize(size_t rank, const size_vec_t &dim)
Resize the tensor to rank rank with sizes given in dim.
void vector_out(std::ostream &os, size_t n, const vec_t &v, bool endline=false)
Output the first n elements of a vector to a stream, os.
void swap_data(vec_t &dat)
Swap the data vector.
index_spec ix_sum(size_t ix)
Sum over index ix.
std::string szttos(size_t x)
Convert a size_t to a string.
index_spec()
Default constructor.
static const size_t empty
Empty specification.
void max(vec_size_t &index, data_t &val)
Compute the index and value of the maximum value in the tensor and return the maximum.
size_t total_size() const
Returns the size of the tensor (the product of the sizes over every index)
const data_t & get(size_t ix1, size_t ix2, size_t ix3, size_t ix4) const
Get the element indexed by (ix1,ix2,ix3,ix4)
index_spec ix_fixed(size_t ix, size_t ix2)
Fix index ix to value ix2.
void is_valid() const
Check that the o2scl::tensor3 object is valid.
A data structure containing one or more slices of two-dimensional data points defined on a grid.
void set_all(data_t x)
Set all elements in a tensor to some fixed value.
void minmax_value(data_t &min, data_t &max)
Compute the minimum and maximum values in the tensor.
void unpack_index(size_t ix, size_vec_t &index)
Unpack the single vector index into indices.
void set(size_t index, data_t val)
Set the element indexed by index to value val.
Documentation generated with Doxygen. Provided under the
GNU Free Documentation License (see License Information).