Re: How to delay action of ...[[i]] (Part[...,i]) until appropriate time?
- To: mathgroup at smc.vnet.net
- Subject: [mg112252] Re: How to delay action of ...[[i]] (Part[...,i]) until appropriate time?
- From: Greylander <greylander at gmail.com>
- Date: Tue, 7 Sep 2010 01:59:50 -0400 (EDT)
- References: <i627rm$9c$1@smc.vnet.net>
I have answered my own question! And understand a bit more how to 'think' in Mathematica... The problem is that Part[[]] acts on any expression. What I have finally figured out is that I need a version of part (or a version of [[...]]) that acts *only* on lists (i.e. expressions with "List" head), and searching and exploring the documentation, I eventually learned how to do that. This will have exactly the desired result. Here is what I did: VPart[expr_List,k_] :=Part[expr,k] And I also define subscripting in an expression to invoke VPart[]: expr_List[Ctrl+-]k_ :=VPart[expr,k] Above the [Ctrl+-] means literally press the Ctrl key and the - (minus) key, so that "k_" will then be a subscript. Now I have one remaining question: why is there not something like this already defined in the core of Mathematica? It seems obvious (to me) that one would want a version of Part[] (element selection) restricted to List expressions only. On Sep 6, 4:13 am, Greylander <greylan... at gmail.com> wrote: > Consider the following toy example: > > in> goo[v_] := v.{1, 1}*(3 v) > in> foo[k_][v_] := goo[v] [[k]] > > in> goo[nnn] > out> 3 nnn nnn.{1, 1} > > in>foo[1][{2, 3}] > out>30 > > in> foo[2][nnn] > out>nnn > > Notice how in this last output, because the parameter nnn is > undefined, [[k]] ends up acting on the structure of the expression in > goo, but the intuitive behavior would be for evaluation of [[k]] to > also be delayed. The output I would like to see is: > > in>foo[2][nnn] > out> ( 3 nnn nnn.{1,1} ) [[2]] > in> nnn = {4,5} > % > out> 135 > > But of course it does not happen that way. > > I realize that Mathematica is a specification rather than procedural > language, but surely there is some reasonably elegant way to to delay > the effect of [[2]] above so it acts on the eventual result of > goo[nnn] and not on the structure of the expression. > > This is related to my other questions about using NDSolve on dependent > variables that have arbitrary structure and therefore require some > kind of indexing. > > There must some basic aspect of the Mathematica language that I have > missed which makes indexing and list manipulation more intuitive. (I > hope!) > > Anyone have any insights?