| 1 |
The \eslmod{dmatrix} module implements 2D matrices and linear algebra |
| 2 |
operations. |
| 3 |
|
| 4 |
There are two objects. The main one is a \ccode{ESL\_DMATRIX}, a 2D |
| 5 |
real-valued matrix of n rows and m columns. There is also |
| 6 |
\ccode{ESL\_PERMUTATION}, a special matrix used in LU decompositions. |
| 7 |
|
| 8 |
It is straightforward to call standard BLAS and LAPACK linear algebra |
| 9 |
routines on the data in an \ccode{ESL\_DMATRIX}. |
| 10 |
|
| 11 |
\begin{table}[hbp] |
| 12 |
\begin{center} |
| 13 |
{\small |
| 14 |
\begin{tabular}{|ll|}\hline |
| 15 |
\hyperlink{func:esl_dmatrix_Create()}{\ccode{esl\_dmatrix\_Create()}} & Create general matrix.\\ |
| 16 |
\hyperlink{func:esl_dmatrix_CreateUpper()}{\ccode{esl\_dmatrix\_CreateUpper()}} & Create packed upper triangular matrix.\\ |
| 17 |
\hyperlink{func:esl_dmatrix_Destroy()}{\ccode{esl\_dmatrix\_Destroy()}} & Free a matrix.\\ |
| 18 |
\hyperlink{func:esl_dmatrix_Dump()}{\ccode{esl\_dmatrix\_Dump()}} & Dump matrix internals to output stream.\\ |
| 19 |
\hyperlink{func:esl_dmatrix_Copy()}{\ccode{esl\_dmatrix\_Copy()}} & Make a copy of a matrix (no new allocation).\\ |
| 20 |
\hyperlink{func:esl_dmatrix_Clone()}{\ccode{esl\_dmatrix\_Clone()}} & Duplicate a matrix (allocate new storage).\\ |
| 21 |
\hyperlink{func:esl_dmatrix_Compare()}{\ccode{esl\_dmatrix\_Compare()}} & Compare two matrices for equality.\\ |
| 22 |
\hyperlink{func:esl_dmatrix_Set()}{\ccode{esl\_dmatrix\_Set()}} & Set all cells in matrix to same scalar value.\\ |
| 23 |
\hyperlink{func:esl_dmatrix_SetZero()}{\ccode{esl\_dmatrix\_SetZero()}} & Set all cells in matrix to zero.\\ |
| 24 |
\hyperlink{func:esl_dmatrix_SetIdentity()}{\ccode{esl\_dmatrix\_SetIdentity()}} & Set diagonal elements to 1, all others to zero.\\ |
| 25 |
\hyperlink{func:esl_dmx_Max()}{\ccode{esl\_dmx\_Max()}} &Returns maximum element value.\\ |
| 26 |
\hyperlink{func:esl_dmx_Min()}{\ccode{esl\_dmx\_Min()}} &Returns maximum element value.\\ |
| 27 |
\hyperlink{func:esl_dmx_Sum()}{\ccode{esl\_dmx\_Sum()}} &Returns sum of all elements.\\ |
| 28 |
\hyperlink{func:esl_permutation_Create()}{\ccode{esl\_permutation\_Create()}} & Create a permutation matrix.\\ |
| 29 |
\hyperlink{func:esl_permutation_Destroy()}{\ccode{esl\_permutation\_Destroy()}} & Free a permutation matrix.\\ |
| 30 |
\hyperlink{func:esl_permutation_Reuse()}{\ccode{esl\_permutation\_Reuse()}} & Reuse a permutation matrix.\\ |
| 31 |
\hyperlink{func:esl_permutation_Dump()}{\ccode{esl\_permutation\_Dump()}} & Dump permutation matrix internals to output stream.\\ |
| 32 |
\hyperlink{func:esl_dmx_Multiply()}{\ccode{esl\_dmx\_Multiply()}} & Matrix multiplication.\\ |
| 33 |
\hyperlink{func:esl_dmx_Transpose()}{\ccode{esl\_dmx\_Transpose()}} & Matrix transpostion.\\ |
| 34 |
\hyperlink{func:esl_dmx_Add()}{\ccode{esl\_dmx\_Add()}} & Matrix addition.\\ |
| 35 |
\hyperlink{func:esl_dmx_Scale()}{\ccode{esl\_dmx\_Scale()}} & Multiply a matrix by a scalar.\\ |
| 36 |
\hyperlink{func:esl_dmx_AddScale()}{\ccode{esl\_dmx\_AddScale()}} & $A + kB$ \\ |
| 37 |
\hyperlink{func:esl_dmx_Permute_PA()}{\ccode{esl\_dmx\_Permute\_PA()}} & $B = PA$, a row-wise permutation of $A$.\\ |
| 38 |
\hyperlink{func:esl_dmx_LUP_decompose()}{\ccode{esl\_dmx\_LUP\_decompose()}} & Permuted LU decomposition.\\ |
| 39 |
\hyperlink{func:esl_dmx_LU_separate()}{\ccode{esl\_dmx\_LU\_separate()}} & Get answers from a LU decomposition.\\ |
| 40 |
\hyperlink{func:esl_dmx_Invert()}{\ccode{esl\_dmx\_Invert()}} & Matrix inversion.\\ |
| 41 |
\hline |
| 42 |
\end{tabular} |
| 43 |
} |
| 44 |
\end{center} |
| 45 |
\caption{The \eslmod{dmatrix} API.} |
| 46 |
\label{tbl:dmatrix_api} |
| 47 |
\end{table} |
| 48 |
|
| 49 |
|
| 50 |
\subsection{Example of using the dmatrix API} |
| 51 |
|
| 52 |
A toy example that demonstrates the syntax of creating three 4x4 |
| 53 |
square matrices and doing some simple operations on them: |
| 54 |
|
| 55 |
\input{cexcerpts/dmatrix_example} |
| 56 |
|
| 57 |
|
| 58 |
\subsection{Accessing matrix values} |
| 59 |
|
| 60 |
The accessible internals of the \ccode{ESL\_DMATRIX} structure are: |
| 61 |
|
| 62 |
\input{cexcerpts/dmatrix_obj} |
| 63 |
|
| 64 |
The matrix is stored in row-major orientation: the value in cell |
| 65 |
$(i,j)$ in row $i$ and column $j$ is in \ccode{mx->mx[i][j]}. |
| 66 |
|
| 67 |
Elements are stored in a single array \ccode{mx->mx[0]}. This is |
| 68 |
important for interoperability with BLAS and LAPACK; see below. The |
| 69 |
row pointers \ccode{mx->mx[i]} are initialized so that elements may be |
| 70 |
accessed simply as \ccode{mx->mx[i][j]}, rather than by pointer |
| 71 |
arithmetic \ccode{mx->mx[0] + i*mx->m + j}. |
| 72 |
|
| 73 |
\subsection{Specialized matrix types} |
| 74 |
|
| 75 |
Normally matrices are created with \ccode{esl\_dmatrix\_Create()}, |
| 76 |
which allocates storage for all $n \times m$ cells. Easel calls this a |
| 77 |
matrix of type \ccode{eslGENERAL}. |
| 78 |
|
| 79 |
Matrices may have more restricted forms, which may constrain certain |
| 80 |
values and may allow packed storage. For example, an upper triangular |
| 81 |
matrix is one in which all elements $i>j$ have a value of zero. When |
| 82 |
we calculate the minimum in such a matrix with |
| 83 |
\ccode{esl\_dmatrix\_Min()}, we probably don't want to consider the |
| 84 |
$i>j$ elements. We also can save almost two-fold in storage by not |
| 85 |
storing the $i>j$ elements at all. Other types include square, lower |
| 86 |
triagonal, and symmetric matrices. |
| 87 |
|
| 88 |
We expect to need to expand Easel's implementation of different matrix |
| 89 |
types in the future, but right now, Easel has just one other matrix |
| 90 |
type, \ccode{eslUPPER}, for packed upper triangular matrices. |
| 91 |
|
| 92 |
\subsubsection{\ccode{eslUPPER}: packed upper triangular matrices} |
| 93 |
|
| 94 |
An \ccode{eslUPPER} matrix is created with |
| 95 |
\ccode{esl\_dmatrix\_CreateUpper(int n)}. It is necessarily square $n |
| 96 |
\times n$, so only one dimension argument is passed. Most but not all |
| 97 |
functions in \eslmod{dmatrix} can operate on \ccode{eslUPPER} matrix |
| 98 |
types in addition to the usual \ccode{eslGENERAL} type. |
| 99 |
|
| 100 |
The caller must not access any cell $i>j$ in an \ccode{eslUPPER} |
| 101 |
matrix. Setting a cell $i>j$ will corrupt the matrix. Accessing cell |
| 102 |
$i>j$ will return an incorrect value, not zero. |
| 103 |
|
| 104 |
The $n (n+1) / 2$ elements of the upper triagonal matrix are packed |
| 105 |
into an array \ccode{mx->mx[0]}. You can access element $i,j$ by |
| 106 |
pointer arithmetic at \ccode{mx->mx[j + i(2*mx->m-i-1)/2]} if you |
| 107 |
like, but it is easier to access element $i,j$ by the usual |
| 108 |
\ccode{mx->mx[i][j]}. This is made possible because the row pointers |
| 109 |
\ccode{mx->mx[i]} in an \ccode{eslUPPER} matrix are tricksily |
| 110 |
initialized in an overlapping fashion so that \ccode{mx->mx[i][j]} |
| 111 |
does the right thing for $i \leq j$. This overlapping is also the |
| 112 |
reason why \ccode{mx->mx[i][j]} accesses the wrong element when $i>j$. |
| 113 |
|
| 114 |
\subsubsection{Notes on the current implementation of matrix types} |
| 115 |
|
| 116 |
Easel matrix types conflate packing and element validity together. For |
| 117 |
example, an upper triangular matrix may be stored either in an |
| 118 |
\ccode{eslGENERAL} matrix type (in which case elements $i>j$ are set |
| 119 |
to zero) or the packed \ccode{eslUPPER} matrix type (in which case |
| 120 |
elements $i>j$ aren't even stored). Using the \ccode{eslUPPER} matrix |
| 121 |
type is 2x more space efficient, and also, operations like |
| 122 |
\ccode{esl\_dmatrix\_Min()} and \ccode{esl\_dmatrix\_Max()} will |
| 123 |
examine all elements in an \ccode{eslGENERAL} matrix (including the |
| 124 |
zeros), but only the elements $i \leq j$ in a \ccode{eslUPPER} matrix. |
| 125 |
|
| 126 |
This design is provisional. We may adopt a system more closely akin to |
| 127 |
BLAS/LAPACK in the future, which distinguish between matrix type and |
| 128 |
matrix storage. For example, BLAS has matrices of form \ccode{TR} and |
| 129 |
\ccode{TP} for triangular and packed triangular. Easel's |
| 130 |
implementation seems sufficient for the moment, and should also extend |
| 131 |
to lower diagonal and symmetric matrices without difficulty when and |
| 132 |
if they become needed. In any future development, look to BLAS and |
| 133 |
LAPACK for guidance. |
| 134 |
|
| 135 |
\subsection{Interoperability with BLAS and LAPACK} |
| 136 |
|
| 137 |
The BLAS and LAPACK libraries provide optimized, standardized linear |
| 138 |
algebra routines. The storage in \ccode{ESL\_DMATRIX} is designed so |
| 139 |
you can call routines in these libraries. The \ccode{mx->mx[0]} array |
| 140 |
is a valid matrix for BLAS and LAPACK so long as you know the right |
| 141 |
incantations. These are summarized here: |
| 142 |
|
| 143 |
{\small |
| 144 |
\begin{tabular}{llllll} |
| 145 |
Easel type & \ccode{CBLAS\_ORDER} & stride & \ccode{CBLAS\_UPLO} & type & code \\ \hline |
| 146 |
\ccode{eslGENERAL} & \ccode{CblasRowMajor} & \ccode{mx->m} & n/a & double & \ccode{GE} (GEneral) \\ |
| 147 |
\ccode{eslUPPER} & \ccode{CBlasRowMajor} & \ccode{mx->m} & \ccode{CblasUpper} & double & \ccode{TP} (Triangular Packed) \\ |
| 148 |
\end{tabular} |
| 149 |
} |
| 150 |
|
| 151 |
For example, to call the CBLAS (C implementation of BLAS) for an |
| 152 |
operation on an Easel matrix of type \ccode{eslGENERAL}, you look for |
| 153 |
a routine that starts with prefix \ccode{cblas\_dge*} (\ccode{d} for |
| 154 |
double, \ccode{ge} for general). An example is |
| 155 |
\ccode{cblas\_dgemm()}, the matrix multiplication (\ccode{mm}) |
| 156 |
routine, which computes $C = \alpha \mathit{op}(A) \mathit{op}(B) + |
| 157 |
\beta C$ for matrices $A,B,C$ and scalars $\alpha,\beta$, where |
| 158 |
$\mathit{op}(A)$ means $A$, $A^T$ (the transpose), or $A^H$ (the |
| 159 |
conjugate transpose). $\mathit{op}(A)$ is an $M \times K$ matrix, |
| 160 |
$\mathit{op}(B)$ is $K \times N$ matrix, and the result $C$ is $M |
| 161 |
\times N$. The prototype for \ccode{cblas\_dgemm} is: |
| 162 |
|
| 163 |
\begin{cchunk} |
| 164 |
void |
| 165 |
cblas_dgemm (const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA, |
| 166 |
const enum CBLAS_TRANSPOSE TransB, const int M, const int N, |
| 167 |
const int K, const double alpha, const double *A, const int lda, |
| 168 |
const double *B, const int ldb, const double beta, double *C, |
| 169 |
const int ldc) |
| 170 |
\end{cchunk} |
| 171 |
|
| 172 |
The \ccode{Order} argument is always \ccode{CblasRowMajor} for Easel |
| 173 |
matrices. The \ccode{TransA} and \ccode{TransB} arguments specify |
| 174 |
$\mathit{op}()$: \ccode{CblasNoTrans} means just the matrix |
| 175 |
itself. The \ccode{ld*} arguments are the major strides for each |
| 176 |
matrix: the number of elements in each row, for our row-major |
| 177 |
matrices. So, we could call: |
| 178 |
|
| 179 |
\begin{cchunk} |
| 180 |
cblas_dgemm (CblasRowMajor, CblasNoTrans, CblasNoTrans, |
| 181 |
A->n, B->m, A->m, |
| 182 |
1.0, A->mx[0], A->m, |
| 183 |
B->mx[0], B->m, |
| 184 |
1.0, C->mx[0], C->m); |
| 185 |
\end{cchunk} |
| 186 |
|
| 187 |
|