Slice assignment
(New to Analytica 4.0)
Slice assignment means assigning to a slice of a local array-valued variable:
x[I = n ] := e
x must be a local variable, I is an index (local or global), n is a single value of I, and e is any expression.
Slice assignment allows some algorithms to be much easier and more efficient than was possible in previous releases. (Previously, you could assign only an entire array to a local variable.)
For example, consider:
Function Fibonacci_series(f1, f2, n) := INDEX m := 1..n; VAR 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];
In the first slice assignment in this function:
result[m = 1] := f1;
result was not previously indexed by m. So the assignment adds the index m to result, sets the value to f1 for m=1 and leaves result with its original value, 0, for other values of m.
More generally, in a slice assignment:
x[i = n ] := e
if x was already indexed by i, it will set x[i=n] to the value of e. For other values of i, x will retain its previous value. If x was not already indexed by i, the assignment adds i as a dimension of x, and sets the slice x[i=n] to e. All other slices of x over i will retain their previous values. If x was indexed by other indexes, say j, the result will be indexed by I and j. The assigned slice x[i=n] will have the value e for all values of the other index(es) j. Again, slices for other values of i will retain their original values of x.
You may index by position as well as name in a slice assignment, for example:
x[@I = 2] := e
Slice assignment, e.g. x[I = A] := e, has these limitations:
- x must be a local variable.
- a must be a single value, not an array.
- You may use only one index: For example, X[I = A, J=B] := e, with two index expressions for I and J is not allowed.
Enable comment auto-refresher