Bug introduced in 7.0 or earlier and persisting through 11.1
It took me quite a lot of time to finally trace down to this strange output. I really don't know why.
First, I create a list
rot1 = RotationTransform[{{1, 0, 1}, {0, 0, -1}}, {0, 0, 0}];
tmp = rot1@Tuples[Range[0, 10, 1], 3];
Then run
grouptmp1 = GroupBy[tmp, N[Last[#], 8] &];
grouptmp2 = GroupBy[tmp, N[Last[#], 8] &];
grouptmp1 === grouptmp2
The first time you run the above code, it will give "False", the second time and after, it will give true. So strange, what is wrong here?
BTW, I use N[Last[#], 8] &
to add some tolerance to the GroupBy operation. If there is better method, welcome to leave a comment or write an answer
Updata*
New discovery!! GatherBy also suffers, Try
gathertmp1 = GatherBy[tmp, N[Last[#], 8] &];
gathertmp2 = GatherBy[tmp, N[Last[#], 8] &];
gathertmp1 === gathertmp2
Maybe all By
suffers
Update2
Temporary workaround I figured out at this moment:
test1 = KeySort@KeyMap[(#[[1]]*N[FromDigits[#[[2]]]]) &,
GroupBy[tmp, {Sign[Last@#], RealDigits[N@Last@#, 10, 8]} &]]
test2 = KeySort@KeyMap[(#[[1]]*N[FromDigits[#[[2]]]]) &,
GroupBy[tmp, {Sign[Last@#], RealDigits[N@Last@#, 10, 8]} &]]
test1 === test2
This gives true no matter how many times of evaluating
Update3
Simon Woods provide a simple example to reproduce the bug
ClearSystemCache[]; a = 3/Sqrt[2] + 2 Sqrt[2]; Table[ GroupBy[{a, a}, N[#, 3] &], {2}]
I tested it in mma version 7, since ver 7 doesn't have GroupBy
, I use GatherBy
instead, this is the result
{{{3/Sqrt[2] + 2 Sqrt[2]}, {3/Sqrt[2] + 2 Sqrt[2]}}, {{3/Sqrt[2] +
2 Sqrt[2], 3/Sqrt[2] + 2 Sqrt[2]}}}
So this bug lurking since version 7 !
Range[3, 6]
- makes it a lot easier to analyse. Investigating. $\endgroup$GatherBy
in version 9 (what I have access to at the moment) I notice that the problematic result is the first one, not the second one (the first has repeats that are numerically the same to 8 digits). That is to say, the operation appears to improve with use of cached numeric values. Needs further investigation though. $\endgroup$