Convolution (Computer Science): History
Please note this is an old version of this entry, which may differ significantly from the current revision.
Subjects: Others

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. This name zip derives from the action of a zipper in that it interleaves two formerly disjoint sequences. The reverse function is unzip which performs a deconvolution.

  • deconvolution
  • convolution
  • tuple

1. Example

Given the three words cat, fish and be where |cat| is 3, |fish| is 4 and |be| is 2. Let [math]\displaystyle{ \ell }[/math] denote the length of the longest word which is fish; [math]\displaystyle{ \ell = 4 }[/math]. The convolution of cat, fish, be is then 4 tuples of elements:

[math]\displaystyle{ (c,f,b)(a,i,e)(t,s,\#)(\#,h,\#) }[/math]

where # is a symbol not in the original alphabet. In Haskell this truncates to shortest sequence [math]\displaystyle{ \underline{\ell} }[/math], where [math]\displaystyle{ \underline{\ell} = 2 }[/math]:

zip3 "cat" "fish" "be" -- [('c','f','b'),('a','i','e')]

2. 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 [math]\displaystyle{ \ell }[/math] denote the length of the longest word, i.e. the maximum of |x|, |y|, |z|, ... .

The convolution of these words is a finite sequence of n-tuples of elements of (Σ ∪ {#}), i.e. an element of [math]\displaystyle{ ((\Sigma\cup\{\# \})^n)^* }[/math]:

[math]\displaystyle{ (x_1,y_1,\ldots)(x_2,y_2,\ldots)\ldots(x_\ell,y_\ell,\ldots) }[/math],

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 xyz ⋆ ...

The inverse to convolution is sometimes denoted unzip.

A variation of the convolution operation is defined by:

[math]\displaystyle{ (x_1,y_1,\ldots)(x_2,y_2,\ldots)\ldots(x_{\underline{\ell}},y_{\underline{\ell}},\ldots) }[/math]

where [math]\displaystyle{ \underline{\ell} }[/math] is the minimum length of the input words. It avoids the use of an adjoined element [math]\displaystyle{ \# }[/math], but destroys information about elements of the input sequences beyond [math]\displaystyle{ \underline{\ell} }[/math].

3. 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 number of lists as argument. An example from Clojure:[1]

;; `nums' contains an infinite list of numbers (0 1 2 3 ...) (def nums (range)) (def tens [10 20 30]) (def firstname "Alice") ;; To convolve (0 1 2 3 ...) and [10 20 30] into a vector, invoke `map vector' on them; same with list (map vector nums tens) ; ⇒ ([0 10] [1 20] [2 30]) (map list nums tens) ; ⇒ ((0 10) (1 20) (2 30)) (map str nums tens) ; ⇒ ("010" "120" "230") ;; `map' truncates to the shortest sequence; note missing \c and \e from "Alice" (map vector nums tens firstname) ; ⇒ ([0 10 \A] [1 20 \l] [2 30 \i]) (map str nums tens firstname) ; ⇒ ("010A" "120l" "230i") ;; To unzip, apply `map vector' or `map list' (apply map list (map vector nums tens firstname)) ;; ⇒ ((0 1 2) (10 20 30) (\A \l \i))

In Common Lisp:

(defparameter nums '(1 2 3)) (defparameter tens '(10 20 30)) (defparameter firstname "Alice") (mapcar #'list nums tens) ;; ⇒ ((1 10) (2 20) (3 30)) (mapcar #'list nums tens (coerce firstname 'list)) ;; ⇒ ((1 10 #\A) (2 20 #\l) (3 30 #\i)) — truncates on shortest list ;; Unzips (apply #'mapcar #'list (mapcar #'list nums tens (coerce firstname 'list))) ;; ⇒ ((1 2 3) (10 20 30) (#\A #\l #\i))

Languages such as Python provide a zip() function, older version (Python 2.*) allowed mapping None over lists to get a similar effect.[2] zip() in conjunction with the * operator unzips a list:[2]

>>> nums = [1, 2, 3] >>> tens = [10, 20, 30] >>> firstname = 'Alice' >>> zipped = zip(nums, tens) >>> zipped [(1, 10), (2, 20), (3, 30)] >>> zip(*zipped) # unzip [(1, 2, 3), (10, 20, 30)] >>> zipped2 = zip(nums, tens, list(firstname)) >>> zipped2 # zip, truncates on shortest [(1, 10, 'A'), (2, 20, 'l'), (3, 30, 'i')] >>> zip(*zipped2) # unzip [(1, 2, 3), (10, 20, 30), ('A', 'l', 'i')] >>> # mapping with `None' doesn't truncate; deprecated in Python 3.* >>> map(None,nums, tens, list(firstname)) [(1, 10, 'A'), (2, 20, 'l'), (3, 30, 'i'), (None, None, 'c'), (None, None, 'e')]

Haskell has a method of convolving sequences but requires a specific function for each arity (zip for two sequences, zip3 for three etc.),[3] similarly the functions unzip and unzip3 are available for unzipping:

-- nums contains an infinite list of numbers [1, 2, 3,...] nums = [1..] tens = [10, 20, 30] firstname = "Alice" zip nums tens -- ⇒ [(1,10),(2,20),(3,30)] — zip, truncates infinite list unzip $ zip nums tens -- ⇒ ([1,2,3],[10,20,30]) — unzip zip3 nums tens firstname -- ⇒ [(1,10,'A'),(2,20,'l'),(3,30,'i')] — zip, truncates unzip3 $ zip3 nums tens firstname -- ⇒ ([1,2,3],[10,20,30],"Ali") — unzip

4. Language Comparison

List of languages by support of convolution:

Zip in various languages
Language Zip Zip 3 lists Zip n lists Notes
Chapel zip (iter1 iter2) zip (iter1 iter2 iter3) zip (iter1 ... itern) The shape of each iterator, the rank and the extents in each dimension, must be identical.[4]
Clojure (map list list1 list2)
(map vector list1 list2)
(map list list1 list2 list3)
(map vector list1 list2 list3)
(map list list1listn)
(map vector list1listn)
Stops after the length of the shortest list.
Common Lisp (mapcar #'list list1 list2) (mapcar #'list list1 list2 list3) (mapcar #'list list1 ... listn) Stops after the length of the shortest list.
D zip(range1,range2)
The stopping policy defaults to shortest and can be optionally provided as shortest, longest, or requiring the same length.[5] The second form is an example of UFCS.
F# list1 list2 source1 source2 array1 array2
List.zip3 list1 list2 list3
Seq.zip3 source1 source2 source3
Array.zip3 array1 array2 array3
Haskell zip list1 list2 zip3 list1 list2 list3 zipn list1listn zipn for n > 3 is available in the module Data.List. Stops after the shortest list ends.
Python zip(list1, list2) zip(list1, list2, list3) zip(list1, …, listn) zip() and map() (3.x) stops after the shortest list ends, whereas map() (2.x) and itertools.zip_longest() (3.x) extends the shorter lists with None items
Ruby, list3), .., listn) When the list being executed upon (list1) is shorter than the lists being zipped the resulting list is the length of list1. If list1 is longer nil values are used to fill the missing values[6]
Scala     If one of the two collections is longer than the other, its remaining elements are ignored. [7]
Unzip in various languages
Language Unzip Unzip 3 tuples Unzip n tuples Notes
Clojure (apply map vector convlist) (apply map vector convlist) (apply map vector convlist)  
Common Lisp (apply #'mapcar #'list convlist) (apply #'mapcar #'list convlist) (apply #'mapcar #'list convlist)  
F# List.unzip list1 list2
Seq.unzip source1 source2
Array.unzip array1 array2
List.unzip3 list1 list2 list3
Seq.unzip3 source1 source2 source3
Array.unzip3 array1 array2 array3
Haskell unzip convlist unzip3 convlist unzipn convlist unzipn for n > 3 is available in the module Data.List.
Python zip(*convvlist) zip(*convvlist) zip(*convvlist)  

The content is sourced from:


  1. map from ClojureDocs
  2. map(function, iterable, ...) from section Built-in Functions from Python v2.7.2 documentation
  3. zip :: [a] -> [b] -> [(a, b)] from Prelude, Basic libraries
This entry is offline, you can click here to edit this entry!
Video Production Service