Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HoldForm

0 views
Skip to first unread message

Bruce Colletti

unread,
Jul 23, 2006, 3:42:26 AM7/23/06
to
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


Andrzej Kozlowski

unread,
Jul 24, 2006, 1:50:47 AM7/24/06
to

An easier way to get identical looking output Traditional or
StandardForm is:

Subtract @@ Map[HoldForm, {A, B}, {3}]

The difference is that HoldForm is in a different place than in your
case. If you really prefer your position of HoldForm you can use:

Subtract @@ Map[HoldForm, {A, B}, {3}] /. HoldForm[x_] - HoldForm[
y_] :> HoldForm[x - y]

but this no longer looks much simpler than your original approach.
However, you can use analogous approach to get the same answer as
yours but without the double minus signs:

Plus @@ Map[HoldForm, {A, -B}, {3}] /.
HoldForm[x_] + HoldForm[y_] :> HoldForm[y + x]


Andrzej Kozlowski

Jean-Marc Gulliet

unread,
Jul 24, 2006, 1:51:48 AM7/24/06
to
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

J Siehler

unread,
Jul 24, 2006, 1:52:49 AM7/24/06
to
> 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 +?

A={{1,2},{3,4}};
B={{5,6},{7,-8}};

inertPlus[l___]:=HoldForm[Plus[l]]
SetAttributes[inertPlus,Listable]

inertPlus[A,-B]

{{1-5,2-6},{3-7,4+8}}

Jean-Marc Gulliet

unread,
Jul 24, 2006, 1:53:49 AM7/24/06
to
A simpler approach that my preceding post is

MapThread[TraditionalForm[HoldForm[#1] - #2] &, Flatten /@ {A, B}]

that returns {1 - 5, 2 - 6, 3 - 7, 4 + 8}.

Regards,
Jean-Marc

Bob Hanlon

unread,
Jul 24, 2006, 1:54:50 AM7/24/06
to
A={{1,2},{3,4}};

B={{5,6},{7,-8}};

MapThread[HoldForm[#1 + #2] &, {A, -B},2]

{{1 - 5, 2 - 6}, {3 - 7, 4 + 8}}


Bob Hanlon

Bharat Bhole

unread,
Jul 24, 2006, 2:00:53 AM7/24/06
to
Hi Bruce,

A simple modification of your program gives the desired result. Basically,
perform A+(-B) rather than performing A-B. So your code will look like,

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}}.

I am not sure why HoldForm treats + and - differently. It performs addition
but not subtraction in the sense that,

In: HoldForm[2+ (-3)]
Out: 2-3

In:HoldForm[2 - (-3) ]
Out: 2 - - 3

Bharat.


On 7/23/06, Bruce Colletti <vze2...@verizon.net> 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
>
>
>


--
----------------------------------------------------------
"No problem can withstand the assault
of sustained thinking."
Voltaire
----------------------------------------------------------


Jean-Marc Gulliet

unread,
Jul 24, 2006, 5:58:12 AM7/24/06
to

Of course, the above result is not a matrix. To match exactly what you
wanted, a component by component difference of two matrices, the
expression should read

MapThread[HoldForm[#1 - #2] & , {A, B}, 2] /. HoldForm[(x_) -

(y_)?Negative] -> myPlus[x, -y] /. myPlus[x_, y_] -> HoldForm[x + y]

Now, you get

{{1-5,2-6},{3-7,4+8}}

as required.

Best regards,
Jean-Marc

Jean-Marc Gulliet

unread,
Jul 24, 2006, 5:59:12 AM7/24/06
to

Again, the above result is not a matrix. Sorry about these mistakes.


To match exactly what you wanted, a component by component difference of
two matrices, the expression should read

MapThread[HoldForm[#1] - #2 & , {A, B}, 2]

returns

{{1 - 5, 2 - 6}, {3 - 7, 4 + 8}}

as desired.

Best regards,
Jean-Marc

0 new messages