Revision as of 15:39, 24 April 2007 by Lchrisman (Talk | contribs) (Removed a vacuous CopyIndex, clarified the prerequisites for Z's def.)

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:=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[Rows=XX.I2], XX.I2)

The above definition of Z assumes that the model contains a matrix variable, X, indexed by Rows and Cols, and a variable Y indexed by Rows.

Detailed Notes

Specifying the Index Identifier

You can explicitly specify the identifier for an index using:

Index I / identExpr := seqExpr Do bodyExpr

the identExpr is evaluated, so that the identifier can be computed. The identExpr 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 identExpr. If the index appears in result windows or elsewhere, its identifier is displayed.


 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 )];

Setting attributes

Index..Do creates a new index object. It does not exist in the global namespace, but it is a full-fledged object with attributes of its own, in the same way a global index or variable object has attributes. Therefore, you can set its Units, Title, Description, etc., attributes after it is created. Analytica 4.0 will allow these side-effects, even if the expression is not being evaluated from a button script.


Index T := 1..12;
Title of T := "Time Offset";
Units of T := "Months";

When a local index appears in result views, attributes such as Title, Units, and description may impact what the user sees. For example, the "Show By Identifier" toggle on the object menu determines whether the title or identifier of the index is shown to the user.

Using an alias to an index

Within an expression, the A.I syntax can sometimes be a lengthy way to refer to an existing index. This can be shortened by using a local variable as an alias to an existing index. For example, the following are equivalent:

Subscript( TheFinalResult, TheFinalResult.MyLongIndex, Unique( TheFinalResult, TheFinalResult.MyLongIndex ) )
Var R := VarTerm(TheFinalResult);
Var I := VarTerm(R.MyLongIndex,AsIndex:True);  { I is an alias for TheFinalResult.MyLongIndex }

Note that when creating an alias for an existing index, you use Var..Do, not Index..Do. If you were to use Index..Do, you would instantiate a new index object.

See Also


You are not allowed to post comments.