In computer science, specifically formal languages, convolution (sometimes referred to as zip) is a function which maps a tuple of sequences into a sequence of tuples.
Contents
Example
Given the three words and, fish and be where |and| is 3, |fish| is 4 and |be| is 2. Let
where # is a symbol not in the original alphabet. In Haskell this truncates to shortest sequence
Definition
Let Σ be an alphabet, # a symbol not in Σ.
Let x1x2... x|x|, y1y2... y|y|, z1z2... z|z|, ... be n words (i.e. finite sequences) of elements of Σ. Let
The convolution of these words is a finite sequence of n-tuples of elements of (Σ ∪ {#}), i.e. an element of
where for any index i > |w|, the wi is #.
The convolution of x, y, z, ... is denoted conv( x, y, z, ...), zip( x, y, z, ...) or x ⋆ y ⋆ z ⋆ ...
The inverse to convolution is sometimes denoted unzip.
A variation of the convolution operation is defined by:
where
In programming languages
Convolution functions are often available in programming languages, often referred to as zip. In Lisp-dialects one can simply map the desired function over the desired lists, map is variadic in Lisp so it can take an arbitrary amount of lists as argument. An example from Clojure:
In Common Lisp:
Languages such as Python provide a zip() function, older version (Python 2.*) allowed mapping None over lists to get a similar effect. zip() in conjunction with the * operator unzips a list:
Haskell has a method of convolving sequences but requires a specific function for each arity (zip for two sequences, zip3 for three etc.), similarly the functions unzip and unzip3 are available for unzipping:
Language comparison
List of languages by support of convolution: