Type dolfin.Vector unhashable

Issue #853 new
Miguel Rodriguez created an issue

Hi All,

I recently installed fenics 2017.1.0.dev0 (with python 3.5.2), as well as CBC-Block (used 2to3 tool for conversion), and found that applying the boundary conditions to a block_mat object failed because an object of type dolfin.Vector is not hashable. Thus, this Vector object cannot be added to the set of objects in object_pool since it must be hashable. On the other hand, dolfin.Matrix does not have this issue. Here is an example with minimal code:

import dolfin as dlf
A = dlf.Matrix()
b = dlf.Vector()
hash(A) # Works fine
hash(b) # Raises exception

I tried the above code with dolfin 2016.1.0 and 2016.2.0.dev0 (both with python 2.7.12), and this issue is not present.

I believe python 3 requires a __hash__ function when __eq__ is overwritten, which is the case for GenericVector.

Comments (6)

  1. Jan Blechta

    How should a hash be computed? (I can see that changing numerical values of a matrix does not influence its hash.) Maybe we can just hash Variable::id()?

  2. Miguel Rodriguez reporter

    The GenericVector class in the swig-generated file ./site-packages/dolfin/cpp/la.py contains the __eq__ without a __hash__ function, but the GenericMatrix and Matrix classes do not. I looked at the cpp header files, but I can't tell what is causing __eq__ to be generated since there is no operator== defined (and I am also not too familiar with swig).

    As for the hash not being influenced by the numerical values of a matrix; isn't the point of hash to "tag" a particular value even if they are different objects? I realize dolfin.Matrix and dolfin.Vector objects are really proxies of swig objects, and thus their "value" might mean something different here.

  3. Log in to comment