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
matrixarguments 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
matrixargument 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 othermatrixarguments 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
matrixargument 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 othermatrixarguments with all the same rows.⎡XXX⎤ [XXX] ⎡XXX⎤ [XXX] ⎡XXX⎤ ⎡XXX⎤ ⎢XXX⎥ o = ⎢XXX⎥ , o ⎢XXX⎥ = ⎢XXX⎥ ⎣XXX⎦ ⎣XXX⎦ ⎣XXX⎦ ⎣XXX⎦ - One
matrixargument 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
matrixargument is scalar. The binary map function is applied as if the scalar matrix orflonumhad as many rows and columns as the othermatrixargument 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)- Returnsmatrixof entry-wise sums of argumentmatrixes entries.(mx* x y)- Returnsmatrixof entry-wise differences of argumentmatrixes entries.(mx- x y)- Returnsmatrixof entry-wise products of argumentmatrixes entries.(mx/ x y)- Returnsmatrixof entry-wise divisions of argumentmatrixes entries.
Advanced Functions
(mx^ x y)- Returnsmatrixof entry-wise exponentiation of basematrixargument to exponentmatrixargument.(mx-where pred x y)- Returnsmatrixof entries of first or secondmatrixargument, depending on binary predicatefunctionargument evaluated on respectivematrixarguments’ entries.(mx-axpy a x y)- Returnsmatrixof entry-wise sums of firstmatrixargument, scaled byflonumargument, with secondmatrixargument.
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)- Returnsmatrixof entry-wise negation of argumentmatrixes entries.(mx/ x)- Returnsmatrixof entry-wise reciprocal of argumentmatrixes entries.(mx*2 x)- Returnsmatrixof entry-wise double of argumentmatrixes entries.(mx^2 x)- Returnsmatrixof entry-wise square of argumentmatrixes entries.
Rounding Functions
(mx-round mat)- Returnsmatrixof entry-wise roundings of argumentmatrix.(mx-floor mat)- Returnsmatrixof entry-wise greatest integer less than or equal to argumentmatrix.(mx-ceil mat)- Returnsmatrixof entry-wise least integer greater than or equal to argumentmatrix.
Generalized Functions
(mx-abs mat)- Returnsmatrixbeing entry-wise absolute value of argumentmatrix.(mx-sign mat)- Returnsmatrixbeing entry-wise sign of argumentmatrix.(mx-delta mat)- Returnsmatrixbeing entry-wise Kronecker delta of argumentmatrix.(mx-heaviside mat)- Returnsmatrixbeing entry-wise Heaviside step of argumentmatrix.
Trigonometric Functions
(mx-sin mat)- Returnsmatrixbeing entry-wise sine of argumentmatrix.(mx-cos mat)- Returnsmatrixbeing entry-wise cosine of argumentmatrix.(mx-tan mat)- Returnsmatrixbeing entry-wise tangent of argumentmatrix.
Arcus Functions (Inverse Trigonometric Functions)
(mx-asin mat)- Returnsmatrixbeing entry-wise arcsine of argumentmatrix.(mx-acos mat)- Returnsmatrixbeing entry-wise arccosine of argumentmatrix.(mx-atan mat)- Returnsmatrixbeing entry-wise arctangent of argumentmatrix.
Hyperbolic Functions
(mx-sinh mat)- Returnsmatrixbeing entry-wise hyperbolic sine of argumentmatrix.(mx-cosh mat)- Returnsmatrixbeing entry-wise hyperbolic cosine of argumentmatrix.(mx-tanh mat)- Returnsmatrixbeing entry-wise hyperbolic tangent of argumentmatrix.
Area Functions (Inverse Hyperbolic Functions)
(mx-asinh mat)- Returnsmatrixbeing entry-wise area hyperbolic sine of argumentmatrix.(mx-acosh mat)- Returnsmatrixbeing entry-wise area hyperbolic cosine of argumentmatrix.(mx-atanh mat)- Returnsmatrixbeing entry-wise area hyperbolic tangent of argumentmatrix.
Haversed Trigonometric Functions
(mx-hsin mat)- Returnsmatrixbeing entry-wise haversine of argumentmatrix.(mx-hcos mat)- Returnsmatrixbeing entry-wise havercosine of argumentmatrix.
Logarithmic Hyperbolic Functions
(mx-lnsinh mat)- Returnsmatrixbeing entry-wise logarithmic hyperbolic sine of argumentmatrix.(mx-lncosh mat)- Returnsmatrixbeing entry-wise logarithmic hyperbolic cosine of argumentmatrix.
Squareroots
(mx-sqrt mat)- Returnsmatrixbeing entry-wise squareroot of argumentmatrix.(mx-signsqrt mat)- Returnsmatrixbeing entry-wise sign times squareroot of absolute value of argumentmatrix.
Logarithms
(mx-ln mat)- Returnsmatrixbeing entry-wise natural logarithm of argumentmatrix.(mx-lb mat)- Returnsmatrixbeing entry-wise common logarithm of argumentmatrix.(mx-lg mat)- Returnsmatrixbeing entry-wise binary logarithm of argumentmatrix.
Exponentials
(mx-exp mat)- Returnsmatrixbeing entry-wise exponential of argumentmatrix.(mx-gauss mat)- Returnsmatrixbeing entry-wise Gaussian of argumentmatrix.
Special Functions
(mx-sinc mat)- Returnsmatrixbeing entry-wise cardinale sine of argumentmatrix.(mx-sigm mat)- Returnsmatrixbeing entry-wise sigmoid of argumentmatrix.(mx-stirling mat)- Returnsmatrixbeing 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
matrixby fold ing each row into a scalar.⎡XXX⎤ ⎡X⎤ ⎢XXX⎥ -> ⎢X⎥ ⎣XXX⎦ ⎣X⎦ - Composition of column entries yielding a row
matrixby fold ing each column into a scalar.⎡XXX⎤ [XXX] ⎢XXX⎥ -> ⎣XXX⎦ - Composition of all entries yielding a
flonumby fold ing all entries into a scalar.⎡XXX⎤ ⎢XXX⎥ -> X ⎣XXX⎦
Sums
(mx-rowsum mat)- Returns columnmatrixof row sums of argumentmatrix.(mx-colsum mat)- Returns rowmatrixof column sums of argumentmatrix.(mx-sum mat)- Returnsflonumsum of allmatrixargument’s entries.
Products
(mx-rowprod mat)- Returns columnmatrixof row products of argumentmatrix.(mx-colprod mat)- Returns rowmatrixof column products of argumentmatrix.(mx-prod mat)- Returnsflonumproduct of allmatrixargument’s etnries.
Minima
(mx-rowmin mat)- Returns columnmatrixof row minima of argumentmatrix.(mx-colmin mat)- Returns rowmatrixof column minima of argumentmatrix.(mx-min mat)- Returnsflonumminimum over allmatrixargument’s entries.
Maxima
(mx-rowmax mat)- Returns columnmatrixof row maxima of argumentmatrix.(mx-colmax mat)- Returns rowmatrixof column maxima of argumentmatrix.(mx-max mat)- Returnsflonummaximum over allmatrixargument’s entries.
Midrange
(mx-rowmidr mat)- Returns columnmatrixof row midranges of argumentmatrix.(mx-colmidr mat)- Returns rowmatrixof column midranges of argumentmatrix.(mx-midr mat)- Returnsflonummidrange over allmatrixargument’s entries.
Generalized Means
(mx-rowmean mat typ)- Returns columnmatrixof row power-means of argumentmatrixof powersymbolargument.(mx-colmean mat typ)- Returns rowmatrixof column power-means of argumentmatrixof powersymbolargument.(mx-mean mat typ)- Returnsflonumpower-mean over allmatrixargument’s entries of powersymbolargument.
Vector and Matrix Norms
(mx-rownorm mat typ)- Returns columnmatrixof row norms of argumentmatrixof typesymbolargument.(mx-colnorm mat typ)- Returns rowmatrixof column norms of argumentmatrixof typesymbolargument.(mx-norm mat typ)- Returnsflonummatrix norm over allmatrixargument’s entries of typesymbolargument.
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.