 zip - Maple Programming Help

# Online Help

###### All Products    Maple    MapleSim

zip

zip together two lists, matrices, or vectors

 Calling Sequence zip(f, u, v) zip(f, u, v, d) zip[dense]( ... ) zip[sparse]( ... )

Parameters

 f - binary function u, v - lists, rtables, Arrays, Matrices, Vectors, or scalars d - (optional) value

Description

 • The function zip applies the binary function f to the components of two data sets u and v, creating a new data set $r$ defined as follows.  If m is the length of u and n is the length of v then $r$ is a data set of length $\mathrm{min}\left(m,n\right)$ with ${r}_{i}=f\left({u}_{i},{v}_{i}\right)$ for i in $1..\mathrm{min}\left(m,n\right)$.
 • If u and v are Matrices of the same size, then zip applies the binary function f to the pairs of corresponding entries of the matrices u and v. The $i,j$-entry of the resulting matrix is $f\left({u}_{i,j},{v}_{i,j}\right)$.
 • If u and v are not both rtables, Arrays, or lists (ie. both are scalars) then $f\left(u,v\right)$ is returned. If only u is a scalar then $\mathrm{map2}\left(f,u,v\right)$ is computed. If only v is a scalar then $\mathrm{map}\left(f,u,v\right)$ is returned. In other words, zipping a matrix against a scalar is the same as zipping against a same-size matrix filled with that scalar at each entry.
 • If the optional fourth argument d is given, it is used as a default value for f when one of the data sets is shorter than the other. Thus $r$ is a data set of length $\mathrm{max}\left(m,n\right)$ with ${r}_{i}=f\left({u}_{i},{v}_{i}\right)$ for i in $1..\mathrm{min}\left(m,n\right)$ and ${r}_{i}=f\left({t}_{i},d\right)$ for $i$ in $1+\mathrm{min}\left(m,n\right)..\mathrm{max}\left(m,n\right)$ where $t$ is the longer of u and v.
 • If u and v are Matrices of the same size, then the optional fourth argument is accepted, but ignored.  Similarly, if either u or v are not matrices then the fourth argument is also ignored.
 • If u and v are not Matrices of the same size, then the size of the result is determined by the presence or absence of the optional fourth parameter. If this parameter is omitted in the calling sequence then the result object's dimensions are determined by taking the minimum of the corresponding dimensions of u and v.
 • The sparse and dense options control how zip is applied to zero data in sparse storage Arrays, Vectors, and Matrices.  The zip[sparse] command will apply f for each matched element in the array provided they are not both zero.  This scans the actual stored entries (zeros are not stored in sparse arrays).  The zip[dense] command will apply f for every matched element in the result index space, as if both input arrays were dense. By default sparse Array inputs, where f(0,0) is non-zero will have the zip[dense] algorithm applied.  Sparse Matrix and Vector inputs will have the zip[sparse] algorithm applied by default.  The zip[sparse] algorithm is used by default when zipping a sparse matrix against a scalar.
 • An in-place version of zip is available for Vectors and Matrices, via the LinearAlgebra[Zip] routine.

