Re: Collecting Positive and Negative Terms
- To: mathgroup at smc.vnet.net
- Subject: [mg111737] Re: Collecting Positive and Negative Terms
- From: "David Park" <djmpark at comcast.net>
- Date: Thu, 12 Aug 2010 05:26:36 -0400 (EDT)
Here is an attempt. It is a little difficult to know if all the relevant forms have been caught, and how complex values should be handled. Here I try to separate by the manifest appearance of the sign. manifestNegativePositive[complexvars : ({_Symbol ...}) : {}][ expr : (_Plus | _Complex)] := Module[{re, im, reneg, repos, imneg, impos}, re = ComplexExpand[Re[expr], complexvars]; im = ComplexExpand[Im[expr], complexvars]; reneg = Plus @@ Cases[ If[Head[re] =!= Plus, {re}, re], (_?Negative | HoldPattern[Times[_?Negative, __]])]; repos = re - reneg; imneg = Plus @@ Cases[ If[Head[im] =!= Plus, {im}, im], (_?Negative | HoldPattern[Times[_?Negative, __]])]; impos = im - imneg; {reneg + I imneg, repos + I impos} ] Some tests: 5/3 + a - b + 3 c - 4 d // manifestNegativePositive[] 5/3 + a - b + 3 c - 4 d // manifestNegativePositive[{a, b, c, d}] 5/3 (2 - 3 I) + a - b + 3 c - 4 d // manifestNegativePositive[] (a - b) (c + d) - e // manifestNegativePositive[] 1/(a - I b) + 3 f[-x] // manifestNegativePositive[] 3.5 - 2 I // manifestNegativePositive[] David Park djmpark at comcast.net http://home.comcast.net/~djmpark/ From: Michael Knudsen [mailto:micknudsen at gmail.com] Hi, I'm currently doing calculations often involving very large expressions with a lot of unknown parameters. To investigate whether an expression is positive or negative, I usually expand it using the Expand command and manually copy and paste the positive and negative terms into new expressions and simplify them using FullSimplify. In my case it often turns out that comparing the two is much easier than looking at the entire expression as a whole. However, it's very cumbersome to do all this manually. Is there an easy way in Mathematica to pick out terms begging with + (or -) in a sum? Thanks, Michael Knudsen