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}.}