Difference between revisions of "Slice assignment"

m
Line 1: Line 1:
 
(New to Analytica 4.0)
 
(New to Analytica 4.0)
  
Slice assignment means assigning to a slice of a local array-valued variable:
+
'''Slice assignment''' means assigning a value into an element or slice into an array contained by a local variable:
 
   x[I = n ] := e
 
   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.  
+
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.) 
+
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:
 
For example, consider:
Line 26: Line 26:
 
You may index by position as well as name in a slice assignment, for example:
 
You may index by position as well as name in a slice assignment, for example:
 
   x[@I = 2] := e
 
   x[@I = 2] := e
 +
assigns the value of e as the second slice of x over index I.
  
Slice assignment, e.g. x[I = A] := e,  has these limitations:  
+
Slice assignment, e.g. x[I = A] := e,  has three important limitations:  
  
 
* x must be a local variable.
 
* x must be a local variable.
 
* a must be a single value, not an array.
 
* 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.
+
* You may use only one index: For example, you may not use an expression like x[I = A, J=B] := e, with two index expressions.  If x has two (or more) dimensions, you can create and assign a slice (e.g. a row) to x.
 +
 
 +
It is possible to assign to a global variable (or slice

Revision as of 20:19, 26 July 2007

(New to Analytica 4.0)

Slice assignment means assigning a value into an element or slice into an array contained by a local 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.

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:

  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

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

Slice assignment, e.g. x[I = A] := e, has three important limitations:

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

It is possible to assign to a global variable (or slice

Comments


You are not allowed to post comments.