The following happens on Have



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.

  • 1
    $\begingroup$ 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. $\endgroup$
    – rhermans
    Sep 17, 2021 at 8:12
  • $\begingroup$ @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. $\endgroup$ Sep 17, 2021 at 8:17
  • 1
    $\begingroup$ 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. $\endgroup$
    – rhermans
    Sep 17, 2021 at 10:38
  • 2
    $\begingroup$ It could be that it ran out of memory. Probably should abort with a mesage in that case. Will investigate. $\endgroup$ Sep 17, 2021 at 22:06
  • 2
    $\begingroup$ It seems to be crashing in the Flatten step. Been difficult to track to its lair but I'm cautiously ptimistic. $\endgroup$ Sep 19, 2021 at 16:29

2 Answers 2


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}]


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.