Array-reducing functions
An array-reducing function operates across a dimension of an array and returns a result that has one dimension less than the number of dimensions of its input array. When applied to an array of n
dimensions, a reducing function produces an array that contains n-1
dimensions. Examples include, Sum(x, i), Product(x,i), Max(x, i), Min(x, i), and others described below. The subscript construct x[i = v] and related subscript and slice functions also reduce arrays by a dimension (see Subscript and slice of a subarray).
The function Sum(x, i) illustrates some properties of reducing functions.
Examples
Sum(Car_prices, Car_type) →
Years ▶ | ||||
---|---|---|---|---|
2005 | 2006 | 2007 | 2008 | 2009 |
59K | 62K | 66K | 71K | 76K |
Sum(Car_prices, Years) →
Car_type ▶ | ||
---|---|---|
VW | Honda | BMW |
99K | 103K | 141K |
Sum(Sum(Car_prices, Years), Car_type) → 334K
See Array Function Example Variables for example array variables used here and below.
Reducing over an unused index: If the index, i, is not a dimension of x, Sum(x, i) returns x unreduced (i.e., with the same number of indexes), but multiplied by the size (number of elements) of i. The reason is that if x is not indexed by i, it means that it has the same value for all values of i. This is true even if x is an atom with no dimensions:
Variable x := 5 Sum(x, Car_type) → 15
This is because Car_type
has three elements (3 x 5 = 15). For Product
:
Product(x, Car_type) → 125
That is, it multiplies x
three times (53 = 125).
In this way, if we later decide to change the value for x for each value of Car_type
, we can redefine x as an edit table indexed by Car_type
. Any expression containing a Sum() or other reducing function on x works correctly whether it is indexed by Car_type
or not.
Elements that are ignored: The array-reducing functions described in this section ignore elements of an array that have the special value Null. For example, the Average(x,i) function sums all the non-null elements of x and divide by the number of elements that are not null.
When a NaN value (signifying an indeterminate number) appears as an element of an array, the
result of the function that operates on the array will usually be NaN as well. NaN values result
from indeterminate operations such as 0/0
, and the fact that they propagate forward in this fashion helps ensure that you will not accidentally compute an indeterminate result without realizing it. However, in some cases you might wish to ignore NaN values in an array-reducing operation. The array-reducing functions Sum, Product, Average, Min, and Max all accept an optional parameter, ignoreNaN that can be set to True. IgnoreNan requires a named-parameter syntax, for example:
Max(x,i,ignoreNaN:True)
When you operate over an array containing some text and some numeric values, the Sum, Min and Max functions can be instructed to ignore all the non-numeric values using an optional ignoreNonNumbers parameter, for example:
Max(x,i,ignoreNonNumbers:True)
Enable comment auto-refresher