From: James N. Knight (nate_at_[hidden])
Date: 2007-02-23 15:31:07
Preben Hagh Strunge Holm wrote:
> Hi everyone
> Is there an easy way to go from a vector to a diagonal matrix
> / Preben
I posted something about this a while ago. You can download the file here
Basically, this defines a special diagonal_vector_adaptor type and
diag and diagm.
The diagonal_vector_adaptor is a matrix expression allowing a vector to
be viewed as the diagonal of a square matrix with zeros in all off
diagm - creates a diagonal_vector_adaptor from a vector.
diag - returns the diagonal of a matrix as a matrix_vector_slice so that
the diagonal can be used as a vector.
Perhaps there is a better way, or the code can be improved in some way.
Hope this helps.