RTableSparseCompact - Maple Programming Help

Online Help

All Products    Maple    MapleSim

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


remove zero entries of a NAG sparse rtable in external code


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


retrieve a pointer to a NAG sparse rtable's index vector in external code


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


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


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


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


Calling Sequence




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 returned by StartMaple



Maple 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 are part of the OpenMaple interface to Microsoft Visual Basic.


The RTableSparseCompact function 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.


The RTableSparseIndexRow function retrieves a copy of 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. ptrRTableSparseIndexRow retrieves the address of the memory used to store the index row entries.


The RTableSparseIndexSort function 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 already sorted. Only newly appended entries plus the unsorted block are sorted and merged into the sorted block. 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.


Public Sub MyFillRight(ByVal kv As Long)

    Dim rt As Long

    Dim rts As RTableSettings

    Dim i, j, size, numelems, p As Long

    ' input

    rt = EvalMapleStatement(kv, _

        "Matrix(6,6,{(1,1)=1, (2,5)=2, (3,3)=3, (3,4)=4, (5,2)=5}, " _

        + "datatype=float[8],storage=sparse[8]);")

    ' ensure we have a 2D sparse float[8] rtable

    RTableGetSettings kv, rts, rt

    numelems = RTableNumElements(kv, rt)

    If rts.num_dimensions = 2 _

      And rts.storage = RTABLE_SPARSE _

      And rts.data_type = RTABLE_FLOAT64 _

      And numelems > 0 _


        Dim row() As Long

        Dim col() As Long

        Dim data() As Double

        ' we want the index vectors sorted

        RTableSparseIndexSort kv, rt, 1

        ' get a copy of the data vector

        data = RTableF64DataBlock(kv, rt)

        ' get the index vectors

        row = RTableSparseIndexRow(kv, rt, 1)

        col = RTableSparseIndexRow(kv, rt, 2)

        ' walk through the data and duplicate elem [i,j] into

        '  [i+1,j] if that spot is not already filled.

        colbound = RTableUpperBound(kv, rt, 1)

        j = numelems

        ReDim newrow(0 To 2 * numelems) As Long

        ReDim newcol(0 To 2 * numelems) As Long

        ReDim newdata(0 To 2 * numelems) As Double

        For i = 0 To numelems - 1

            'copy elem

            newrow(i) = row(i)

            newcol(i) = col(i)

            newdata(i) = data(i)

            'if there is no adjacent entry, insert one

            If col(i) < colbound _

               And (i = numelems - 1 _

                    Or row(i + 1) <> row(i) _

                    Or col(i + 1) <> col(i) + 1) _


                ' insert new element

                newrow(j) = row(i)

                newcol(j) = col(i) + 1

                newdata(j) = data(i)

                j = j + 1

            End If

        Next i

        ' ensure there is enough room to hold the new data

        If j > RTableSparseSize(kv, rt) Then

            RTableSparseResize kv, rt, j

        End If

        ' copy the data and indices back

        p = ptrRTableSparseIndexRow(kv, rt, 1)

        CopyMemory ByVal p, newrow(0), j * 4

        p = ptrRTableSparseIndexRow(kv, rt, 2)

        CopyMemory ByVal p, newcol(0), j * 4

        p = RTableDataBlock(kv, rt)

        CopyMemory ByVal p, newdata(0), j * 8

        ' update the number of elements Maple thinks are stored in the rtable

        RTableSparseSetNumElems kv, rt, j

        ' Note: No need to resort because elements were not reordered,

        '  just appended.  Also no need to remove zeros because

        '  we did not insert any.

        MapleALGEB_Printf1 kv, "updated rtable = %a", rt

    End If

End Sub

See Also