Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: Redefining the minus operator

  • To: mathgroup at smc.vnet.net
  • Subject: [mg52046] Re: Redefining the minus operator
  • From: David Bailey <dave at Remove_Thisdbailey.co.uk>
  • Date: Mon, 8 Nov 2004 03:13:35 -0500 (EST)
  • References: <cmkkn6$j99$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Ben Kovitz wrote:
> Is there a way in Mathematica to redefine the minus sign to stand for a 
> function of your choosing?
> 
> For example, I'd like to be able to do something like this:
> 
> In[1]:= Subtract[a_, b_] := MINUS[a, b]
> 
> In[2]:= FullForm[x - y]
> Out[2]:= MINUS[x, y]
> 
> In[3]:= 1 - 0
> Out[3]:= MINUS[1, 0]
> 
> 
> What actually happens is:
> 
> In[2]:= FullForm[x - y]
> Out[2]:= PLUS[x, Times[-1, y]]
> 
> In[3]:= 1 - 0
> Out[3]:= 1
> 
> 
> Naturally, I'd also like to redefine the plus sign, too.  When Plus[a_, 
> b_] is redefined as PLUS[a,b], then x + y indeed evaluates as PLUS[x, 
> y].  But Mathematica still simplifies if there are numbers in the 
> expression: FullForm[1 + 0] comes out as 1.
> 
> 
> Ben Kovitz
> Humboldt State University
> 

This is definitely an unwise thing to do unless it is just for interest 
or you have a very specialised application in mind. However, the first 
thing to realise is that you don't want to redefine Plus - that is too 
late in the translation process and also extremely disruptive to the 
rest of Mathematica. What you need to do is add extra definitions to 
MakeExpression to change the translation from BoxForm. The following 
code is designed to work in StandardForm:

restructure[{a_}] := a;
restructure[{a_, "+", b_, c___}] :=
restructure[{RowBox[{"PLUS", "[", a, ",", b, "]"}], c}];
restructure[{a_, "-", b_, c___}] :=
          restructure[{RowBox[{"MINUS", "[", a, ",", b, "]"}], c}];
restructure[{"+", b_, c___}] :=
restructure[{RowBox[{"PLUS", "[", b, "]"}], c}];
restructure[{ "-", b_, c___}] :=
restructure[{RowBox[{"MINUS", "[", b, "]"}], c}];

MakeExpression[RowBox[s : {___, "+" | "-", ___}],
         StandardForm] := MakeExpression[restructure[s], StandardForm]

Notice that this code will also translate +a into PLUS[a] and -a into 
MINUS[a]. Also 1+2 is translated into PLUS[1,2]

I would be interested to know what you want to do with this code - as 
there may be better ways to solve your problem.

Please note that this is a rather unusual piece of code, so TEST IT WELL 
before you rely on it.

David Bailey


  • Prev by Date: Re: non-Markov base ten random number generator based on Pi
  • Next by Date: Re: Re: Adding Vectors -- Newbie help please
  • Previous by thread: Redefining the minus operator
  • Next by thread: using the prime gaps to make a convergent series