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);
103 typename tbci_traits<T>::const_refval_type
107 typename tbci_traits<T>::const_refval_type
113 typename tbci_traits<T>::const_refval_type
121 {
return (*
this)(
i); }
122 typename tbci_traits<T>::const_refval_type
124 {
return (*
this)(
i); }
141 {
data.fill (value);
return *
this; }
152 {
return this->
fill (value); }
177 T max (
unsigned long& pos)
const {
return data.max (pos); }
178 T min (
unsigned long& pos)
const {
return data.min (pos); }
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 >
272 rank (va), shape ((unsigned)va), layout ((unsigned)va)
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);
310 offs += ix (
i) * layout (
i);
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++)
346 if (layout(
i) > div && layout(
i) < maxdiv && shape(
i) > 1)
347 { div = layout(
i); ixnr =
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>
418 BCHK((
unsigned)va != rank,
TensErr,
vararg no must be eq to rank, (
unsigned)va, data(0));
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
432 BCHK((
unsigned)va != rank,
TensErr,
vararg no must be eq to rank, (
unsigned)va, data(0));
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>
447 BCHK((
unsigned)va != rank,
TensErr,
vararg no must be eq to rank, (
unsigned)va, data.getcref(0));
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)
545 template <
typename T >
550 this->layout.
resize ((
unsigned)va);
551 this->shape.resize ((
unsigned)va);
554 for (
unsigned i = 0;
i < (unsigned)va; ++
i)
555 this->shape (
i) = va_arg (vl,
unsigned);
557 this->calcsize (); this->calclayout ();
558 this->data.resize (value, this->noel);
562 template <
typename T >
567 this->layout.
resize ((
unsigned)va);
568 this->shape.resize ((
unsigned)va);
571 for (
unsigned i = 0;
i < this->rank; ++
i)
572 this->shape (
i) = va_arg (vl,
unsigned);
574 this->calcsize (); this->calclayout ();
575 this->data.resize (this->noel);
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>
603 Index sh2 (this->shape); sh2.append (t2.
shape);
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
641 Index ixr (result.rank);
642 Index ix1 (this->rank);
644 for (
unsigned long i = 0;
i < result.noel; ++
i) {
645 ixr = result.calc_indx (
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>
749 if (this->layout == t.
layout)
750 for (
unsigned long i = 0;
i < this->noel;
i++)
751 this->data(
i) += t.
data(
i);
753 Index ix (0, this->rank);
754 while (ix(0) < this->shape(0)) {
755 (*this)(ix) += t(ix);
762 template <
typename T>
766 if (this->layout == t.
layout)
767 for (
unsigned long i = 0;
i < this->noel;
i++)
768 this->data(
i) -= t.
data(
i);
770 Index ix (0, this->rank);
771 while (ix(0) < this->shape(0)) {
772 (*this)(ix) -= t(ix);
779 template <
typename T>
784 if (this->layout == t.
layout)
785 for (
unsigned long i = 0;
i < this->noel;
i++)
788 Index ix (0, this->rank);
789 while (ix(0) < this->shape(0)) {
790 r(ix) = (*this)(ix) + t(ix);
797 template <
typename T>
802 if (this->layout == t.
layout)
803 for (
unsigned long i = 0;
i < this->noel;
i++)
806 Index ix (0, this->rank);
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)
821 res.
data(
i) = m * this->data(
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)
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)
CTensor< T > & operator=(const CTensor< T > &ct)
unsigned long lin_size(void) const
CTensor< T > & resize(const Index &ix)
TensErr(const char *t, const long i=0)
STD__ istream & operator>>(STD__ istream &istr, BdMatrix< T > &mat)
Tensor< T > drctmul(const Tensor< T > &) const
Tensor< T > contract(const unsigned, const unsigned) const
Tensor< T > & operator*=(const T m)
exception base class for the TBCI NumLib
T min(unsigned long &pos) const
Note (KG, 981214): We don't handle rank == 0.
Tensor< T > operator/(const T m) const
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 & operator()(const Index &ix)
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)
bool operator!=(const CTensor< T > &ct)
Tensor class including arithmetics.
T aligned_value_type TALIGN(MIN_ALIGN2)
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)
BVector< T > & append(const T &)
performs poorly
Index calc_indx(const unsigned long i) const
Tensor< T > & operator/=(const T m)
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 = ...
bool operator==(const CTensor< T > &ct)
Tensor< T > cntrmul(const Tensor< T > &, const unsigned, const unsigned) const
unsigned long size() const
const T & getcref(vararg va,...) const
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)
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > long int res
unsigned lin_read(vararg va,...)
unsigned rank_size(void) const
Tensor(const unsigned dim_rank)
Tensor< T > & operator+=(const Tensor< T > &)
Tensor< T > operator*(const T m) const
friend Tensor< T > FRIEND_TBCI2__ dctmul FGD(const Tensor< T > &, 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)