#12290: Fix the hash of matrix spaces and improve its performance
------------------------------+---------------------------------------------
Reporter: SimonKing | Owner: jason, was
Type: defect | Status: needs_review
Priority: critical | Milestone: sage-5.0
Component: linear algebra | Keywords: hash matrix space unique parent
Work_issues: | Upstream: N/A
Reviewer: | Author: Simon King
Merged: | Dependencies:
------------------------------+---------------------------------------------
Changes (by SimonKing):
* keywords: hash matrix space => hash matrix space unique parent
* status: needs_work => needs_review
Old description:
> 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:
> {{{
> sage: M = MatrixSpace(ZZ, 10)
> sage: N = MatrixSpace(ZZ, 10,sparse=True)
> sage: N
> Full MatrixSpace of 10 by 10 sparse matrices over Integer Ring
> sage: M
> Full MatrixSpace of 10 by 10 dense matrices over Integer Ring
> sage: M == N
> True
> sage: hash(M)==hash(N)
> False
> }}}
>
> That has to be fixed. Moreover, the hash of matrix spaces is rather
> sluggish and should thus be improved speed-wise:
> {{{
> sage: %timeit hash(M)
> 625 loops, best of 3: 13.8 µs per loop
> }}}
>
> The root of both evils is the generic `__hash__` method inherited from
> `SageObject`:
> {{{
> def __hash__(self):
> return hash(self.__repr__())
> }}}
New description:
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:
{{{
sage: M = MatrixSpace(ZZ, 10)
sage: N = MatrixSpace(ZZ, 10,sparse=True)
sage: N
Full MatrixSpace of 10 by 10 sparse matrices over Integer Ring
sage: M
Full MatrixSpace of 10 by 10 dense matrices over Integer Ring
sage: M == N
True
sage: hash(M)==hash(N)
False
}}}
That has to be fixed. Moreover, the hash of matrix spaces is rather
sluggish and should thus be improved speed-wise:
{{{
sage: %timeit hash(M)
625 loops, best of 3: 13.8 µs per loop
}}}
The root of both evils is the generic `__hash__` method inherited from
`SageObject`:
{{{
def __hash__(self):
return hash(self.__repr__())
}}}
__Apply__
[attachment:trac12290_unique_matrix_space.patch]
--
Comment:
I have attached a patch that follows a totally different approach: Use
`UniqueRepresentation` as a base class for matrix spaces!
Advantage: One gets `__hash__`, `__cmp__` and `__reduce__` for free, and
the hash is even faster than with my previous patch.
{{{
sage: M = MatrixSpace(ZZ, 10)
sage: N = MatrixSpace(ZZ, 10,sparse=True)
sage: M == N
False
sage: timeit("hash(M)", number=10^6)
1000000 loops, best of 3: 511 ns per loop
}}}
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:
{{{
sage: M = MatrixSpace(ZZ, 10)
sage: N = MatrixSpace(ZZ, 10,sparse=True)
sage: a = M.random_element()
sage: b = N.random_element()
sage: (a+b).parent()
Full MatrixSpace of 10 by 10 dense matrices over Integer Ring
sage: (b+a).parent()
Full MatrixSpace of 10 by 10 sparse matrices over Integer Ring
}}}
The parent of the sum depends on the order of summands!!
But with the new patch, one has
{{{
sage: M = MatrixSpace(ZZ, 10)
sage: N = MatrixSpace(ZZ, 10,sparse=True)
sage: a = M.random_element()
sage: b = N.random_element()
sage: (a+b).parent()
Full MatrixSpace of 10 by 10 dense matrices over Integer Ring
sage: (b+a).parent()
Full MatrixSpace of 10 by 10 dense matrices over Integer Ring
}}}
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
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/12290#comment:5>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en.