<?xml version="1.0" encoding="utf-8"?>
<!--
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2009  - DIGITEO - 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="Sparse_management_reading_API" xml:lang="en">
    <refnamediv>
        <refname>Sparse matrix reading (Scilab gateway)</refname>
        <refpurpose>
            How to read sparse matrices in a gateway.
        </refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <para>Input argument profile:</para>
        <synopsis>SciErr getSparseMatrix(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, int* _piNbItem, int** _piNbItemRow, int** _piColPos, double** _pdblReal)</synopsis>
        <synopsis>SciErr getComplexSparseMatrix(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, int* _piNbItem, int** _piNbItemRow, int** _piColPos, double** _pdblReal, double** _pdblImg)</synopsis>
        <para>Named variable profile:</para>
        <synopsis>SciErr readNamedSparseMatrix(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, int* _piNbItem, int* _piNbItemRow, int* _piColPos, double* _pdblReal)</synopsis>
        <synopsis>SciErr readNamedComplexSparseMatrix(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, int* _piNbItem, int* _piNbItemRow, int* _piColPos, double* _pdblReal, double* _pdblImg)</synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>_pvCtx</term>
                <listitem>
                    <para>
                        Scilab environment pointer, pass in "pvApiCtx" provided by api_scilab.h.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_piAddress</term>
                <listitem>
                    <para>
                        Address of the Scilab variable.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_pstName</term>
                <listitem>
                    <para>
                        Name of the variable for "named" functions.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_piRows</term>
                <listitem>
                    <para>
                        Return number of rows.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_piCols</term>
                <listitem>
                    <para>
                        Return number of columns.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_piNbItem</term>
                <listitem>
                    <para>
                        Return number of non zero value.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_piNbItemRow</term>
                <listitem>
                    <para>
                        Return number of item in each rows (size: _iRows).
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_piColPos</term>
                <listitem>
                    <para>
                        Return column position for each item (size: _iNbItem).
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_pdblReal</term>
                <listitem>
                    <para>
                        Return address of real data array (size: _iCols * _iRows)
                        For "Named" function, _pdblReal must be allocated before calling function.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>_pdblImg</term>
                <listitem>
                    <para>
                        Return address of imaginary data array (size: _iCols * _iRows)
                        For "Named" function, _pdblImg must be allocated before calling function.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>SciErr</term>
                <listitem>
                    <para>
                        Error structure where is stored errors messages history and first error number.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>This help describes how sparse matrix can be handled through the Scilab API.</para>
    </refsection>
    <refsection>
        <!--File_gateway: SCI/modules/api_scilab/tests/unit_tests/sparse_reading_api.c-->
        <!--File_scilab: SCI/modules/api_scilab/tests/unit_tests/sparse_reading_api.tst-->
        <!--Lib_name: sparse_reading-->
        <!--Func_list: read_sparse-->
        <title>Gateway Source</title>
        <programlisting role="code_gateway"><![CDATA[
#include "api_scilab.h"
int read_sparse(char *fname,void* pvApiCtx)
{
	SciErr sciErr;
	int i,j,k;
	int* piAddr			= NULL;
	int iRows			= 0;
	int iCols			= 0;
	int iNbItem			= 0;
	int* piNbItemRow	= NULL;
	int* piColPos		= NULL;
	double* pdblReal	= NULL;
	double* pdblImg		= NULL;

    CheckInputArgument(pvApiCtx, 1, 1);

	sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}

	if(isVarComplex(pvApiCtx, piAddr))
	{
		sciErr = getComplexSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &iNbItem, &piNbItemRow, &piColPos, &pdblReal, &pdblImg);
	}
	else
	{
		sciErr = getSparseMatrix(pvApiCtx, piAddr, &iRows, &iCols, &iNbItem, &piNbItemRow, &piColPos, &pdblReal);
	}

	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}

	sciprint("Sparse %d item(s)\n", iNbItem);
	k = 0;

	for(i = 0 ; i < iRows ; i++)
	{
		for(j = 0 ; j < piNbItemRow[i] ; j++)
		{
			sciprint("(%d,%d) = %f", i+1, piColPos[k], pdblReal[k]);
			if(isVarComplex(pvApiCtx, piAddr))
			{
				sciprint(" %+fi", pdblImg[k]);
			}

			sciprint("\n");
			k++;
		}
	}

	//assign allocated variables to Lhs position
	AssignOutputVariable(pvApiCtx, 1) = 0;
	return 0;
}
 ]]></programlisting>
    </refsection>
    <refsection>
        <title>Scilab test script</title>
        <programlisting role="code_scilab"><![CDATA[
sp=sparse([1,2;4,5;3,10],[1 + 2*%i,2 - 3*%i,-3 + 4*%i]);
read_sparse(sp);
 ]]></programlisting>
    </refsection>
</refentry>
