**19.1****
****Sparse Vectors** (.NET, C#, CSharp, VB, Visual Basic, F#)

**NMath**
provides four classes for storing sparse vectors:

● Class **FloatSparseVector** stores a sparse vector
of float values.

● Class **DoubleSparseVector** stores a sparse vector
of double values.

● Class **FloatComplexSparseVector** stores a sparse
vector of **FloatComplex** values.

● Class **DoubleComplexSparseVector** stores a sparse
vector of **DoubleComplex** values.

Only the non-zero elements are stored.

Class **SparseVectorData**
stores sparse vector data, and is parameterized on the type, *T,* of values stored in the vector. The nonzero
elements of the vector are stored in a resizable array of type *T*, and their corresponding indexes are stored
in a separate, parallel array of integers.

For example, the vector

v = ( 0 0 1.15 0 3.14 0 0 -2.23 0 0 )

is stored as

values = ( 1.15, 3.14, -2.23 );

indices = ( 2, 4, 7 );

The sparse vector classes extend **SparseVectorData**.

Instances of sparse vectors are created in two ways:
by providing the necessary storage arrays to constructors, or by *gathering* data from a dense vector. For
example, this code uses a **FloatSparseVector**
constructor:

Code Example – C# sparse vector

var indices = new IndexArray( 1, 12, 2, 15 );

var values = new float[] { 2, 3.14, -4, -.6 };

var sv = new FloatSparseVector( values, indices );

Code Example – VB sparse vector

Dim Indices As New IndexArray(1, 12, 2, 15)

Dim Values = New Single() {2.0, 3.14, -4.0, -0.6}

Dim SV As New FloatSparseVector(Values, Indices)

This code uses the MatrixFunctions.Gather()
method to create a **DoubleSparseVector**
from the non-zero elements in a **DoubleVector**
v:

Code Example – C# sparse vector

DoubleSparseVector sv = MatrixFunctions.Gather( v );

Code Example – VB sparse vector

Dim SV As DoubleSparseVector = MatrixFunctions.Gather(V)

You can also create a sparse vector by selecting specific elements from a dense vector. For instance, this code creates a sparse vector containing the specified values from v:

Code Example – C# sparse vector

DoubleSparseVector sv = MatrixFunctions.Gather( v, indices );

Code Example – VB sparse vector

Dim SV As DoubleSparseVector = MatrixFunctions.Gather(Y, Indices)

**Accessing and Modifying Sparse
Vector Values**

The sparse vector classes inherit the following properties
from **SparseVectorData**:

● Entries gets and set the array of non-zero entries.

● Indices gets and sets the array of indices of the non-zero elements.

● NumberNonZero gets and sets the number of non-zero elements.

The sparse vector classes also provide standard indexers for getting and setting individual element values.

Code Example – C# sparse vector

int nnz = 3;

var sv = new DoubleSparseVector( nnz );

sv[4] = 10;

sv[100] = 3;

Code Example – VB sparse vector

Dim NNZ As Integer = 3

Dim SV As New DoubleSparseVector(NNZ)

SV(4) = 10

SV(100) = 3

Operator == tests for equality of two sparse vectors, and returns true if both vecrtors have the same nonzero elements; otherwise, false. Following the convention of the .NET Framework, if both objects are null, they test equal. Operator != returns the logical negation of ==. The Equals() member function also tests for equality.

**NMath**
provides overloaded arithmetic operators for general sparse vectors with
their conventional meanings for those .NET languages that support them,
and equivalent named methods for those that do not. All binary operators
and equivalent named methods work either with two vectors, or with a
vector and a scalar.

Code Example – C# sparse vector

double a = 3.18;

var sv1 = new DoubleSparseVector( data, indices );

DoubleSparseVector sv2 = a * sv1;

Code Example – VB sparse vector

Dim A As Double = 3.18

Dim SV1 As New DoubleSparseVector(Data, Indices)

Dim SV2 As DoubleSparseVector = A * SV1

The sparse vector classes provide the following member functions that operate on the elements of the vector:

● TwoNorm() computes the Euclidean norm of the elements of a sparse vector.

● Scale() scales each element in a sparse vector by the specified value.

**MatrixFunctions**
also provides a variety of functions that take general sparse vectors
as arguments:

● AbsSum calculates the sum of the absolute value of a given vector's elements.

● MaxAbsIndex calculates the index of the maximum absolute value a given the vector's elements.

● MinAbsIndex calculates the index of the minimum absolute value of a sparse vector's elements.

● Dot calculates the dot product of a sparse vector and a dense vector.

For example:

Code Example – C# sparse vector

double sumOfAbsValues = MatrixFunctions.AbsSum( sv );

double maxAbsValueIndex = MatrixFunctions.MaxAbsIndex( sv );

var w = new DoubleVector( 66, 1.2 );

double dot = MatrixFunctions.Dot( w, sv );

Code Example – VB sparse vector

Dim SumOfAbsValues As Double = MatrixFunctions.AbsSum(SV)

Dim MaxAbsValueIndex As Integer = MatrixFunctions.MaxAbsIndex(SV)

Dim W As New DoubleVector(66, 1.2)

Dim Dot As Double = MatrixFunctions.Dot(W, SV)

**Creating Dense Vectors from
Sparse Vectors**

The MatrixFunctions.Scatter() method scatters the elements of a compressed sparse vector to a full storage vector. For example, this code constructs a dense vector from a sparse vector by specifying the length of the dense vector and scattering the nonzero values from the sparse vector into the dense vector:

Code Example – C# sparse vector

DoubleVector v = MatrixFunctions.Scatter( sv, 20 );

Code Example – VB sparse vector

Dim Y As DoubleVector = MatrixFunctions.Scatter(SV, 20)