Programming Search⌘K

Conceptcore

Array programming

Also called array programming, vector programming, vectorization, broadcasting, APL style, whole-array operations.

Array programming treats a whole array as one value, so operations such as + and max apply to every element at once, and reductions, scans and outer products replace explicit loops.

Applies to
core-languages

Idea

Array programming treats a whole array as one value. An operation such as + or max applies to every element of its arguments at once, and the program has no explicit loop over the indices. Where an imperative program writes for i in range(n): c[i] = a[i] * b[i], an array program writes c = a * b. Higher-order operations such as reduce, scan and outer product turn functions on numbers into functions on arrays, so most computations become short expressions without loops. APL, J, K, BQN, Fortran, MATLAB, Julia and R are array languages, and NumPy makes Python one.

import operator


def pervade(f):
    """Lift a function of two numbers to numbers, lists and nested lists."""

    def g(a, b):
        if isinstance(a, list) and isinstance(b, list):
            if len(a) != len(b):
                raise ValueError(f"length error: {len(a)} and {len(b)}")
            return [g(x, y) for x, y in zip(a, b)]
        if isinstance(a, list):
            return [g(x, b) for x in a]
        if isinstance(b, list):
            return [g(a, y) for y in b]
        return f(a, b)

    return g


add, mul = pervade(operator.add), pervade(operator.mul)

price = [12, 5, 8]
qty = [3, 10, 2]
print(mul(price, qty))       # element by element
print(sum(mul(price, qty)))  # a reduction gives the total
print(add(price, 1))         # the scalar meets every element

table = [[1, 2, 3], [4, 5, 6]]
print(add(table, 10))
print(add(table, [100, 200]))  # one number for each row

try:
    add(price, [1, 2])
except ValueError as e:
    print(e)
Output
[36, 50, 16]
102
[13, 6, 9]
[[11, 12, 13], [14, 15, 16]]
[[101, 102, 103], [204, 205, 206]]
length error: 3 and 2

pervade takes a function of two numbers and gives a function that works on numbers, lists and lists of lists. Two lists of the same length combine element by element, a number combines with each element of a list, and two lists of different lengths are an error. After this one definition, mul(price, qty) and add(table, 10) need no loop. Array languages give every arithmetic function this behavior, and they run the loops in compiled code.

How it works

Arrays, rank and shape. An array is a rectangular collection of values. Its rank is the number of axes, and its shape is the list of the lengths of the axes. A single number is an array of rank 0, a vector has rank 1, a matrix has rank 2, and a batch of color images has rank 4. Because the data is rectangular, an implementation can store it in one block of memory and find any element from its indices with arithmetic.

Scalar functions pervade. Arithmetic, comparison and logic functions work on numbers. An array language extends each of them to arrays: on two arrays of the same shape the function applies to each pair of elements, and a scalar combines with every element of the other argument. A comparison such as a > 2 gives an array of booleans, which is the input to selection and counting.

Agreement and broadcasting. When two arguments have different shapes, the language must state which shapes agree. NumPy uses broadcasting: "The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array so that they have compatible shapes."1 NumPy compares the shapes from the last axis, and two lengths agree when they are equal or when one of them is 1. J and BQN match from the first axis, as the pervade function does: a vector of two numbers adds one number to each row of a matrix with two rows. APL accepts only equal shapes or a single element. Shapes that do not agree are an error, not a silent truncation.

Operators make new functions. APL calls its higher-order functions operators. Reduce puts a function between the elements of an array: +/ gives the sum, ×/ the product and ⌈/ the maximum. Scan gives the running results of a reduction. Outer product applies a function to every pair of elements of two arrays and gives a table, and inner product combines two functions to give, for example, the product of two matrices. Iverson named three ways in which a notation subordinates detail: "the use of arrays, the assignment of names to functions and variables, and the use of operators."2 He added that "functions defined on vectors are extended systematically to arrays of higher rank."2 See higher-order functions.

Selection without loops. A boolean array selects elements: a[a > 2] in NumPy and (a>2)/a in APL keep the elements that pass the test. An array of indices reorders or gathers elements, and a grade function returns the indices that would sort an array. Counting, searching, grouping and sorting become operations on whole arrays.

