Local Values
Release: |
4.6 • 5.0 • 5.1 • 5.2 • 5.3 • 5.4 • 6.0 • 6.1 • 6.2 • 6.3 • 6.4 • 6.5 |
---|
Defining a local value: Local v := e
The Local construct creates a local identifier «v» that refers to the value obtained from evaluating expression «e». You can then use «v» in subsequent expressions within this context — that is, in following expressions in this group, or nested within expressions in this group. You cannot refer to a local value outside its context — for example, in the definition of another variable or function.
If «v» has the same identifier (name) as a global variable, any subsequent mention of «v» in this context refers to the just-defined local value, not the global.
Examples
Instead of defining a value as:
Define it as:
To compute a correlation between Xdata
and Ydata
, instead of:
Define the correlation as:
Local mx := Sum(Xdata, Data_index)/Nopts;
Local my :=Sum(Ydata, Data_index)/Nopts;
Local dx := Xdata - mx;
Local dy := Ydata - my;
Sum(dx*dy, Data_index)/Sqrt(Sum(dx^2, Data_index)*Sum(dy^2, Data_index))
The latter expression is faster to execute and easier to read. The correlation expression in this example is an alternative to Analytica’s built-in Correlation() function when data is dimensioned by an index other than the system index Run.
Assigning to a local value: v := e
The := (assignment operator) sets the local value «v» to the value of expression «e».
The assignment expression also returns the value of «e», although it is usually the effect of the assignment that is of primary interest.
The equal sign =
does not do assignment. It tests for equality between two values.
Within the definition of a function, you can also assign a new value to any parameter. This only changes the parameter and does not affect any global variables used as actual parameters in the call to the function.
V
from the definition of «X» when V
is defined as ComputedBy(X).ComputedBy(x)
This function indicates that the value of a variable is computed as a side-effect of another variable, «x». Suppose «v» is defined as ComputedBy(x), and the value of «v» needs to be computed, then Analytica will evaluate «x». During the evaluation of «x», «x» must set the value of «v» using an assignment operator.
Even though «v» is a side-effect of «x», its definition is still referentially transparent, which means that its definition completely describes its computed value.
ComputedBy is useful when multiple items are computed simultaneously within an expression. It is particularly useful from within an Iterate() function when several variables need to be updated in each iteration.
Variable rot := ... { a 2-D rotation matrix indexed by Dim and Dim2 }
Variable X_rot := ComputedBy(Y_rot)
Variable Y_rot :=
Assigning to a slice of a local value
Slice assignment means assigning a value into an element or slice of an array contained by a local value, for example:
x[i = n] := e
«x» must be a local value, «i» is an index (local or global), «n» evaluates to be a value or values of «i», and «e» is any expression. If «x» was not array or was an array not indexed by «i», the slice assignment adds i as a dimension of «x». The result returned from the assignment operator is the value «e», not the full value of «x», which can be a source of confusion but is that way so you can chain assignments, e.g.:
x[i = 3] := x[i = 5] := 7
You can write some algorithms much more easily and efficiently using slice assignment. For example:
Function Fibonacci_series(f1, f2, n: Number Atom) :=
LocalIndex m := 1..n;
Local result := 0;
result[m = 1] := f1;
result[m = 2] := f2;
For I := 3..n DO result[m = i] := result[m = i - 1] + result[m = i - 2];
result
In the first slice assignment:
result[m = 1] := f1;
result
was not previously indexed by m
. So the assignment adds the index m
to result, making it into an array with value f1 for m = 1
and its original value, 0, for all other values of m
.
More generally, in a slice assignment:
x[i = n] := e
If «x» was already indexed by «i», it sets x[i = n] to the value of «e». All other slices of «x» over «i» retain their previous values. If «x» was indexed by other indexes, say «j», the result is indexed by «i» and «j». The assigned slice x[i = n] has the value «e» for all values of the other index(es) «j».
You can index by position as well as name in a slice assignment, for example:
x[@i = 2] := e
This assigns the value of e
as the second slice of x
over index i
.
To set a cell in a multi-dimensional array, include multiple subscript coordinates, e.g.:
x[i = 2, j = 5, k = 3] := 7
Slice assignment array abstracts when «x», «n», or «e» have extra dimensions, and the abstraction is coordinated when an index is shared by «x», «n», or «e». Using abstraction, it is possible to assign to many cells in a single assignment operation.
Enable comment auto-refresher