3
$\begingroup$

Mathematica often writes simplied expressions with -1 first followed by any variables (eg. -1+x). However I want to write it as -(1-x) i.e. retaining 1-x form everywhere. The reason is two fold: first is of course beautification. Secondly sometimes when multiplying a number of expressions containing some parts in the numerator and denominator differ by overall negative sign do not simplify unless I use FullSimplify. Now sometimes using FullSimplify at a later stage of simplification could be considerably inefficient in terms of performance.

Some naive custom simplification rules to cure this problem could be dangerous as the following toy example:

t1=(-((-1+u) /(1+u (-1+v))))^(-(1/2))//PowerExpand

enter image description here

Now this is not nice looking. A naive delayed substitution could be fatal.

t2 = (( t1 /. Plus[-1,x_] :> (1-x)*$g ) //PowerExpand)/. {$g->-1}

enter image description here

This is of course wrong.

How should one safely do such simplification to force (1-x) form correctly?

Edit: The desired form of the input (restricting in a unit hypercube) will look like following:

 t0=(-((-1+u) /(1+u (-1+v))))^(-(1/2))

enter image description here

 t3=Assuming[{0<=u<=1,0<=v<=1},t0 /.  Power[x_,y_] :> Simplify[Expand[x]]^y /. {Plus[-1,x_] :> (1-x)*$g} /. {$g->-1}]//PowerExpand

enter image description here

Note that t0 and t1 differ by PowerExpand. So the results differ if I first do Simplification within brackets and then PowerExpand or first do PowerExpand and then do simplification.

$\endgroup$
17
  • $\begingroup$ Use TraditionalForm for display: (t1 = (-((-1 + u)/(1 + u (-1 + v))))^(-(1/2)) // PowerExpand) // TraditionalForm $\endgroup$
    – Bob Hanlon
    Feb 20 '21 at 19:53
  • $\begingroup$ Sorry I corrected a mistake in the question x-1 is not I am after rather (1-x) form. $\endgroup$
    – Boogeyman
    Feb 20 '21 at 19:55
  • $\begingroup$ Did you evaluate the code that I posted? $\endgroup$
    – Bob Hanlon
    Feb 20 '21 at 19:56
  • 1
    $\begingroup$ I'm confused—the second image, which you get from naive delayed replacement, is algebraically correct for real numbers. I think the problem for complex numbers only comes with having to choose a branch for Sqrt, which must be done manually anyway. It also only uses the form 1-x. Is it nonetheless still problematic somehow? $\endgroup$
    – thorimur
    Feb 21 '21 at 1:10
  • 1
    $\begingroup$ This seems to be an issue with PowerExpand being, well...questionably implemented! The "Possible issues" section of PowerExpand notes that it will sometimes do weird things under automatic assumptions. If you know that u,v > 0, you can do PowerExpand[t0, Assumptions -> {u > 0, v > 0}] and get the right answer. You can also do PowerExpand[t0, Assumptions -> {}] for maximum safety and generality, but you get an ugly sign factor in front. In general idk...but either way PowerExpand seems responsible for your woes here! I wonder if Simplify by itself could be made to work... $\endgroup$
    – thorimur
    Feb 21 '21 at 7:11
1
$\begingroup$

This question is beyond the expression that OP uses to illustrate the problem. Namely, indeed, sometimes the internal order used by Mathematica differs from the wish of the user to present an expression.

As much as I know from experience, this typically happens in two main cases.

  1. The main reason is to represent the result in an easily readable form after the calculations have been finished. In this form, the expression is less prone to errors when rewriting it into another document.

  2. As a second reason, one may need to make terms easily visible before applying operations requiring assumptions such as neglecting some terms in comparison to other ones or expanding parts of the expression in series and alike.

In the first case, the approach of @ Bob Hanlon may be effectively applied.

Here I would like, however, to share a function that makes the job without transforming the expression to TraditionalForm. The advantage is that if you do it in the middle point of your transformations, you can continue the calculations in the StandardForm.

The function factorMinus[expr, fun] takes minus out of the expression. Arguments:

expr is the expression in which one needs to take minus out.

fun is the optional function applied to the expression left after it has been multiplied by -1. By default it is Identity.

factorMinus[expr_, fun_ : Identity] := (-1)*HoldForm[Evaluate[fun[(-1)*expr]]] 

Basic example:

factorMinus[-1 + x]

enter image description here

In more complex cases, one has to map this function onto the parts of the expression where one wants to take minus out.

Do not forget that prior to making further calculations with Mathematica one should apply ReleaseHold to the resulting expression.

Have fun!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.