Re: Types in Mathematica, a practical example

*To*: mathgroup at smc.vnet.net*Subject*: [mg62816] Re: [mg62800] Types in Mathematica, a practical example*From*: Sseziwa Mukasa <mukasa at jeol.com>*Date*: Tue, 6 Dec 2005 00:03:12 -0500 (EST)*References*: <200512051841.NAA21133@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On Dec 5, 2005, at 1:41 PM, 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. x was not assumed to be a number, x is a Symbol and without any definitions for x, x-a is {{-1 + x, -2 + x}, {-3 + x, -4 + x}} as expected. > Then x was > substituted by the matrix a. Actually first 1 is substituted for x then 2 etc. The result is what happens when you evaluate the expression for x-a above for each of those values of x. > 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? The unbound symbol x has a value (x) and given the rules for Subtract and lists and the value of a the rest of the results follow. If you want to treat x as having some other value (such as a list of lists) you have to assign that value to it, since the values of the elements in your case are unimportant(although in this case they actually are as you say below) then x=Table[y, Evaluate[Sequence @@ Transpose[{Dimensions[a]}]]] should suffice, but this is an unwieldy structure to work with, especially for the problem at hand. > 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. Define expressions which returns the desired values when x is supplied with arguments: x[a_,b_] := a[[a,b]] in this case. The problem is then you have to cognizant of what x in x-a means because of the rules around lists. > 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}} > This specification makes clear that you are actually interested in the values of the elements of x, they are supposed to be equivalent to some unspecified matrix. x=Table[y[i,j],{i,Length[a]},{j,Length[a[[1]]]}] is such a structure and you can use appropriate substitutions for y [i,j]. This is not an easy exercise because if a is larger than 2x2 there's a lot of bookkeeping to do, and you have to remember to leave y defined as a Symbol. This is also using the facilities of Mathematica in a rather perverse way, it is possible. As always until the advent of natural language understanding AIs with reasoning ability programming will remain a task of clearly specifying what is meant and expressing it unambiguously. > I am not allowed to Unset or Clear any part of a list either. Why not? What would clearing part of a list mean? Similarly for Unset. If you have a good idea of what such an expression should evaluate to you can use UpValues and Unprotect to make it happen. Regards, Ssezi

**References**:**Types in Mathematica, a practical example***From:*"Ingolf Dahl" <ingolf.dahl@telia.com>