# Kernel dies when trying to compute coefficients in a linear sum involving expressions like b[1,2][3,4]

The following happens on 11.0.1.0: Have

fun=a[1][5,1]b[1,2][1,1]+a[1][4,1]b[1,2][1,2]+a[1][5,2]b[1,2][2,1]
+a[1][4,2]b[1,2][2,2]+a[1][5,3]b[1,2][3,1]+a[1][4,3]b[1,2][3,2]
+a[1][5,4]b[1,2][4,1]-a[2][1,2]b[1,2][4,1]+a[1][4,4]b[1,2][4,2]
-a[2][2,2]b[1,2][4,2]+a[1][5,5]b[1,2][5,1]-a[2][1,1]b[1,2][5,1]
+a[1][4,5]b[1,2][5,2]-a[2][2,1]b[1,2][5,2]+a[1][5,6]b[1,2][6,1]
+a[1][4,6]b[1,2][6,2]+a[1][5,7]b[1,2][7,1]+a[1][4,7]b[1,2][7,2]
+a[1][5,8]b[1,2][8,1]+a[1][4,8]b[1,2][8,2]+a[1][3,1]b[1,3][1,1]
-a[3][1,3]b[1,3][1,1]+a[1][2,1]b[1,3][1,2]-a[3][2,3]b[1,3][1,2]
+a[1][1,1]b[1,3][1,3]-a[3][3,3]b[1,3][1,3]+a[1][3,2]b[1,3][2,1]
-a[3][1,2]b[1,3][2,1]+a[1][2,2]b[1,3][2,2]-a[3][2,2]b[1,3][2,2]
+a[1][1,2]b[1,3][2,3]-a[3][3,2]b[1,3][2,3]+a[1][3,3]b[1,3][3,1]
-a[3][1,1]b[1,3][3,1]+a[1][2,3]b[1,3][3,2]-a[3][2,1]b[1,3][3,2]
+a[1][1,3]b[1,3][3,3]-a[3][3,1]b[1,3][3,3]+a[1][3,4]b[1,3][4,1]
+a[1][2,4]b[1,3][4,2]+a[1][1,4]b[1,3][4,3]+a[1][3,5]b[1,3][5,1]
+a[1][2,5]b[1,3][5,2]+a[1][1,5]b[1,3][5,3]+a[1][3,6]b[1,3][6,1]
+a[1][2,6]b[1,3][6,2]+a[1][1,6]b[1,3][6,3]+a[1][3,7]b[1,3][7,1]
+a[1][2,7]b[1,3][7,2]+a[1][1,7]b[1,3][7,3]+a[1][3,8]b[1,3][8,1]
+a[1][2,8]b[1,3][8,2]+a[1][1,8]b[1,3][8,3]


Trying

vars = Select[Variables[fun], Head[Head[#]] == b &];
Union[Flatten[CoefficientList[fun, vars]]]


kills the kernel.

If I take only few first summands, the result is computed fairly quickly. When gradually adding summands, it works more and more slowly and seems to need more and more memory. It dies when about 3/4 of the summands are added.

Why? How to deal with such cases? Of course I can redenote my variables but I would prefer to work with them the way they are here.

• The crash is a task for Wolfram Support, not for us. The best you can expect regarding the crash from us is to check if it is reproducible in all platforms and versions. Looking for a more efficient way to deal with your problem in Wolfram Language is a valid question. So, I would first report the crash to Wolfram support, then edit your question saying you reported the crash but still are looking for alternative efficient approaches. Sep 17, 2021 at 8:12
• @rhermans I added information about the version. As for the rest, - there is a bug tag, I am just hesitant to add it since it may turn out it is not a bug and I am doing something wrong. When I ask "Why?", I mean precisely that, I ask for expert opinion whether the reason is a bug or my error or something else. Sep 17, 2021 at 8:17
• A crash is never a feature, always a bug, and therefore necessarily a job for Wolfram Support. You should not use the bug tag until is confirmed by the community. Sep 17, 2021 at 10:38
• It could be that it ran out of memory. Probably should abort with a mesage in that case. Will investigate. Sep 17, 2021 at 22:06
• It seems to be crashing in the Flatten step. Been difficult to track to its lair but I'm cautiously ptimistic. Sep 19, 2021 at 16:29

Your vars is a list of 40 element, so, given your expression involves quadratic powers of some variables, CoefficientList appears to generate 40-folded list with too many elements (5566277615616, to be precise). So you run out of memory. Meanwhile, most of those elements are zeros (except 36 elements). Use CoefficientRules instead. In particular,

Last/@CoefficientRules[fun, vars]


will have the same effect as your (not working) line

Union[Flatten[CoefficientList[fun, vars]]]


PS: here is how I got the number 5566277615616:

Times @@ (1 + Max /@ Transpose[First /@ CoefficientRules[fun, vars]])


PPS: The bottom line is when you think of using CoeffficientList command, consider CoeffficientRules as a much better alternative. I myself mostly use CoeffficientList only for one-variable case.

It seems that CoefficientList becomes enormous in this case. I managed to obtain what I want using Table[Coefficient[fun,v],{v,vars}]