Revision as of 21:07, 7 March 2007 by Lchrisman (Talk | contribs) (hyperlinks)

Index I := seqExpr Do

The construct, Index i := seqExpr defines an index local to the definition in which it is used. The expression seqExpr may be a sequence, literal list, or other expression that generates an unindexed array, as used to define a global index. For example:

Variable PowersOf2
Definition: Index J := 0..5; 

The new Variable PowersOf2 is an array of powers of two, indexed by the local index J, with values from 0 to 5:

PowersOf2 → PowersOf2.jpg

Dot Operator: A.I

The dot operator in A.I lets you access a local index I via an array A that it dimensions. If a local index identifies a dimension of an array that becomes the value of a global Variable, it may persist long after valuation of the expression—unlike other local Variables which disappear after the expression is evaluated.

Even though local index J has no global identifier, you can access it via its parent Variable with the dot operator, ’.’, for example:

PowersOf2.J → [0,1,2,3,4,5]

When using the subscript operation on a Variable with a local index, you need to include the ’.’ operator, but do not need to repeat the name of the Variable:

PowersOf2[.J=5] → 32

Any other Variables depending on PowersOf2 may inherit J as a local index—for example:

Variable P2
Definition: PowersOf2/2
P2[.J=5] → 16

Examples using a local index

In this example, MatSqr is a user-defined function that returns the square of a matrix—i.e., A x A, where A is the transpose of A. The result is a square matrix. Rather than require a third index as a parameter, MatSqr creates the local index, I2, as a copy of index I.

Function MatSqr(a: ArrayType; i,j: IndexType)
  Index I2:=CopyIndex(i);
  Sum(a*a[i=I2], j)

The local Variable, I2, in MatSqr is not within lexical scope in the definition of Z, so we must use the dot operator ’.’ to access this dimension. We underline the dot operator for clarity:

Variable Z
  Var XX := MatSqr(X, Rows, Cols);
  Sum(XX * Y[I=XX.I2], XX.I2)

Detailed Notes

Specifying the Index Name

You can explicitly specify the name for an index using:

Index I / nameExpr := seqExpr Do bodyExpr

the nameexpr is evaluated, so that the name can be computed. The nameExpr must evaluate to a text string containing a legal Analytica identifier. Within the lexical scope of bodyExpr, the local identifier I refers to the index. To refer to an index using the dot operator, use the nameExpr.


 Var A := ( Index I / "MyInd" := 1..10 do I^2 );
 Sum(A, A.MyInd)

The next, more complex example, creates a list of indexes, one index corresponding to each column of a table (sans the final column). It then transforms the table into a multi-dimensional array. The number of indexes created is arbitrary. The column label is used for the local index name.

 Function Table_to_array(A : Array[Rows,Columns] ; Rows,Columns : IndexType )
    MetaIndex Inds := 
         For c := 1..Size(Columns)-1 do (
              Var v := A[@Columns=c] ;
              Index I / Slice(Columns,c) := v[Rows=Unique(v, Rows )];

See Also


You are not allowed to post comments.