Re: Types in Mathematica, a practical example
- To: mathgroup at smc.vnet.net
- Subject: [mg63087] Re: [mg62800] Types in Mathematica, a practical example
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Tue, 13 Dec 2005 03:41:55 -0500 (EST)
- References: <200512051841.NAA21133@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Here is an alternative approach, which I am suspect you will not like, but which is the kind of thing i would probably do if I felt the problem you describe as a serious enough inconvenience. The solution is in the spirit of what I regard as the main principle of functional programming: try to solve problems with syntax by defining approapriate functions. In this case the function we need is very simple: MatrixPlus[x_?MatrixQ, y_?MatrixQ] /; Dimensions[x] == Dimensions [y] := x + y Now you can at once do: a = {{1, 2}, {3, 4}}; MatrixPlus[a, -x] /. x -> a {{0, 0}, {0, 0}} Since you like to be "in the typing business" you would probably want to have an infix notation for MatrixPlus. One could choose some suitable symbol and use the Notation package to achieve it. In fact, it might be a good idea to add some more specialised matrix functions that would have properties like: MatrixExpand[(a_).MatrixPlus[b_, c_]] := MatrixPlus[a.b, a.c ] etc, that would make it possible to do more with symbols that represent matrices (some of which could be actual matrices while others just undefined symbols). All of this seems quite feasible and probably might even be useful for someone who does a lot of symbolic work with matrices. For all I know someone might even have implemented something like this already. 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. > >
- References:
- Types in Mathematica, a practical example
- From: "Ingolf Dahl" <ingolf.dahl@telia.com>
- Types in Mathematica, a practical example