MatrixView offers a means to view one or more
existing objects as though they were a
MatrixViewthen the objects change;
MatrixViewmay become invalid (and using it could lead to the dreaded undefined behaviour, i.e. probably a crash).
NB The views do not make copies, so be careful with
temporaries! Look at these examples (
val is a
// OK const vector<RingElem> v(3, val); MatrixView MV = RowMat(v); // MV reads/writes in the vector v // NO this compiles, but the vector disappears after the ";"!! ConstMatrixView MVGhost = RowMat(vector<RingElem>(3, val)); // OK NewDenseMat makes a copy of the vector before it disappears matrix M = NewDenseMat(RowMat(vector<RingElem>(3, val)));
NB no entry is writable
ZeroMat(R, r, c)-- constant
czero matrix over
IdentityMat(R, n)-- constant
nidentity matrix over
FilledMat(R, r, c, val)-- constant
Rfilled with val (a
You can view a
std::vector<RingElem>, all of whose entries belong to the
ring, as a matrix in three ways:
ColMat(v)-- view a
vas a column matrix
RowMat(v)-- view a
vas a row matrix
DiagMat(v)-- view a
vas a diagonal matrix (NB: only the diagonal entries are writable)
transpose(M)-- transposed view of the matrix
submat(M, rows, cols)-- submatrix view into
M; the rows and columns visible in the submatrix are those specified in the arguments
cols(which are of type
The following pseudo-constructors assemble several matrices into a bigger one;
the argument matrices must all have the same
ConcatVer(A, B)-- matrix view with the rows of
Aabove those of
ConcatHor(A, B)-- matrix view with the cols of
Abefore those of
ConcatDiag(A,B)-- block diagonal matrix view
ConcatAntiDiag(A,B)-- block antidiagonal matrix view
BlockMat(A, B, C, D)-- block matrix view
NB the boundaries of the four submatrices must be aligned.
Most of the implementations are quite straightforward; the tricky part was getting the design of the abstract classes right (well, I hope it is right now). Below are a few comments on some less obvious aspects of the implementations.
Note: it is a mathematical fact that the determinant of the 0x0 matrix is 1.
IdentityMatImpl are both derived from
MatrixViewBase rather than
ConstMatrixViewBase as one might
naturally expect. The main reason for this is to simplify the
BlockMat views. I wanted to be lazy and
while this may not be the best implementation, it is a natural
approach and should certainly work as one might reasonably expect.
However, the pseudo-ctor
BlockMat has just two signatures: if any
one of the submatrices is const then whole result becomes const.
I didn't want to implement sixteen different signatures for
BlockMat, and the easy way out seemed to be to make
IdentityMatImpl non-const. As a consequence
there are a number of useless member functions in
IdentityMatImpl. I believe this compromise is reasonable. It
seemed reasonable to allow
ZeroMatImpl::myAssignZero to succeed.
There is a small problem with creating a matrix from an empty
because there is no indication of what the base ring should be. I have
chosen to throw an error if one tries to create a matrix view from an empty
The routines which access the (i,j) entry in a BlockMat are messy.
I could not see an elegant way to make them simpler (or to avoid repeating
similar structure in several places in the code). See Bugs about implementing
BlockMat in terms of
There is an appalling amount of code duplication in the implementations. I do not yet see a good way of reducing this. I hope someone will sooner or later find an elegant way to avoid the duplication. Maybe a diagonal abstract class for ZeroMatImpl, IdentityMatImpl, DiagMatImpl, ConstDiagMatImpl?
It is a great nuisance to have to implement two very similar classes: one for the const case, and the other for the non-const case. Is there a better way?
DiagMat for a free module element?
submatrix allow repeated row/col indices? It could lead to
some some funny behaviour (e.g. setting one entry may change other
entries), so perhaps it would be better to forbid it? Currently, it
The pseudo-ctor for
submatrix ought to accept begin/end iterators instead
of insisting that the caller put the indices in
Should there be a more general version of
BlockMat which allows
BlockMat could be eliminated and replaced by
suitable calls to
Tensor product of two matrices: we implement it as a DenseMatrix instead of MatrixView because the latter would give no practical advantage and hide the cost of accessing the entries.