one element is missing while using Set( generator ) [closed]
In the code:
import itertools
Q = [[factor(5),factor(13)],[factor(9),factor(7),factor(13)]]
print( sorted( Set( [lcm(*qq) for qq in itertools.product(*Q)] ) ) )
print( sorted( Set( (lcm(*qq) for qq in itertools.product(*Q)) ) ) )
first print
produces the expected result:
[13, 5 * 7, 3^2 * 5, 5 * 13, 7 * 13, 3^2 * 13]
while second print
produces a list with one element (3^2 * 5
) missing:
[13, 5 * 7, 5 * 13, 7 * 13, 3^2 * 13]
Is this a bug?
ADDED. More weirdness - the following code
Q = [[factor(3), factor(9)], [factor(9), factor(7)]]
print( Set( [lcm(*qq) for qq in itertools.product(*Q)] ) )
produces a set with two equal elements:
Set of elements of [3^2, 3 * 7, 3^2, 3^2 * 7]
It seems that these issues are related to the fact that Factorization
objects are not hashable, but why then does Set
allow to create sets of them with all kinds of side effects?
and Set is not working correctly with iterators
I can't reproduce the first issue: I get the same answer — the first one — when I repeat the
print
command. What version of Sage are you using? I agree with the final question:Set
allows nonhashable objects, and it doesn't look very good, for exampleSet([[3,4], [3,4]])
.I use Sage 9.5 The first issue can be also seen in SageMathCell: https://sagecell.sagemath.org/?q=ainesk
Maybe it's been fixed in the 9.6 prerelease.
I can reproduce with 9.6.rc3 on macOS 12.3.1