15 #include "tbci/vector.h" 16 #include "tbci/index.h" 19 #if !defined(NO_GD) && !defined(AUTO_DECL) 20 # include "tbci/tensor_gd.h" 25 #ifndef TBCI_DISABLE_EXCEPT 31 :
NumErr(
"Error in Tensor class") {}
41 # pragma interface "tensor.h" 69 unsigned long fac = 1;
85 explicit CTensor (
const unsigned dim_rank);
95 unsigned long calc_offs (
const Index & ix)
const;
96 Index calc_indx (
const unsigned long i)
const;
102 {
return data (calc_offs (ix)); }
103 typename tbci_traits<T>::const_refval_type
104 operator () (
const Index & ix)
const 105 {
return data (calc_offs (ix)); }
106 T & operator () (
vararg va, ...);
107 typename tbci_traits<T>::const_refval_type
108 operator () (
vararg va, ...)
const;
110 T & operator () (
const unsigned long i)
113 typename tbci_traits<T>::const_refval_type
114 operator () (
const unsigned long i)
const 118 const T& getcref (
vararg va, ...)
const;
121 {
return (*
this)(
i); }
122 typename tbci_traits<T>::const_refval_type
124 {
return (*
this)(
i); }
127 unsigned lin_read (
vararg va, ...);
133 BCHK (i>=rank,
TensErr, swap_idx: idx1 out of range, i, *
this);
134 BCHK (j>=rank,
TensErr, swap_idx: idx2 out of range, j, *
this);
135 SWAP (shape(i), shape(j));
SWAP (layout(i), layout(j));
141 { data.
fill (value);
return *
this; }
152 {
return this->fill (value); }
158 layout.
resize(rank); calclayout ();
159 calcsize(); data.
resize (noel);
165 layout.
resize(rank); calclayout ();
166 calcsize(); data.
resize (val, noel);
177 T max (
unsigned long& pos)
const {
return data.
max (pos); }
178 T min (
unsigned long& pos)
const {
return data.
min (pos); }
188 if (shape != ct.
shape)
return false;
189 if (layout == ct.
layout) {
190 if (data == ct.
data)
return true;
else return false; }
193 while (ix(0) < shape(0))
195 if ((*
this)(ix) != ct(ix))
return false;
213 friend STD__ ostream & operator << FGD (STD__ ostream&, const CTensor <T>&);
220 template <
typename T >
222 rank (dim_rank), shape (dim_rank, dim_rank), layout (dim_rank)
225 calcsize (); calclayout ();
229 template <
typename T >
231 rank (ix.size()), shape (ix), layout (ix.size())
234 calcsize (); calclayout ();
238 template <
typename T >
240 rank (ix.size()), shape (ix), layout (ix.size())
244 calcsize (); calclayout ();
245 data.resize (value, noel);
249 template <
typename T >
251 : data(ct.data), rank(ct.rank), noel(ct.noel),
252 shape(ct.shape), layout(ct.layout)
256 template <
typename T >
258 rank (va), shape ((unsigned)va), layout ((unsigned)va)
262 for (
unsigned i = 0;
i < (unsigned)va; ++
i)
263 shape (
i) = va_arg (vl,
unsigned);
270 template <
typename T >
276 for (
unsigned i = 0;
i <
rank; ++
i)
277 shape (
i) = va_arg (vl,
unsigned);
284 template <
typename T >
290 #if !defined(__clang__) || !defined(CPLX) 291 for (i = 0; i < (unsigned)va; ++
i)
292 data (i) = va_arg (vl,
T);
294 throw TensErr(
"vararg not supported for cplx in clang");
295 #warning no vararg support with cplx numbers and clang 303 template <
typename T >
307 unsigned long offs (0);
317 template <
typename T >
321 unsigned long mod =
i;
322 for (
unsigned r =
rank; r > 0; r--)
324 ix (r - 1) = mod %
shape (r - 1);
325 mod /=
shape (r - 1);
333 template <
typename T >
337 unsigned long mod =
i;
340 unsigned long maxdiv =
noel;
344 unsigned long div = 0;
unsigned ixnr = 0;
345 for (
unsigned i = 0;
i <
rank;
i++)
349 ix(ixnr) = mod / div; mod %= div;
356 template <
typename T >
357 STD__ ostream & operator << (STD__ ostream & os, const CTensor < T > & ct)
362 Index ix (0, ct.rank);
363 for (
unsigned long i = 0;
i < ct.noel;
i++)
365 os << ct(ix) <<
"\t"; ix.
next_idx (ct.shape);
366 for (
unsigned j = ct.rank - 1; j > 0; j--)
367 if (ix(j) == 0) os <<
"\n";
else break;
375 template <
typename T>
379 for (
unsigned long i = 0;
i < ct.
noel;
i++)
381 is >> ct(ix); ix.
next_idx (ct.shape);
388 template <
typename T>
391 Index ix ((
unsigned)va);
394 for (
unsigned i = 0;
i < (unsigned)va; ++
i)
395 ix (
i) = va_arg (vl,
int);
400 template <
typename T>
401 typename tbci_traits<T>::const_refval_type
404 Index ix ((
unsigned)va);
407 for (
unsigned i = 0;
i < (unsigned)va; ++
i)
408 ix (
i) = va_arg (vl,
int);
415 template <
typename T>
419 unsigned lin_idx = 0;
422 for (
unsigned i = 0;
i <
rank; ++
i)
423 lin_idx += va_arg (vl,
int) *
layout(
i);
425 return (
data (lin_idx));
428 template <
typename T>
429 typename tbci_traits<T>::const_refval_type
433 unsigned lin_idx = 0;
436 for (
unsigned i = 0;
i <
rank; ++
i)
437 lin_idx += va_arg (vl,
int) *
layout(
i);
439 return (
data (lin_idx));
444 template <
typename T>
448 unsigned lin_idx = 0;
451 for (
unsigned i = 0;
i <
rank; ++
i)
452 lin_idx += va_arg (vl,
int) *
layout(
i);
454 return data.getcref (lin_idx);
502 for (
unsigned long i = 0;
i < this->
noel; ++
i)
514 for (
unsigned long i = 0;
i < this->
noel; ++
i)
522 for (
unsigned long i=0;
i < this->
noel; ++
i)
528 Tensor<T> contract (
const unsigned,
const unsigned)
const;
545 template <
typename T >
554 for (
unsigned i = 0;
i < (unsigned)va; ++
i)
555 this->
shape (
i) = va_arg (vl,
unsigned);
558 this->
data.resize (value, this->
noel);
562 template <
typename T >
571 for (
unsigned i = 0;
i < this->
rank; ++
i)
572 this->
shape (
i) = va_arg (vl,
unsigned);
579 template <
typename T>
586 BCHK (this->
shape (i1) != this->
shape (i2),
TensErr, Contraction over diff. sized indices, this->shape (i2), t);
589 while (bx(0) < ix(0)) {
591 for (
unsigned m = 0; m < this->
shape (i1); ++m)
599 template <
typename T>
609 while (ixr(0) < ix(0)) {
611 for (
unsigned m = 0; m < this->
shape (i1); ++m) {
614 result.
data(i) += (*this) (ix1) * t2 (ix2);
623 template <
typename T>
630 for (
unsigned i = 0;
i < this->
rank;
i++)
632 ix (c++) = this->
shape (
i);
635 for (
unsigned i = 0;
i < t2.
rank;
i++)
i 644 for (
unsigned long i = 0;
i < result.
noel; ++
i) {
646 for (
unsigned m = 0; m < this->
shape (i1); ++m) {
648 for (
unsigned k = 0; k < this->
rank; ++k) {
654 for (
unsigned l = 0; l < t2.
rank; ++l) {
660 result.
data(
i) += (*this)(ix1) * t2(ix2);
669 template <
typename T>
678 while (ixr(0) < ix(0)) {
680 for (
unsigned m = 0; m < t1.
shape (i1); ++m) {
689 template <
typename T>
699 unsigned long io = 1 - i1;
703 while (ixr(0) < ix(0)) {
705 for (
unsigned m = 0; m < metr.
shape (i1); ++m) {
706 mix (io) = ixr (i2); mix (i1) = m;
707 result.
data (i) += metr (mix) * t (
idx_set1 (ixr, i2, m));
714 template <
typename T>
721 while (ixr(0) < ix(0)) {
722 result (ixr) = (*this) (ixr.
slice (0, this->
rank))
723 * t1 (ixr.
slice (this->rank, result.
rank));
730 template <
typename T>
737 while (ixr(0) < ix(0)) {
738 result (ixr) = t0 (ixr.
slice (0, t0.
rank))
745 template <
typename T>
750 for (
unsigned long i = 0;
i < this->
noel;
i++)
754 while (ix(0) < this->
shape(0)) {
755 (*this)(ix) += t(ix);
762 template <
typename T>
767 for (
unsigned long i = 0;
i < this->
noel;
i++)
771 while (ix(0) < this->
shape(0)) {
772 (*this)(ix) -= t(ix);
779 template <
typename T>
785 for (
unsigned long i = 0;
i < this->
noel;
i++)
789 while (ix(0) < this->
shape(0)) {
790 r(ix) = (*this)(ix) + t(ix);
797 template <
typename T>
803 for (
unsigned long i = 0;
i < this->
noel;
i++)
807 while (ix(0) < this->
shape(0)) {
808 r(ix) = (*this)(ix) - t(ix);
816 template <
typename T>
820 for (
unsigned long i = 0;
i < this->
noel; ++
i)
826 template <typename T>
828 {
return t.
mult (m); }
Tensor< T > metrmul(const Tensor< T > &metr, const Tensor< T > &t, const unsigned i1, const unsigned i2)
Tensor< T > mult(const T) const
Vector< T > & fill(const T &v)
#define BCHKNR(cond, exc, txt, ind)
cplx< T > operator-(const T a, const cplx< T > &b)
unsigned long calcsize(void)
Tensor< T > dctmul(const Tensor< T > &t0, const Tensor< T > &t1)
Tensor< T > ctrmul(const Tensor< T > &t1, const Tensor< T > &t2, const unsigned i1, const unsigned i2)
unsigned long lin_size(void) const
CTensor< T > & resize(const Index &ix)
TensErr(const char *t, const long i=0)
Tensor< T > drctmul(const Tensor< T > &) const
exception base class for the TBCI NumLib
T min(unsigned long &pos) const
Note (KG, 981214): We don't handle rank == 0.
const unsigned TMatrix< T > * res
Tensor< T > operator-() const
#define BCHK(cond, exc, txt, ind, rtval)
unsigned long calc_offs(const Index &ix) const
TVector< T > slice(unsigned long, unsigned long) const
TVector< unsigned > idx_remove2(const Index &idx, const unsigned which1, const unsigned which2)
T & get_lin_idx(const unsigned long i)
TVector< unsigned > idx_set1(const Index &idx, const unsigned which, const unsigned what)
Tensor(const Tensor< T > &ct)
CTensor< T > & fill(const T &value)
Tensor class including arithmetics.
T aligned_value_type TALIGN(MIN_ALIGN2)
Tensor< T > operator*(const T m, const Tensor< T > &t)
Note that this #pragma interface might create problems as the class index is not templated! ...
long int Vector< T > & index
Index next_idx(const Index &max)
STD__ istream & operator>>(STD__ istream &is, CTensor< T > &ct)
BVector< T > & append(const T &)
performs poorly
Index calc_indx(const unsigned long i) const
T max(unsigned long &pos) const
Tensor< T > & operator-=(const Tensor< T > &)
void SWAP(T &a, T &b)
SWAP function Note: We could implement a swap function without temporaries: a -= b b += a a -= b a = ...
Tensor< T > cntrmul(const Tensor< T > &, const unsigned, const unsigned) const
unsigned long size() const
bool operator!=(const F_BandMatrix< T > &m1, const F_BandMatrix< T > &m2)
TensErr(const TensErr &te)
BVector< T > & resize(const BVector< T > &)
Actually it's a resize and copy (some people would expect the assignment op to do this) ...
CTensor< T > & transpose(const unsigned i, const unsigned j)
tbci_traits< T >::const_refval_type get_lin_idx(const unsigned long i) const
Index index_size(void) const
Tensor(const T &value, const Index &ix)
CTensor< T > & resize(const T &val, const Index &ix)
unsigned rank_size(void) const
bool operator==(const F_BandMatrix< T > &m1, const F_BandMatrix< T > &m2)
cplx< T > operator/(const T a, const cplx< T > &b)
Tensor(const unsigned dim_rank)
Tensor< T > & operator+=(const Tensor< T > &)
Tensor< T > operator+(const Tensor< T > &) const
TVector< unsigned > idx_fill_in2(const Index &idx, const unsigned where1, const unsigned what1, const unsigned where2, const unsigned what2)
TVector< unsigned > idx_fill_in1(const Index &idx, const unsigned where, const unsigned what)
CTensor< T > & resize(const CTensor< T > &ct)
T aligned_value_type TALIGN(MIN_ALIGN2)
cplx< T > operator+(const T a, const cplx< T > &b)