Re: Types in Mathematica, a practical example

*To*: mathgroup at smc.vnet.net*Subject*: [mg62950] Re: [mg62800] Types in Mathematica, a practical example*From*: Kristen W Carlson <carlsonkw at gmail.com>*Date*: Fri, 9 Dec 2005 05:10:32 -0500 (EST)*References*: <4394EE4E.7090901@wolfram.com> <000f01c5fbf8$e3979330$996f1081@fy.chalmers.se>*Sender*: owner-wri-mathgroup at wolfram.com

On 12/8/05, Ingolf Dahl <ingolf.dahl at telia.com> wrote: >Andrzej Kozlowski, who suggests such a solution, thinks that I am looking at >Mathematica from the wrong view point. It is my strong belief that one >should be able, especially when mathematics is involved, to observe from as >many different view points as possible. An item is not really beautiful, or >properly designed, if it is not beautiful from all view points. The same >should apply to Mathematica, and I have also understood that Mathematica was >intentionally designed to be well-behaved when seen from different view >points. Or? Could we re-phrase "wrong" to say "inefficacious" and then say, *To observe from many points of view is quite efficacious. *To see how Mathematica can achieve my goal is efficacious and necessary--if it's a priority and not just idle curiosity about the language. *To want Mathematica to achieve my goal my way or the way that my favorite language does it may be possible, but may also be inefficient and inefficacious. *If you're using Mathematica, embrace the Mathematica Way. Zen: Be Mathematica. :-) I think that's what Andrzej is saying. I suggest this applies to many discussions on MathGroup. >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, The usage of "force" is what made me write some of this. > In my previous submission I asked for a way to define/declare list with > undefined elements. All the answers that I have got indicate that it is > difficult in the present versions of Mathematica. I would not really enjoy having to explain to a newbie what error he had > done if she/he had encountered the same problem as I stated initially. I > would have to explain that in Mathematica it is necessary to choose between > two ways to define matrices and similar structures: Either from bottom and > up, e.g. by using Array, or from top and down, e.g. by using Table. The principle you voiced above applies here: Two different ways of looking at the task and solving it. Therefore this facet is efficacious, maybe even elegant. > In the > first case, if you give the whole structure a name, it is not possible to > use the same name as is used for assigning values to the individual > elements. Payment for the efficacy; yes it is confusing at first. At first. > What I would prefer is a new Mathematica word: "Undefined". Before taking another shot at this, we should ask do you need Undefined, do you need to do it this way? See Help 2.5.5, "Making Definitions for Indexed Objects," which starts: "In many kinds of calculations, you need to set up "arrays" which contain sequences of expressions, each specified by a certain index. One way to implement arrays in Mathematica is by using lists. You can define a list, say a = {x, y, z, â?¦ }, then access its elements using a[[i]], or modify them using a[[i]] = value. This approach has a drawback, however, in that it requires you to fill in all the elements when you first create the list. "Often, it is more convenient to set up arrays in which you can fill in only those elements that you need at a particular time. You can do this by making definitions for expressions such as a[i].... " a[i] = value add or overwrite a value a[i] access a value a[i] =. remove a value ?a show all defined values Clear[a] clear all defined values Table[a[i], {i, 1, n}] â??or â??Array[a, n] convert to an explicit List " You can continue from there; it's a Mathematica Way, but I don't know that it solves your issues. Addressing your way below. Maybe I'm repeating myself except for the upvalues defined, but I'm doing it in the context just given. > Then I could > define a list x as > > x = {1,Undefined,2}; > > x[[1]] should evaluate to 1 as usual, but for this word "Undefined" the list > part x[[2]] should evaluate to x[[2]], precisely as for an undefined > unindexed symbol. > x[[4]] should give the usual error message: > > "Part::partw: Part 4 of {1, Undefined, 2} does not exist." undefined = "undefined"; (*so that you could globally assign something to the undefineds, even as you make other assignments elsewhere & so you get a meaningful token at the undefineds*) table1 = Table[undefined, {3}] {undefined,undefined,undefined} table1[[2]] undefined ReplacePart[table1, 2, {3}] {undefined,undefined,2} Now with Array ar1 = Array[u, {3}] {u[1],u[2],u[3]} u[1] = 1 1 Do[ u[i]= undefined,{i,2,3} ]; ar1 {1,undefined,undefined} ar1[[4]] Part::partw: "Part 4 of \!\({1, u[2], u[3]}\) does not exist. {1,u[2],u[3]} [[4]] > x[[3]] =. and Clear[x[[3]]] should redefine x as {1,Undefined,Undefined}, > and x = Undefined should be interpreted as x =. > > Could this be implemented by operator overloading or by unprotecting and > redefining Part? Whether we modify Part or Clear, better to do it as an upvalue for reasons mentioned (it's safer than unprotecting Part and you can define it contextually, which is much more powerful, and won't slow down Clear or Part in other contexts). For Array i.e. indexed notation: Clear[ar1[i_]] ^:= ar1[[i]] = undefined; (*the upvalue*) Clear[ar1[2]] undefined ar1 {1,undefined,u[3]} For Matrix: Clear[mat[i_List] ] ^:= With[{matrix = mat, partspec = i}, ReplacePart[matrix, undefined, partspec]]; (* the upvalue *) mat = Table[x, {4}] {x,x,x,x} Clear[mat[{2}]] {x,undefined,x,x} > > I believe that this could be implemented in Mathematica without destroying > compatibility with existing code, since it would be an added-on feature. And > my intuition (if that is of any value) tells me that such an addition would > have benefits I fail to see. Well, if your choice is to use Mathematica, just getting it done efficaciously is enough, and extra benefits may result, esp if you try to view it in one of the Mathematica Ways. Best, Kris