Re: Interesting failure of Collect
- To: mathgroup at smc.vnet.net
- Subject: [mg61333] Re: [mg61307] Interesting failure of Collect
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sun, 16 Oct 2005 00:17:49 -0400 (EDT)
- References: <200510150222.WAA17294@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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
- References:
- Interesting failure of Collect
- From: "Blimbaum, Jerry CIV NSWC PC" <jerry.blimbaum@navy.mil>
- Interesting failure of Collect