Software
Services
News
Company Info
APTL
SCTL
Documentation
Download
Performance
Opstone Benchmarks
 Sparse-Matrix/Sparse-Vector Example SCTL Code
The following is an example of using sparse-vectors and sparse-matricies:
#include <SCTL/Math/Vector.h>
#include <SCTL/Math/SparseVector.h>
#include <SCTL/Math/SparseMatrix.h>

int main( int argc, char** argv )
{
  // Create a sparse vector that can hold 500000 elements
  // Element data is dynamically inserted by issuing 'vec1[idx] = val` commands
  SCTL::SparseVector<fp64_t> vec1( 500000 );
  vec1[123] = 3.0;
  vec1[64039] = -1.0;
  vec1[23495] = 2.0;
  
  // Reset an element back to zero
  // The element data is dynamically deleted in this instance
  vec1[123] = 0.0;
  
  // Create a sparse matrix of size 10000x5000 elements
  // This is just a static vector (row index) of sparse vectors (row vectors)
  SCTL::SparseMatrix<fp64_t> mat( 10000, 5000 );
  mat[1235][1264] = 1.5;
  mat[6363][3462] = 2.0;
  mat[2363][4932] = mat[1235][1264];
  
  // Create a static vector of 10000 elements
  SCTL::StaticVector<fp64_t> vec2( 10000, 0.0 );
  vec2[2363] = 3.0;
  vec2[8334] = 1.0;
  
  // Multiply sparse matrix by static vector
  SCTL::StaticVector<fp64_t> vec3 = mat * vec2;
  
  // Create a sparse-columns matrix of size 5000x8000 elements
  // (Still uses [row][column] indexing)
  SCTL::SparseColumnMatrix<fp64_t> mat2( 5000, 8000 );
  mat[3523][7431] = -2.5;
  
  // Multiply sparse-columns matrix by sparse matrix
  SCTL::SparseMatrix<fp64_t> mat3 = mat1 * mat2;

  return( 0 );
}
NOTES:

- Sparse-vector is implemented using a sorted-list of small, dense vectors whereby individual non-zero elements are dynamically set/removed.
- Sparse-matrix uses a row-major format of sparse row-vectors.
- Individual elements set back to zero can/will be deleted if possible.
top of page
   Info Copyright ©2004 Blue Sail Software. All rights reserved. Legal