Examples

 > $\mathrm{zip}\left(\left(x,y\right)↦x+y,\left[1,2,3\right],\left[4,5,6\right]\right)$
 $\left[{5}{,}{7}{,}{9}\right]$ (1)
 > $\mathrm{zip}\left(\mathrm{gcd},\left[0,14,8\right],\left[2,6,12\right]\right)$
 $\left[{2}{,}{2}{,}{4}\right]$ (2)
 > $\mathrm{zip}\left(\mathrm{cat},\left["trap","hill"\right],\left["door","side"\right]\right)$
 $\left[{"trapdoor"}{,}{"hillside"}\right]$ (3)
 > $\mathrm{zip}\left(\mathrm{>},\left[x,y\right],\left[4,-2\right]\right)$
 $\left[{4}{<}{x}{,}{-2}{<}{y}\right]$ (4)
 > $\mathrm{zip}\left(\mathrm{op},\left[2,1\right],\left[{x}^{2}+3,{x}^{2}+3\right]\right)$
 $\left[{3}{,}{{x}}^{{2}}\right]$ (5)
 > $\mathrm{zip}\left(\left(x,y\right)↦x+y,\left[1,2,3\right],\left[4,5\right],0\right)$
 $\left[{5}{,}{7}{,}{3}\right]$ (6)
 > $a≔\mathrm{Matrix}\left(2,3,\left[1,2,3,4,5,6\right]\right):$
 > $b≔\mathrm{Matrix}\left(2,3,\left[1,3,5,7,9,2\right]\right):$
 > $\mathrm{zip}\left(\left(x,y\right)↦x+y,a,b\right)$
 $\left[\begin{array}{ccc}{2}& {5}& {8}\\ {11}& {14}& {8}\end{array}\right]$ (7)
 > $\mathrm{zip}\left(\mathrm{+},a,b\right)$
 $\left[\begin{array}{ccc}{2}& {5}& {8}\\ {11}& {14}& {8}\end{array}\right]$ (8)
 > $a≔\mathrm{Matrix}\left(3,4,\left[2,3,5,7,11,13,17,19,23,29,31,37\right]\right):$
 > $b≔\mathrm{Matrix}\left(2,2,\mathrm{shape}=\mathrm{antisymmetric}\right)$
 ${b}{≔}\left[\begin{array}{cc}{0}& {0}\\ {0}& {0}\end{array}\right]$ (9)
 > $b\left[1,2\right]≔3$
 ${{b}}_{{1}{,}{2}}{≔}{3}$ (10)
 > $\mathrm{zip}\left(\left(x,y\right)↦x+y,a,b\right)$
 $\left[\begin{array}{cc}{2}& {6}\\ {8}& {13}\end{array}\right]$ (11)
 > $\mathrm{zip}\left(\left(x,y\right)↦x+y,b,a,-1\right)$
 $\left[\begin{array}{cccc}{2}& {6}& {4}& {6}\\ {8}& {13}& {16}& {18}\\ {22}& {28}& {30}& {36}\end{array}\right]$ (12)
 > $\mathrm{zip}\left(\mathrm{+},a,100\right)$
 $\left[\begin{array}{cccc}{102}& {103}& {105}& {107}\\ {111}& {113}& {117}& {119}\\ {123}& {129}& {131}& {137}\end{array}\right]$ (13)
 > $c≔\mathrm{Array}\left(\left[\left[1,0\right],\left[0,1\right]\right]\right):$
 > $d≔\mathrm{Matrix}\left(2,2,\left[3,0,2,5\right]\right):$
 > $\mathrm{zip}\left(\left(x,y\right)↦y\cdot x,c,d\right)$
 $\left[\begin{array}{cc}{3}& {0}\\ {0}& {5}\end{array}\right]$ (14)
 > $f≔\mathrm{Vector}\left(3,5\right)$
 ${f}{≔}\left[\begin{array}{c}{5}\\ {5}\\ {5}\end{array}\right]$ (15)
 > $g≔\mathrm{Vector}\left[\mathrm{row}\right]\left(\left[1,2\right]\right)$
 ${g}{≔}\left[\begin{array}{cc}{1}& {2}\end{array}\right]$ (16)
 > $\mathrm{zip}\left(\left(x,y\right)↦x-y,f,g,10\right)$
 $\left[\begin{array}{ccc}{4}& {3}& {-5}\end{array}\right]$ (17)

To use the list constructor as the function to apply, enter back quotes around [].

 > $\mathrm{zip}\left(\mathrm{\left[\right]},\left[1,2,3\right],\left[4,5,6\right]\right)$
 $\left[\left[{1}{,}{4}\right]{,}\left[{2}{,}{5}\right]{,}\left[{3}{,}{6}\right]\right]$ (18)

 See Also