From 874c2d5f83700e67b58638aa2e415e43318fe555 Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Fri, 24 Oct 2025 11:24:12 +0200 Subject: [PATCH] tensor-fourindex_dense conversion for nosymmetry --- fourindex.h | 56 +++++++++++++++++++++++++++-------------------------- t.cc | 15 +++++++++++++- tensor.h | 20 +++++++++++++++++++ 3 files changed, 63 insertions(+), 28 deletions(-) diff --git a/fourindex.h b/fourindex.h index 3b64d8a..4aa8531 100644 --- a/fourindex.h +++ b/fourindex.h @@ -1142,34 +1142,36 @@ return NRSMat::v[SMat_index(I,J)]; template class fourindex_dense : public NRMat { protected: - unsigned int nn; + unsigned int nnbas; friend class explicit_t2; public: - fourindex_dense(): NRMat() {nn=0;}; - void resize(const int nnn) {nn=nnn; (*this).NRMat::resize(nn*nn,nn*nn);}; - explicit fourindex_dense(const int nnn): NRMat(nnn*nnn,nnn*nnn) {nn=nnn;}; + unsigned int nbas() const {return nnbas;}; + fourindex_dense(): NRMat() {nnbas=0;}; + void resize(const int nnn) {nnbas=nnn; (*this).NRMat::resize(nnbas*nnbas,nnbas*nnbas);}; + explicit fourindex_dense(const int nnn): NRMat(nnn*nnn,nnn*nnn) {nnbas=nnn;}; + explicit fourindex_dense(const int nnn, const NRMat &mat): NRMat(mat) {nnbas=nnn;}; inline T& operator() (unsigned int i, unsigned int j, unsigned int a, unsigned int b) { #ifdef DEBUG -if(i<1||i>nn ||j<1||j>nn|| a<1||a>nn||b<1||b>nn) laerror("nosymmetry fourindex out of range"); +if(i<1||i>nnbas ||j<1||j>nnbas|| a<1||a>nnbas||b<1||b>nnbas) laerror("nosymmetry fourindex out of range"); if (!NRMat::v) laerror("access to unallocated fourindex_dense"); #endif -return (*this).NRMat::operator() ((j-1)*nn+i-1,(b-1)*nn+a-1); +return (*this).NRMat::operator() ((j-1)*nnbas+i-1,(b-1)*nnbas+a-1); } inline const T& operator() (unsigned int i, unsigned int j, unsigned int a, unsigned int b) const { #ifdef DEBUG -if(i<1||i>nn ||j<1||j>nn|| a<1||a>nn||b<1||b>nn) laerror("nosymmetry fourindex out of range"); +if(i<1||i>nnbas ||j<1||j>nnbas|| a<1||a>nnbas||b<1||b>nnbas) laerror("nosymmetry fourindex out of range"); if (!NRMat::v) laerror("access to unallocated fourindex_dense"); #endif -return (*this).NRMat::operator() ((j-1)*nn+i-1,(b-1)*nn+a-1); +return (*this).NRMat::operator() ((j-1)*nnbas+i-1,(b-1)*nnbas+a-1); } void print(std::ostream &out) const { unsigned int i,j,a,b; - for(i=1; i<=nn; ++i) for(j=1; j<=nn; ++j) for(a=1; a<=nn; ++a) for(b=1; b<=nn; ++b) out << i<<" "<::operator() ((j-1)*noca+i-1,(b-1)*nvra+a-1); template class fourindex_dense : public NRSMat { protected: - unsigned int nbas; + unsigned int nnbas; friend class explicit_t2; public: - fourindex_dense(): NRSMat() {nbas=0;}; - void resize(const int n) {nbas=n; (*this).NRSMat::resize(nbas*nbas);}; - explicit fourindex_dense(const int n): NRSMat(n*n) {nbas=n;}; + fourindex_dense(): NRSMat() {nnbas=0;}; + void resize(const int n) {nnbas=n; (*this).NRSMat::resize(nnbas*nnbas);}; + explicit fourindex_dense(const int n): NRSMat(n*n) {nnbas=n;}; //here i,a are alpha j,b beta inline T& operator() (unsigned int i, unsigned int j, unsigned int a, unsigned int b) { #ifdef DEBUG -if(i<1||i>nbas ||j<1||j>nbas|| a<1||a>nbas||b<1||b>nbas) laerror("antisymtwoelectronrealdiracAB fourindex out of range"); +if(i<1||i>nnbas ||j<1||j>nnbas|| a<1||a>nnbas||b<1||b>nnbas) laerror("antisymtwoelectronrealdiracAB fourindex out of range"); if (!NRSMat::v) laerror("access to unallocated fourindex_dense"); #endif -return (*this).NRSMat::operator() ((j-1)*nbas+i-1,(b-1)*nbas+a-1); +return (*this).NRSMat::operator() ((j-1)*nnbas+i-1,(b-1)*nnbas+a-1); } inline const T& operator() (unsigned int i, unsigned int j, unsigned int a, unsigned int b) const { #ifdef DEBUG -if(i<1||i>nbas ||j<1||j>nbas|| a<1||a>nbas||b<1||b>nbas) laerror("antisymtwoelectronrealdiracAB fourindex out of range"); +if(i<1||i>nnbas ||j<1||j>nnbas|| a<1||a>nnbas||b<1||b>nnbas) laerror("antisymtwoelectronrealdiracAB fourindex out of range"); if (!NRSMat::v) laerror("access to unallocated fourindex_dense"); #endif -return (*this).NRSMat::operator() ((j-1)*nbas+i-1,(b-1)*nbas+a-1); +return (*this).NRSMat::operator() ((j-1)*nnbas+i-1,(b-1)*nnbas+a-1); } void print(std::ostream &out) const { unsigned int i,j,a,b; - for(i=1; i<=nbas; ++i) for(j=1; j<=nbas; ++j) - for(a=1; a<=i; ++a) for(b=1; b<= (a class fourindex_dense : public NRSMat { private: - int nbas; + int nnbas; public: fourindex_dense(): NRSMat() {}; - explicit fourindex_dense(const int n): nbas(n), NRSMat(n*(n-1)/2) {}; - fourindex_dense(const T &a, const int n): nbas(n), NRSMat(a,n*(n-1)/2) {}; - fourindex_dense(const T *a, const int n): nbas(n), NRSMat(a,n*(n-1)/2) {}; + explicit fourindex_dense(const int n): nnbas(n), NRSMat(n*(n-1)/2) {}; + fourindex_dense(const T &a, const int n): nnbas(n), NRSMat(a,n*(n-1)/2) {}; + fourindex_dense(const T *a, const int n): nnbas(n), NRSMat(a,n*(n-1)/2) {}; //and also construct it from sparse and externally stored fourindex classes //it seems not possible to nest template just for the two constructors fourindex_dense(const fourindex &rhs); @@ -1332,11 +1334,11 @@ public: void add(unsigned int i, unsigned int j, unsigned int k, unsigned int l, T elem); void add_unique(unsigned int i, unsigned int j, unsigned int k, unsigned int l, T elem); const T operator() (unsigned int i, unsigned int j, unsigned int k, unsigned int l) const; - void resize(const int n) {nbas=n; (*this).NRSMat::resize(n*(n-1)/2);}; + void resize(const int n) {nnbas=n; (*this).NRSMat::resize(n*(n-1)/2);}; void print(std::ostream &out) const { unsigned int i,j,k,l; - for(i=1; i<=nbas; ++i) + for(i=1; i<=nnbas; ++i) for(k=1;k::v[SMat_index(I,J)] += elem; template -fourindex_dense::fourindex_dense(const fourindex &rhs) : nbas(rhs.size()), NRSMat((T)0,rhs.size()*(rhs.size()-1)/2) +fourindex_dense::fourindex_dense(const fourindex &rhs) : nnbas(rhs.size()), NRSMat((T)0,rhs.size()*(rhs.size()-1)/2) { if(rhs.getsymmetry() != twoelectronrealmullikan ) laerror("fourindex_dense symmetry mismatch"); typename fourindex_ext::piterator p; //we have to run over equivalents in non-canonical order to build the antisymmetrization properly; it could be done less elegantly but more efficiently moving the if's to outer parts of the piterator loop, if needed @@ -1429,7 +1431,7 @@ for(p= const_cast *>(&rhs)->pbegin(); p.notend(); ++p) template -fourindex_dense::fourindex_dense(const fourindex_ext &rhs) : nbas(rhs.size()), NRSMat((T)0,rhs.size()*(rhs.size()-1)/2) +fourindex_dense::fourindex_dense(const fourindex_ext &rhs) : nnbas(rhs.size()), NRSMat((T)0,rhs.size()*(rhs.size()-1)/2) { if(rhs.getsymmetry() != twoelectronrealmullikan ) laerror("fourindex_dense symmetry mismatch"); typename fourindex_ext::piterator p; //we have to run over equivalents in non-canonical order to build the antisymmetrization properly; it could be done less elegantly but more efficiently moving the if's to outer parts of the piterator loop, if needed diff --git a/t.cc b/t.cc index f28f4df..f5674ea 100644 --- a/t.cc +++ b/t.cc @@ -3675,7 +3675,7 @@ x0.split_index_group(0); cout <<"Error = "<<(x0-y).norm()<(range,NRMat(t.data,range*range,range*range)); }