Matrico Module II
matrico’s matrico Module (Part II)
Series:
In this second part of the series of posts describing the matrico module, I will focus on the
Constructor, Dimension, Predicate, and Accessor type functions, which are contained in
src/mx.scm Scheme source code file,
that is included into the module’s main file matrico.scm.
Constructors
As I mentioned in an earlier post,
the actual matrix record is hidden from the user. Hence, to instantiate a matrix, constructors are needed,
which are provided in terms of basic as well as specialized matrices and column matrices;
but also a way to manually enter a matrix as a list of lists is given:
(mx rows cols val)- Returnsmatrixof row and column argumentnumbers and all entries set toflonumvalue.(mx% lst)- Returnsmatrixfrom row-majorlist-of-list-of-flonums.(mx-identity dims)- Returns an identitymatrixof dimension argumentnumber.(mx-exchange dims)- Returns an exchangematrixof dimension argumentnumber.(mx-hilbert dims)- Returns a Hilbertmatrixof dimension argumentnumber.(mx-pascal dims)- Returns a lower triangular Pascalmatrixof dimension argumentnumber.(mx-lehmer rows cols)- Returns a Lehmermatrixof row and column dimension argumentsnumber.(mx-random rows cols low upp)- Returns uniformly randommatrixwithinflonumlower and upper limit arguments of row and column argumentnumbers.(mx-tridiag dims low mid upp)- Returns tridiagonalmatrixwith lower, middle, and upper band value arguments of dimension argumentnumber.(mx-unit dims num)- Returns columnmatrixof dimension argumentnumberwith all entries zero except at index argumentnumberwhere it is one.(mx-iota dims)- Returns columnmatrixof dimension argumentnumber, with entries corresponding to the row index.(mx-linspace x y num)- Returnsmatrixwith linearly interpolated rows between columnmatrixarguments andnumberof columns argument.(mx-logspace x y num)- Returnsmatrixwith base-10 logarithmically interpolated rows between columnmatrixarguments andnumberof columns argument.
Dimensions
Next, functions are provided to determine the dimensionality of a matrix:
(mx-cols mat)- Returnsnumberof columns of argumentmatrix.(mx-rows mat)- Returnsnumberof rows of argumentmatrix.(mx-numel mat)- Returnsnumberof entries of argumentmatrix.(mx-dims mat)- Returnsnumberof dimensions of argumentmatrix.
Predicates
Also, the properties of one matrix or jointly of two matrixes, need to be testable.
To this end a set of predicates is implemented, including a flonum-safe equality comparison of matrixes:
(mx? any)- Returnsbooleananswering if argument is amatrix.(mx-col? mat)- Returnsbooleananswering if argumentmatrixhas just one column.(mx-row? mat)- Returnsbooleananswering if argumentmatrixhas just one row.(mx-scalar? mat)- Returnsbooleananswering if argumentmatrixhas just one column and row.(mx-vector? mat)- Returnsbooleananswering if argumentmatrixhas just one column or row.(mx-square? mat)- Returnsbooleananswering if argumentmatrixhas same number of rows and columns.(mx-samecols? x y)- Returnsbooleananswering if argumentmatrixes have same number of columns.(mx-samerows? x y)- Returnsbooleananswering if argumentmatrixes have same number of rows.(mx-samedims? x y)- Returnsbooleananswering if argumentmatrixes have same number of rows and columns.(mx-any? pred mat)- Returnsbooleananswering if at least one entry of the argumentmatrixfulfills predicate argument.(mx-all? pred mat)- Returnsbooleananswering if all entries of argumentmatrixfulfill predicate argument.(mx=? x y tol)- Returnsbooleananswering if the entry-wise absolute difference between argumentmatrixes is belowflonumtolerance argument.
Accessors
Given functions to create, measure and test matrices, a basic functionality completing this set of basic matrix functions,
are functions to get or set entries or parts of a matrix:
(mx-ref11 mat)- Returnsflonumbeing the entry in the first column and first row of argumentmatrix.(mx-ref mat row col)- Returnsflonumbeing the entry at column and row arguments of argumentmatrix.(mx-set mat row col val)- Returnsmatrixwith entry at column and row arguments of argumentmatrixset toflonumvalue argument.(mx-set! mat row col val)- Returns void, mutates entry at column and row arguments of argumentmatrixtoflonumvalue argument.(mx-col mat col)- Returnsmatrixbeing the column at index argument of argumentmatrix.(mx-row mat row)- Returnsmatrixbeing the row at index argument of argumentmatrix.(mx-diag mat)- Returns columnmatrixbeing the main diagonal of argumentmatrix.(mx-submatrix mat row1 row2 col1 col2)- Returnsmatrixbeing the sub-matrix between row and column index arguments of thematrixargument.
In the next part, I will describe the basic arithmetic and calculator functions
in matrico via Expanders, Mappers, and Reducers.