tensor: operator== and hash

This commit is contained in:
jiri committed 2026-11-26 20:46:53 +01:00
1 parent ed96821e0a
commit e91d4d0036
4 files changed
+28 -3

No files matched your search

+2 -1
View File
@@ -351,4 +351,5 @@ std::unordered_map<NRVec<int>,double> a;
std::unordered_map<NRPerm<int>,double,LA::Hash> b;
\end{lstlisting}
A key stored in such a container shares its data with the object it was copied from, which therefore must not be
modified afterwards without \code{copyonwrite()}. \code{Tensor} has no \code{operator==} and cannot be a key.
modified afterwards without \code{copyonwrite()}. \code{std::hash} is specialized also for \code{Tensor} (by its
\code{hash()} without the names of the indices).
+1
View File
@@ -319,6 +319,7 @@ indexing, which is the same as the matrix convention above for \code{x} built by
\begin{lareference}
\lafunc{void put(int fd, bool with_names=false) const; void get(int fd, bool with_names=false)}{binary I/O of shape, auxiliary arrays, data and optionally names (section~\ref{sec:concepts:io}); with \code{with_names} the names are always written (an empty list if the tensor has none), so \code{put} and \code{get} must use the same \code{with_names}.}
\lafunc{uint64\_t hash(uint64\_t h=LA\_HASH\_INIT, bool with\_names=false) const; const typename LA\_traits<T>::scalartype checksum(const uint64\_t seed=0) const}{Hash of the exact contents and numerical checksum of the data (section~\ref{sec:concepts:io}). The shape (number of indices, symmetry, offset and range of every index group) enters both, so that tensors with the same data and different shapes differ; the names of the indices enter the hash only with \code{with\_names} and never the checksum. \code{uint64\_t shapehash(uint64\_t h) const} is the part which depends on the shape.}
\lafunc{bool operator==(const Tensor \&rhs) const; bool operator!=(const Tensor \&rhs) const}{Equality: the same shape, exactly the same data (bit by bit, as for vectors) and the same names of the indices; a named tensor is not equal to an unnamed one with the same data. This is an equivalence relation, so that tensors can be the keys of \code{std::unordered\_map} and \code{std::unordered\_set} (\code{std::hash} is specialized for \code{Tensor}). It differs from the test of compatibility in the arithmetic operators, which compares the names only if both tensors are named. Shapes and names are compared field by field and as strings (not by \code{memcmp}, which was wrong for names with undefined characters after the terminating zero).}
\lafunc{template<typename T> std::ostream & operator<<(std::ostream &s, const Tensor<T> &x)}{text output: the shape, the names, then one line per stored element with its flat indices and value.}
\lafunc{template<typename T> std::istream & operator>>(std::istream &s, Tensor<T> &x)}{reads the format written by \code{<<}.}
\lafunc{std::ostream & operator<<(std::ostream &s, const INDEXGROUP &x); std::istream & operator>>(std::istream &s, INDEXGROUP &x)}{\code{number symmetry offset range}.}
+12
View File
@@ -4940,6 +4940,18 @@ t3.data=t.data;
cout <<"tensors: equal "<<(t.hash()==t2.hash())<<", other symmetry of the same data "<<(t.hash()==t3.hash())<<", difference of their checksums "<<t.checksum()-t3.checksum()<<endl;
t2.defaultnames();
cout <<"named tensor: hash without names equal "<<(t.hash()==t2.hash())<<", with names "<<(t.hash(LA_HASH_INIT,true)==t2.hash(LA_HASH_INIT,true))<<endl;
//equality of tensors: shape, data and names; tensors as keys of a hash table
Tensor<double> t4(t), t5(t2);
cout <<"tensor equal to its copy "<<(t==t4)<<", to the named copy "<<(t==t2)<<", named copies equal "<<(t2==t5)<<", other shape "<<(t==t3)<<endl;
t5.defaultnames("j");
t4.data.copyonwrite();
t4.data[0]+=1e-14;
cout <<"other names "<<(t2==t5)<<", data changed by 1e-14 "<<(t==t4)<<endl;
std::unordered_map<Tensor<double>,int> ttable;
ttable[t]=1;
ttable[t2]=2;
ttable[t4]=3;
cout <<"hash table with tensors as keys: size "<<ttable.size()<<", values "<<ttable[t]<<" "<<ttable[t2]<<" "<<ttable[t4]<<endl;
//fourindex_dense has both from the matrix it is derived from
fourindex_dense<twoelectronrealmullikan,double,int> f(n);
f.randomize(1.);
+13 -2
View File
@@ -136,7 +136,7 @@ class LA_traits<INDEXNAME> {
static bool is_plaindata() {return true;};
static void copyonwrite(INDEXNAME& x) {};
typedef INDEXNAME normtype;
static inline int gencmp(const INDEXNAME *a, const INDEXNAME *b, int n) {return memcmp(a,b,n*sizeof(INDEXNAME));};
static inline int gencmp(const INDEXNAME *a, const INDEXNAME *b, int n) {for(int i=0; i<n; ++i) if(a[i]!=b[i]) return 1; return 0;}; //not memcmp: the characters after the terminating zero are undefined
static inline void put(int fd, const INDEXNAME &x, bool dimensions=1) {if(sizeof(INDEXNAME)!=write(fd,&x,sizeof(INDEXNAME))) laerror("write error 1 in INDEXNAME put"); }
static inline void multiput(int nn, int fd, const INDEXNAME *x, bool dimensions=1) {if(nn*sizeof(INDEXNAME)!=write(fd,x,nn*sizeof(INDEXNAME))) laerror("write error 1 in INDEXNAME multiiput"); }
static inline void get(int fd, INDEXNAME &x, bool dimensions=1) {if(sizeof(INDEXNAME)!=read(fd,&x,sizeof(INDEXNAME))) laerror("read error 1 in INDEXNAME get");}
@@ -183,7 +183,7 @@ class LA_traits<INDEXGROUP> {
static bool is_plaindata() {return true;};
static void copyonwrite(INDEXGROUP& x) {};
typedef INDEXGROUP normtype;
static inline int gencmp(const INDEXGROUP *a, const INDEXGROUP *b, int n) {return memcmp(a,b,n*sizeof(INDEXGROUP));};
static inline int gencmp(const INDEXGROUP *a, const INDEXGROUP *b, int n) {for(int i=0; i<n; ++i) if(a[i]!=b[i]) return 1; return 0;}; //not memcmp: the struct can contain padding (LA_TENSOR_INDEXPOSITION)
static inline void put(int fd, const INDEXGROUP &x, bool dimensions=1) {if(sizeof(INDEXGROUP)!=write(fd,&x,sizeof(INDEXGROUP))) laerror("write error 1 in INDEXGROUP put"); }
static inline void multiput(int nn, int fd, const INDEXGROUP *x, bool dimensions=1) {if(nn*sizeof(INDEXGROUP)!=write(fd,x,nn*sizeof(INDEXGROUP))) laerror("write error 1 in INDEXGROUP multiiput"); }
static inline void get(int fd, INDEXGROUP &x, bool dimensions=1) {if(sizeof(INDEXGROUP)!=read(fd,&x,sizeof(INDEXGROUP))) laerror("read error 1 in INDEXGROUP get");}
@@ -379,6 +379,12 @@ public:
void put(int fd, bool with_names=false) const;
void get(int fd, bool with_names=false);
//equality: the same shape, exactly the same data and the same names of the indices (a named tensor is not equal to
//an unnamed one); it is an equivalence relation, as needed for the keys of hash tables, unlike the test of compatibility
//in the arithmetic operators, where the names are compared only if both tensors are named
bool operator==(const Tensor &rhs) const {return shape==rhs.shape && names==rhs.names && data==rhs.data;};
bool operator!=(const Tensor &rhs) const {return !(*this==rhs);};
//the shape mixed into a hash or into the seed of a checksum
uint64_t shapehash(uint64_t h) const
{
@@ -757,4 +763,9 @@ std::istream & operator>>(std::istream &s, Tensor<T> &x);
}//namespace
//hash() (without the names of the indices) as the hash function of the standard library, see the note in vec.h
namespace std {
template<typename T> struct hash<LA::Tensor<T> > {size_t operator()(const LA::Tensor<T> &x) const {return x.hash();}};
}
#endif