TSparseMtx.SparseToTriplets Method

Overload List

#SignatureDescription
1procedure SparseToTriplets(const DstTriplets: TMtx; ImInRow: Boolean);Convert sparse matrix to triplets.
2procedure SparseToTriplets(var dstRow: TIntegerArray; var DstColumn: TIntegerArray; var DstCValues: TCplxArray);Convert sparse matrix to triplets (complex version).
3procedure SparseToTriplets(var dstRow: TIntegerArray; var DstColumn: TIntegerArray; var DstValues: TDoubleArray);Convert sparse matrix to triplets.

Overload 1: procedure SparseToTriplets(const DstTriplets: TMtx; ImInRow: Boolean);

Convert sparse matrix to triplets.

#NameTypeDescription
1DstTripletsTMtx
2ImInRowBoolean

Result: stored in self (calling object)

Remarks:

Triplets are stored in the first three rows of the matrix. First row stores the row indices, the second stores the column indices and the third row stores the matrix values. In case of a complex matrix and if ImInRow is True, the imaginary part's of the complex numbers are stored in the fourth row.

Overload 2: procedure SparseToTriplets(var dstRow: TIntegerArray; var DstColumn: TIntegerArray; var DstCValues: TCplxArray);

Convert sparse matrix to triplets (complex version).

#NameTypeDescription
1dstRowTIntegerArray
2DstColumnTIntegerArray
3DstCValuesTCplxArray

Result: stored in self (calling object)

Overload 3: procedure SparseToTriplets(var dstRow: TIntegerArray; var DstColumn: TIntegerArray; var DstValues: TDoubleArray);

Convert sparse matrix to triplets.

#NameTypeDescription
1dstRowTIntegerArray
2DstColumnTIntegerArray
3DstValuesTDoubleArray

Result: stored in self (calling object)

Remarks:

A sparse matrix can also be presented as pairs of the three elements called triplets. For each non zero value in the matrix we specify it's position: Row, Column, and it's Value. SparseToTriplets will convert the calling sparse matrix data to the triplets format. The data will be sorted first by columns and then by rows. Triplets can be represented by three arrays: Row, Column and Values or they can all be stored in one TMtx matrix.

Examples
uses MtxExpr, Sparse, Math387;

procedure Example;
var R1,C1: TIntArray;
V1: TDoubleArray;
SparseA, SparseB: TSparseMtx;
A: Matrix;
begin
    SparseA := TSparseMtx.Create;
    SparseB := TSparseMtx.Create;
    try
        // setup sparse , rows, cols, non-zero elements
        SetLength(R1,SparseA.NonZeros);
        SetLength(C1,SparseA.NonZeros);
        SetLength(V1,SparseA.NonZeros);
        SparseA.SparseToTriplets(R1,C1,V1);
        SparseB.TripletsToSparse(SparseA.Cols,SparseA.Cols,R1,C1,V1);

        SparseA.SparseToDense(A);
        if not SparseB.Equal(SparseA,1E-5) then ERaise('Not equal');
        if not SparseA.Equal(A,0,True) then ERaise('Not equal');

        SparseA.SparseToTriplets(A);
        SparseB.TripletsToSparse(SparseA.Cols,SparseA.Cols,A);
        if not SparseA.Equal(SparseB) then ERaise('Not equal');
    finally
        SparseA.Free;
        SparseB.Free;
    end;
end;
See Also: TSparseMtx.TripletsToSparse