working on hash(), also for tensor
This commit is contained in:
1 parent
8b2b413db7
commit
ed96821e0a
9 files changed
+126
-1
No files matched your search
+15
-1
@@ -306,7 +306,8 @@ void get(int fd, bool dimensions=1, bool transp=0, bool orca_format=false);
|
||||
For storing many objects of the same size in a scratch file, see \code{AuxStorage} (chapter~\ref{ch:utilities}).
|
||||
|
||||
\paragraph{Hash and checksum.} Two functions characterize the contents of an object by one number; they are members of
|
||||
\code{NRVec}, \code{NRMat} and \code{NRSMat} (and of the classes derived from them) and work for nested objects.
|
||||
\code{NRVec}, \code{NRMat} and \code{NRSMat}, of the classes derived from them (e.g.\ \code{fourindex\_dense}) and of
|
||||
\code{Tensor}, and they work for nested objects. The sparse classes do not have them.
|
||||
\begin{lstlisting}
|
||||
uint64_t hash(uint64_t h=LA_HASH_INIT) const;
|
||||
const typename LA_traits<T>::scalartype checksum(const uint64_t seed=0) const;
|
||||
@@ -338,3 +339,16 @@ the user can replace (section~\ref{sec:utilities:random}), and on the order of a
|
||||
frozen: their values are meant to be stored in files and will not change in later versions. The method
|
||||
\code{checksum} is formally a template, so that traits of user-defined element types need not define
|
||||
\code{scalartype} unless \code{checksum} is called for them.
|
||||
|
||||
\code{std::hash} is specialized for \code{NRVec}, \code{NRMat} and \code{NRSMat} (each in its header) by means
|
||||
of \code{hash()}, so that these objects (in practice with integer elements) can be the keys of
|
||||
\code{std::unordered\_map} and \code{std::unordered\_set}; the keys are compared by \code{operator==}. A specialization
|
||||
of \code{std::hash} does not apply to derived classes (\code{NRVec\_from1}, \code{NRVec\_colex}, \code{NRPerm},
|
||||
\code{Partition}, \code{fourindex\_dense}, \dots); for these, and for any class with the method \code{hash()}, the
|
||||
hash function \code{LA::Hash} (\code{miscfunc.h}) is given as the third template argument:
|
||||
\begin{lstlisting}
|
||||
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.
|
||||
@@ -318,6 +318,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{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}.}
|
||||
|
||||
@@ -169,6 +169,7 @@ iteration on the polynomial $s(d,n)$ followed by an integer correction); 0 for $
|
||||
\lafunc{static inline uint64\_t hash\_mix(const uint64\_t counter, const uint64\_t seed)}{Pseudorandom 64-bit number as a fixed function of its arguments (splitmix64); a generator without a state, independent of \code{la\_random.h}. It generates the weights of \code{checksum} and combines the dimensions into \code{hash} (section~\ref{sec:concepts:io}); it must never be changed.}
|
||||
\lafunc{static inline double checksum\_weight(const uint64\_t z)}{The number \code{z} mapped to $[-1,1)$.}
|
||||
\lafunc{uint64\_t hash\_bytes(const void *x, const size\_t nbytes, uint64\_t h=LA\_HASH\_INIT)}{Hash of \code{nbytes} bytes of plain data, taken by words of 8 bytes, continuing from \code{h}; used by \code{hash} of the classes for scalar element types. Frozen as well.}
|
||||
\lafunc{struct Hash \{template<typename X> size\_t operator()(const X \&x) const;\}}{Hash function for the containers of the standard library which calls \code{x.hash()}; for the classes without an own specialization of \code{std::hash} (section~\ref{sec:concepts:io}).}
|
||||
\end{lareference}
|
||||
|
||||
%=====================================================================
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
#ifndef _LA_MAT_H_
|
||||
#define _LA_MAT_H_
|
||||
#include "la_traits.h"
|
||||
#include <functional>
|
||||
|
||||
using namespace LA_Vecmat3;
|
||||
|
||||
@@ -1829,5 +1830,10 @@ NRVECMAT_OPER2(Mat, +)
|
||||
NRVECMAT_OPER2(Mat, -)
|
||||
|
||||
}//end of the LA-namespace
|
||||
|
||||
//hash() as the hash function of the standard library, see the note in vec.h
|
||||
namespace std {
|
||||
template<typename T> struct hash<LA::NRMat<T> > {size_t operator()(const LA::NRMat<T> &x) const {return x.hash();}};
|
||||
}
|
||||
#endif/* _LA_MAT_H_ */
|
||||
|
||||
@@ -76,6 +76,14 @@ return (double)(int64_t)z*(1./9223372036854775808.);
|
||||
//hash of plain data continuing from the value h; the data are taken by words of 8 bytes for speed
|
||||
extern uint64_t hash_bytes(const void *x, const size_t nbytes, uint64_t h=LA_HASH_INIT);
|
||||
|
||||
//hash function for the containers of the standard library for any class which has the method hash(),
|
||||
//e.g. std::unordered_map<NRPerm<int>,double,LA::Hash>; std::hash itself is specialized only for NRVec, NRMat and NRSMat,
|
||||
//and a specialization does not apply to the classes derived from them
|
||||
struct Hash
|
||||
{
|
||||
template<typename X> size_t operator()(const X &x) const {return x.hash();}
|
||||
};
|
||||
|
||||
|
||||
|
||||
}//namespace
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#ifndef _LA_SMAT_H_
|
||||
#define _LA_SMAT_H_
|
||||
#include "la_traits.h"
|
||||
#include <functional>
|
||||
|
||||
namespace LA {
|
||||
#define NN2 ((size_t)nn*(nn+1)/2)
|
||||
@@ -1579,4 +1580,9 @@ public:
|
||||
};
|
||||
|
||||
}//namespace
|
||||
|
||||
//hash() as the hash function of the standard library, see the note in vec.h
|
||||
namespace std {
|
||||
template<typename T> struct hash<LA::NRSMat<T> > {size_t operator()(const LA::NRSMat<T> &x) const {return x.hash();}};
|
||||
}
|
||||
#endif /* _LA_SMAT_H_ */
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "graph.h"
|
||||
#include "numbers.h"
|
||||
#include "miscfunc.h"
|
||||
#include <unordered_map>
|
||||
|
||||
|
||||
using namespace std;
|
||||
@@ -4922,6 +4923,53 @@ s.randomize(1.);
|
||||
NRVec<int> iv(n);
|
||||
for(int i=0; i<n; ++i) iv[i]=i;
|
||||
cout <<"symmetric matrix: hash "<<s.hash()<<" checksum "<<s.checksum()<<"; hash of an integer vector chained with it "<<iv.hash(s.hash())<<endl;
|
||||
//tensor: the shape enters, the names only on request
|
||||
INDEXGROUP g;
|
||||
g.number=3;
|
||||
g.symmetry= -1;
|
||||
#ifndef LA_TENSOR_ZERO_OFFSET
|
||||
g.offset=0;
|
||||
#endif
|
||||
g.range=n;
|
||||
Tensor<double> t(g);
|
||||
t.randomize(1.);
|
||||
Tensor<double> t2(t);
|
||||
g.symmetry=1;
|
||||
Tensor<double> t3(g);
|
||||
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;
|
||||
//fourindex_dense has both from the matrix it is derived from
|
||||
fourindex_dense<twoelectronrealmullikan,double,int> f(n);
|
||||
f.randomize(1.);
|
||||
cout <<"fourindex_dense: hash "<<f.hash()<<" checksum "<<f.checksum()<<endl;
|
||||
//vectors as keys of a hash table
|
||||
std::unordered_map<NRVec<int>,int> table;
|
||||
for(int i=0; i<5; ++i)
|
||||
{
|
||||
NRVec<int> key(3);
|
||||
key[0]=i;
|
||||
key[1]=i*i;
|
||||
key[2]= -i;
|
||||
table[key]=10*i;
|
||||
}
|
||||
NRVec<int> key(3);
|
||||
key[0]=3;
|
||||
key[1]=9;
|
||||
key[2]= -3;
|
||||
cout <<"hash table with vectors as keys: size "<<table.size()<<", value found for the key (3,9,-3) = "<<table[key]<<", number of entries with another key "<<table.count(iv)<<endl;
|
||||
//for a derived class the hash function has to be given
|
||||
std::unordered_map<NRPerm<int>,int,LA::Hash> ptable;
|
||||
NRPerm<int> perm(4);
|
||||
perm.identity();
|
||||
for(int i=0; i<24; ++i)
|
||||
{
|
||||
ptable[perm]=i;
|
||||
perm.next();
|
||||
}
|
||||
perm.identity();
|
||||
cout <<"hash table with permutations as keys: size "<<ptable.size()<<", value found for the identity = "<<ptable[perm]<<endl;
|
||||
}
|
||||
|
||||
if(0) //testbed of the general methods
|
||||
|
||||
@@ -379,6 +379,38 @@ public:
|
||||
void put(int fd, bool with_names=false) const;
|
||||
void get(int fd, bool with_names=false);
|
||||
|
||||
//the shape mixed into a hash or into the seed of a checksum
|
||||
uint64_t shapehash(uint64_t h) const
|
||||
{
|
||||
h=hash_mix(shape.size(),h);
|
||||
for(int i=0; i<shape.size(); ++i)
|
||||
{
|
||||
h=hash_mix(shape[i].number,h);
|
||||
h=hash_mix(shape[i].symmetry,h);
|
||||
h=hash_mix(shape[i].offset,h);
|
||||
h=hash_mix(shape[i].range,h);
|
||||
#ifdef LA_TENSOR_INDEXPOSITION
|
||||
h=hash_mix(shape[i].upperindex,h);
|
||||
#endif
|
||||
}
|
||||
return h;
|
||||
}
|
||||
//hash of the shape and of the exact binary contents of the data, continuing from h (see miscfunc.h);
|
||||
//the names of the indices enter only if with_names
|
||||
uint64_t hash(uint64_t h=LA_HASH_INIT, bool with_names=false) const
|
||||
{
|
||||
h=data.hash(shapehash(h));
|
||||
if(with_names)
|
||||
{
|
||||
h=hash_mix(names.size(),h);
|
||||
for(int i=0; i<names.size(); ++i) h=hash_bytes(names[i].name,strnlen(names[i].name,N_INDEXNAME),h);
|
||||
}
|
||||
return h;
|
||||
}
|
||||
//numerical checksum of the data with pseudorandom weights given by the seed and the shape (see miscfunc.h)
|
||||
template<typename U=T>
|
||||
const typename LA_traits<U>::scalartype checksum(const uint64_t seed=0) const {return data.checksum(shapehash(seed));}
|
||||
|
||||
void enforce_hermiticity(); //zero out real/imag parts for repeated indices appropriately
|
||||
bool fulfills_hermiticity() const; //check it is so
|
||||
inline void randomize(const typename LA_traits<T>::normtype &x) {data.randomize(x); enforce_hermiticity();};
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "miscfunc.h"
|
||||
#include "vecmat3.h"
|
||||
#include <list>
|
||||
#include <functional>
|
||||
|
||||
using namespace LA_Vecmat3;
|
||||
|
||||
@@ -2994,4 +2995,12 @@ void NRVec<T>::storesubvector_to(const NRVec<int> &selection, NRVec &rhs) const
|
||||
|
||||
}//namespace
|
||||
|
||||
//hash() as the hash function of the standard library, so that vectors (best of integer types) can be keys of
|
||||
//std::unordered_map and std::unordered_set; the keys are compared by operator==
|
||||
//a specialization is not inherited: for a class derived from NRVec give LA::Hash (miscfunc.h) as the hash function
|
||||
//NOTE: a key shares its data with the object it was copied from, which then must not be changed without copyonwrite()
|
||||
namespace std {
|
||||
template<typename T> struct hash<LA::NRVec<T> > {size_t operator()(const LA::NRVec<T> &x) const {return x.hash();}};
|
||||
}
|
||||
|
||||
#endif /* _LA_VEC_H_ */
|
||||
Reference in new issue
Block a user