       Re: Types in Mathematica, a practical example

• To: mathgroup at smc.vnet.net
• Subject: [mg62820] Re: [mg62800] Types in Mathematica, a practical example
• From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
• Date: Tue, 6 Dec 2005 00:03:16 -0500 (EST)
• References: <200512051841.NAA21133@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```It seems to me that you are looking here at Mathematica from the
wrong view point. Although Mathematica is not a strict "functional"
language, it is "functional" at least in the sense that it is
"functions" and not "objects" that play the central role (I am being
deliberately vague here but I have run out of patience with long
discussions of programming languages and  general design issues.). In
this case the issue is with the function Plus and not with the nature
or "type" of x etc. Plus has the attribute Listable, which is
responsible for the behaviour you see (and it does really matter if
Mathematica assumes that x is a number or not, for the same thing
will happen, for example,  if you use the string "x" instead of the
symbol  x). Hence the natural way to deal with your problem is by
temporarily blocking the Listable attribute like this:

In:=
a = {{1, 2}, {3, 4}};

In:=
Block[{Plus}, x - a /. x -> a]

Out=
{{0, 0}, {0, 0}}

As far as I am concerned this works just as I would wish and I see no
benefit in adding any kind of "types" to deal with this sort of
problems.

Andrzej Kozlowski

On 6 Dec 2005, at 03:41, Ingolf Dahl wrote:

> To MathGroup,
>
> I am not an advocate for strong typing in Mathematica, but consider
> the
> following simple example: I want to see if two matrices are equal.
> One of
> them was the result from some equation, and is given inside a rule.
> Then I
> write some code similar to this:
>
>
>
> a = {{1, 2}, {3, 4}};
>
> x - a /. {x -> a}
>
>
>
> I of course hope to get a matrix filled by zeroes, but if x is
> undefined,
> the following is returned:
>
>
>
> {{{{0, 1}, {2, 3}}, {{-1, 0}, {1, 2}}}, {{{-2, -1}, {0, 1}}, {{-3,
> -2}, {-1,
> 0}}}}
>
>
>
> First x was assumed to be a number, and (x - a) was evaluated. Then
> x was
> substituted by the matrix a. No bug in Mathematica, but it was not
> what I
> wanted as user. It is easy to make such a mistake in the
> programming. Of
> course there are many ways to get around this problem, but is there
> any
> reasonably simple way to "type" x to be a list of lists without
> specifying
> the elements, in such a way that the above example works?
>
>
>
> I could do
>
>
>
> ReleaseHold[Hold[x - a] /. {x -> a}]
>
>
>
> but then we are not in the "typing business" any longer.
>
>
>
> I think this question illuminates one aspect of the typing issue in
> Mathematica. I remember that I as a newbie looked for ways to declare
> matrices, in such a way that I later could specify matrix elements
> one-by-one, without initializing them first. I soon learned that
> there are
> other ways to achieve similar results, but still I do not see any good
> reason why I cannot force Mathematica to give the following
> response from
> x-a, if x in some way is declared to be a 2x2 list of lists:
>
>
>
> {{x[[1,1]] - 1, x[[1,2]] - 2},{x[[2,1]] - 3, x[[2,2]] - 4}}
>
>
>
> I am not allowed to Unset or Clear any part of a list either. Why not?
>
>
>
> Ingolf Dahl
>
> Sweden
>
>

```

• Prev by Date: Re: Types in Mathematica, a practical example
• Next by Date: Re: Types in Mathematica, a practical example
• Previous by thread: Re: Types in Mathematica, a practical example
• Next by thread: Re: Re: Types in Mathematica, a practical example