Difference between revisions of "Associative vs. Positional Indexing"
Line 72: | Line 72: | ||
= Functions without Duals = | = Functions without Duals = | ||
− | Three built-in Analytica functions, [[SortIndex]], [[Subset]] and [[Unique]], are missing a built-in dual function. | + | Three built-in Analytica functions, [[SortIndex]], [[Subset]] and [[Unique]], are missing a built-in dual function. All three are associational, and in these three cases positional equivalents do make sense and are needed on occassion. Until such duals are added to the set of built-in Analytica functions, the following discussions how to express the positional equivalents. |
+ | |||
+ | [[SortIndex]](A,I) returns a permutation of I such that A[I=SortIndex(A,I)] are in ascending sorted order. However, if I might contains duplicates, the result of SortIndex does not unique identify the corresponding element, in which case a positional dual of SortIndex must be used. This is accomplished as follows: | ||
+ | |||
+ | Index J:=@I Do SortIndex(A[@I=J],J) |
Revision as of 19:07, 2 May 2007
When accessing array elements from Analyica expressions, we must identify which position along an index that we wish to access. In Analytica, this can usually be done either associatively or positionally.
Suppose Index Year has the following elements:
Year→
2007 | 2008 | 2009 | 2010 | 2011 | 2012 |
And variable Earnings evaluates to an array indexed by Year,
Year → | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 |
---|---|---|---|---|---|---|
Earnings→ | 4.5M | 5.6M | 6.6M | 7.9M | 9.0M | 10.1M |
Then we can access particular elements of Earnings either associatively:
- Earnings[Year=2009] → 6.6M
or by position:
- Earnings[@Year=3] → 6.6M
Referring to an index element associatively is generally refered to as subscripting, and can also be accomplished using the [{Subscript]] function, which is equivalent to the above, e.g.:
- Subscript(Earnings,Year,2009) → 6.6M
Referring to an index element by position is called slicing, and can equivalently be accomplished using the Slice function, e.g.:
- Slice(Earnings,Year,3) → 6.6M
Positions in Analytica are always 1-based, and range from 1 to Size(I).
Associative / Positional Duals
In Analytica, there are many functions that either require an index element be specified, or return an index position or element. Most functions that identify to an index element associationally have a positional dual, and vise versa. The following table indicates associational / positional duals.
Associational | By Position | Notes |
---|---|---|
A[I=x] | A[@I=n] | |
Subscript(A,I,x) | Slice(A,I,n) | |
Slice(I,n) | @[I=x] | Slice(I,n) : returns the nth element of an index.
@[I=x] : returns the position of element x in an index. |
ArgMax(A,I) | ArgMax(A,I,position:true) | |
ArgMin(A,I) | ArgMin(A,I,position:true) | |
Choice(I,n) | Choice(I,n,result:I) | The second parameter is always positional.
The return value here is the element of I or the position along I. |
For x:=I Do... | For n:=@I Do... | |
IndexValue(I) | @I
or 1..Size(I) || The elements of an index or the position along an index. | |
SubIndex(A,U,I) | PositionInIndex(A,U,I) | |
(no equiv) | Rank(A,I) | Rank is related to SortIndex, returns positions.
There is no logical associational dual. |
SortIndex(A,I) | ? | See below |
Subset(D) | ? | see below |
Unique(A,I) | ? | see below |
Functions without Duals
Three built-in Analytica functions, SortIndex, Subset and Unique, are missing a built-in dual function. All three are associational, and in these three cases positional equivalents do make sense and are needed on occassion. Until such duals are added to the set of built-in Analytica functions, the following discussions how to express the positional equivalents.
SortIndex(A,I) returns a permutation of I such that A[I=SortIndex(A,I)] are in ascending sorted order. However, if I might contains duplicates, the result of SortIndex does not unique identify the corresponding element, in which case a positional dual of SortIndex must be used. This is accomplished as follows:
Index J:=@I Do SortIndex(A[@I=J],J)
Enable comment auto-refresher