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
118 const T&
getcref (vararg va, ...)
const;
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;
220template <
typename T >
229template <
typename T >
238template <
typename T >
249template <
typename T >
256template <
typename T >
262 for (
unsigned i = 0;
i < (unsigned)va; ++
i)
263 shape (
i) = va_arg (vl,
unsigned);
270template <
typename T >
276 for (
unsigned i = 0;
i <
rank; ++
i)
277 shape (
i) = va_arg (vl,
unsigned);
284template <
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
303template <
typename T >
307 unsigned long offs (0);
317template <
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);
333template <
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;
356template <
typename T >
363 for (
unsigned long i = 0;
i < ct.
noel;
i++)
366 for (
unsigned j = ct.
rank - 1; j > 0; j--)
367 if (ix(j) == 0) os <<
"\n";
else break;
379 for (
unsigned long i = 0;
i < ct.
noel;
i++)
381 is >> ct(ix); ix.
next_idx (ct.shape);
389T& CTensor <T>::operator () (vararg va, ...)
391 Index ix ((
unsigned)va);
394 for (
unsigned i = 0;
i < (unsigned)va; ++
i)
395 ix (
i) = va_arg (vl,
int);
401typename tbci_traits<T>::const_refval_type
402CTensor <T>::operator () (vararg va, ...)
const
404 Index ix ((
unsigned)va);
407 for (
unsigned i = 0;
i < (unsigned)va; ++
i)
408 ix (
i) = va_arg (vl,
int);
416T& CTensor <T>::operator () (vararg va, ...)
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));
429typename tbci_traits<T>::const_refval_type
430CTensor <T>::operator () (vararg va, ...)
const
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));
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);
484 Tensor (
const T, vararg va, ...);
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)
545template <
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);
558 this->
data.resize (value, this->
noel);
562template <
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);
584 Tensor < T > t (0, ix);
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)
606 Tensor <T> result (0, ix);
609 while (ixr(0) < ix(0)) {
611 for (
unsigned m = 0; m < this->
shape (i1); ++m) {
614 result.
data(
i) += (*this) (ix1) * t2 (ix2);
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
640 Tensor <T> result (0, ix);
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);
675 Tensor < T > result (0, ix);
678 while (ixr(0) < ix(0)) {
680 for (
unsigned m = 0; m < t1.
shape (i1); ++m) {
697 Tensor < T > result (0, ix);
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;
719 Tensor < T > result (0, ix);
721 while (ixr(0) < ix(0)) {
722 result (ixr) = (*this) (ixr.
slice (0, this->rank))
723 * t1 (ixr.
slice (this->rank, result.
rank));
735 Tensor < T > result (0, ix);
737 while (ixr(0) < ix(0)) {
738 result (ixr) = t0 (ixr.
slice (0, t0.
rank))
750 for (
unsigned long i = 0;
i < this->
noel;
i++)
753 Index ix (0, this->rank);
754 while (ix(0) < this->
shape(0)) {
755 (*this)(ix) += t(ix);
767 for (
unsigned long i = 0;
i < this->
noel;
i++)
770 Index ix (0, this->rank);
771 while (ix(0) < this->
shape(0)) {
772 (*this)(ix) -= t(ix);
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);
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);
820 for (
unsigned long i = 0;
i < this->
noel; ++
i)
828{
return t.
mult (m); }
long int Vector< T > & index
#define BCHK(cond, exc, txt, ind, rtval)
#define BCHKNR(cond, exc, txt, ind)
BVector< T > & append(const T &)
performs poorly
Note (KG, 981214): We don't handle rank == 0.
unsigned rank_size(void) const
CTensor< T > & operator=(const CTensor< T > &ct)
tbci_traits< T >::const_refval_type get_lin_idx(const unsigned long i) const
unsigned long calcsize(void)
bool operator==(const CTensor< T > &ct)
const T & getcref(vararg va,...) const
T & operator()(const Index &ix)
CTensor< T > & resize(const Index &ix)
T aligned_value_type TALIGN(MIN_ALIGN2)
CTensor< T > & fill(const T &value)
T & get_lin_idx(const unsigned long i)
unsigned long lin_size(void) const
CTensor< T > & resize(const T &val, const Index &ix)
T max(unsigned long &pos) const
unsigned long calc_offs(const Index &ix) const
T min(unsigned long &pos) const
CTensor< T > & transpose(const unsigned i, const unsigned j)
bool operator!=(const CTensor< T > &ct)
CTensor< T > & resize(const CTensor< T > &ct)
Index calc_indx(const unsigned long i) const
Index index_size(void) const
unsigned lin_read(vararg va,...)
Note that this pragma interface might create problems as the class index is not templated!
Index next_idx(const Index &max)
unsigned long size() const
TensErr(const char *t, const long i=0)
TensErr(const TensErr &te)
Tensor class including arithmetics.
Tensor< T > & operator-=(const Tensor< T > &)
T aligned_value_type TALIGN(MIN_ALIGN2)
Tensor< T > mult(const T) const
Tensor< T > cntrmul(const Tensor< T > &, const unsigned, const unsigned) const
Tensor< T > operator/(const T m) const
Tensor< T > operator-() const
friend Tensor< T > FRIEND_TBCI2__ metrmul FGD(const Tensor< T > &, const Tensor< T > &, const unsigned, const unsigned)
Tensor(const unsigned dim_rank)
Tensor< T > contract(const unsigned, const unsigned) const
friend Tensor< T > FRIEND_TBCI2__ dctmul FGD(const Tensor< T > &, const Tensor< T > &)
Tensor< T > drctmul(const Tensor< T > &) const
friend Tensor< T > FRIEND_TBCI2__ ctrmul FGD(const Tensor< T > &, const Tensor< T > &, const unsigned, const unsigned)
Tensor< T > & operator*=(const T m)
Tensor(const T &value, const Index &ix)
Tensor(const Tensor< T > &ct)
Tensor< T > operator*(const T m) const
Tensor< T > operator+(const Tensor< T > &) const
Tensor< T > & operator/=(const T m)
Tensor< T > & operator+=(const Tensor< T > &)
TVector< T > slice(unsigned long, unsigned long) const
Vector< T > & fill(const T &v)
TVector< unsigned > idx_fill_in1(const Index &idx, const unsigned where, const unsigned what)
TVector< unsigned > idx_fill_in2(const Index &idx, const unsigned where1, const unsigned what1, const unsigned where2, const unsigned what2)
TVector< unsigned > idx_remove2(const Index &idx, const unsigned which1, const unsigned which2)
TVector< unsigned > idx_set1(const Index &idx, const unsigned which, const unsigned what)
const unsigned TMatrix< T > * res
Tensor< T > dctmul(const Tensor< T > &t0, const Tensor< T > &t1)
STD__ istream & operator>>(STD__ istream &is, CTensor< T > &ct)
Tensor< T > metrmul(const Tensor< T > &metr, const Tensor< T > &t, const unsigned i1, const unsigned i2)
STD__ ostream & operator<<(STD__ ostream &os, const CTensor< T > &ct)
Tensor< T > ctrmul(const Tensor< T > &t1, const Tensor< T > &t2, const unsigned i1, const unsigned i2)