Speed from vectorization. An interpreter pays its overhead once for each operation, not once for each element, so a * b on a million numbers costs one dispatch and one compiled loop. The NumPy documentation says that "Vectorization describes the absence of any explicit looping, indexing, etc., in the code - these things are taking place, of course, just “behind the scenes” in optimized, pre-compiled C code."3 The compiled loop can use the SIMD instructions of the processor, many cores, or a GPU, because the operation states that the elements are independent.

In languages

LanguageVerdictNote
PythonLibrary
C++Standard library
JavaIdiom
CAbsent
C#Idiom
JavaScriptIdiom
TypeScriptIdiomAs JavaScript; typed arrays and the map, filter and reduce methods have full type definitions.
GoAbsent
RustIdiom
KotlinIdiomArrays and lists have map, zip, filter, fold and scan, and operator overloading lets a library define + on vectors; no example file because kotlinc is not installed on the machine that recorded the examples.
SQLIdiomA query applies its expressions to every row of a table at once, and aggregate functions such as SUM are reductions; array columns exist in some engines.
BashAbsentBash arrays hold strings and have no element-wise arithmetic; a loop or an awk program does the work.
PowerShellIdiomComparison operators filter arrays, so @(3,1,4,1,5) -gt 2 gives 3, 4 and 5, and ForEach-Object applies a script block to each element; no example file because pwsh is not installed on the machine that recorded the examples.
HTMLDoes not applyHTML has no values to compute with.
CSSDoes not applyCSS has no arrays of values to compute with.
Regular expressionsDoes not applyRegular expressions match text and have no arrays of values.
Language Verdict Mechanism
Python library map, sum, comprehensions; NumPy ndarray
JavaScript idiom map, filter, reduce on arrays and typed arrays
TypeScript idiom as JavaScript
Java idiom Arrays.setAll, IntStream, Arrays.parallelPrefix
Kotlin idiom map, zip, fold, scan; operator overloading
C# idiom LINQ Zip, Select, Sum; Vector<T>
Go absent loops and generic helper functions
Rust idiom iterator adaptors; the ndarray crate
C absent loops; vector extensions of the compilers; BLAS
C++ stdlib std::valarray; Eigen and xtensor
Bash absent loops over arrays of strings; awk
PowerShell idiom comparison operators that filter arrays
SQL idiom expressions over all rows, aggregate functions
Regex n/a no arrays of values
HTML n/a no values to compute with
CSS n/a no arrays of values

None of the core languages of this site is an array language. Their operators work on single numbers, and array operations come from higher-order functions such as map, zip and reduce, or from libraries. The C++ standard library is the exception: std::valarray has element-wise operators, masks and slices on one axis.

Libraries carry array programming into general-purpose languages. NumPy gives Python n-dimensional arrays, broadcasting and fast reductions, and the data science stack of Python, from pandas to PyTorch and JAX, builds on the same model. Eigen and xtensor do this for C++, ndarray for Rust, and Gonum for Go. In these libraries, operator overloading lets a * b + c read as it does in an array language.

Fortran, MATLAB, Julia and R have array operations in the core language. In Fortran, c = a * b on two arrays multiplies the elements, and array sections such as a(1:n:2) select every second element. MATLAB and R treat every number as an array. Julia uses a dot to apply any function element-wise, as in sin.(x) .+ 1, and fuses the dotted calls of one expression into a single loop.

PythonGitHub
from itertools import accumulate
import operator

a = [3, 1, 4, 1, 5]
b = [2, 7, 1, 8, 2]

print(list(map(operator.add, a, b)))       # element-wise sum
print(sum(map(operator.mul, a, b)))        # dot product
print(list(accumulate(a)))                 # scan: running totals
print(max(a), [x for x in a if x > 2])     # reduce, and compress by a mask
print([[x * y for y in range(1, 4)] for x in range(1, 4)])  # outer product
Output
[5, 8, 5, 9, 7]
35
[3, 4, 8, 9, 14]
5 [3, 4, 5]
[[1, 2, 3], [2, 4, 6], [3, 6, 9]]

This code runs but gives a wrong answer on purpose.

a = [1, 2, 3]
print(a * 2)
print(a + [10, 20, 30])
Output
[1, 2, 3, 1, 2, 3]
[1, 2, 3, 10, 20, 30]
  • Verdict: library. The core language has no operations on whole arrays. map with two lists, sum, itertools.accumulate and comprehensions give element-wise operations, reductions, scans and outer products, one Python call per element. NumPy is the library that makes Python an array language: its ndarray type has element-wise operators, broadcasting and reductions that run as compiled loops.
  • The wrong block treats a list as an array of numbers. For a list, * repeats the list and + joins two lists. NumPy arrays give [2, 4, 6] and [11, 22, 33] for the same expressions.
