Types in Mathematica, a practical example
- To: mathgroup at smc.vnet.net
- Subject: [mg62800] Types in Mathematica, a practical example
- From: "Ingolf Dahl" <ingolf.dahl at telia.com>
- Date: Mon, 5 Dec 2005 13:41:04 -0500 (EST)
- Organization: Goteborg University
- Reply-to: <ingolf.dahl at telia.com>
- Sender: owner-wri-mathgroup at wolfram.com
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
- Follow-Ups:
- Re: Types in Mathematica, a practical example
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Re: Types in Mathematica, a practical example
- From: Sseziwa Mukasa <mukasa@jeol.com>
- Re: Types in Mathematica, a practical example
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Re: Types in Mathematica, a practical example
- From: Pratik Desai <pdesai1@umbc.edu>
- Re: Types in Mathematica, a practical example
- From: Kristen W Carlson <carlsonkw@Gmail.com>
- Re: Types in Mathematica, a practical example