Coefficient is Mathematica 10.0.x seems to be affected by a bug. While in 10.3.1 the following

 Coefficient[Expand[ApartSquareFree[x/(y1 - x), x]*
 ApartSquareFree[x/(y2 + x), x]], y1 y2]


1/((x-y1) (x+y2))

as it should, in 10.0.2 I get just 0.

Is there a good workaround for this problem, if my code needs to run also on older Mathematica versions? Are there other functions that use Coefficient internally and would be affected by this?

  • $\begingroup$ actually, the 10.3.1 result might be a bug.. $\endgroup$ – george2079 Dec 29 '15 at 16:48
  • $\begingroup$ hmm, may be you are right. Versions 8 and 9 also return 0. I contacted WRI (CASE:3500508), so I'm waiting for a statement from them. $\endgroup$ – vsht Dec 29 '15 at 21:02
  • $\begingroup$ According to WRI, this is in some way undefined behavior, as my original expression is not quite a polynomial, so that it's difficult to say what would be the "correct" answer. Still, in their opinion, the behavior of Mathematica 10.3.1 is more correct than that of version 8 and 9. $\endgroup$ – vsht Jan 3 '16 at 18:33
  • $\begingroup$ can we see their full response? $\endgroup$ – george2079 Jan 4 '16 at 12:41
  • $\begingroup$ I don't think that it is OK to post private communication without the consent of all the participants. The case number is given above, so you could contact Kyle Martin from WRI and ask him to repeat his arguments, if you're interested. $\endgroup$ – vsht Jan 5 '16 at 17:02

10.1 gives zero as well. This is a workaround (in the sense of giving the answer you seem to expect) - I don't know how general but it works for this example.

expr=Expand[ApartSquareFree[x/(y1 - x), x]*ApartSquareFree[x/(y2 + x), x]]

-1 - y1/(x - y1) + y2/(x + y2) + (y1 y2)/((x - y1) (x + y2))

Coefficient[expr /. y1 y2 -> y1y2prod , y1y2prod ]

1/((x - y1) (x + y2))

My feeling, this is not a bug because your input is not a polynomial in y1,y2, and so the docs don't say what to expect. A message to that effect would be preferable to returning zero however. Note that CoefficientList[expr,{y1,y2}] appropriately gives the error expr is not a polynomial.

Edit - this may be a more robust way to extract terms in the given form of the expression based on their numerator:

Select[ List @@ expr ,
     CoefficientList[ Numerator[#] , {y1, y2}]  ,
        {{0, 0}, {0, _}}] &]/(y1 y2) // Total

1/((x - y1) (x + y2))

Another path is to substitute all the symbols as they appear in the term numerators:

 Total[(Numerator[#] /. { y1 -> \[FormalY]1 , y2 -> \[FormalY]2})/
      Denominator[#] & /@ List @@ expr ]

-1 - [FormalY]1/(x - y1) + [FormalY]2/( x + y2) + ([FormalY]1 [FormalY]2)/((x - y1) (x + y2))

This is now a polynomial in the \[FormalY] so you can use Coefficient and CoefficientList directly.

  • $\begingroup$ I think that CoefficientList is indeed the way to go. At least it would generate an error message if the underlying expression is not a polynomial, so that one catch it in time and take appropriate measures. $\endgroup$ – vsht Jan 3 '16 at 18:35

I think there is a slight change in the evaluation of Coefficient[] and the other functions do not have any discrepancy in the two versions. However, it is to be noted that the answer $0$ is correct as well. I guess the difference in the two versions is that the expression is simplified in Mathematica 10.0.2. I think you should use Simplify[] before taking the coefficient.

In both the versions:

Expand[ApartSquareFree[x/(y1 - x), x]*ApartSquareFree[x/(y2 + x), x]]


$-1 - \frac{y1}{(x - y1)} + \frac{y2}{(x + y2)} + \frac{y1 y2}{(x - y1)(x + y2)}$

If you use Simplify the expression becomes: $-\frac{x^2}{(x - y1) (x + y2)}$

The coefficient of $y1y2$ is $0$ in this case.

Taking Simplify[] may be computationally time taking if the expressions are huge.


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.