Other array functions

Revision as of 05:55, 14 December 2015 by Bbecane (talk | contribs)

AddIndex(a, i)

Adds one or more indexes to an array. The result is constant (does not vary) along any of the indexes that are added.

A basic philosophy in Analytica is that a value only needs to contain an index if its data varies along that index; hence, this function is an unnecessary function in almost all “normal” cases. When arrays with different dimensionalities get combined, array abstraction treats the values as if they are constant across the missing dimensions, so there is no need for you to add the dimensions explicitly, and is generally undesirable to do so.

Library: Array

Example: A vector on index ones, indexed by Period:

AddIndex(1, Period) :=

Period ▶
Early Middle Late
1 1 1

AddIndex(Rate_of_inflation, Period) :=

Years ▶
Period ▼ 2005 2006 2007 2008 2009
Early 1 1.01 1.02 1.03 1.04
Middle 1 1.01 1.02 1.03 1.04 Late 1 1.01 1.02 1.03 1.04

AddIndex(Miles, Period, MaintType)

A 4-D array indexed by Car_type, Years, Period and MaintType. Each slice along a given Period and MaintType contains the data from Miles.

AddIndex(Miles, ... IndexesOf(Cost_of_ownership))

Contains the data from Miles, with all the dimensions of Cost_of_ownership.

Aggregate(x, map, i, targetIndex)

Converts from an array x indexed by fine-grain index i, to an array indexed by the coarser-grained index targetIndex. Map is an array indexed by i, specifying for each element if i the value of targetIndex that the i-value maps to. An optional parameter, positional:true, can be specified if map contains the target index position rather than the target index value.

Aggregate is used when many index elements in i correspond to the same targetIndex element. By default, the values mapping to the same target position are aggregated by summing them. An optional parameter, type, can be used to specify alternative aggregation methods. Common aggregation methods include: “Sum”, “Max”, “Min”, “Average”, “Last”, “First”, and “Median”, but in general any built-in or user-defined function able to accept two parameters, an array and index, such as with a declaration: (A : Array[I] ; I : Index), can be named.

An optional parameter, defaultValue, specifies the value to use when no value maps to a given target position.

Library: Array

Example: To use Aggregate, you need to define the many-to-one map, here from Time to Period:

Index Period := ['Early', 'Middle', 'Late']

Variable Time2Period :=

\[Index_a]Array_s →

Time ▶
0 1 2 3 4
'Early' 'Middle' 'Middle' 'Late' 'Late'

Aggregate(Cost_of_ownership, Time2Period, Time, Period) →

Period ▶
CarType ▼ Early Middle Late
VW 2810 6049 6669
Honda 3535 7744 8531
BMW 3185 6703 7185

Aggregate(Cost_of_ownership, Time2Period, Time, Period, type: 'Average') →

Period ▶
CarType ▼ Early Middle Late
VW 2810 3025 3335
Honda 3535 3872 4266
BMW 3185 3352 3593

Concat(a1, a2, i, j, k)

Appends array a2 to array a1. i and j are indexes of a1 and a2, respectively. k is the index of the resulting dimension, and usually consists of the list created by concatenating i and j. When k is omitted and the result has two or more dimensions, a local index named .k will automatically be created by concatenating the elements of i and j, and the result will be indexed by this .k.

The parameter i (or j) can be omitted when a1 (or a2) is one-dimensional, if a1 (or a2) is indexed by a local index .k created by a previous call to Concat, when a1 (or a2) contains an implicit dimension, or when a1 (or a2) is atomic. The default to a local index .k makes it easy to nest calls to Concat when concatenating three or more arrays (or indexes) together. When a1 (or a2) is not array valued an i (or j) is omitted, a single element is concatenated to the front of a2 (or to the end of a1).

Library: Array

Examples: These examples use these variables:

Index Years :=

2005 2006 2007 2008 2009

Index More_years :=

2010 2011 2012

Index All_years := Concat(Years, More_years) →

2005 2006 2007 2008 2009 2010 2011 2012

Variable More_prices :=

More_years ▶
CarType ▼ 2010 2011 2012
VW 21K 22K 24K
Honda 25K 28K 29K
BMW 32K 33K 35K

Concat(Car_prices, More_prices, Years, More_years, All_years) →

Car_type ▶
All_years ▼ VW Honda BMW
2005 16K 18K 25K
2006 17K 19K 26K
2007 18K 20K 28K 2008 19K 22K 30K
2009 20K 24K 32K
2010 21K 25K 32K 2011 22K 28K 33K
2012 24K 29K 35K

Example of nested usage and local .k index: Concat(Car_prices, Concat(' ', Miles, , Years), Years) →

.K ▶
Car_type ▼ 2005 2006
VW 17K 19K 26K
Honda 18K 20K 28K BMW 19K 22K 30K

See Also

Comments


You are not allowed to post comments.