MathGroup Archive 2005

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

Search the Archive

Types in Mathematica, a practical example

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,


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


  • Prev by Date: Re: Sending again: Symbolizing subscripts
  • Next by Date: How to set up a diff equation for circuit with a diode?
  • Previous by thread: Re: Rendering goes into infinite loop
  • Next by thread: Re: Types in Mathematica, a practical example