Functions that create indexes

Revision as of 21:48, 6 November 2015 by Bbecane (talk | contribs)


It is usually easiest to define an index as a list, list of labels, or sequence. Sometimes, you need to define an index using a more general expression, as a list of expressions, a list of variables, or a function such as Subset(), Concat(), and SortIndex(). This section describes these and other functions that you can use to create indexes.

[ u1, u2, u3, … um ]

A simple way to define an index is specify its definition as a list of values separated by commas and surrounded by square brackets. The values can be numbers, text values, or other expressions.

Examples

[8000, 12K, 15K]

['VW', 'Honda', 'BMW']

These lists are equivalent to using the List or List of Labels options in the expr menu.

List of variables

A list of variables contains identifiers of variables in square brackets, separated by commas. Usually, the simplest way to create a list of variables is to define the variable initially as an empty list, for example:

Variable CompareVars := []

When you draw an arrow from a variable, A, into CompareVars, it will automatically add A as the next item in the list:

CompareVars := [A]

Suppose you draw arrows from B and C, the definition will become:

CompareVars := [A, B, C]

When you draw an arrow from a variable already in the list, it removes it from the list. Suppose we draw an arrow from B to CompareVars, it will become:

CompareVars := [A, C]

The result of CompareVars is an array of the values of the variables it contains, with a self index, also called CompareVars, that usually shows the titles of the variables.

If any or all the variables contain arrays, the result contains the union of the indexes of the con- tained variables. For example if A is an atom (not an array) and C is indexed by c, the result will be indexed by I. The slice of CompareVars for A will have the same value of A repeated for each value of A.

Self index: The result will contain an extra index, a self index of CompareVars, comprising the list of the variables.

Clickable titles or identifiers in table: Usually these display the titles of the variables in a table or graph result. (If you select Show by Identifier from the Object menu (or press Control+y) it toggles to show the identifiers instead of titles. If you double-click a title (or identifier) in a table, it will open the Object window for that variable. The values in the self index are actually handles to the variables.

m .. n

Returns a sequence of successive integers from m to n — increasing if n < m, or decreasing if n> m. For example:

2003..2006 → [2003, 2004, 2005, 2006]
5 .. 1 → [5, 4, 3, 2, 1]

It is equivalent to Sequence(m, n).

<tip title="Tip">The parameters n and m must be atoms, that is single numbers. Otherwise, it would result in a non-rectangular array.

Sequence(start, end, stepSize, strict, dateUnit)

Creates a list of numbers increasing or decreasing from start to end by increments (or decrements) of stepSize, which is optional and defaults to 1. When the strict parameter is omitted or false, stepSize must be a positive number and the sequence will decrement by stepSize when end is less than start, guaranteeing at least one element. When strict is specified as true, a positive stepSize increments and negative stepSize returns a decrementing sequence, possibly with zero elements if end would come before start.

The optional dateUnit parameter is used when creating a sequence of dates, with increments in units of Years (dateUnit:'Y'), Months ('M'), Days ('D' or omitted), Weekdays ('WD'<7code>), Hours ('h'), minutes ('m') or seconds ('s').

All parameters must be deterministic scalar numbers, not arrays.

You can also select this function using the Sequence option from the expr menu.

The expression m .. n using the operator ".." is equivalent to Sequence(m, n, 1).

Library

Array

Examples

If end is greater than start, the sequence is increasing:

Sequence(1,5) →

Chapter11 77.jpg

If start is greater than end, the sequence is decreasing:

Sequence(5, 1) → [5, 4, 3, 2, 1]

Unless strict is true:

Sequence(5, 1, strict:true) → []
Sequence(5, 1, -2, strict:true ) → [5, 3, 1]

If start and end are not integers, and you omit stepSize, it rounds them:

Sequence(1.2, 4.8) → [1, 2, 3, 4, 5]

If you specify stepSize, it can create non-integer values:

Sequence(0.5, 2.5, 0.5) → [0.5, 1, 1.5, 2, 2.5]

Concat(i, j)

Returns a list containing the elements of index i concatenated to the elements of index j. Thus the number of items in the result is the sum of the number of items in i and the number of items in j.

Index Year1 := 2006 .. 2008
Index Years2 := 2009 .. 2010
Index YearsAll := Concat(Years1, Years2)
YearsAll → [2006, 2007, 2008, 2009, 2010]

Subset(d)

Returns a list containing all the elements of d’s index for which d’s values are true (that is, non- zero). d must be a one-dimensional array.

The optional parameter position:true can be specified to return the positions along d’s index for which d’s values are true. You would need to use positions if your index might contain duplicate values.

The basic use of Subset does not allow d to contain more than one dimension.

When to use

Use Subset() to create a new index that is a subset of an existing index.

Library

Array

Examples

Subset(YearsAll < 2010) → [2006, 2007, 2008, 2009]
Subset(YearsAll>2007 and YearsAll<2010, position:true) → [3,4]

CopyIndex(i)

Makes a copy of the values of index i, to be assigned to a new index variable, global or local. For example, suppose you want to create a matrix of distances between a set of origins and destinations, which are each the same set of cities:

Index Origins
Definition:= ['London', 'New York', 'Tokyo', 'Paris', 'Delhi']
Index Destinations
Definition:= CopyIndex(Origins)
Variable Flight_times := Table(Origins, Destinations)

If you defined Destinations as equal to Origins, without using Copyindex(), Destinations would be indexed by Origins, and the resulting table would have only one dimension index. By defining Destinations with CopyIndex(), it becomes a separate index, so that the table has two dimensions.

Sortindex(d, i)

Assuming d is an array indexed by i, SortIndex() returns the elements of index i, reordered so that the corresponding values in d would go from smallest to largest value. The result is indexed by i. If d is indexed by dimensions other than i, each “column” is individually sorted, with the resulting sort order being indexed by the extra dimensions. To obtain the sorted array d, use this:

d[i=Sortindex(d, i)]

When d is a one-dimensional array, the index parameter i is optional. When omitted, the result is an unindexed list. Use the one-parameter form only when you want an unindexed result, for example to define an index variable. The one-parameter form does array abstract when a new dimension is added to d.

Library

Array

Examples

Maint_costs →

Car_type ▶
VW Honda BMW
1950 1800 2210

SortIndex(Maint_costs, Car_type) →

Car_type ▶
VW Honda BMW
Honda VW BMW

SortIndex(Maint_costs) →

SortIndex ▶
Honda VW BMV

Define Sorted_cars as an index node:

INDEX Sorted_cars := Sortindex(Maint_costs)

Maint_costs[Car_type = Sorted_cars] →

Honda VW BMW
1800 1950 2210


Unique(a, i)

Returns a maximal subset of i such that each indicated slice of a along i is unique.

The optional parameter position:true returns the positions of element in i, rather than the elements themselves. Specifying caseInsensitive:true ignores differences in upper and lower case in text values when determining if values are unique.

When to use

Use Unique() to remove duplicate slices from an array, or to identify a single member of each equivalence class.

Library

Array

DataSet →

Field ▶
PersonNum LastName FirstName Company
1 Smith Bob Acme
2 Jones John Acme
3 Johnson Bob Floorworks
4 Smith Bob Acme

Unique(DataSet, PersonNum) → [1, 2, 3]

Unique(DataSet[Field='Company'], PersonNum) → [1, 3]

See Also

Creating an index <- Functions that create indexes -> Defining a variable as an edit table

<footer>Creating an index / Functions that create indexes / Defining a variable as an edit table</footer>

Comments


You are not allowed to post comments.