Difference between revisions of "References and Data Structures"

(Created page with "Category: Analytica User Guide <breadcrumbs> Analytica User Guide > {{PAGENAME}}</breadcrumbs><br /> A '''reference''' is an indirect link to a value, an atom or an array...")
 
m
Line 15: Line 15:
  
 
The result of Ref_to_M looks like this:
 
The result of Ref_to_M looks like this:
 +
 +
[[File:result_ref-to-m.png|400px]]
 +
 +
You can double-click the cell containing «ref» to view the value referenced, in this case:
 +
 +
[[File:result_ref-to-m2.png|400px]]
 +
 +
You can also create an array of references. Suppose:
 +
 +
Index K
 +
Definition: 1..5
 +
 +
Variable Ksquare
 +
Definition: K^2
 +
 +
Ksquare →
 +
 +
[[File:result-ksquare.png|400px]]
 +
 +
Variable Ref_to_Ksquare
 +
Definition: \ Ksquare
 +
 +
Ref_to_Ksquare →
 +
 +
[[File:result_ref_ksquare.png|400px]]
 +
 +
If you click the «ref» cell, it opens:
 +
 +
[[File:result_ref_ksquare2.png|400px]]
 +
 +
You can also create an array of references from an array, for example:
 +
 +
Variable Ref_Ksquare_array
 +
Definition: \ [] Ksquare
 +
Ksquare →
 +
 +
The empty square brackets [ ] specify that the values referred to have no indexes, i.e., they are atoms. You can now click
 +
any of these cells to see what it refers to.
 +
 +
[[File:result_ref_ksquare_array.png|400px]]
 +
 +
Clicking the third cell, for example, gives:
 +
 +
[[File:result_ref_ksquare_array2.png|400px]]
 +
 +
==Managing indexes of referenced subarrays: \[i, j,...] e==
 +
 +
More generally, you can list in the square brackets any indexes of e that you want to be indexes of each subarray referenced by the result. The other indexes of e (if any) are used as indexes for the referencing array. Thus, in the example above, since there were no indexes in square brackets, the index K was used as an index of the reference array. If instead we write:
 +
 +
\ [K] Ksquare →
 +
 +
[[File:result_ref_ksquare_array3.png|400px]]
 +
 +
It creates a similar result to \ Ksquare, since K is the only index of Ksquare.
 +
 +
To summarize:
 +
 +
:{| class="wikitable"
 +
|-
 +
|'''\ e'''
 +
|Creates a reference to the value of expression e, whether it is an atom or an array.
 +
|-
 +
|'''\ [] e'''
 +
|Creates an array indexed by all indexes of e containing references to all atoms from e.
 +
|-
 +
|'''For x[i] := a DO e'''
 +
|Assigns to loop variable <code>x</code> successive subarrays from array <code>a</code>, each indexed only by <code>i</code>. It repeats evaluation of expression <code>e</code> for each index value of <code>a</code> other than <code>i</code>. As before, the result has the same indexes as <code>a</code>.
 +
|-
 +
|'''For x[i, j …] := a DO e'''
 +
|Assigns to loop variable <code>x</code> successive subarrays from array <code>a</code>, each indexed only by <code>i, j ….</code> It repeats evaluation of expression <code>e</code> for each index value of <code>a</code> other than <code>i, j ….</code> . As before, the result has the same indexes as <code>a</code>.
 +
|}
 +
 +
 +
  
 
<footer>Ensuring Array Abstraction / {{PAGENAME}} / Handles to Objects</footer>
 
<footer>Ensuring Array Abstraction / {{PAGENAME}} / Handles to Objects</footer>

Revision as of 06:37, 17 December 2015


A reference is an indirect link to a value, an atom or an array. A variable can contain a single reference to a value, or it can contain an array of references. Variables and arrays can themselves contain references, nested to any depth. This lets you create complex data structures, such as linked lists, trees, and non-rectangular structures. Use of references is provided by two operators:

  • \e is the reference operation. It creates a reference to the value of expression e.
  • #e is the dereference operation. It obtains the value referred to by e. If e is not a reference, it issues a warning and returns Null.

An example: Variable M Definition: 100

Variable Ref_to_M Definition: \ M

The result of Ref_to_M looks like this:

Result ref-to-m.png

You can double-click the cell containing «ref» to view the value referenced, in this case:

Result ref-to-m2.png

You can also create an array of references. Suppose:

Index K Definition: 1..5

Variable Ksquare Definition: K^2

Ksquare →

Result-ksquare.png

Variable Ref_to_Ksquare Definition: \ Ksquare

Ref_to_Ksquare →

Result ref ksquare.png

If you click the «ref» cell, it opens:

Result ref ksquare2.png

You can also create an array of references from an array, for example:

Variable Ref_Ksquare_array Definition: \ [] Ksquare Ksquare →

The empty square brackets [ ] specify that the values referred to have no indexes, i.e., they are atoms. You can now click any of these cells to see what it refers to.

Result ref ksquare array.png

Clicking the third cell, for example, gives:

Result ref ksquare array2.png

Managing indexes of referenced subarrays: \[i, j,...] e

More generally, you can list in the square brackets any indexes of e that you want to be indexes of each subarray referenced by the result. The other indexes of e (if any) are used as indexes for the referencing array. Thus, in the example above, since there were no indexes in square brackets, the index K was used as an index of the reference array. If instead we write:

\ [K] Ksquare →

Result ref ksquare array3.png

It creates a similar result to \ Ksquare, since K is the only index of Ksquare.

To summarize:

\ e Creates a reference to the value of expression e, whether it is an atom or an array.
\ [] e Creates an array indexed by all indexes of e containing references to all atoms from e.
For x[i] := a DO e Assigns to loop variable x successive subarrays from array a, each indexed only by i. It repeats evaluation of expression e for each index value of a other than i. As before, the result has the same indexes as a.
For x[i, j …] := a DO e Assigns to loop variable x successive subarrays from array a, each indexed only by i, j …. It repeats evaluation of expression e for each index value of a other than i, j …. . As before, the result has the same indexes as a.



Comments


You are not allowed to post comments.