Assignment and copying

next - skip - up - start

The operator = is used for copying matrices, converting matrices, or evaluating expressions. For example

    A = B;  A = L;  A = L * U;
Only conversions that don't lose information are supported. The dimensions of the matrix on the left hand side are adjusted to those of the matrix or expression on the right hand side. Elements on the right hand side which are not present on the left hand side are set to zero.

The operator << can be used in place of = where it is permissible for information to be lost.

For example

    SymmetricMatrix S; Matrix A;
    ......
    S << A.t() * A;
is acceptable whereas
    S = A.t() * A;                            // error
will cause a runtime error since the package does not (yet?) recognise A.t()*A as symmetric.

Note that you can not use << with constructors. For example

    SymmetricMatrix S << A.t() * A;           // error
does not work.

Also note that << cannot be used to load values from a full matrix into a band matrix, since it will be unable to determine the bandwidth of the band matrix.

A third copy routine is used in a similar role to =. Use

    A.Inject(D);
to copy the elements of D to the corresponding elements of A but leave the elements of A unchanged if there is no corresponding element of D (the = operator would set them to 0). This is useful, for example, for setting the diagonal elements of a matrix without disturbing the rest of the matrix. Unlike = and <<, Inject does not reset the dimensions of A, which must match those of D. Inject does not test for no loss of information.

You cannot replace D by a matrix expression. The effect of Inject(D) depends on the type of D. If D is an expression it might not be obvious to the user what type it would have. So I thought it best to disallow expressions.

Inject can be used for loading values from a regular matrix into a band matrix. (Don't forget to zero any elements of the left hand side that will not be set by the loading operation).

Both << and Inject can be used with submatrix expressions on the left hand side. See the section on submatrices.

To set the elements of a matrix to a scalar use operator =

    Real r; Matrix A(m,n);
    ......
    Matrix A(m,n); A = r;