Search code examples
wolfram-mathematicaalgebra

How can I get rid of irritating prefactor of one in Mathematica?


I am trying to refine algebraic expressions into a convenient form using Mathematica to make sure I don't drop a sign or make some other trivial slip. After a lot of swearing, I have come to accept that this is not a deterministic process and I will have to do it step by step and also that the algebraic manipulation palette is my friend. However, there is still one thing that's driving me nuts. Sometimes mathematica spits out expressions with these seemingly extraneous leading ones. For example, right now I'm looking at:

0.5*(1.*Log[-1.*a^2 + 1.*bigr^2] - 1.*Log[1.*a^2 - 2.*a*bigr + 1.*bigr^2])

when I would much rather be looking at:

0.5*(Log[-a^2 + bigr^2] - Log[a^2 - 2.*a*bigr + bigr^2])

It's more than just a cosmetic problem because it confuses Factor[] when I try to apply it to some of the obvious quadratic factorizations in the above expression. Any clean fixes?


Solution

  • your = 0.5*(1.*Log[-1.*a^2 + 1.*bigr^2] - 1.*Log[1.*a^2 - 2.*a*bigr + 1.*bigr^2])
    your  /. {1. -> 1, -1. -> -1}
    
    (* -> 0.5 (Log[-a^2 + bigr^2] - Log[a^2 - 2. a bigr + bigr^2]) *)
    

    The dot after the 1 tells mathematica to treat the number as a non-exact quantity. So for example

    1. * 1/2
    (* -> .5 *)
    

    but

    1 * 1/2
    (* -> 1/2 *)
    

    Use exact quantities in your calculations (1, 2, 1/2) instead of decimal numbers (1., 2. ,0.5) if you need exact results