Re: Extracting indices of variable
- To: MathGroup at yoda.physics.unc.edu
- Subject: Re: Extracting indices of variable
- From: Robby Villegas <Villegas at knox.bitnet>
- Date: Wednesday, Nov 6, 1991
Vital Aelion asks: > I am trying to find a Mathematica function which returns the index > of an indexed variable. > > For example, f[ a[1] ] = 1 If a variable has only one index, then the function f could be "First": First[ a[1] ] --> 1 Another way to extract the index, which is really an element of the expression a[1], is to use Part (implicitly, with the "[[ ]]" special-input form): a[1] [[1]] --> 1 More generally, if a variable has several indices, you can produce the list of its indices using the function Level: Level[ a[i1, i2, ..., in], {1} ] --> {i1, i2, ..., in} By default, Level extracts all elements of the given expression which are at the specified levels, and puts them into a _list_, as above. If you want some function applied to the n-tuple of indices, then Level allows you to indicate this in an optional third argument: Level[ a[i1, i2, ..., in], {1}, T ] --> T[i1, i2, ..., in] So to define the convenient function 'f' that you suggested, you could make the assignment f[var_] := Level[var, {1}] if you want the ordered n-tuple as a list, and you could define f[var_] := Level[var, {1}, T] if you want to repeatedly apply a single function T to indices of some variables. Robby Villegas Knox College E: Villegas at Knox.Bitnet