#include <iostream>
#include <valarray>

void show(const char* name, const std::valarray<int>& v) {
    std::cout << name << ":";
    for (int x : v) std::cout << " " << x;
    std::cout << "\n";
}

int main() {
    std::valarray<int> a{3, 1, 4, 1, 5};
    std::valarray<int> b{2, 7, 1, 8, 2};

    show("a + b", a + b);                  // element-wise
    show("a * 10", a * 10);                // a scalar meets every element
    std::cout << "dot: " << (a * b).sum() << "\n";
    show("a[a > 2]", a[a > 2]);            // a boolean mask selects elements
    show("every 2nd", a[std::slice(0, 3, 2)]);
}
Output
a + b: 5 8 5 9 7
a * 10: 30 10 40 10 50
dot: 35
a[a > 2]: 3 4 5
every 2nd: 3 4 5
  • Verdict: stdlib. std::valarray has element-wise operators, operators between an array and a scalar, reductions such as sum and max, boolean masks and slices, which is a small array language in the standard library. Few programs use it. It has only one axis, so a matrix needs index arithmetic or std::gslice. Libraries such as Eigen and xtensor give matrices and n-dimensional arrays, and use expression templates to fuse operations into one loop.
JavaGitHub
import java.util.Arrays;
import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        int[] a = {3, 1, 4, 1, 5};
        int[] b = {2, 7, 1, 8, 2};

        int[] sum = new int[a.length];
        Arrays.setAll(sum, i -> a[i] + b[i]);               // element-wise
        int dot = IntStream.range(0, a.length).map(i -> a[i] * b[i]).sum();
        int[] scan = a.clone();
        Arrays.parallelPrefix(scan, Integer::sum);          // running totals
        int[] big = Arrays.stream(a).filter(x -> x > 2).toArray();

        System.out.println(Arrays.toString(sum) + " " + dot);
        System.out.println(Arrays.toString(scan) + " " + Arrays.toString(big));
    }
}
Output
[5, 8, 5, 9, 7] 35
[3, 4, 8, 9, 14] [3, 4, 5]
  • Verdict: idiom. Arrays.setAll fills an array from a function of the index, IntStream gives reductions, and Arrays.parallelPrefix gives a scan that can use more than one core. The operators do not work on arrays. The Vector API, an incubator module of the JDK, gives explicit SIMD operations on short vectors.
#include <stdio.h>

#define N 5

int main(void) {
    int a[N] = {3, 1, 4, 1, 5};
    int b[N] = {2, 7, 1, 8, 2};
    int sum[N], dot = 0, scan[N], run = 0;

    for (int i = 0; i < N; i++) {
        sum[i] = a[i] + b[i]; /* element-wise */
        dot += a[i] * b[i];   /* reduction */
        run += a[i];
        scan[i] = run;        /* running total */
    }
    for (int i = 0; i < N; i++) printf("%d ", sum[i]);
    printf("| %d |", dot);
    for (int i = 0; i < N; i++) printf(" %d", scan[i]);
    printf("\n");
    return 0;
}
Output
5 8 5 9 7 | 35 | 3 4 8 9 14
  • Verdict: absent. C has no operations on whole arrays: an array name in an expression becomes a pointer to the first element, and every operation is a loop over the indices. Compilers vectorize simple loops of this kind, and GCC and Clang have vector types as an extension. BLAS and LAPACK are the libraries that give array operations to C and Fortran programs.
using System;
using System.Linq;

int[] a = { 3, 1, 4, 1, 5 };
int[] b = { 2, 7, 1, 8, 2 };

int[] sum = a.Zip(b, (x, y) => x + y).ToArray();      // element-wise
int dot = a.Zip(b, (x, y) => x * y).Sum();            // reduction
int[] big = a.Where(x => x > 2).ToArray();            // compress by a test
int[][] table = Enumerable.Range(1, 3)
    .Select(x => Enumerable.Range(1, 3).Select(y => x * y).ToArray())
    .ToArray();                                       // outer product

