- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix the hash of matrix spaces and improve its performance #12290
Comments
Make the hashes of two equal matrix spaces equal. Improve the performance |
Author: Simon King |
comment:1
Attachment: trac12290_matrixspace_hash.patch.gz With my patch, the
Now about speed. If one computes the hash value over and over again by def __hash__(self):
return hash((self.base(),self.__nrows, self.__ncols)) then the timing is
If the hash value is stored in a single underscore attribute, such as def __hash__(self):
try:
return self._hash
except AttributeError:
self._hash = h = hash((self.base(),self.__nrows, self.__ncols))
return h then one gets
With a double-underscore
With directly accessing the def __hash__(self):
try:
return self.__dict__['_hash']
except KeyError:
self.__dict__['_hash'] = h = hash((self.base(),self.__nrows, self.__ncols))
return h one has
and with the patch, one has
How is that possible? Apparently a "try-except" block has some overhead. Hence, simply returning a lazy attribute (which is solution of my patch) is fastest. Note that it is not possible to use Needs review (although I still need to run full doctests)! |
comment:2
make ptest results in one error:
Quite an interesting error, I think! |
comment:3
Interesting indeed. Without the patch, one has
Obvious reason: M1 and M2 are equal (so then the length of D should be one, not two!), but they have different hash and are thus in different buckets of the dictionary. With my patch, they have the same hash, and by consequence they yield the same dictionary item - and that is bad for coercion! Hence, non-unique parents strike again... |
comment:4
The problem could be fixed by turning matrix spaces into unique parents (which would be the straight-forward thing to do). I just asked sage-devel for permission to make matrix spaces unique. Adding a dense and a sparse matrix would not be a problem for the coercion model. |
Attachment: trac12290_unique_matrix_space.patch.gz Use |
comment:5
I have attached a patch that follows a totally different approach: Use Advantage: One gets
The price to pay (as one can see in the example): The spaces of dense versus sparse matrices are not considered equal anymore. For applications, this shouldn't matter, since the coercion model can easily deal with it. In fact, I like the new behaviour a lot better than the old behaviour! Old:
The parent of the sum depends on the order of summands!! But with the new patch, one has
independent of the summation order. I had to change some existing doctests in a trivial way, and then the whole test suite passes. Ready for review! Apply trac12290_unique_matrix_space.patch |
Changed keywords from hash matrix space to hash matrix space unique parent |
This comment has been minimized.
This comment has been minimized.
Dependencies: #11900 |
comment:6
The new patch has a dependency: #11900, which has positive review and is already in sage-5.0.prealpha0 |
comment:7
Note that inspite of the red blob, the patchbot finds that all tests pass with the the latest prerelease. So, review anyone? |
comment:8
Hi Simon, This looks good, and I have doctests running on it as I write; but can you tell us what's going on with the new doctest at line 334 of David |
comment:9
I see: the old copy method returned a copy whose base ring was None (!). Anyway, this looks like good stuff, and patchbot seems happy with it on two different v5.0 builds, so let's get it in. |
Reviewer: David Loeffler |
Merged: sage-5.0.beta9 |
The central assumptions for any hash function is: If two objects are equal then they must have the same hash. That assumption is violated for matrix spaces:
That has to be fixed. Moreover, the hash of matrix spaces is rather sluggish and should thus be improved speed-wise:
The root of both evils is the generic
__hash__
method inherited fromSageObject
:Apply
attachment: trac12290_unique_matrix_space.patch
Depends on #11900
Component: linear algebra
Keywords: hash matrix space unique parent
Author: Simon King
Reviewer: David Loeffler
Merged: sage-5.0.beta9
Issue created by migration from https://trac.sagemath.org/ticket/12290
The text was updated successfully, but these errors were encountered: