Post date: Feb 12, 2016 6:30:46 AM
Using the hash function which I found here
https://wjngkoh.wordpress.com/2015/03/04/c-hash-function-for-eigen-matrix-and-vector/
// Hash function for Eigen matrix and vector.
// The code is from `hash_combine` function of the Boost library. See
// http://www.boost.org/doc/libs/1_55_0/doc/html/hash/reference.html#boost.hash_combine .
template<typename T>
struct matrix_hash : std::unary_function<T, size_t> {
std::size_t operator()(T const& matrix) const {
// Note that it is oblivious to the storage order of Eigen matrix (column- or
// row-major). It will give you the same hash value for two different matrices if they
// are the transpose of each other in different storage order.
size_t seed = 0;
for (size_t i = 0; i < matrix.size(); ++i) {
auto elem = *(matrix.data() + i);
seed ^= std::hash<typename T::Scalar>()(elem)+0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
};
Then declared as
typedef unordered_map<VectorXd, VectorXd, matrix_hash<VectorXd>> Mymap;
and finally,
VectorXd X1(3), x1(2);
X1 << 1, 2, 3;
x1 << 2, 4;
VectorXd X2(3), x2(4);
X2 << 2, 3, 4;
x2 << 4, 5, 6, 7;
VectorXd X3(3), x3(3);
X3 << 3, 4, 5;
x3 << 5, 4, 3;
Mymap c1;
c1.insert(Mymap::value_type(X1, x1));
c1.insert(Mymap::value_type(X2, x2));
c1.insert(Mymap::value_type(X3, x3));
for (const auto& c : c1)
{
std::cout << "c.first\n"<< c.first << std::endl;
std::cout << "c.second\n" << c.second << std::endl;
std::cout << "- - - - - - - - - - -" << std::endl;
}