Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Interesting failure of Collect

  • To: mathgroup at
  • Subject: [mg61344] Re: [mg61307] Interesting failure of Collect
  • From: Andrzej Kozlowski <akoz at>
  • Date: Sun, 16 Oct 2005 00:17:59 -0400 (EDT)
  • References: <> <>
  • Sender: owner-wri-mathgroup at

On 15 Oct 2005, at 15:34, Andrzej Kozlowski wrote:

> On 15 Oct 2005, at 11:22, Blimbaum, Jerry CIV NSWC PC wrote:
>> Given the expr =  Sin[p]/(4 Pi a) + Sin[p]/(4 Pi b)
>> and then applying the command
>> Collect[expr, Sin[p]/(4 Pi)] works as it should, however,
>> expr2 = Sin[p]/(4 Pi a) - Sin[p]/(4 Pi b)
>> leaves the expression untouched.   (I realize I could use Simplify  
>> but
>> this problem occurred on a much longer expression and this is  
>> intended
>> just to convey the basic idea)....for longer expressions the Collect
>> process works for all quantities with a + sign but the one with a  
>> minus
>> sign will not be collected.....strikes me as a "bug".....
>> thanks....jerry blimbaum
> This looks like the standard problem with Mathematica FullForm of  
> expressions, or more precisely with the way rational numbers are  
> represented. To see what I mean just compare:
> FullForm[a-3 b/4]
> Plus[a,Times[Rational[-3,4],b]]
> FullForm[a+3 b/4]
> Plus[a,Times[Rational[3,4],b]]
> You see that one involves Rational[3,4] and the other Rational 
> [-3,4] so a pattern involving 3/4 (Rational[3,4]) will only match  
> the first. So in your case you could do something like this:
> Collect[expr /. Rational[p_,x_] :> f[p]*Rational[1,x], Sin[p]/ 
> (4*Pi)] /. f -> Identity
> ((1/b + 1/a)*Sin[p])/(4*Pi)
> Collect[expr2 /.Rational[p_, x_] :>f[p]*Rational[1, x],Sin[p]/ 
> (4*Pi)] /. f -> Identity
> ((1/a - 1/b)*Sin[p])/(4*Pi)
> I should add that it is not entirely clear if Collect simply relies  
> on pattern matching but it seems to suffer from the same problems  
> as pattern matching. One imagines that by using something like the  
> above workaround it should be possible to make Collect more robust,  
> so I tend to agree with you that this may be called a "bug" - in  
> quotation marks of course ;-) .
> Andrzej Kozlowski
> Tokyo, Japan

On further reflection I wish to withdraw my last remark. I do not  
think this is a "bug", even if the quotation marks are meant to  
suggest poor design or judgement rather than a programming error. The  
point is that, as the documentation tells you:

Collect[expr, x] collects together terms involving the same powers of  
object matching x.

Obviously the key word here is "Powers of x". This means that Collect  
is only going to work in general for certain types of x. To  
illustrate what I mean consider the expression:

p = 1 + y*a + y*b + y^2*c + y^2*d;

we can collect with respect to y:

Collect[p, y]

(c + d)*y^2 + (a + b)*y + 1

O.K., now let's substitute 2 x for y:

q = p /. y -> 2*x

4*c*x^2 + 4*d*x^2 + 2*a*x + 2*b*x + 1

Now, even though this is essentially the same polynomial Collecting  
with respect to 2x will not work as before:

Collect[q, 2*x]

4*c*x^2 + 4*d*x^2 + 2*(a + b)*x + 1

Collect[q, x]

(4*c + 4*d)*x^2 + (2*a + 2*b)*x + 1

The point I am making is that Collect[expr, Sin[p]/(4 Pi)] is only  
going to work  in the relatively unimportant  linear expressions in    
Sin[p]/(4 Pi), while Collect is actually intended for situations  
involving higher powers. And of course if you collect only with  
respect to Sin[p] rather than Sin[p]/(4*Pi)  then your problem does  
not arise:

Collect[Sin[p]/(4*Pi*a) - Sin[p]/(4*Pi*b), Sin[p]]

(1/(4*a*Pi) - 1/(4*b*Pi))*Sin[p]

Collect[Sin[p]/(4*Pi*a) + Sin[p]/(4*Pi*b), Sin[p]]

(1/(4*b*Pi) + 1/(4*Pi*a))*Sin[p]

Another illustration of this point: note that an extension of your  
example no longer works properly in this non-linear case:

Collect[Sin[p]/(4*Pi*a) + (Sin[p]/(4*Pi*a))^2 +
    Sin[p]/(4*Pi*b) + (Sin[p]/(4*Pi*b))^2, Sin[p]/(4*Pi)]

Sin[p]^2/(16*a^2*Pi^2) + Sin[p]^2/(16*b^2*Pi^2) +
   ((1/b + 1/a)*Sin[p])/(4*Pi)

Although of course this works:

Collect[Sin[p]/(4*Pi*a) + (Sin[p]/(4*Pi*a))^2 +
    Sin[p]/(4*Pi*b) + (Sin[p]/(4*Pi*b))^2, Sin[p]]

(1/(16*b^2*Pi^2) + 1/(16*Pi^2*a^2))*Sin[p]^2 +
   (1/(4*b*Pi) + 1/(4*Pi*a))*Sin[p]

So no, I do not agree with you. Collect[..., Sin[p]/(4 Pi)] is not  
the intended usage of Collect, it works in a special case by accident  
rather than by design. Collecting linear expressions is not important  
enough to warrant any changes in Collect to make it work as you think  
it should. Making it work with non linear expressions would require a  
completely different approach to pattern matching. So I conclude  
that  neither a bug nor even a "bug" is involved here.

Andrzej Kozlowski
Tokyo, Japan

  • Prev by Date: Re: Re: How to reverse sign on the y's in a list of (x, y)'s?
  • Next by Date: Getting a pure text widget?
  • Previous by thread: Re: Interesting failure of Collect
  • Next by thread: Re: Interesting failure of Collect