Console.WriteLine($"{string.Join(" ", sum)} | {dot} | {string.Join(" ", big)}");
foreach (int[] row in table) Console.WriteLine(string.Join(" ", row));
Output
5 8 5 9 7 | 35 | 3 4 5
1 2 3
2 4 6
3 6 9
  • Verdict: idiom. LINQ's Zip, Select, Where and Sum give element-wise operations, compression and reductions over any sequence. For numeric work, System.Numerics.Vector<T> gives SIMD operations whose width matches the processor, and the System.Numerics.Tensors package gives operations on whole spans of numbers.
JavaScriptGitHub
const a = Float64Array.from([3, 1, 4, 1, 5]);
const b = Float64Array.from([2, 7, 1, 8, 2]);

const sum = a.map((x, i) => x + b[i]);           // element-wise
const dot = a.reduce((acc, x, i) => acc + x * b[i], 0);
const scaled = a.map((x) => x * 10);              // scalar extension
const big = a.filter((x) => x > 2);               // compress by a test

console.log(Array.from(sum), dot, Array.from(scaled), Array.from(big));
Output
[ 5, 8, 5, 9, 7 ] 35 [ 30, 10, 40, 10, 50 ] [ 3, 4, 5 ]

This code runs but gives a wrong answer on purpose.

const a = [1, 2, 3];
const b = [10, 20, 30];
console.log(a + b);
console.log(a * 2);
Output
1,2,310,20,30
NaN
  • Verdict: idiom. Arrays and typed arrays have map, filter and reduce, which take a function and apply it to each element, but the arithmetic operators do not work on arrays. A Float64Array stores the numbers in one block of memory, which is the layout that array libraries and WebGPU use.
  • The wrong block uses + and * on arrays. JavaScript converts each array to a string, so + joins two strings and * gives NaN.
package main

import "fmt"

type Number interface{ ~int | ~float64 }

// ZipWith applies f to each pair of elements. The lengths must match.
func ZipWith[T Number](f func(T, T) T, a, b []T) []T {
	if len(a) != len(b) {
		panic(fmt.Sprintf("length error: %d and %d", len(a), len(b)))
	}
	out := make([]T, len(a))
	for i := range a {
		out[i] = f(a[i], b[i])
	}
	return out
}

func Reduce[T Number](f func(T, T) T, start T, a []T) T {
	acc := start
	for _, x := range a {
		acc = f(acc, x)
	}
	return acc
}

func main() {
	a := []int{3, 1, 4, 1, 5}
	b := []int{2, 7, 1, 8, 2}
	add := func(x, y int) int { return x + y }
	mul := func(x, y int) int { return x * y }

	fmt.Println(ZipWith(add, a, b))
	fmt.Println(Reduce(add, 0, ZipWith(mul, a, b)))
}
Output
[5 8 5 9 7]
35
  • Verdict: absent. Go has no operations on whole arrays or slices, and no map or reduce in the standard library. Loops are the normal style, and generic functions such as ZipWith give the array vocabulary once. The gc compiler does little automatic vectorization of loops, so libraries such as Gonum use assembly for their fast paths.
RustGitHub
fn main() {
    let a = [3, 1, 4, 1, 5];
    let b = [2, 7, 1, 8, 2];

    let sum: Vec<i32> = a.iter().zip(&b).map(|(x, y)| x + y).collect();
    let dot: i32 = a.iter().zip(&b).map(|(x, y)| x * y).sum();
    let scaled = a.map(|x| x * 10); // a fixed-size array maps to an array
    let scan: Vec<i32> = a
        .iter()
        .scan(0, |acc, &x| {
            *acc += x;
            Some(*acc)
        })
        .collect();

    println!("{sum:?} {dot} {scaled:?}");
    println!("{scan:?}");
}
Output
[5, 8, 5, 9, 7] 35 [30, 10, 40, 10, 50]
[3, 4, 8, 9, 14]
  • Verdict: idiom. Iterator adaptors such as zip, map, scan and sum express whole-array operations, and the compiler turns simple chains into vectorized loops. The arithmetic operators do not work on arrays. The ndarray crate gives n-dimensional arrays with broadcasting, in the style of NumPy.
  • The APL family. APL, J, K, q, BQN and Uiua write programs as short expressions of primitive functions and operators, each usually one symbol or one letter. They differ in their symbols, in how arrays nest, and in how functions apply to the axes of an array.
  • Numeric array languages. Fortran, MATLAB, Julia and R are built for scientific and statistical computing, with matrices and vectors as their main values.
  • Tensor libraries. PyTorch, JAX and TensorFlow add automatic differentiation and GPU execution to the NumPy model. A tensor is an array of any rank.
  • Data frames and columnar databases. A data frame is a table of named columns, each an array. Columnar databases such as kdb+, which uses the q language, run queries as array operations over whole columns.
  • Data parallelism. Each element-wise operation is a parallel loop with no dependencies between iterations, so array programs map well to SIMD units and GPUs. NESL and the data-parallel Haskell projects studied nested data parallelism.
  • Tacit programming. J and APL let a programmer write functions without naming their arguments, by combining functions with trains and operators. This is the point-free style of functional programming.
  • Rank polymorphism. One definition works on arrays of any rank, because the language applies it to the cells of the right rank. J's rank operator makes this explicit.
  • Declarative style. An array expression states what to compute over a whole collection and leaves the order of the work to the implementation. See declarative programming.

