<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2008 - INRIA
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 -->
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
        xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml"
        xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
        xmlns:scilab="http://www.scilab.org" xml:id="diag" xml:lang="en">
    <refnamediv>
        <refname>diag</refname>
        <refpurpose>diagonal including or extracting</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
           y = diag(vm)
           y = diag(vm, k)
        </synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>vm</term>
                <listitem>
                    <para>vector or matrix (full or sparse storage)</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>k</term>
                <listitem>
                    <para>integer (default value 0)</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>y</term>
                <listitem>
                    <para>vector or matrix</para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>
            for <literal>vm</literal> a (row or column) n-vector
            <literal>diag(vm)</literal> returns a diagonal matrix with entries of
            <literal>vm</literal> along the main diagonal.
        </para>
        <para>
            <literal>diag(vm,k)</literal> is a
            <literal>(n+abs(k))x(n+abs(k))</literal> matrix with the entries of
            <literal>vm</literal> along the kth diagonal. <literal>k=0</literal> is
            the main diagonal <literal>k&gt;0</literal> is for upper diagonals and
            <literal>k&lt;0</literal> for lower diagonals.
        </para>
        <para>
            For a matrix <literal>vm</literal>, <literal>diag(vm,k)</literal> is
            the column vector made of entries of the kth diagonal of
            <literal>vm</literal>. <literal>diag(vm)</literal> is the main diagonal of
            <literal>vm</literal>. <literal>diag(diag(x))</literal> is a diagonal
            matrix.
        </para>
        <para>
            If <literal>vm</literal> is a sparse matrix
            <literal>diag(vm,k)</literal> returns a sparse matrix.
        </para>
        <para>To construct a diagonal linear system, use <literal>blockdiag</literal>.
        </para>
        <para>
            Note that <literal>eye(A).*A</literal> returns a diagonal matrix
            made with the diagonal entries of <literal>A</literal>. This is valid for
            any matrix (constant, polynomial, rational, state-space linear
            system,...).
        </para>
    </refsection>
    <refsection>
        <title>Examples</title>
        <programlisting role="example"><![CDATA[
diag([1,2])

A=[1,2;3,4];
diag(A)  // main diagonal
diag(A,1)

diag(sparse(1:10))  // sparse diagonal matrix

// form a tridiagonal matrix of size 2*m+1
m=5;diag(-m:m) +  diag(ones(2*m,1),1) +diag(ones(2*m,1),-1)
 ]]></programlisting>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="toeplitz">toeplitz</link>
            </member>
            <member>
                <link linkend="eye">eye</link>
            </member>
            <member>
                <link linkend="blockdiag">blockdiag</link>
            </member>
            <member>
                <link linkend="spec">spec</link>
            </member>
            <member>
                <link linkend="bdiag">bdiag</link>
            </member>
        </simplelist>
    </refsection>
</refentry>
