MathGroup Archive 2010

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

Search the Archive

Re: Collecting Positive and Negative Terms

  • To: mathgroup at
  • Subject: [mg112335] Re: Collecting Positive and Negative Terms
  • From: Albert Retey <awnl at>
  • Date: Thu, 9 Sep 2010 05:34:24 -0400 (EDT)
  • References: <i6a5ep$kjl$>


> myExpression = -2 a b^5 c^3 - 5 a b^4 c^4 - 2 a b^3 c^5 - ...

> expr2 = DeleteCases[myExpression, Times[_Integer?Negative, ___]];
> Length[expr2]
> 185
> expr3 = Cases[
>    myExpression, _?Positive | _Symbol | Times[_?Positive, ___]];
> Length[expr3]
> 183
> expr2 - Total[expr3]
> a b^3 c^4 Subscript[k, 2] \[Gamma]+a b c^4 Subscript[k, 2] \[Alpha]^2

I don't know what the purpose of this mail was, but since the suggestion
was originally by me I couldn't resist to answer:

1) if it is meant as a warning that when dealing with expression in such
a "heuristically" way, you better check that the result is what you
expect, then I of course agree and I just can repeat that you absolutely
need to check that the expressions you are dealing with are correctly
handled with the pattern you come up with.

2) if it is meant as a proof that the pattern matching approach is
probably not very robust, I also agree and think I have tried to
indicate that in my original post.

3) if it is meant as a question how to make the approach work in this
case, here is one possibility. As stated in the original post, I'm
confident that it is easy to construct cases where this again will fail.
So whoever uses this approach better checks and maybe adopts to his/her
relevant cases.

expr2 = DeleteCases[myExpression, Times[_?Negative, ___]];

expr3 = Cases[
  myExpression, _?Positive | _Symbol |
   Times[__?(FreeQ[#, _?Negative, {1}] &)]];

expr2 - Total[expr3]

=> 0

I have only tested with a subset of the terms Bob sent, since some of
them I couldn't copy and paste to Mathematica, but it handles the two
cases that the old pattern didn't correctly. It should be also mentioned
that due to the Orderless attribute of Times, some extra care needs to
be taken when constructing patterns containing Times...



  • Prev by Date: Re: FindRoots?
  • Next by Date: Re: FindRoots?
  • Previous by thread: Re: Collecting Positive and Negative Terms
  • Next by thread: drawing polygon diagonals