Pitfalls

  • Broadcasting by accident. Adding an array of shape (3,) to an array of shape (3, 1) in NumPy gives a 3 by 3 table, not an error. Check the shapes, or reshape the arguments with intent.
  • Lists are not arrays. In Python [1, 2, 3] * 2 repeats the list, and in JavaScript [1, 2] + [3, 4] joins two strings. The wrong blocks of the Python and JavaScript files show both.
  • Loops over array elements. A Python loop that reads one NumPy element at a time pays the interpreter cost for each element, and is often slower than a plain list. Express the work as whole-array operations.
  • Large temporaries. a * b + c makes a temporary array for a * b before it adds c. On large arrays the temporaries use memory and memory bandwidth. Use operations in place, or a library that fuses expressions.
  • Views and copies. A NumPy slice is a view of the original data, so writing to the slice changes the original, while indexing with a mask gives a copy. Know which operations share memory.
  • Fixed-width overflow. Arrays of 32-bit or 64-bit integers wrap around on overflow with no error, unlike Python integers. Choose the element type for the range of the data.
  • Dense code. A line of APL or NumPy can do the work of a page of loops. Name the intermediate results and comment the shapes, so that a reader can follow the data.

Cost

An element-wise operation on arrays of n elements takes O(n) time and makes a result of n elements. A reduction takes O(n) time and a scan O(n) time and memory. An outer product of arrays of m and n elements takes O(mn) time and memory, which is the main way an array program uses more memory than the loop it replaces. The constant factor is small: the interpreter pays once for each operation, and the inner loop runs as compiled code over contiguous memory, often with SIMD instructions. A chain of operations reads and writes memory once for each step, unless the implementation fuses the steps into one loop, as Julia's dot syntax, expression templates in C++ and tracing compilers such as JAX do.

History

Kenneth E. Iverson developed a mathematical notation for algorithms at Harvard in the late 1950s and published it in his 1962 book A Programming Language. At IBM, Iverson, Adin Falkoff and others implemented it as APL\360, which ran on time-sharing systems from 1966. Iverson wrote that APL "originated in an attempt to provide clear and precise expression in writing and teaching, and which was implemented as a programming language only after several years of use and development".2 He received the Turing Award in 1979, and his lecture Notation as a Tool of Thought argued that programming languages are "executable and unambiguous" and so can be tools of thought, as mathematical notation is.2

Cleve Moler wrote the first MATLAB in the late 1970s as an interactive interface to the LINPACK and EISPACK libraries. Iverson and Roger Hui released J, a successor of APL that uses ASCII characters, in 1990, and Arthur Whitney released K in 1993. Fortran 90 added array operations and array sections to Fortran in 1991. In Python, Jim Hugunin wrote the Numeric package in 1995, and Travis Oliphant merged Numeric and Numarray into NumPy in 2006. Deep learning made array programming common: PyTorch, TensorFlow and JAX are array languages embedded in Python, and their arrays run on GPUs.

References

  1. NumPy user guide: Broadcasting, Broadcasting
  2. Kenneth E. Iverson, Notation as a Tool of Thought, 1979 ACM Turing Award Lecture, Introduction; 1.3 Subordination of Detail
  3. NumPy user guide: What is NumPy?, Why is NumPy fast?

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/QA48DE/text.yaml