MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Those Pesky Minus Signs

  • To: mathgroup at smc.vnet.net
  • Subject: [mg43352] RE: Those Pesky Minus Signs
  • From: "David Park" <djmp at earthlink.net>
  • Date: Tue, 26 Aug 2003 07:13:26 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Hi Ted,

There was an error in the first MakeBoxes statement: "(" instead of "[". So
I think it should have been:

MakeBoxes[a_*b : Plus[_?Negative*_, (_?Negative*_) ..],
    form : (StandardForm | TraditionalForm)] :=
  RowBox[{MakeBoxes @@ {-a, form},
      RowBox[{"(", MakeBoxes @@ {-b, form}, ")"}]}]
MakeBoxes[p_Rational?Negative, form : (StandardForm | TraditionalForm) ] :=
  FractionBox[ MakeBoxes @@ {Numerator[p], form},
    MakeBoxes @@ {Denominator[p], form} ]

But what about

-(a + b + c)

or

(-1/2)(a - b - c)

Actually, I think this is not a simple question because the user will often
have various desires about which term in the sum he wants to be positive and
which factors he wants taken out. There is a similar problem in lists and
arrays.

Sin[x]*E^y*{{1, 1}, {-1, 1}}
{{E^y*Sin[x], E^y*Sin[x]}, {(-E^y)*Sin[x],
   E^y*Sin[x]}}

leading to a lot of duplicated expressions. It would certainly be nice if
Mathematica had a controlled factoring of expressions from sums and arrays.
For sums it's the minus sign that is the problem and for arrays it is
everything.

Of course, without introducing HoldForm (if possible).

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/




From: Ersek, Ted R [mailto:ErsekTR at navair.navy.mil]
To: mathgroup at smc.vnet.net

David Park wanted to fix it so that
(-1/2)(a+b+c)  wouldn't return  (1/2)(-a-b-c)

I don't know why it works that way, but the following definitions will fix
the problem.
The second definition puts the minus sign in the numerator of a rational
number.

---------------------------

MakeBoxes(a_*b:Plus[_?Negative*_, (_?Negative*_)..],
form:(StandardForm|TraditionalForm)]:=
  RowBox[{MakeBoxes@@{-a,form}, RowBox[{"(", MakeBoxes@@{-b, form}, ")"}] }]

MakeBoxes[p_Rational?Negative, form:(StandardForm|TraditionalForm) ]:=
  FractionBox[ MakeBoxes@@{Numerator[p],form},
    MakeBoxes@@{Denominator[p],form} ]

-----------------------------

This fixes situations you didn't address.
Now  (-1/2)(a+2.3 b+c)  returns itself.


----------------------------
Regards,
Ted Ersek
Get Mathematica tips, tricks from
http://www.verbeia.com/mathematica/tips/Tricks.html


  • Prev by Date: Re: How does MathReader work?
  • Next by Date: Mathematica problem
  • Previous by thread: RE: Those Pesky Minus Signs
  • Next by thread: Pick random element from list