Ticket #8506 (closed defect: fixed)

Opened 3 years ago

Last modified 3 years ago

Inefficient hash for homsets

Reported by: robertwb Owned by: cremona
Priority: major Milestone: sage-4.4
Component: elliptic curves Keywords:
Cc: Work issues:
Report Upstream: N/A Reviewers: John Cremona
Authors: Robert Bradshaw Merged in: sage-4.4.alpha0
Dependencies: Stopgaps:

Description

This ends up affecting, in particular, hashing of elliptic curves and their point sets.

Attachments

8506-homset-hashing.patch Download (4.0 KB) - added by robertwb 3 years ago.
8506-homset-hashing-take2.patch Download (5.0 KB) - added by robertwb 3 years ago.

Change History

comment:1 Changed 3 years ago by robertwb

As I side effect, I changed ainvs to be stored as a tuple, rather than being converted at each request.

Changed 3 years ago by robertwb

comment:2 Changed 3 years ago by robertwb

  • Status changed from new to needs_review

comment:3 Changed 3 years ago by hivert

Replying to robertwb:

Hi robert,

The problem is not only for homset ! See #8119

sage: h = Hom(ZZ, QQ)
sage: hash(h)
-8106914618552251573
sage: h.rename("toto")
sage: hash(h)
2314052222105390764

I don't know exactly what would be a generic solution of this problem. There is one if the parent inherits from UniqueRepresentation (see ##8120) instead of using one of the at least three other mechanisms I've seen troughout sage library. Not that as I said in that ticket, I'm not sure how robust my solution is I've a proposal for a better option. If you have any comment, please do not hesitate.

Cheers,

Florent

comment:4 Changed 3 years ago by robertwb

My original concern was that hashing the parents I was using was way to expensive. In particular, it was using the Parent as a key in the coercion model, and the dict lookup was many times more expensive than the actual arithmetic (or anything else I was doing in that function...) UniqueRepresentation is special, as one doesn't have to worry about hashes being unequal for equal objects.

comment:5 Changed 3 years ago by cremona

  • Status changed from needs_review to needs_work

I am not competent to comment on the hashing issues. But I applied the patch to 4.3.4.alpha1 and had the following test failures (I only tested sage/schemes/elliptic_curves, and without -long):

sage -t  sage/schemes/elliptic_curves/heegner.py
**********************************************************************
File "/storage/jec/sage-4.3.4.alpha1/devel/sage-8505/sage/schemes/elliptic_curves/heegner.py", line 2588:
    sage: hash(y)
Expected:
    -5236815264926108755       
Got:
    -756867903203770682
**********************************************************************
File "/storage/jec/sage-4.3.4.alpha1/devel/sage-8505/sage/schemes/elliptic_curves/heegner.py", line 2893:
    sage: hash(EllipticCurve('389a').heegner_point(-7,5))
Expected:
    -5236815264926108755             
Got:
    -756867903203770682
**********************************************************************
2 items had failures:
   1 of   4 in __main__.example_107
   1 of   3 in __main__.example_121
***Test Failed*** 2 failures.
For whitespace errors, see the file /home/jec/.sage//tmp/.doctest_heegner.py
	 [83.6 s]

comment:6 Changed 3 years ago by robertwb

Looks like this is once again a case where the alpha differs enough from the latest release to cause issues. The changes above look innocuous enough, I'll make a new patch.

Changed 3 years ago by robertwb

comment:7 Changed 3 years ago by robertwb

  • Status changed from needs_work to needs_review

OK, I ran all tests against the latest alpha, and those two were the only failures I got as well. I've posted a new patch fixing them. As for the hashing issues, I just changed the hash to not rely on the string representation (e.g. hashing an elliptic curve now hashes the basering and a-invariants).

comment:8 follow-up: ↓ 9 Changed 3 years ago by cremona

  • Reviewers set to John Cremona
  • Authors set to Robert Bradshaw

New patch applies to 4.3.5 OK. Now testing on both 32- and 64-bit...

comment:9 in reply to: ↑ 8 Changed 3 years ago by cremona

  • Status changed from needs_review to positive_review

Replying to cremona:

New patch applies to 4.3.5 OK. Now testing on both 32- and 64-bit...

All tests pass on both -- positive review!

comment:10 Changed 3 years ago by jhpalmieri

  • Status changed from positive_review to closed
  • Resolution set to fixed
  • Merged in set to sage-4.4.alpha0

Merged "8506-homset-hashing-take2.patch" in 4.4.alpha0.

Note: See TracTickets for help on using tickets.