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)
- Returnsmatrix
of row and column argumentnumber
s and all entries set toflonum
value.(mx% lst)
- Returnsmatrix
from row-majorlist
-of-list
-of-flonum
s.(mx-identity dims)
- Returns an identitymatrix
of dimension argumentnumber
.(mx-exchange dims)
- Returns an exchangematrix
of dimension argumentnumber
.(mx-hilbert dims)
- Returns a Hilbertmatrix
of dimension argumentnumber
.(mx-pascal dims)
- Returns a lower triangular Pascalmatrix
of dimension argumentnumber
.(mx-lehmer rows cols)
- Returns a Lehmermatrix
of row and column dimension argumentsnumber
.(mx-random rows cols low upp)
- Returns uniformly randommatrix
withinflonum
lower and upper limit arguments of row and column argumentnumber
s.(mx-tridiag dims low mid upp)
- Returns tridiagonalmatrix
with lower, middle, and upper band value arguments of dimension argumentnumber
.(mx-unit dims num)
- Returns columnmatrix
of dimension argumentnumber
with all entries zero except at index argumentnumber
where it is one.(mx-iota dims)
- Returns columnmatrix
of dimension argumentnumber
, with entries corresponding to the row index.(mx-linspace x y num)
- Returnsmatrix
with linearly interpolated rows between columnmatrix
arguments andnumber
of columns argument.(mx-logspace x y num)
- Returnsmatrix
with base-10 logarithmically interpolated rows between columnmatrix
arguments andnumber
of columns argument.
Dimensions
Next, functions are provided to determine the dimensionality of a matrix
:
(mx-cols mat)
- Returnsnumber
of columns of argumentmatrix
.(mx-rows mat)
- Returnsnumber
of rows of argumentmatrix
.(mx-numel mat)
- Returnsnumber
of entries of argumentmatrix
.(mx-dims mat)
- Returnsnumber
of dimensions of argumentmatrix
.
Predicates
Also, the properties of one matrix
or jointly of two matrix
es, need to be testable.
To this end a set of predicates is implemented, including a flonum
-safe equality comparison of matrix
es:
(mx? any)
- Returnsboolean
answering if argument is amatrix
.(mx-col? mat)
- Returnsboolean
answering if argumentmatrix
has just one column.(mx-row? mat)
- Returnsboolean
answering if argumentmatrix
has just one row.(mx-scalar? mat)
- Returnsboolean
answering if argumentmatrix
has just one column and row.(mx-vector? mat)
- Returnsboolean
answering if argumentmatrix
has just one column or row.(mx-square? mat)
- Returnsboolean
answering if argumentmatrix
has same number of rows and columns.(mx-samecols? x y)
- Returnsboolean
answering if argumentmatrix
es have same number of columns.(mx-samerows? x y)
- Returnsboolean
answering if argumentmatrix
es have same number of rows.(mx-samedims? x y)
- Returnsboolean
answering if argumentmatrix
es have same number of rows and columns.(mx-any? pred mat)
- Returnsboolean
answering if at least one entry of the argumentmatrix
fulfills predicate argument.(mx-all? pred mat)
- Returnsboolean
answering if all entries of argumentmatrix
fulfill predicate argument.(mx=? x y tol)
- Returnsboolean
answering if the entry-wise absolute difference between argumentmatrix
es is belowflonum
tolerance 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)
- Returnsflonum
being the entry in the first column and first row of argumentmatrix
.(mx-ref mat row col)
- Returnsflonum
being the entry at column and row arguments of argumentmatrix
.(mx-set mat row col val)
- Returnsmatrix
with entry at column and row arguments of argumentmatrix
set toflonum
value argument.(mx-set! mat row col val)
- Returns void, mutates entry at column and row arguments of argumentmatrix
toflonum
value argument.(mx-col mat col)
- Returnsmatrix
being the column at index argument of argumentmatrix
.(mx-row mat row)
- Returnsmatrix
being the row at index argument of argumentmatrix
.(mx-diag mat)
- Returns columnmatrix
being the main diagonal of argumentmatrix
.(mx-submatrix mat row1 row2 col1 col2)
- Returnsmatrix
being the sub-matrix between row and column index arguments of thematrix
argument.
In the next part, I will describe the basic arithmetic and calculator functions
in matrico
via Expanders, Mappers, and Reducers.