<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2011 - DIGITEO - Michael Baudin
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 * Copyright (C) 2020 - Samuel GOUGEON
 *
 * 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="complex" xml:lang="en">
    <refnamediv>
        <refname>complex</refname>
        <refpurpose>Build an array of complex numbers from their parts</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
            c = complex(u)
            c = complex(a, b)
        </synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>u, a, b</term>
                <listitem>
                    scalars, vectors, matrices or hypermatrices of real numbers.
                    If both <varname>a</varname> and <varname>b</varname> are not
                    scalars, they must have the same sizes.
                    <para/>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>c</term>
                <listitem>
                    array of complex numbers, of the size of <varname>u</varname> or of
                    <varname>a</varname> and <varname>b</varname>.
                    If <varname>u</varname> is sparse, or if <varname>a</varname> and
                    <varname>b</varname> have the same sizes and both are sparse,
                    then <varname>c</varname> is also sparse.
                    Otherwise <varname>c</varname> is dense-encoded.
                    <para/>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>
            <emphasis role="bold">c = complex(u)</emphasis> creates an array
            of complex numbers from their real parts <varname>u</varname>,
            and zero as imaginary parts.
        </para>
        <para>
            <emphasis role="bold">c = complex(a,b)</emphasis> creates an array
            of complex numbers from their respective real parts <varname>a</varname>
            and their imaginary parts <varname>b</varname>.
        </para>
        <para>
            This function is equivalent to <literal>a + imult(b)</literal>, which
            handles properly special Inf and NaN real and imaginary parts.
        </para>
    </refsection>
    <refsection>
        <title>Examples</title>
        <para>
            In the following example, we create a complex number from
            its real and imaginary parts.
        </para>
        <programlisting role="example"><![CDATA[
complex(1,2)
complex([1 2],[3 4])
 ]]></programlisting>
        <para>
            If <varname>a</varname> only is specified, then the imaginary
            part is set to zero.
        </para>
        <programlisting role="example"><![CDATA[
complex([1 2 3])
 ]]></programlisting>
        <para>
            If <varname>a</varname> is a scalar and <varname>b</varname>
            is a matrix, then the result <varname>c</varname> has the same
            size as <varname>b</varname>.
            Similarly, if <varname>b</varname> is a scalar and <varname>a</varname>
            is a matrix, then the result <varname>c</varname> has the same
            size as <varname>a</varname>.
        </para>
        <programlisting role="example"><![CDATA[
c = complex([1 2 3], 4)
c = complex(1, [2 3 4])
 ]]></programlisting>
        <para>
            If <varname>a</varname> and <varname>b</varname> are two
            matrices with different sizes, an error is generated, as in the
            following session.
        </para>
        <screen><![CDATA[
-->complex(ones(2,3),ones(4,5))
 !--error 10000
complex: Incompatible input arguments #1 and #2: Same sizes expected.
at line      33 of function complex called by :
complex(ones(2,3),ones(4,5))
 ]]></screen>
        <para>
            With special real or/and imaginary parts:
        </para>
        <programlisting role="example"><![CDATA[
r = [0   0    0 %inf %inf %inf %nan %nan %nan].';
i = [0 %inf %nan  0  %inf %nan   0  %inf %nan].';
[r, i]

// Then let's compare complex(r, i) with r + i*%i :
[complex(r,i), r+i*%i]
 ]]></programlisting>
        <screen><![CDATA[
--> [r, i]
 ans  =
   0.    0.
   0.    Inf
   0.    Nan
   Inf   0.
   Inf   Inf
   Inf   Nan
   Nan   0.
   Nan   Inf
   Nan   Nan

// Then let's compare complex(r, i) with r + i*%i :
--> [complex(r,i), r+i*%i]
 ans  =
   0.  + 0.i    0.  + 0.i
   0.  + Infi   Nan + Infi
   0.  + Nani   Nan + Nani
   Inf + 0.i    Inf + 0.i
   Inf + Infi   Nan + Infi
   Inf + Nani   Nan + Nani
   Nan + 0.i    Nan + 0.i
   Nan + Infi   Nan + Infi
   Nan + Nani   Nan + Nani
 ]]></screen>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="percenti">%i</link>
            </member>
            <member>
                <link linkend="imult">imult</link>
            </member>
        </simplelist>
    </refsection>
    <refsection role="history">
        <title>History</title>
        <revhistory>
            <revision>
                <revnumber>6.1.1</revnumber>
                <revdescription>Extension to sparse arrays.
                </revdescription>
            </revision>
        </revhistory>
    </refsection>
</refentry>
