Difference between revisions of "Associative vs. Positional Indexing"

Line 6: Line 6:
 
Suppose Index Year has the following elements:
 
Suppose Index Year has the following elements:
  
Year → {|  
+
Year→
border="1" || 2007 || 2008 || 2009 || 2010 || 2011 || 2012 |}
+
{| border="1"  
 +
| 2007 || 2008 || 2009 || 2010 || 2011 || 2012  
 +
|}
 +
 
 +
And variable Earnings evaluates to an array indexed by Year,
 +
 
 +
{|border="1"
 +
! 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:
 +
:[[Subscript/Slice Operator|Earnings[Year=2009] ]] → 6.6M
 +
or by position:
 +
:[[Subscript/Slice Operator|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.
 +
 
 +
{| border="1"
 +
|+ Associational / Positional Duals
 +
! Associational !! By Position || Notes
 +
|-
 +
| [[Subscript/Slice Operator|A[I=x] ]] || [[Subscript/Slice Operator|A[@I=n] ]] ||
 +
|-
 +
| [[Subscript]](A,I,x) || [[Slice]](A,I,n) ||
 +
|-
 +
| [[Slice]](I,n) || [[Index Position Operator::@|@[I=x] ]] || [[Slice]](I,n) : returns the nth element of an index.
 +
[[Index Position Operator::@|@[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..Do|For]] x:=I Do... || [[For..Do|For]] n:=[[Position operator|@I]] Do... ||
 +
|-
 +
| [[IndexValue]](I) || [[Position operator|@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.

Revision as of 19:00, 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 / 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.

Comments


You are not allowed to post comments.