Boost C++ Libraries: Ticket #3510: Introduce new diag function for creating diagonal matrices and for returning the diagonal of a matrix https://svn.boost.org/trac10/ticket/3510 <p> Introduce a new <strong>diag</strong> free function in the spirit of the MATLAB's <em>diag</em> function and Mathematica's <em><a class="missing wiki">DiagonalMatrix</a> function</em>. </p> <p> Basically it allows both the creation of a <em>generalized</em> diagonal matrix and the creation of a <em>diagonal</em> view of an existing matrix. </p> <p> A <em>generalized</em> k-th diagonal matrix is a special kind of matrix which has all elements set to zero but the ones on its k-th diagonal. The integer k is the offset from the main diagonal, that is: </p> <ul><li>k = 0: the elements on the main diagonal can be different from zero. </li></ul><ul><li>k &gt; 0: only the elements on the k-th upper diagonal can be different from zero. </li></ul><ul><li>k &lt; 0: only the elements on the k-th lower diagonal can be different from zero. </li></ul><p> A generalized diagonal matrix can be a rectangular matrix. </p> <p> Here below is a list of the requested cases: </p> <ul><li>Create a square diagonal matrix M with vector V </li></ul><p> being the k-th diagonal </p> <blockquote> <p> <code>M = diag(v,k)</code> </p> </blockquote> <ul><li>Like the above, but M has layout l (e.g., column major) <code>M = diag(v,k,l)</code> </li></ul><ul><li>Create a rectangular diagonal matrix M of size mXn with vector V being the k-th diagonal <code>M = diag(v,m,n,k)</code> </li></ul><ul><li>Like the above, but M has layout l (e.g., column major) <code>M = diag(v,m,n,k,l)</code> </li></ul><ul><li>Create a diagonal view of the k-th diagonal of matrix M <code>v = diag(M,k)</code> </li></ul> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3510 Trac 1.4.3 marco.guazzone@… Mon, 05 Oct 2009 08:38:18 GMT attachment set https://svn.boost.org/trac10/ticket/3510 https://svn.boost.org/trac10/ticket/3510 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">ublas_diag.zip</span> </li> </ul> <p> I've included a possible implementation. In addition to the <strong>diag</strong> operation, two new types are provided: (1) <strong>generalized_diagonal_matrix</strong>: a new matrix container representing a generalized diagonal matrix. (2) <strong>matrix_diagonal</strong>: a new matrix proxy representing the k-th diagonal of a given matrix. </p> Ticket