<?xml version="1.0" encoding="utf-8"?>
<!--
 * Scilab ( https://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2015  - Scilab Enterprises - Antoine ELIAS
 *
 * 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="api_double" xml:lang="en">
    <refnamediv>
        <refname>API double functions</refname>
        <para>This page lists all the functions to manipulate Scilab native objects containing doubles.</para>
        <para></para>
    </refnamediv>
    <refsynopsisdiv>
        <refsection id="api_double_scilab_createEmptyMatrix">
            <literal>
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                <varname>scilab_createEmptyMatrix</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env)
            </literal>
            <para>Create an empty matrix.</para>
        </refsection>
        <refsection id="api_double_scilab_createDoubleMatrix">
            <literal>
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                <varname>scilab_createDoubleMatrix</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env, int dim, const int* dims, int complex)
            </literal>
            <para>
                Create a double matrix with <varname>dim</varname> dimensions (<varname>dims</varname>[0], <varname>dims</varname>[1], ..., <varname>dims</varname>[<varname>dim</varname>-1]).
            </para>
            <para>
                <varname>complex</varname>: 0 to real matrix or 1 to complex matrix
            </para>
        </refsection>
        <refsection id="api_double_scilab_createDoubleMatrix2d">
            <literal>
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                <varname>scilab_createDoubleMatrix2d</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env, int row, int col, int complex)
            </literal>
            <para>
                Create a double matrix with 2 dimensions (<varname>row</varname>, <varname>col</varname>).
            </para>
            <para>
                <varname>complex</varname>: 0 to real matrix or 1 to complex matrix
            </para>
        </refsection>
        <refsection id="api_double_scilab_createDouble">
            <literal>
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                <varname>scilab_createDouble</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env, double real)
            </literal>
            <para>Create a scalar double.</para>
        </refsection>
        <refsection id="api_double_scilab_createDoubleComplex">
            <literal>
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                <varname>scilab_createDoubleComplex</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env, double real, double img)
            </literal>
            <para>Create a scalar complex double.</para>
        </refsection>
        <refsection id="api_double_scilab_getDoubleArray">
            <literal>
                <link linkend="api_types_scilabStatus">
                    <command>scilabStatus</command>
                </link>
                <varname>scilab_getDoubleArray</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, double** real)
            </literal>
            <para>
                Get pointer on internal array of double of <literal>var</literal>.
            </para>
        </refsection>
        <refsection id="api_double_scilab_getDoubleComplexArray">
            <literal>
                <link linkend="api_types_scilabStatus">
                    <command>scilabStatus</command>
                </link>
                <varname>scilab_getDoubleComplexArray</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, double** real, double** img)
            </literal>
            <para>
                Get pointers on internal arrays of double of <literal>var</literal>.
            </para>
        </refsection>
        <refsection id="api_double_scilab_getDouble">
            <literal>
                <link linkend="api_types_scilabStatus">
                    <command>scilabStatus</command>
                </link>
                <varname>scilab_getDouble</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, double* real)
            </literal>
            <para>Get double value from an scalar double variable.</para>
        </refsection>
        <refsection id="api_double_scilab_getDoubleComplex">
            <literal>
                <link linkend="api_types_scilabStatus">
                    <command>scilabStatus</command>
                </link>
                <varname>scilab_getDoubleComplex</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, double* real, double* img)
            </literal>
            <para>Get double values from an scalar double variable.</para>
        </refsection>
        <refsection id="api_double_scilab_setDoubleArray">
            <literal>
                <link linkend="api_types_scilabStatus">
                    <command>scilabStatus</command>
                </link>
                <varname>scilab_setDoubleArray</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, const double* real)
            </literal>
            <para>
                Set values of double variable <literal>var</literal>.
            </para>
        </refsection>
        <refsection id="api_double_scilab_setDoubleComplexArray">
            <literal>
                <link linkend="api_types_scilabStatus">
                    <command>scilabStatus</command>
                </link>
                <varname>scilab_setDoubleComplexArray</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, const double* real, const double* img)
            </literal>
            <para>
                Set values of complex double variable <literal>var</literal>.
            </para>
        </refsection>
        <refsection id="api_double_scilab_setDouble">
            <literal>
                <link linkend="api_types_scilabStatus">
                    <command>scilabStatus</command>
                </link>
                <varname>scilab_setDouble</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, double real)
            </literal>
            <para>
                Set value of a scalar double variable <literal>var</literal>.
            </para>
        </refsection>
        <refsection id="api_double_scilab_setDoubleComplex">
            <literal>
                <link linkend="api_types_scilabStatus">
                    <command>scilabStatus</command>
                </link>
                <varname>scilab_setDoubleComplex</varname>(
                <link linkend="api_types_scilabEnv">
                    <command>scilabEnv</command>
                </link>
                env,
                <link linkend="api_types_scilabVar">
                    <command>scilabVar</command>
                </link>
                var, double real, double img)
            </literal>
            <para>
                Set values of a scalar complex double variable <literal>var</literal>.
            </para>
        </refsection>
    </refsynopsisdiv>
    <refsection>
        <title>Examples</title>
        <title>Scilab test script</title>
        <programlisting role="example"><![CDATA[
scinotes("SCI/modules/api_scilab/tests/unit_tests/api_c/double_test.c", "readonly")
]]>     </programlisting>
        <programlisting role="code_gateway">
            <![CDATA[
#include "api_scilab.h"
#include "Scierror.h"
#include "localization.h"
#include "sciprint.h"

const char fname[] = "double_test";

int sci_double_test(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt opt, int nout, scilabVar* out)
{
    int i = 0;
    int inr1 = 0;
    int inc1 = 0;
    int size1 = 0;
    double* in1 = NULL;

    double in2 = 0;

    double* out1 = NULL;
    double* out2 = NULL;

    double out3 = 0;

    if (nin != 2)
    {
        Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), fname, 2);
        return STATUS_ERROR;
    }

    if (nout != 3)
    {
        Scierror(999, _("%s: Wrong number of output arguments: %d expected.\n"), fname, 3);
        return STATUS_ERROR;
    }

    //in[0] : matrix 2d of double
    if (scilab_isDouble(env, in[0]) == 0 || scilab_isMatrix2d(env, in[0]) == 0)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A double matrix expected.\n"), fname, 1);
        return STATUS_ERROR;
    }

    size1 = scilab_getDim2d(env, in[0], &inr1, &inc1);
    scilab_getDoubleArray(env, in[0], &in1);

    //in[1] : double
    if (scilab_isDouble(env, in[1]) == 0 || scilab_isScalar(env, in[1]) == 0)
    {
        Scierror(999, _("%s: Wrong type for input argument #%d: A double expected.\n"), fname, 2);
        return STATUS_ERROR;
    }

    scilab_getDouble(env, in[1], &in2);

    //out1 : matrix 2d of double with same size of in[0]
    out[0] = scilab_createDoubleMatrix2d(env, inr1, inc1, 0);
    scilab_getDoubleArray(env, out[0], &out1);

    for (i = 0; i < size1; ++i)
    {
        out1[i] = in1[i] * 10;
    }

    //out2 : 3d matrix of double
    int out2dims[3] = {inr1, inc1, 2};

    out[1] = scilab_createDoubleMatrix(env, 3, out2dims, 0);
    scilab_getDoubleArray(env, out[1], &out2);

    for (i = 0; i < size1; ++i)
    {
        out2[i] = in1[i] * 10;
        out2[i + size1] = in1[i] * 100;
    }

    //out3 : double
    out[2] = scilab_createDouble(env, in2 * 1000);
    return STATUS_OK;
}
            ]]>
        </programlisting>
    </refsection>
    <refsection>
        <title>Scilab test script</title>
        <programlisting role="code_scilab">
            <![CDATA[
mkdir(pathconvert(TMPDIR+"/api_c/"));
cd(pathconvert(TMPDIR+"/api_c/"));
copyfile(SCI+"/modules/api_scilab/tests/unit_tests/api_c/double_test.c",pathconvert(TMPDIR+"/api_c/double_test.c",%F));

ilib_build("libdouble", ["double_test","sci_double_test", "csci6"],"double_test.c",[],"","","");
exec("loader.sce");

in1 = rand(3,4);
in2 = rand();

[out1, out2, out3] = double_test(in1, in2);

assert_checkequal(out1, in1 * 10);
ref(:,:, 1) = in1 * 10;
ref(:,:, 2) = in1 * 100;
assert_checkequal(out2, ref);
assert_checkequal(out3, in2 * 1000);
disp("OK");
            ]]>
        </programlisting>
    </refsection>
</refentry>
