remove zero entries of a NAG sparse rtable in external code - Maple Help

Online Help

All Products    Maple    MapleSim

Home : Support : Online Help : Programming : OpenMaple : C Application Programming Interface : RTable Manipulation : OpenMaple/C/RTableSparse

RTableSparseCompact - remove zero entries of a NAG sparse rtable in external code

RTableSparseIndexRow - retrieve a NAG sparse rtable's index vector in external code

RTableSparseIndexSort - sort a NAG sparse rtable's index vectors in external code

RTableSparseSetNumElems - set the number of stored elements of a NAG sparse rtable in external code

RTableSparseSize - find the size of the data-block of a NAG sparse rtable in external code

RTableSparseResize - resize the data-block of a NAG sparse rtable in external code

Calling Sequence

RTableSparseCompact(kv, rt)

RTableSparseIndexRow(kv, rt, dim)

RTableSparseIndexSort(kv, rt, by_dim)

RTableSparseSetNumElems(kv, rt, num)

RTableSparseSize(kv, rt)

RTableSparseResize(kv, rt, size)




kernel handle of type MKernelVector



type ALGEB rtable object



specify dimension of the rtable



specify dimension of the rtable



number of stored elements in the rtable



number of storable elements in the existing rtable data block



These functions can be used in external code with OpenMaple or define_external.


RTableSparseCompact removes zero entries from a NAG sparse rtable.  Such rtables can be manipulated in external code, but on return to Maple, they must not contain zero entries or duplicates.


RTableSparseIndexRow retrieves the ith index vector from a NAG sparse rtable.  NAG sparse rtables have one index vector for every dimension, plus a data vector.  The ith entries of the index vectors combine to form the index that specifies the ith data entry.


RTableSparseIndexSort sorts the index vectors in a NAG sparse rtable. The by_dim parameter indicates which vector is sorted first.  A value of 1 indicates that the row vector in a 2-D rtable is sorted first. A value of 2 indicates the column vector of the same rtable is sorted first. Internally Maple maintains the index vectors in two sections, the first part is assumed to be sorted, and the second part is unsorted. A sort is automatically triggered when the unsorted section becomes too large.  A value of by_dim = -1 indicates that the order of the first part may have changed, so the entire data vector must be resorted. Otherwise, sorting by row assumes the first block is sorted so only sorts the second block, and then merges the two blocks.  Changing the order without resorting results in unpredictable rtable access from Maple.


NAG sparse rtables usually have space for inserting new elements without reallocating the index and data vectors.  If external code makes use of this space, then RTableSparseSetNumElems must be called to update the internal structure with the new number of elements stored in the rtable.  The size of the data block can be retrieved by calling RTableSparseSize.  To increase or reduce the size of the data block, use RTableSparseResize.


    #include "maplec.h"

    ALGEB M_DECL MyFillRight( MKernelVector kv, ALGEB *args )


    M_INT argc, i, j, colbound, size, numelems;

    RTableSettings rts;

    ALGEB rt;

    NAG_INT *row, *col;

    FLOAT64 *data;

    argc = MapleNumArgs(kv,(ALGEB)args);

    if( argc != 1 ) {

        MapleRaiseError(kv,"one argument expected");

        return( NULL );


    rt = args[1];

    if( !IsMapleRTable(kv,rt) ) {

        MapleRaiseError(kv,"rtable expected");

        return( NULL );



    if( rts.num_dimensions != 2 ) {

        MapleRaiseError(kv,"2D rtable expected");

        return( NULL );


    if( != RTABLE_SPARSE ) {

        MapleRaiseError(kv,"sparse rtable expected");

        return( NULL );


    if( rts.data_type != RTABLE_FLOAT64 ) {

        MapleRaiseError(kv,"float[8] rtable expected");

        return( NULL );


    size = RTableSparseSize(kv,rt);

    numelems = RTableNumElements(kv,rt);

    if( numelems == 0 )

        return( rt );


    if( 2*numelems > size ) {



    data = (FLOAT64*)RTableDataBlock(kv,rt);

    if( !data )

        return( rt );

    row = RTableSparseIndexRow(kv,rt,1);

    col = RTableSparseIndexRow(kv,rt,2);

    colbound = RTableUpperBound(kv,rt,1);

    for( i=0, j=numelems; i<numelems; ++i ) {

        if( col[i] == colbound )


        if( i < numelems-1 && row[i+1] == row[i] && col[i+1] == col[i]+1 )


        row[j] = row[i];

        col[j] = col[i]+1;

        data[j++] = data[i];



    return( rt );


Execute the external function from Maple.














See Also

CustomWrapper, define_external, OpenMaple, OpenMaple/C/API, OpenMaple/C/Examples, rtable

Download Help Document

Was this information helpful?

Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam