Local Indexes

Revision as of 23:06, 22 February 2007 by Lchrisman (talk | contribs)

Analytica's arrays are, in general, multi-dimensional, where each dimension is identified with an Index. An Index is an Analytica object with a list of IndexValues. Indexes in Analytica come in three varieties:

Global Indexes: These appear as parallelagrams on a diagram. (e.g., IndexNode.jpg )
Self Indexes: A variable whose that can serve as an index, but also has a value.
Local Indexes: Index objects that do not exist in the global namespace.

Local indexes may be temporary, disappearing after they fall out of lexical context, or they may continue to exist outside of the lexical context where they were declared if an array indexed by the local index is returned.

Local indexes are declared within expressions via the Index..Do declaration, as in this example:

Index X := Sequence(x1,x2,(x2-x1)/1000) Do ArgMax( f(X^2), X )

In the above example, the identifier X exists as a local variable only within the body of the Do clause, and refers to the index defined as a sequence with 1000 points ranging from x1 to x2. ArgMax finds the maximum of f(X^2) and returns a single value, after which the index X ceases to exist.

Syntactically, an index can also be written using this syntax:

Index X := Sequence(x1,x2,(x2-x1)/1000);
ArgMax( f(X^2), X )

Here the local variable X is recognized to the end of the current lexical context.

It is possible for an expression to return an array that is indexed by a local index. When this happens, the local index continues to exist, although no local variable or identifier refers to it directly. This happens, for example, in the following expression:

Index Bit := 7..0;
Mod( Floor(N/2^Bit),2 )

which returns the following when N:=137:

.Bit → 7 6 5 4 3 2 1 0
1 0 0 0 1 0 0 1

After this expression is evaluated, the local variable Bit no longer exists, but a 1-D array indexed by .Bit does exist. The only way to access this local index is through the array result. Suppose BinaryN is defined by the expression above. The syntax BinaryN.Bit identifies the index. So, for example, to count the number of bits:

Sum( BinaryN, BinaryN.Bit )  → 3

where the second parameter to Sum expects an index.

To access the 3rd bit of BinaryN (counting from 0), we could write

BinaryN[ BinaryN.Bit = 3 ]

or we can abbreviate in this case as

BinaryN[ .Bit = 3 ]

since it is clear that .Bit is refering to an index of BinaryN. This abbreviation is only possible within the Subscript/Slice Operator, not in other contexts.

It is theoretically possible to create an array with more than one local index having the same name. In this case, the A.I syntax is ambiguous, and one index is identified.

Within an expression, if you have an array containing a local index, it is possible to define a local variable as an alias to refer to this index in order to avoid having to type, e.g., BinaryN.Bit every time. This is done using the Var..Do declaration, not the Index..Do, since the Index..Do construct would create an entirely new index object, and is done as follows:

Var I := VarTerm( BinaryN.Bit, asIndex:True ) Do expr

where inside expr, the local variable I is an alias for the actual index object. Note that the local name used in expr does not have to be the same as the local index's object name. The optional asIndex parameter of VarTerm is not actually needed in the example here, but is not a bad idea to includ. As a full example, the following expression bit-shifts the complement of BinaryN (divides the complement by 2), note how using I as an alias for BinaryN.Bit helps with conciseness within the expression:

Var I := VarTerm( BinaryN.Bit ) Do 
  @I=1 Or (Not BinaryN)[@I=@I-1]

.Bit → 7 6 5 4 3 2 1 0
1 0 1 1 1 0 1 1

The numeric equivalent is computed by

Var I := VarTerm( BinaryN.Bit ) Do 
  2^I * (@I=1 Or (Not BinaryN)[@I=@I-1])
Comments


You are not allowed to post comments.