<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2009  - DIGITEO - Sylvestre LEDRU
 *
 * 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="DoubleManagement_callscilab" xml:lang="en">
    <refnamediv>
        <refname>Double management</refname>
        <refpurpose>How to manage Scilab's variable read and write process using
            call_scilab and api_scilab
        </refpurpose>
    </refnamediv>
    <refsection>
        <title>Description</title>
        <para>This help describes how doubles and matrix of doubles can be handle
            through the <link linkend="call_scilab">Call Scilab API</link> and <link linkend="api_scilab">api Scilab</link>.
        </para>
        <para>There are several functions which can be used to read / write from
            the memory of Scilab. These functions are described in dedicated
            pages.
        </para>
        <para>
            <note>Note that the default type of a numeric value in Scilab is double.
                For example, in <emphasis>a=1</emphasis> or <emphasis>a=[1,2]</emphasis>,
                <emphasis>a</emphasis> will be consider as a double.
            </note>
        </para>
        <para>
            <note>
                Note: Access to variables is done through <link linkend="api_scilab">api Scilab</link> (named variable).
            </note>
        </para>
    </refsection>
    <refsection>
        <title>Examples</title>
        <programlisting role="example"><![CDATA[
/*
 * Write a matrix into Scilab
 * B=[1 4 2 3;
 *    3 9 8 2 ]
 * Note that it is done column by column
 */
double B[] = {1,3,4,9,2,8,3,2};   /* Declare the matrix */
int rowB = 2, colB = 4; /* Size of the matrix */
char variableNameB[] = "B";
SciErr sciErr;

/*
 * Write it into Scilab's memory
 */
sciErr = createNamedMatrixOfDouble(pvApiCtx,variableNameB,rowB,colB, B); /* pvApiCtx is a global variable */
if(sciErr.iErr)
{
	printError(&sciErr, 0);
}

/*
 * Prior to Scilab 5.2:
 * C2F(cwritemat)(variableNameB, &rowB, &colB, B, strlen(variableNameB));
*/

printf("\n");
printf("Display from Scilab of B:\n");
SendScilabJob("disp(B);"); /* Display B */
 ]]></programlisting>
        <programlisting role="example"><![CDATA[
/* Read the previously declared matrix of double B */
int rowB_ = 0, colB_ = 0, lp_ = 0;
double *matrixOfDoubleB = NULL;
int i = 0, j = 0;
char variableToBeRetrievedB[] = "B";
SciErr sciErr;

/* First, retrieve the size of the matrix */
sciErr = readNamedMatrixOfDouble(pvApiCtx, variableToBeRetrievedB, &rowB_, &colB_, NULL);
if(sciErr.iErr)
{
	printError(&sciErr, 0);
}

/*
 * Prior to Scilab 5.2:
 * C2F(cmatptr)(variableToBeRetrievedB, &rowB_, &colB_, &lp_, strlen(variableToBeRetrievedB));
 */


/* Alloc the memory */
matrixOfDoubleB = (double*)malloc((rowB_*colB_)*sizeof(double));

/* Load the matrix */
sciErr = readNamedMatrixOfDouble(pvApiCtx, variableToBeRetrievedB, &rowB_, &colB_, matrixOfDoubleB);
if(sciErr.iErr)
{
	printError(&sciErr, 0);
}

/*
 * Prior to Scilab 5.2:
 * C2F(creadmat)(variableToBeRetrievedB,&rowB_,&colB_,matrixOfDoubleB,strlen(variableToBeRetrievedB) );
 */

printf("\n");
printf("Display from B formatted (size: %d, %d):\n",rowB_, colB_);
for(j = 0 ; j < rowB_ ; j++)
 {
  for(i = 0 ; i < colB_ ; i++)
   {
    /* Display the formatted matrix ... the way the user
     * expect */
    printf("%5.2f ",matrixOfDoubleB[i * rowB_ + j]);
   }
  printf("\n"); /* New row of the matrix */
 }
 ]]></programlisting>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="call_scilab">Call_Scilab</link>
            </member>
            <member>
                <link linkend="api_scilab_legacy">api Scilab</link>
            </member>
            <member>
                <link linkend="Double_management_reading_API">API_Scilab: Double reading</link>
            </member>
            <member>
                <link linkend="Double_management_writing_API">API_Scilab: Double writing</link>
            </member>
            <member>
                <link linkend="SendScilabJob">SendScilabJob</link>
            </member>
            <member>
                <link linkend="StartScilab">StartScilab</link>
            </member>
            <member>
                <link linkend="BooleanManagement_callscilab">Call_Scilab: BooleanManagement</link>
            </member>
            <member>
                <link linkend="ComplexManagement_callscilab">Call_Scilab: ComplexManagement</link>
            </member>
            <member>
                <link linkend="StringManagement_callscilab">Call_Scilab: StringManagement</link>
            </member>
            <member>
                <link linkend="boolean_reading_API">API_Scilab: Boolean Reading</link>
            </member>
            <member>
                <link linkend="boolean_writing_API">API_Scilab: Boolean Writing</link>
            </member>
            <member>
                <link linkend="String_management_reading_API">API_Scilab: String Reading</link>
            </member>
            <member>
                <link linkend="String_management_writing_API">API_Scilab: String Writing</link>
            </member>
        </simplelist>
    </refsection>
</refentry>
