wiki:AdvancedArraysCowan

Version 7 (modified by cowan, 6 years ago) (diff)

Removed cruft

This is a continuation of ArraysCowan, put on a separate page for convenience.

Advanced procedures

These procedures are mostly derived in function, and sometimes in name, from ISO/IEC 8485 and ISO 17351, which standardize basic and extended APL respectively.

(array-collapse array j)

Let k be the rank of array. This procedure constructs and returns an array of rank j, which MUST be less than or equal to k, whose components are arrays of rank k - j. The shape of the returned array is equal to the first j components of the shape of array, and the shapes of its subarrays are equal to the remaining k-j components.

(array-explode array j)

Let k be the rank of array. This procedure constructs and returns an array of rank j, which MUST be greater than or equal to k. Each component of array MUST be an array of rank j - k, all of which MUST have the same shape. The shape of the returned array is the shape of array concatenated with the shape of any of its components, and each component is the corresponding component of the corresponding subarray of array.

(array-reshape shape array)

Constructs and returns a new array of shape shape whose components in row-major order are the same (in the sense of eqv?) as the components of array in row-major order. (APL reshape.)

(array-reverse array axis)

Constructs and returns an array with the same shape as array, but whose elements on the specified axis are reversed. Axis must be a non-negative integer less than the rank of array. (APL reverse.)

(array-compress array booleans axis)

Constructs and returns an array with the same shape as array except possibly along the axis dimension. The array is sliced along axis and the elements of booleans (a vector of boolean values) are used to decide which slices to incorporate into the result: if the corresponding boolean is #t, the slice is incorporated, otherwise not. (APL compress.)

(array-expand array booleans nil axis)

Constructs and returns an array with the same shape as array except possibly along the axis dimension. Array is sliced along axis and the elements of booleans (which MUST be a vector of boolean values) are used to decide where, if anywhere, nil (which must have the same shape as a slice) is to be interpolated: if the corresponding boolean is #t, nil is interpolated, otherwise the next slice is incorporated. The size of booleans MUST be equal to the value of the axis dimension in the result. (APL expand.)

(array-rearrange array vector axis)

Constructs and returns an array with the same shape as array. Array is sliced along the axis dimension, and the slices are reassembled in the order given by vector, which MUST be a vector of exact integers. The slice whose number appears in the first element of vector appears first in the result, and so on. (Generalized version of APL rotate.)

(array-rearrange-axes array vector)

Constructs and returns an array whose shape is a permutation of the shape of array. Vector, which MUST be a vector of exact integers, specifies how to permute it. The axis whose number appears in the first element of vector appears as the first axis of the result, and so on. (APL dyadic transpose with integer-valued vector.)

(subarray array start-subscripts end-subscripts)

Constructs and returns a smaller array with the same rank as array whose elements begin at the "lower left" corner specified by the list start-subscripts and end at the "upper right" corner specified by the list end-subscripts. (APL take and drop.)

(array-recursive-ref array subscript ...)

Applies array-ref to the array using the first i subscripts, where i is the rank of array. If there are more subscripts, the result MUST be an array. Apply array-ref to the result using the next j subscripts, where j is the rank of the result. Repeat until there are no more subscripts, returning the last result. (APL enlist.)

Higher-order procedures

These procedures are mostly derived in function, and sometimes in name, from ISO/IEC 8485 and ISO 17351, which standardize basic and extended APL respectively.

(array-reduce proc array axis)

Constructs and returns an array whose rank is one less than the rank of array, by combining all the elements along axis using proc, which MUST be a two-argument procedure. The order and number of invocations of proc is unspecified. If there is only one such element, it is unchanged. (APL reduce.)

(array-reduce-by-groupsproc array axis n)

Constructs and returns an array with the same rank as the rank of array, by combining all the groups of elements of length n along axis using proc, which MUST be a two-argument procedure. The order and number of invocations of proc is unspecified. If there is only one such group of elements, it is unchanged. (APL N-wise reduction.)

(array-scan proc array axis)

Constructs and returns an array whose shape is the same as the shape of array. Each element along axis is constructed by reducing (as if by array-reduce) successive prefixes of the elements of array along that axis. (APL scan.)

(array-outer-product proc array1 array2)

Constructs and returns an array whose shape is the concatenation of the shapes of array1 and array2. Each component of the new array is the result of applying proc to every element of array1 and every element of array2. The order and number of invocations of proc is unspecified. (APL outer product.)

(array-inner-product proc1 proc2 array1 array2)

Constructs and returns an array whose shape is equal to the shape of array1 without its last element concatenated with the shape of array2 without its first element; these elements MUST be numerically equal. It is an error if both arrays have rank 0.

Each element of the result array results from applying proc2 to the corresponding elements of the last vectors of array1 and the first vectors of array2 and then reducing them with proc1 to a single value. The order and number of invocations of the procedures is unspecified.

In particular, if both arrays have rank 1, the last and first vectors are the whole of the arrays, and the result has rank 0; if both arrays have rank 2, the last vectors of array1 are the column-wise vectors, and the first vectors of array2 are the row-wise vectors, and the result has rank 2. (APL inner product.)

Example: (array-inner-product + * #(1 2 3) #(4 5 6)) computes the usual dot product of vector1 and vector2.