Message Boards Message Boards

GROUPS:

Remove the negative sign in front of a term while using FullSimplify?

Posted 3 years ago
2148 Views
|
4 Replies
|
1 Total Likes
|

Hi all, when using 'FullSimplify' for fractions it often happens that Mathematica puts a negative sign '-' in front of the whole fraction. Is there any possibility to supress this and tell the Program to put the negative sign in the numerator? Example: - \frac{a-b}{c} is returned as frac{-a + b}{c}

I know, its just cosmetics. Many thanks, Sebastian

POSTED BY: Seb Wi
Answer
4 Replies

Hi Sebastian,

I think it will be slightly tricky to work in the general case. The reason why it is returned like it does is the following:

-((a-b)/c);
%//FullForm
FullSimplify[%]//FullForm

Times[-1,Plus[a,Times[-1,b]],Power[c,-1]]
Times[Plus[Times[-1,a],b],Power[c,-1]]

As you can see, the second one is 'simpler' as the expression has less 'components'.

You can do the following: make your own ComplexityFunction that gives a large penalty for such an expression. Note that the default complexity function is:

SimplifyCount[p_] :=
 Which[Head[p] === Symbol, 1,
  IntegerQ[p], 
  If[p == 0, 1, Floor[N[Log[2, Abs[p]]/Log[2, 10]]] + If[p > 0, 1, 2]],
  Head[p] === Rational, 
  SimplifyCount[Numerator[p]] + SimplifyCount[Denominator[p]] + 1,
  Head[p] === Complex, 
  SimplifyCount[Re[p]] + SimplifyCount[Im[p]] + 1, NumberQ[p], 2,
  True, SimplifyCount[Head[p]] + 
   If[Length[p] == 0, 0, Plus @@ (SimplifyCount /@ (List @@ p))]]

Another option is to make your own simplification function:

ClearAll[MyFullSimplify]
MyFullSimplify[Times[a_?Negative,x__]]:=Times[a,FullSimplify[Times[x]]]
MyFullSimplify[x_]:=FullSimplify[x]

MyFullSimplify[-4 (-1+x)]
FullSimplify[-4 (-1+x)]

Which leaves the negative untouched:

-4 (-1+x)
4-4 x

It's a bit of a chore to get Simplify to do this so I usually go for precise expression surgery to fix these minus sign problems, which are fairly common. In this case just use Minus on two parts of the expression.

(-a + b)/c
MapAt[Minus, %, {{1}, {2, 1}}]

giving

(-a + b)/c
-((a - b)/c)

The main problem is that you may have to "fish" for the positions. With some experience with Mathematica it gets easier to know what they are. Another drawback is that in a long expression you might inadvertently change the value of the expression.

Posted 1 month ago

ClearAll[MyFullSimplify] MyFullSimplify MyFullSimplify

MyFullSimplify[-4 (-1+x)] FullSimplify[-4 (-1+x)]

https://crbtech.in/100-percent-job-guarantee/mechanical-electrical-civil-design-courses

This is a way:

(-a + b)/c /. (pl_Plus /; MatchQ[ToBoxes[pl],
     RowBox[{RowBox[{"-", _}], ___}]]) :>
  -DisplayForm@RowBox[{"(", -pl, ")"}]

if you can live with unnecessary parentheses.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract