Extreme Optimization > User's Guide > Vector and Matrix Library > Sparse Vectors and Matrices > Sparse Vectors

Extreme Optimization User's Guide

User's Guide

Up: Sparse Vectors and Matrices Next: Sparse Matrices Previous: Sparse Vectors and Matrices Contents

Sparse Vectors

The Extreme Optimization Numerical Libraries for .NET has three types that represent sparse vectors. The SparseVector type is an abstract type that serves as the base class for all types that implement sparse vectors. The SparseGeneralVector is the only type that has a public constructor. Other sparse vector types are used internally to represent rows or columns of a sparse matrix, or subvectors of a sparse vector.

Constructing sparse vectors

The SparseGeneralVector class has four constructors. The first constructor takes one argument: the length of the vector. The second constructor takes an additional parameter: a Double value between 0 and 1 that specifies the proportion of nonzero values that is expected. The third constructor is similar to the second, but takes as its second parameter an integer that specifies the exact capacity.

The example below constructs three sparse vectors, each with one million components. The second and third vectors have an initial capacity of 100 nonzero values:

C# CopyCode imageCopy Code
SparseGeneralVector v1 = new SparseGeneralVector(1000000);
SparseGeneralVector v2 = new SparseGeneralVector(1000000, 1e-4);
SparseGeneralVector v3 = new SparseGeneralVector(1000000, 100);
Visual Basic CopyCode imageCopy Code
Dim v1 As SparseGeneralVector = New SparseGeneralVector(1000000)
Dim v2 As SparseGeneralVector = New SparseGeneralVector(1000000, 1e-4)
Dim v3 As SparseGeneralVector = New SparseGeneralVector(1000000, 100)

The fourth and final constructor takes three parameters. The first is once again the length. The second parameter is an integer array containing the indexes of the nonzero components of the vector. The third parameter is a Double array containing the corresponding values. The lengths of the arrays must be equal, and all indexes must be within the proper range. If the same index occurs multiple times, then the corresponding component equals the sum of the all the values with that index.

The next example constructs a new sparse vector of length one million, with nonzeros at index 10k for k in the range 0 to 5:

C# CopyCode imageCopy Code
int[] indexes = { 1, 10, 100, 1000, 10000 };
double[] values = { 1.0, 10.0, 100.0, 1000.0, 10000.0 };
SparseGeneralVector v4 = new SparseGeneralVector(1000000, indexes, values);
Visual Basic CopyCode imageCopy Code
Dim indexes As Int() = { 1, 10, 100, 1000, 10000 }
Dim values As Double() = { 1.0, 10.0, 100.0, 1000.0, 10000.0 }
Dim v4 As SparseGeneralVector = New SparseGeneralVector(1000000, 100)

Accessing Components

Components of sparse vectors can be accessed in the same way as for any other vector. If a component is set to a nonzero value, the component is automatically added to the list of nonzero components. If the vector was at full capacity, then the capacity is automatically extended.

It is possible to iterate over the nonzero components of a sparse vector efficiently. The NonzeroComponents property returns a collection of IndexValuePair values that can be used to iterate over the nonzero components. The IndexValuePair structure has two read-only fields: Index and Value that contain the index and the value of each successive nonzero.

The code below uses the NonzeroComponents property to list the nonzero components of the vector v4:

C# CopyCode imageCopy Code
foreach(IndexValuePair pair in v4.NonzeroComponents)
    Console.WriteLine("Component {0} = {1}", pair.Index, pair.Value);
Visual Basic CopyCode imageCopy Code
For Each pair As IndexValuePair In v4.NonzeroComponents
    Console.WriteLine("Component {0} = {1}", pair.Index, pair.Value)
Next

Other properties and methods

All properties and methods of vectors are also available for sparse vectors. Nearly all of these methods have optimized sparse implementations. This includes norms, most arithmetic operations including addition, subtraction, scaling and matrix products. For example:

C# CopyCode imageCopy Code
Console.WriteLine("Norm(v4) = {0}", v4.Norm());
Console.WriteLine("Sum(v4) = {0}", v4.GetSum());
Visual Basic CopyCode imageCopy Code
Console.WriteLine("Norm(v4) = {0}", v4.Norm())
Console.WriteLine("Sum(v4) = {0}", v4.GetSum())

Note that some operations convert a sparse vector to a GeneralVector, causing memory to be allocated for all components. Most importantly, static operations that return new vectors, including overloaded operators, usually return a GeneralVector even if the result type can be expected to be sparse. You can avoid this by cloning the sparse vector and calling the corresponding instance method.

Up: Sparse Vectors and Matrices Next: Sparse Matrices Previous: Sparse Vectors and Matrices Contents

Overview
Introduction
Features
Documentation
QuickStart Samples
Sample Applications
Downloads
Get it now!
Download trial version
How to Buy
Information
Resources
Contact Us
Search

"The Extreme Optimization Statistics Library for .NET is a major boon for those doing statistical work in .NET. I strongly recommend this product."
- Marc Brooks

"I have made it my mission to institutionalize the value of good API design.  I strongly believe that this is key to making developers more productive and happy on our platform. It is clear that you value good API design in your work, and take to heart developer productivity and synergy with the .NET framework."
- Brad Abrams,
Lead Program Manager, Microsoft.

This is a partial list of companies who are using our libraries:
ABB Robotics
Allstate
Applied Materials
Arcam
Astra Schedule
Babson College
Canadian Council on Learning
Canyon Associates
Caxton Associates
CECity
Constellation Energy
CreditSights
DeepOcean
Duke University
Dynamotive
Elecsoft
Engelhard Corporation
Epcor
Equipoise Software
Galileo International
GAM UK
Gammex
GlaxoSmithKline
Global Matrix
The Hartford
Infinera Corporation
Intel
JDS Uniphase
LaBranche & Co.
Learning & Skills Council
Jacobs Consultancy
Litman Gregory
Lucas Systems
Malvern Instruments
Medrio
Merck & Co.
Mintera.
Monitor Software
MorningStar
NanoString Technologies
Paletta Invent
Parametric Portfolio Associates
Prosanos
RATA Associates
RiskShield
Ramboll
Standard & Poor's
Strategic Analysis Corporation
Univ. of Alicante
Univ. of South Carolina
vielife
Xerox
US Army