5
$\begingroup$

In this question I will talk about a toy example, it should be clear what do I actually mean in general. Also, not only Factor but also Apart and several other important operators have this behavior.

Say, Factor[1/(1 - t)] produces -(1/(-1 + t)) which is not just formatting but full form change: FullForm[1/(1 - t)] is

Power[Plus[1,Times[-1,t]],-1]

while FullForm[Factor[1/(1 - t)]] gives

Times[-1,Power[Plus[-1,t],-1]]

which I find sort of inconvenient sometimes. Not that I care about internals of representing objects, most probably it is optimized in some way, but when I need to work with such expressions, introduction of redundant extra minus signs, etc. is which I find inconvenient.

What would be the correct way to deal with this? One related question I found is How to put terms in lexicographic order? where one of possibilities named was to issue

$PrePrint = PolynomialForm[#, TraditionalOrder -> True] &;

but some adverse effects of it were also mentioned. And in fact, TraditionalOrder is not what I would need anyway, it gives something still different, namely (in this particular case) -(1/(t-1)).

Also, after having posted the question, I discovered in the Related column to the right another one, How do I prevent Mathematica from reformatting the expressions that I copy as LaTeX?, but also in this case I cannot really figure out whether I can use answers there for my purposes.

As suggested by bbgodfrey here is a more illustrative example. Issuing

enter image description here

gives

enter image description here

while what I would like to see is

enter image description here

$\endgroup$
  • $\begingroup$ Work-arounds tend to be problem-specific, in my experience. In this case, Simplify restores -(1/(-1 + t)) to the desired form. Perhaps, you could provide a typical expression with which you are having such problems. $\endgroup$ – bbgodfrey Oct 30 '15 at 17:48
  • $\begingroup$ @bbgodfrey I encounter such expressions systematically, just to give an example - Apart[((1-3 t^2-t w+t^2 w+5 t^3 w-t^2 w^2-2 t^3 w^2)z w)/((1-3 t^2) (1-w) (1-2 t w)(1-z w)),w] gives -((t (1+2 t))/(2 (-1+3 t^2)))+(-z+t z)/((-1+2 t) (-1+w) (-1+z))-(t (-z+2 t^2 z))/(2 (-1+2 t) (-1+3 t^2) (-1+2 t w) (2 t-z))-(-t^2-2 t^3-t z+t^2 z+5 t^3 z+z^2-3 t^2 z^2)/((-1+3 t^2) (2 t-z) (-1+z) (-1+w z)). Simplify just puts it all together in this case which is not what I want, while Map[Simplify,%] does not change anything. $\endgroup$ – მამუკა ჯიბლაძე Oct 30 '15 at 17:51
  • $\begingroup$ PolynomialForm appears to work well in this case. As I remarked above, such work-arounds tend to be problem-specific. $\endgroup$ – bbgodfrey Oct 30 '15 at 18:15
  • $\begingroup$ @bbgodfrey Could you please post this as an answer, with details? How exactly can I use PolynomialForm? $\endgroup$ – მამუკა ჯიბლაძე Oct 30 '15 at 18:24
  • 1
    $\begingroup$ As requested, I have provided as an answer the use of PolynomialForm applied to the expression in your comment above. However, you should not expect it to give you the desired form in all cases. Also, I recommend against using $PrePrint for the reasons given in How to put terms in lexicographic order. $\endgroup$ – bbgodfrey Oct 30 '15 at 18:43
2
$\begingroup$

The following functions seem to produce the desired result. (Edited to simplify functions.)

f[x_] := Quiet@Module[{ct = 1, den, vv = Denominator[x]}, 
    v = If[Head[vv] === Times, List @@ vv, {vv}]; 
    den = Times @@ (If[IntegerQ[#[[1]]] && #[[1]] < 0, ct = -ct; -#, #] & /@ v);
    Simplify[ct Numerator[x]]/den]
g[x_] := Module[{v}, v = If[Head[x] === Plus, List @@ x, {x}]; Plus @@ Map[f, List @@ v]]

Applied to the expressions in the question,

ex = 1/(1 - t);
ans = Factor[ex];
g[ans]
(* 1/(1 - t) *)

ex = (1 + t - t^2 - t z - 2 t^2 z)/(1 - 2 t z);
ans = Apart[ex, z];
g[ans]
(* 1/2 (1 + 2 t) + (1 - 2 t^2)/(2 (1 - 2 t z)) *)

as requested. A more challenging expression is that given by the OP in a comment above.

ex = ((1 - 3 t^2 - t w + t^2 w + 5 t^3 w - t^2 w^2 - 
    2 t^3 w^2) z w)/((1 - 3 t^2) (1 - w) (1 - 2 t w) (1 - z w));
ans = Apart[ex, z];
g[ans]
(* (t (1 + 2 t))/(2 (1 - 3 t^2)) + (t (-1 + 2 t^2) z)/(2 (1 - 2 t) (1 - 3 t^2) 
   (1 - 2 t w) (2 t - z)) + (z - t z)/((1 - 2 t) (1 - w) (1 - z)) + 
   (-t z + z^2 + t^3 (-2 + 5 z) + t^2 (-1 + z - 3 z^2))/((1 - 3 t^2) (1 - z) 
   (2 t - z) (1 - w z)) *)

This too appears to meet the OP's goal. The accuracy of any of the three examples can be verified by

Simplify[% - %%]
(* 0 *)

The overall strategy of this approach is to decompose ans into a List of ratios of polynomials, decompose the factors in the denominator of each List element into other List, recast those denominator elements into the form requested by the OP, and then reassemble the ratios and ans itself. f handles the inner List, and g the outer. Note that

v = If[Head[x] === Plus, List @@ x, {x}];

has been inserted into g, and a similar expression into f, in an edit to accommodate the special cases of an ans that contains only a single term (as in the first example) or a Denominator that contains only a single factor (as in the third example with ex replaced by 2 ex).

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you for help. Unfortunately this does not always work - in some cases it does not change the expression (for example g[(-2+3t)/(1-t)] returns the same (-2+3t)/(1-t)) while in some other cases it performs simplifications which might or might not be desirable (for example g[-((-1+t) (1+t))/t^2] gives (1-t^2)/t^2). Still, inspired by your approach I came up with something which I will post as an answer. I am not sure if this always works, although I tested it a bit. In any case, thank you very much! $\endgroup$ – მამუკა ჯიბლაძე Nov 1 '15 at 19:55
2
$\begingroup$

Inspired by bbgodfrey's answer I came up with what hopefully gives a solution. I am not accepting it yet though, in case I've overlooked something.

myForm[x_] :=
 Which[
  NumberQ[x] || Head[x] === Symbol, x,
  Head[x] === Plus && Select[x, NumberQ] < 0, With[{t = Map[myForm, -x]}, -HoldForm[t]],
  True, ReleaseHold[Map[myForm, x]]
 ]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Quite elegant recursive solution (+1). Glad I could help. By the way, I am uncertain whether someone can upvote or accept his own answer. Best wishes. $\endgroup$ – bbgodfrey Nov 1 '15 at 23:42
  • $\begingroup$ @bbgodfrey I know I cannot upvote but can accept. I'm still waiting for somebody probably give still cleaner solution. For mine so far I discovered one undesirable thing: myForm[-1+a] gives -(1-a) but myForm[-(1-a)] gives -1+a back. Potentially this might produce infinite loops somehow... $\endgroup$ – მამუკა ჯიბლაძე Nov 4 '15 at 21:10

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.