From: Michael Knudsen on
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

From: David Park on
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(a)comcast.net
http://home.comcast.net/~djmpark/


From: Michael Knudsen [mailto:micknudsen(a)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



From: TJR on
On 11 Aug., 10:44, Michael Knudsen <micknud...(a)gmail.com> wrote:
> 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

It's a hard problem in general, but I suppose you know a lot about
stuff that occurs in your specific application. So use pattern
matching.

separatePositiveNegative::usage= "separatePositiveNegative[hugesum]
expands a hugesum, turns it into a list of summands, and then subsums
those summands deemend positive / negative by myPositiveQ. Returns
{...positive parital sum ..., ... negative partial sum...}.";

Clear[separatePositiveNegative];
separatePositiveNegative[hugesum_] := Module[{sowReapKey, f},
f[sowReapKey[_], list_] := Plus @@ list;

hugesum //
Expand //
If[Head[#] == Plus, List @@ #, {#}] & //
Reap[Map[
If[myPositiveQ[#], Sow[#, sowReapKey["+"]];,
Sow[#, sowReapKey["-"]];] &,
#], sowReapKey[_], f][[2]] &
];

myPositiveQ::usage="myPositiveQ[expr_] tests if expr is positive or
negative. Always returns True/False, but not always correctly. The
algorithm relies on pattern matching. expr is broken up recursively,
and the smallest bits are guessed positive/negative. Modify the
definition of myPositiveQHelper to tweak the patterns.";
Clear[myPositiveQ, myPositiveQHelper];
myPositiveQ[
expr_] := (expr //
myPositiveQHelper) /. {myPositiveQHelper -> ((Print[
"deemed positive by myPositiveQHelper: ", #]; 1) &)} //
Positive;
myPositiveQHelper[Times[exp1_, exp2__]] :=
Times @@ Map[myPositiveQHelper, {exp1, exp2}];
myPositiveQHelper[Power[base_, exponent_/;!OddQ[exponent]]] = 1;(*no
complex numbers*)
myPositiveQHelper[Power[base_, exponent_/;OddQ[exponent]]] :=
myPositiveQHelper[base];(*no complex numbers*)

myPositiveQHelper[exp_Symbol] = 1;
(*override as needed, e.g. myPositiveQHelper[alwaysNegative]=-1; *)

myPositiveQHelper[exp_ /; NumericQ[exp]] := Sign[exp];

From: Albert Retey on
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?

If your expr is a sum of products (which is what Expand usually
creates...), the following should work:

Cases[expr, Times[_?Positive, ___]]

Cases[expr, Times[_?Positive, ___]]

for an expression with more diversity of terms you might need to adopt
the pattern or maybe even the approach. If the above isn't good enough
for your case, it would be a good idea to post an example so people know
what you are working with...

hth,

albert



From: Michael Knudsen on
On Aug 12, 11:29 am, Albert Retey <a...(a)gmx-topmail.de> wrote:

> If your expr is a sum of products (which is what Expand usually
> creates...), the following should work:
>
> Cases[expr, Times[_?Positive, ___]]

Thanks for trying to provide a really simple solution. I tired it out
on one of my rather complicated expressions, but the output didn't
look correct. I therefore tried a trivial example,

myExpression = a + b - c;
Cases[myExpression, Times[_?Positive, ___]]

but the output is just an empty list

{}

What I was hoping for was

a + b

I'll go through some of the more complicated suggestions today and see
how things pan out.

--
Michael Knudsen