You are here: Symbol Reference > Sparse Namespace > Classes > TSparseMtx Class > public > SparseToTriplets Method > TSparseMtx.SparseToTriplets Method (TIntegerArray, TIntegerArray, TDoubleArray)
MtxVec VCL
ContentsIndex
PreviousUpNext
TSparseMtx.SparseToTriplets Method (TIntegerArray, TIntegerArray, TDoubleArray)

Convert sparse matrix to triplets.

Pascal
procedure SparseToTriplets(var DstRow: TIntegerArray; var DstColumn: TIntegerArray; var DstValues: TDoubleArray); overload;

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.

Test sparseToTriplets, TripletsToSparse methods.

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;
Examples on GitHub
Copyright (c) 1999-2025 by Dew Research. All rights reserved.
What do you think about this topic? Send feedback!