Re: HoldForm
- To: mathgroup at smc.vnet.net
- Subject: [mg68098] Re: HoldForm
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Mon, 24 Jul 2006 00:55:40 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <e9v992$gjr$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Bruce Colletti wrote: > Re Mathematica 5.2.0.0. > > This code displays the component differences of matrix subtraction (A-B): > > A = {{1, 2}, {3, 4}}; > > B = {{5, 6}, {7, -8}}; > > Map[MapThread[HoldForm[#1 - #2] &, {A[[#]], B[[#]]}] &, Range@2] > > As desired, the output is {{1 - 5, 2 - 6}, {3 - 7, 4--8}}. > > Two questions: > > - What's a better way to do this? > > - How can I replace the -- by +? > > Thankx. > > Bruce > > Hi Bruce, The first thing to notice is the Mathematica internal form of the expression that matches, say, 4--8. Using FullForm on your result, we see that the expression is HoldForm[Plus[4, Times[-1, -8]]]. In the example below, we use two consecutive transformation rules: first we get ride of the HoldForm function that prevents to change the sign of the second number while in the meantime we bare the interpretation of the Plus function by using a dummy function myPlus. Then, we replace the dummy function by a HoldForm[Plus[...]] that yields the desired form. MapThread[HoldForm[#1 - #2] & , {Flatten[A], Flatten[B]}] /. HoldForm[(x_) - (y_)?Negative] -> myPlus[x, -y] /. myPlus[x_, y_] -> HoldForm[x + y] returns {1 - 5, 2 - 6, 3 - 7, 4 + 8} Best regards, Jean-Marc