# Slice assignment

## Description

Slice assignment means assigning a value into an element or slice into an array contained by a local variable:

`a[I = x] := e`

«a» must be a local variable, «I» is an index (local or global), and «x» and «e» are expressions that are evaluated. In Analytica 4.1 and earlier, «x» must evaluate to a single value of «I», but in Analytica 4.2 and later, «x» may be an array of values of «I» (see array abstraction below).

You can write some algorithms much more easily and efficiently using slice assignment than was possible in previous releases lacking this feature.

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];`
`result`

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:

`a[i = x] := e`

if a was already indexed by «i», it will set a[i = x] to the value of «e». For other values of «i», «a» will retain its previous value. If «a» was not already indexed by «i», the assignment adds «i» as a dimension of «a», and sets the slice a[i = x] to «e». All other slices of «a» over «i» will retain their previous values. If «a» was indexed by other indexes, say `j`, the result will be indexed by «i» and `j`. The assigned slice a[i = x] 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 «a».

You may index by position as well as name in a slice assignment, for example:

`a[@I = 2] := e`

assigns the value of `e` as the second slice of a over index `I`.

## Return Value

The return value of a slice assignment, a[I = x] := v, is the result of «v». Note that it is not the full array assigned to. The assignment is parsed in a right-associative fashion, which means you can cascade assignments such as

`a[I=1] := a[I=3] := v`

having the effect of assigning «v» to both `a[I = 3]` and to `a[I = 1]`.

## Limitations

### Analytica 4.1 and earlier

In Analytica 4.1 and before, Slice assignment, e.g. `a[I = x] := e`, has three important limitations:

• «a» must be a local variable.
• «x» must be a single value, not an array.
• You may use only one index: For example, you may not use an expression like `a[I=x, J=y ] := e`, with two index expressions. If «a» has two (or more) dimensions, you can create and assign a slice (e.g. a row) to «a».

## Extensions in Analytica 4.2

In Analytica 4.2 and later, you can nest several levels of slices and subscripts, enabling easier assignment to individual cells in a multi-dimensional array. So, for example, assignments such as:

`a[I = x, J = y] := v`

are supported. You can have any number (up to Analytica maximum of 16 dimensions per array) of nestings, and can intermix subscripts and slices, e.g.,

`a[@I = n, J =y] := v`

Analytica 4.2 also allows the coordinates, `x` and `y`, to be array-valued. When the coordinates are array-valued, Analytica iterates over those dimensions, performing many assignments in one swoop. The details are described in the next subsection.

## Slice Assignment and Array Abstraction

You can assign to many cells in one operation when the coordinate is an array. For example,

`a[I = [2, 5, 7]] := 7`

would set the indicated three cells to the value of 7. The iteration is coordinated, so that `a[I = J ]:= J` would set each cell named in `J` to the value of `J`. In the assignment a[I = x] := v, any index contained in «x» that does not already appear in «a» becomes an iteration index -- the assignment is repeated for each element of that index, but the index does not appear in the final array. When there are two or more such dimensions and the array contains repeated values, this can create an ambiguity. The following example demonstrates:

`Variable x :=`
J ▶
K ▼ 'j1' 'j2'
'k1' 2 1
'k2' 1 2
`Variable v :=`
J ▶
K ▼ 'j1' 'j2'
'k1' 11 12
'k2' 13 14

Then, after `a[I = x] := v`:

`a →`
I ▶
12 14

or,

`a →`
I ▶
13 14

The ambiguity arises because the result depends on which order Analytica iterates over `J` and `K`. In one case, the following set of assignments occur (in this order):

`a[I=2] := 11 { at J='j1', K='k1' }`
`a[I=1] := 13 { at J='j1', K='k2' }`
`a[I=1] := 12 { at J='j2', K='k1' }`
`a[I=2] := 14 { at J='j2', K='k2' }`

In the other case, the assignments occur in this order:

`a[I=2] := 11 { at J='j1', K='k1' }`
`a[I=1] := 12 { at J='j2', K='k1' }`
`a[I=1] := 13 { at J='j1', K='k2' }`
`a[I=2] := 14 { at J='j2', K='k2' }`

Although currently the `a[I=2]:=11` assignment always occurs before the `a[I=2]:=14` assignment, this is not something that is guaranteed, and future releases of Analytica may parallelize the operation, opening the possibility that the assignment of 14 happens before 11.

When «a» contains one of the indexes of «x», then iteration on that index is coordinated, so that the final result also contains that index, and the associated assignments apply only to the appropriate slice. In the above example, suppose `b` contains the `K` index before the assignment occurs. Now there is no ambiguity, After

`b[I = x] := v`
`b →`
I ▶
K ▼ 1 2
'k1' 12 11
'k2' 13 14

When using array abstraction to iterate, that the right-hand side, «v», is evaluated entirely before any assignment occurs. Thus if «a» occurs in the right-hand side, at each iteration it is the value in «a» that was there before the assignment operation started that is used, not the value present at the previous iteration.

## Examples

A sieve algorithm to efficiently generate all the prime numbers up to MaxPrime:

```Var isPrime :=1;
Index n := Sequence(3,MaxPrime,2);
For i:=n do (
If isPrime[n=i] Then
isPrime[n=Sequence(3*i,MaxPrime,2*i,strict:true)] := 0;
);
Concat([2], Subset(isPrime))
```

Notice that the slice assignment line zeroes out all multiples of `i` in a single step. When the For-loop finishes, `isPrime` is an array of 0s and 1s, with 1s where `n` is prime. On my computer purchased in 2011 (i7-2600), this takes about 40 seconds when `MaxPrimes` is 10M.