Ask Your Question
0

subsets of Set of unhashable items

asked 2022-05-01 19:35:40 +0200

Max Alekseyev gravatar image

updated 2022-05-01 19:37:07 +0200

Sage allows me to create a set:

S = Set( [factor(10), factor(15)] )

However, both S.subsets() and Subsets(S) produce:

TypeError: <class 'sage.structure.factorization_integer.IntegerFactorization'> is not hashable

What's wrong?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-05-02 11:16:31 +0200

tmonteil gravatar image

updated 2022-05-02 11:38:17 +0200

The problem comes from the fat that a Factorization object is not hashable:

sage: f = factor(15)
sage: f
3 * 5
sage: hash(f)
TypeError: <class 'sage.structure.factorization_integer.IntegerFactorization'> is not hashable

It should be (i will open a ticket for that).

You can workaround by replacing the factorization with a hashable equivalent : a tuple:

sage: t = tuple(f)
sage: t
((3, 1), (5, 1))
sage: hash(t)
8332132266671271694

You will be able to recover the factorization as follows:

sage: Factorization(t)
3 * 5
sage: Factorization(t) == f
True

So you can do something like:

sage: S = Set([tuple(factor(10)), tuple(factor(15))])
sage: S
{((2, 1), (5, 1)), ((3, 1), (5, 1))}

sage: len(S.subsets())
4
sage: S.subsets()[3]
{((2, 1), (5, 1)), ((3, 1), (5, 1))}

sage: S.subsets()[3][0]
((2, 1), (5, 1))
sage: Factorization(S.subsets()[3][0])
2 * 5

EDIT Be careful that the sign is lost when taking the tuple:

sage: a = -5
sage: a.factor()
-1 * 5
sage: tuple(a.factor())
((5, 1),)

EDIT Looking at the code, it is currently not possible to define a hash for Factorizaion objects since they are mutable (see, e.g. the simplify method that works in place).

edit flag offensive delete link more

Comments

I don't not worry much about Factorization here, but about behavior of Set in case of unhashable objects in general. On the other hand, it'd be nice to have Factorization hashable - please let me know the ticket number when you open it.

Max Alekseyev gravatar imageMax Alekseyev ( 2022-05-02 17:53:28 +0200 )edit

The documentation for Setsays "Sets with unhashable objects work, but with less functionality". This is presumably part of that loss of functionality. https://doc.sagemath.org/html/en/refe...

John Palmieri gravatar imageJohn Palmieri ( 2022-05-03 00:09:15 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-05-01 19:35:40 +0200

Seen: 131 times

Last updated: May 02 '22