Matrico Module III
matrico
’s matrico
Module (Part III)
Series:
In this third part of the series of posts,
I will describe the various map and reduce specializations provided by the matrico
module.
These are again contained in the src/mx.scm
Scheme source code file,
that is included into the module’s main file matrico.scm
.
Map and Reduce
Map and reduce are two of the fundamental operations in functional programming: The map function applies a given unary function to each element in a container, such as a list, vector or matrix. The reduce (aka fold) function applies a given binary function to an accumulator and sequentially each element in a container, whereas this function’s return value becomes the accumulator for the next evaluation. These concepts of map and reduce have application beyond functional programming, such as in parallel programming, and was mainstream popularized by the MapReduce programming model.
In matrico
’s matrix functor, map and fold functions for matrico
’s matrix type are provided,
which are specialized for typical numerical operations on flonum
matrices in the matrio
module.
These are summarized in three sections:
first the binary expanders, second the unary mappers, and lastly the reducers.
Expanders
Expanders refer in this context to binary map operations, which allow automatic broadcasting (aka implicit expansion). This means the two matrix arguments do not need to be of the same dimension, rather they have to be compatible dimensions, leading to the following five options:
- Both
matrix
arguments have the same number of rows and columns. The binary map function is applied to the arguments’ respective entries with the same row and column index.⎡XXX⎤ ⎡XXX⎤ ⎡XXX⎤ ⎢XXX⎥ o ⎢XXX⎥ = ⎢XXX⎥ ⎣XXX⎦ ⎣XXX⎦ ⎣XXX⎦
- One
matrix
argument is a column matrix, matching the column number of the other argument. The binary map function is applied as if the column matrix argument had as many columns as the othermatrix
arguments with all the same columns.⎡XXX⎤ ⎡X⎤ ⎡XXX⎤ ⎡X⎤ ⎡XXX⎤ ⎡XXX⎤ ⎢XXX⎥ o ⎢X⎥ = ⎢XXX⎥ , ⎢X⎥ o ⎢XXX⎥ = ⎢XXX⎥ ⎣XXX⎦ ⎣X⎦ ⎣XXX⎦ ⎣X⎦ ⎣XXX⎦ ⎣XXX⎦
- One
matrix
argument is a row matrix, matching the row number of the other argument. The binary map function is applied as if the row matrix argument had as many rows as the othermatrix
arguments with all the same rows.⎡XXX⎤ [XXX] ⎡XXX⎤ [XXX] ⎡XXX⎤ ⎡XXX⎤ ⎢XXX⎥ o = ⎢XXX⎥ , o ⎢XXX⎥ = ⎢XXX⎥ ⎣XXX⎦ ⎣XXX⎦ ⎣XXX⎦ ⎣XXX⎦
- One
matrix
argument is a row matrix, the other is a column matrix. The binary map function is applied as if the row matrix argument had as many rows as the column matrix arguments with all the same rows, and the column matrix argument had as many columns as the row matrix arguments with all the same columns.⎡X⎤ [XXX] ⎡XXX⎤ [XXX] ⎡X⎤ ⎡XXX⎤ ⎢X⎥ o = ⎢XXX⎥ , o ⎢X⎥ = ⎢XXX⎥ ⎣X⎦ ⎣XXX⎦ ⎣X⎦ ⎣XXX⎦
- One
matrix
argument is scalar. The binary map function is applied as if the scalar matrix orflonum
had as many rows and columns as the othermatrix
argument all the same entries.⎡XXX⎤ ⎡XXX⎤ ⎡XXX⎤ ⎡XXX⎤ ⎢XXX⎥ o X = ⎢XXX⎥ , X o ⎢XXX⎥ = ⎢XXX⎥ ⎣XXX⎦ ⎣XXX⎦ ⎣XXX⎦ ⎣XXX⎦
Basic Arithmetic
(mx+ x y)
- Returnsmatrix
of entry-wise sums of argumentmatrix
es entries.(mx* x y)
- Returnsmatrix
of entry-wise differences of argumentmatrix
es entries.(mx- x y)
- Returnsmatrix
of entry-wise products of argumentmatrix
es entries.(mx/ x y)
- Returnsmatrix
of entry-wise divisions of argumentmatrix
es entries.
Advanced Functions
(mx^ x y)
- Returnsmatrix
of entry-wise exponentiation of basematrix
argument to exponentmatrix
argument.(mx-where pred x y)
- Returnsmatrix
of entries of first or secondmatrix
argument, depending on binary predicatefunction
argument evaluated on respectivematrix
arguments’ entries.(mx-axpy a x y)
- Returnsmatrix
of entry-wise sums of firstmatrix
argument, scaled byflonum
argument, with secondmatrix
argument.
Mappers
Mappers describe an entry-wise processing of a matrix. The unary map function is applied to each entry yielding a result matrix of the same dimensions.
⎡XXX⎤ ⎡XXX⎤
⎢XXX⎥ -> ⎢XXX⎥
⎣XXX⎦ ⎣XXX⎦
Elementary Functions
(mx- x)
- Returnsmatrix
of entry-wise negation of argumentmatrix
es entries.(mx/ x)
- Returnsmatrix
of entry-wise reciprocal of argumentmatrix
es entries.(mx*2 x)
- Returnsmatrix
of entry-wise double of argumentmatrix
es entries.(mx^2 x)
- Returnsmatrix
of entry-wise square of argumentmatrix
es entries.
Rounding Functions
(mx-round mat)
- Returnsmatrix
of entry-wise roundings of argumentmatrix
.(mx-floor mat)
- Returnsmatrix
of entry-wise greatest integer less than or equal to argumentmatrix
.(mx-ceil mat)
- Returnsmatrix
of entry-wise least integer greater than or equal to argumentmatrix
.
Generalized Functions
(mx-abs mat)
- Returnsmatrix
being entry-wise absolute value of argumentmatrix
.(mx-sign mat)
- Returnsmatrix
being entry-wise sign of argumentmatrix
.(mx-delta mat)
- Returnsmatrix
being entry-wise Kronecker delta of argumentmatrix
.(mx-heaviside mat)
- Returnsmatrix
being entry-wise Heaviside step of argumentmatrix
.
Trigonometric Functions
(mx-sin mat)
- Returnsmatrix
being entry-wise sine of argumentmatrix
.(mx-cos mat)
- Returnsmatrix
being entry-wise cosine of argumentmatrix
.(mx-tan mat)
- Returnsmatrix
being entry-wise tangent of argumentmatrix
.
Arcus Functions (Inverse Trigonometric Functions)
(mx-asin mat)
- Returnsmatrix
being entry-wise arcsine of argumentmatrix
.(mx-acos mat)
- Returnsmatrix
being entry-wise arccosine of argumentmatrix
.(mx-atan mat)
- Returnsmatrix
being entry-wise arctangent of argumentmatrix
.
Hyperbolic Functions
(mx-sinh mat)
- Returnsmatrix
being entry-wise hyperbolic sine of argumentmatrix
.(mx-cosh mat)
- Returnsmatrix
being entry-wise hyperbolic cosine of argumentmatrix
.(mx-tanh mat)
- Returnsmatrix
being entry-wise hyperbolic tangent of argumentmatrix
.
Area Functions (Inverse Hyperbolic Functions)
(mx-asinh mat)
- Returnsmatrix
being entry-wise area hyperbolic sine of argumentmatrix
.(mx-acosh mat)
- Returnsmatrix
being entry-wise area hyperbolic cosine of argumentmatrix
.(mx-atanh mat)
- Returnsmatrix
being entry-wise area hyperbolic tangent of argumentmatrix
.
Haversed Trigonometric Functions
(mx-hsin mat)
- Returnsmatrix
being entry-wise haversine of argumentmatrix
.(mx-hcos mat)
- Returnsmatrix
being entry-wise havercosine of argumentmatrix
.
Logarithmic Hyperbolic Functions
(mx-lnsinh mat)
- Returnsmatrix
being entry-wise logarithmic hyperbolic sine of argumentmatrix
.(mx-lncosh mat)
- Returnsmatrix
being entry-wise logarithmic hyperbolic cosine of argumentmatrix
.
Squareroots
(mx-sqrt mat)
- Returnsmatrix
being entry-wise squareroot of argumentmatrix
.(mx-signsqrt mat)
- Returnsmatrix
being entry-wise sign times squareroot of absolute value of argumentmatrix
.
Logarithms
(mx-ln mat)
- Returnsmatrix
being entry-wise natural logarithm of argumentmatrix
.(mx-lb mat)
- Returnsmatrix
being entry-wise common logarithm of argumentmatrix
.(mx-lg mat)
- Returnsmatrix
being entry-wise binary logarithm of argumentmatrix
.
Exponentials
(mx-exp mat)
- Returnsmatrix
being entry-wise exponential of argumentmatrix
.(mx-gauss mat)
- Returnsmatrix
being entry-wise Gaussian of argumentmatrix
.
Special Functions
(mx-sinc mat)
- Returnsmatrix
being entry-wise cardinale sine of argumentmatrix
.(mx-sigm mat)
- Returnsmatrix
being entry-wise sigmoid of argumentmatrix
.(mx-stirling mat)
- Returnsmatrix
being entry-wise Stirling approximation of argumentmatrix
.
Reducers
Reducers compose a matrix’s entries yielding a matrix
of lower dimension.
There are three variants:
- Composition of row entries yielding a column
matrix
by fold ing each row into a scalar.⎡XXX⎤ ⎡X⎤ ⎢XXX⎥ -> ⎢X⎥ ⎣XXX⎦ ⎣X⎦
- Composition of column entries yielding a row
matrix
by fold ing each column into a scalar.⎡XXX⎤ [XXX] ⎢XXX⎥ -> ⎣XXX⎦
- Composition of all entries yielding a
flonum
by fold ing all entries into a scalar.⎡XXX⎤ ⎢XXX⎥ -> X ⎣XXX⎦
Sums
(mx-rowsum mat)
- Returns columnmatrix
of row sums of argumentmatrix
.(mx-colsum mat)
- Returns rowmatrix
of column sums of argumentmatrix
.(mx-sum mat)
- Returnsflonum
sum of allmatrix
argument’s entries.
Products
(mx-rowprod mat)
- Returns columnmatrix
of row products of argumentmatrix
.(mx-colprod mat)
- Returns rowmatrix
of column products of argumentmatrix
.(mx-prod mat)
- Returnsflonum
product of allmatrix
argument’s etnries.
Minima
(mx-rowmin mat)
- Returns columnmatrix
of row minima of argumentmatrix
.(mx-colmin mat)
- Returns rowmatrix
of column minima of argumentmatrix
.(mx-min mat)
- Returnsflonum
minimum over allmatrix
argument’s entries.
Maxima
(mx-rowmax mat)
- Returns columnmatrix
of row maxima of argumentmatrix
.(mx-colmax mat)
- Returns rowmatrix
of column maxima of argumentmatrix
.(mx-max mat)
- Returnsflonum
maximum over allmatrix
argument’s entries.
Midrange
(mx-rowmidr mat)
- Returns columnmatrix
of row midranges of argumentmatrix
.(mx-colmidr mat)
- Returns rowmatrix
of column midranges of argumentmatrix
.(mx-midr mat)
- Returnsflonum
midrange over allmatrix
argument’s entries.
Generalized Means
(mx-rowmean mat typ)
- Returns columnmatrix
of row power-means of argumentmatrix
of powersymbol
argument.(mx-colmean mat typ)
- Returns rowmatrix
of column power-means of argumentmatrix
of powersymbol
argument.(mx-mean mat typ)
- Returnsflonum
power-mean over allmatrix
argument’s entries of powersymbol
argument.
Vector and Matrix Norms
(mx-rownorm mat typ)
- Returns columnmatrix
of row norms of argumentmatrix
of typesymbol
argument.(mx-colnorm mat typ)
- Returns rowmatrix
of column norms of argumentmatrix
of typesymbol
argument.(mx-norm mat typ)
- Returnsflonum
matrix norm over allmatrix
argument’s entries of typesymbol
argument.
Overall, these functions make up the basic matrix transformation operations,
which in matrico
are specializations of the generic map and reduce matrix functions.
In the next post, I will go through the core linear algebra and analysis functions of matrico
.