Local Values




Release:

4.6  •  5.0  •  5.1  •  5.2

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:

Sum(Array_a*Array_b, N)/(1 + Sum(Array_a*Array_b, N))

Define it as:

Local t := Sum(Array_a*Array*b, N); t/(1 + t)

To compute a correlation between Xdata and Ydata, instead of:

Sum((Xdata - Sum(Xdata, Data_index)/Nopts)*(Ydata-
Sum(Ydata, Data_index)/Nopts), Data_index)/
Sqrt(Sum((Xdata - Sum(Xdata, Data_index)/
Nopts)^2, Data_index) * Sum((Ydata -
Sum(Ydata, Data_index)/Nopts)^2, Data_index))

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.

Tip
Usually, you cannot assign to a global variable — that is, to a variable created as a diagram node. You can assign only to a local value, declared in this definition using {{Local}} or {{LocalIndex}}, in the current context — that is, at the same or enclosing level in this definition. In a function definition, you can also assign to a parameter. This prevents side effects — i.e., where evaluating a global variable or function changes a global variable, other than one that mentions this variable or function in its definition. Analytica’s lack of side effects makes models much easier to write, understand, and debug than normal computer languages that allow side effects. You can tell how a variable is computed just by looking at its definition, without having to worry about parts of the model not mentioned in the definition. There are a few exceptions to this rule of no assignments to globals: You can assign to globals in button scripts or functions called from button scripts. See Button Creation for details. You can also assign to a global variable 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 :=
BEGIN
Local v := Array(Dim, [X, Y]);
Local v_r := Sum(rot*v, Dim);
X_rot := v_r[Dim2 = 'x'];
v_r[Dim2 = 'y'];
END

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.

See Also

Comments


You are not allowed to post comments.