Sparse Matrices

First, methods and functions related to sparse matrices stored as a vector of sparse rows are detailed. This type of matrix is interesting because it allows fast insertion of elements in the matrix. Then the matrices using Harwell-Boeing format are detailed. Functions related to sparse matrices are explained. We advise the user to use always storage of matrix by rows, since this last storage is more efficient for the matrix-vector product.

Basic declaration of easily modifiable sparse matrices:

These matrices are available only after SeldonSolver.hxx has been included.

Classes :

Matrix<T, Prop, ArrayRowSparse>
Matrix<T, Prop, ArrayColSparse>
Matrix<T, Prop, ArrayRowSymSparse>
Matrix<T, Prop, ArrayColSymSparse>

Example :

  
// sparse matrix of doubles
Matrix<double, General, ArrayRowSparse> A;

// sparse symmetric matrix
Matrix<float, Symmetric, ArrayRowSymSparse> B;

Methods for easily modifiable sparse matrices :

Matrix constructors
Matrix operators
GetM returns the number of rows in the matrix
GetN returns the number of columns in the matrix
GetDataSize returns the number of elements effectively stored
GetNonZeros returns the number of elements effectively stored
GetData returns a pointer to the array containing the values
GetIndex returns a pointer to the array containing column numbers
SetData sets the pointer to the arrays containing values and column numbers
Nullify removes elements of the matrix without releasing memory
Clear removes all elements of the matrix
Reallocate changes the size of matrix (does not keep previous elements)
Resize changes the size of matrix (keeps previous elements)
Value direct access to a value
Index direct access to a column number
Zero sets all elements to zero
SetIdentity sets matrix to identity matrix
Fill sets all elements to a given value
FillRand fills randomly the matrix
ReallocateRow / ReallocateColumn changes the size of a row
ResizeRow / ResizeColumn changes the size of a row and keeps previous values
ClearRow / ClearColumn clears a row
SwapRow / SwapColumn exchanges two rows
ReplaceIndexRow / ReplaceIndexColumn changes column numbers
GetRowSize / GetColumnSize returns the number of elements in the row
PrintRow / PrintColumn prints a row
AssembleRow / AssembleColumn assembles a row
AddInteraction adds/inserts an element in the matrix
AddInteractionRow adds/inserts an element in a matrix row
AddInteractionColumn adds/inserts elements in a matrix column
Print displays the matrix
Write writes the vector in binary format
Read reads the vector in binary format
WriteText writes the vector in text format
ReadText reads the vector in text format

Another class of storage of sparse matrices is available, the so-called Harwell-Boeing format. The values are stored in a single array, preventing from a fast insertion of elements. Nevertheless, this type of storage requires slightly less memory and the associated matrix-vector product is more efficient.

Basic declaration of Harwell-Boeing sparse matrices:

Classes :

Matrix<T, Prop, RowSparse>
Matrix<T, Prop, ColSparse>
Matrix<T, Prop, RowSymSparse>
Matrix<T, Prop, ColSymSparse>

Example :

  
// sparse matrix of doubles
Matrix<double, General, RowSparse> A;

// sparse symmetric matrix
Matrix<float, Symmetric, RowSymSparse> B;

Methods for Harwell-Boeing sparse matrices :

Matrix constructors
Matrix operators
GetM returns the number of rows in the matrix
GetN returns the number of columns in the matrix
GetDataSize returns the number of elements effectively stored
GetNonZeros returns the number of elements effectively stored
GetData returns a pointer to the array containing the values
GetInd returns a pointer to the array containing column numbers
GetPtr returns a pointer to the array containing row numbers
GetPtrSize returns size of array Ptr
GetIndSize returns size of array Ind
Clear removes all elements of the matrix
SetData sets the pointer to the array containing the values
Nullify clears the matrix without releasing memory
Print displays the matrix
Write writes the vector in binary format
Read reads the vector in binary format
WriteText writes the vector in text format
ReadText reads the vector in text format

Functions :

Mlt multiplication by a scalar or matrix-vector product
MltAdd performs a matrix-vector product
Add adds two matrices
Copy copies/converts one matrix into another one
PermuteMatrix applies permutation to matrix
ScaleMatrix applies a scaling both for columns and rows
ScaleLeftMatrix applies a left scaling (on rows)
GetRow returns a matrix row
SetRow changes a matrix row
GetCol returns a matrix column
SetCol changes a matrix column
GetLU performs a LU (or LDL^t) factorization
SolveLU solves linear system by using LU factorization
SOR applies successive over-relaxations to matrix
Cg, Gmres, BiCgSTAB, etc solves iteratively a linear system

Matrix constructors for ArrayRowSparse, ArrayRowSymSparse

Syntax :

  Matrix();
  Matrix(int m, int n);

Example :

  
// default constructor -> empty matrix
Matrix<double, General, ArrayRowSparse> V;
cout << "Number of elements "<< V.GetDataSize() << endl; // should return 0 
// then you can use Reallocate to set the number of rows and columns
V.Reallocate(3, 2);
// the last command doesn't create any non-zero element
// you can create one with AddInteraction for example
V.AddInteraction(0, 1, 1.5);

// we construct symmetric matrix with 4 rows
Matrix<double, Symmetric, ArrayRowSymSparse> W(4, 4);

Related topics :

Reallocate
AddInteraction

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

Matrix constructors for RowSparse, RowSymSparse

Syntax :

  Matrix();
  Matrix(int m, int n);
  Matrix(int m, int n, int nnz);
  Matrix(m, n, values, ptr, ind);

Example :

  
// default constructor -> empty matrix
Matrix<double, General, RowSparse> V;
cout << "Number of elements "<< V.GetDataSize() << endl; // should return 0 

// the two other constructors are useless
Matrix<double, Symmetric, ArrayRowSymSparse> W(4, 4);
Matrix<double, General, RowSparse> A(3, 4, 10);

// in the last constructor you provide
int m = 5; // number of rows
int n = 6; // number of columns
int nnz = 12; // number of non-zero entries
Vector<double> values(nnz); // values
values.FillRand(); // you have to initialize values
Vector<int> columns(nnz); // for each value column number
// for each row, column numbers are sorted
// first row
columns(0) = 0;
columns(1) = 2;
columns(2) = 4;
// second row
columns(3) = 1;
// third row
columns(4) = 0;
columns(5) = 1;
columns(6) = 3;
// fourth row
columns(7) = 4;
columns(8) = 5;
// last row
columns(9) = 2;
columns(10) = 3;
columns(11) = 5;
// for each row, index of first value
Vector<int> ptr(m+1); 
ptr(0) = 0;
ptr(1) = 3;
ptr(2) = 4;
ptr(3) = 7;
ptr(4) = 9;
ptr(5) = 12;
// values on the row are to be seeked between ptr(i) and ptr(i+1)

// Now the constructor:
Matrix<double, General, RowSparse> M(m, n, values, ptr, columns);
// Note that 'values', 'ptr' and 'columns' are nullified (that is, emptied):
// the arrays to which they pointed have been transferred to M.

Related topics :

Reallocate
SetData

Location :

Class Matrix_Sparse
Class Matrix_SymSparse
Matrix_Sparse.hxx Matrix_Sparse.cxx
Matrix_SymSparse.hxx Matrix_SymSparse.cxx

Matrix operators for ArrayRowSparse, ArrayRowSymSparse

Syntax :

  const T& operator (int i, int j) const;
  T& operator (int i, int j);
  Matrix& operator =(const T0& alpha)

The operator () can be used to insert or modify elements of matrix.

Example :

  
Matrix<double, General, ArrayRowSparse> V(3, 3);
// use of operator () to modify matrix
V(0, 0) = 2.0;
V(1, 0) = V(0, 0) + 1.0;

// set all elements to a given value
V = 1;

Related topics :

AddInteraction
Fill

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

Matrix operators for RowSparse, RowSymSparse

Syntax :

  T operator (int i, int j) const;

The operator () cannot be used to insert or modify elements of matrix.

Example :

  
Matrix<double, General, RowSparse> V(3, 3);
// use of operator () to read elements of matrix
cout << "V(0,0) = " << V(0, 0) << endl;

Related topics :

SetData

Location :

Class Matrix_Sparse
Class Matrix_SymSparse
Matrix_Sparse.hxx Matrix_Sparse.cxx
Matrix_SymSparse.hxx Matrix_SymSparse.cxx

GetM

Syntax :

  int GetM() const;

This method returns the number of rows.

Example :

  
Matrix<float, General, ArrayRowSparse> V(3, 2);
// V.GetM() should return 3 
cout << "Number of rows of V " << V.GetM() << endl;

Location :

Class Matrix_Base
Class Matrix_ArraySparse
Matrix_Base.hxx Matrix_Base.cxx
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

GetN

Syntax :

  int GetN() const;

This method returns the number of columns

Example :

  
Matrix<float, Symmetric, ArrayRowSymSparse> V(3, 2);
// V.GetN() should return 2 
cout << "Number of rows of V " << V.GetN() << endl;

Location :

Class Matrix_Base
Class Matrix_ArraySparse
Matrix_Base.hxx Matrix_Base.cxx
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

GetDataSize, GetNonZeros

Syntax :

  int GetDataSize() const;
  int GetNonZeros() const;

These methods returns the number of elements effectively stored in the matrix.

Example :

  
Matrix<float, Symmetric, ArrayRowSymSparse> V(3, 3);
V.AddInteraction(0, 1, 1.0);
// V.GetDataSize() should return 1
cout << "Number of elements of V " << V.GetSize() << endl;

Location :

Class Matrix_Sparse
Class Matrix_SymSparse
Class Matrix_ArraySparse
Matrix_Sparse.hxx Matrix_Sparse.cxx
Matrix_SymSparse.hxx Matrix_SymSparse.cxx
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

GetData for ArrayRowSparse, ArrayRowSymSparse

Syntax :

  T* GetData(int i) const;
  Vector<T, Vect_Sparse, Allocator>* GetData() const;
 

These methods are useful to retrieve the pointer to the values, but they need to be used with caution.

Example :

  
Matrix<double, General, ArrayRowSparse> V(3, 4); V.Fill();
// values of second row are retrieved
int irow = 1;
double* data = V.GetData(irow);
// you can use data as a normal C array
// here the sum of elements of the row is computed
double sum = 0;
for (int i = 0; i < V.GetRowSize(irow); i++)
  sum += data[i];

// you can also retrieve all the rows
Vector<double, Vect_Sparse>* all_rows = V.GetData();

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

Clear

Syntax :

  void Clear();

This method removes all the elements of the matrix.

Example :

  
Matrix<double, General, ArrayRowSparse> A(3, 3);
A.AddInteraction(0, 2, 2.8);
// clears matrix A
A.Clear();

Location :

Class Matrix_Sparse
Class Matrix_SymSparse
Class Matrix_ArraySparse
Matrix_Sparse.hxx Matrix_Sparse.cxx
Matrix_SymSparse.hxx Matrix_SymSparse.cxx
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

Reallocate

Syntax :

  void Reallocate(int, int);

This method changes the size of the matrix, but removes previous elements.

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 4);
A.AddInteraction(0, 2, 2.8);
// resizes matrix A
A.Reallocate(4, 3);
// previous elements are removed : A is empty
cout << "number of non-zero entries " << A.GetDataSize() << endl;

Related topics :

Resize

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

Resize

Syntax :

  void Resize(int, int);

This method changes the size of the matrix, and keeps previous elements.

Example :

  
Matrix<double, Symmetric, ArrayRowSymSparse> A(4,4);
A(1,3) = 2.0;
A(0,2) = -1.0;
// resizes matrix A and keeps previous added interactions
A.Resize(5,5);
// GetDataSize() should return 2
cout << "number of non-zero entries of A " << A.GetDataSize() << endl;

Related topics :

Reallocate

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

SetData for ArrayRowSparse, ArrayRowSymSparse

Syntax :

  void SetData(int, int, T*, int*);
  void SetData(int, int, Vector<T, Vect_Sparse>*);

This method changes a row of the matrix or all the rows by directly changing pointers. This is a low-level method and should be used very cautiously. It is better to use other methods to modify the matrix.

Example :

  
// first use, you construct all the rows
int m = 10, n = 9;
Vector<Vector<double, Vect_Sparse> > rows(m);

rows(0).AddInteraction(1, 2.4);
rows(1).AddInteraction(3, -1.2);
// ...

// then you can feed a sparse matrix with rows
Matrix<double, General, ArrayRowSparse> A;
A.SetData(m, n, rows.GetData());
// you nullify rows to avoid segmentation fault
rows.Nullify();

// second use, you specify one row
Vector<double, Vect_Sparse> one_row;
one_row.AddInteraction(4, 2.3);
one_row.AddInteraction(0, -0.7);
// and you put it in the matrix
int irow = 2;
A.SetData(irow, one_row.GetM(), one_row.GetData(), one_row.GetIndex());
one_row.Nullify();

Related topics :

GetData
Nullify

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

Nullify for ArrayRowSparse, ArrayRowSymSparse

Syntax :

  void Nullify();
  void Nullify(int);

This method clears the matrix without releasing memory. You can also clear one single row. This method should be used carefully, and generally in conjunction with method SetData.

Example :

  
Matrix<double, General, ArrayRowSparse> A;
// you retrieve one row of A
int irow = 2;
Vector<double, Vect_Sparse> one_row;
one_row.SetData(A.GetRowSize(irow), A.GetData(irow), A.GetIndex(irow));
// you need to call Nullify to avoid segmentation fault
A.Nullify(irow);

// you can retrieve all the rows
Vector<Vector<double, Vect_Sparse> > rows;
rows.SetData(m, A.GetData());
// again Nullify is necessary
A.Nullify();

Related topics :

SetData
GetData

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

GetIndex for ArrayRowSparse, ArrayRowSymSparse

Syntax :

  void GetIndex(int);

This method returns the pointer to the array containing column numbers of the specified row.

Example :

  
Matrix<double, General, ArrayRowSparse> A;
// you retrieve column numbers of row 1
IVect col;
int irow = 1;
col.SetData(A.GetRowSize(irow), A.GetIndex(irow));
// after use of col, don't forget to call Nullify
col.Nullify();

Related topics :

SetData
GetData

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

SetData for RowSparse, RowSymSparse

Syntax :

  void SetData(int, int, int, T*, int*, int*);
  void SetData(int, int, Vector<T>, Vector<int>, Vector<int>);

This method initializes the matrix and is equivalent to a call to the constructor.

Example :

  
// first you construct the matrix by using arrays ptr, values, columns
int m = 5; // number of rows
int n = 6; // number of columns
int nnz = 12; // number of non-zero entries
Vector<double> values(nnz); // values
values.FillRand(); // you have to initialize values
Vector<int> columns(nnz); // for each value column number
// for each row, column numbers are sorted
// first row
columns(0) = 0;
columns(1) = 2;
columns(2) = 4;
// second row
columns(3) = 1;
// third row
columns(4) = 0;
columns(5) = 1;
columns(6) = 3;
// fourth row
columns(7) = 4;
columns(8) = 5;
// last row
columns(9) = 2;
columns(10) = 3;
columns(11) = 5;
// for each row, index of first value
Vector<int> ptr(m+1); 
ptr(0) = 0;
ptr(1) = 3;
ptr(2) = 4;
ptr(3) = 7;
ptr(4) = 9;
ptr(5) = 12;

// then you call SetData
Matrix<double, General, RowSparse> A;
A.SetData(m, n, values, ptr, columns);

Related topics :

GetData
Nullify

Location :

Class Matrix_Sparse
Class Matrix_SymSparse
Matrix_Sparse.hxx Matrix_Sparse.cxx
Matrix_SymSparse.hxx Matrix_SymSparse.cxx

Nullify

Syntax :

  void Nullify();

This method clears the matrix without releasing memory. This method should be used carefully, and generally in conjunction with method SetData. You can look at the example shown in the explanation of method SetData.

Related topics :

SetData
GetData

Location :

Class Matrix_Sparse
Class Matrix_SymSparse
Matrix_Sparse.hxx Matrix_Sparse.cxx
Matrix_SymSparse.hxx Matrix_SymSparse.cxx

GetData for RowSparse, RowSymSparse

Syntax :

  T* GetData() const;
 

This method retrieves the pointer to the values.

Example :

  
// construction of a sparse matrix
Matrix<double, General, RowSparse> V(m, n, nnz, values, ptr, columns);
// values of non-zero entries are retrieved
double* data = V.GetData();
// you can use data as a normal C array
// here the sum of elements is computed
double sum = 0;
for (int i = 0; i < V.GetDataSize(); i++)
  sum += data[i];

Related topics :

SetData
Nullify

Location :

Class Matrix_Base
Matrix_Base.hxx Matrix_Base.cxx

GetPtr, GetPtrSize for RowSparse, RowSymSparse

Syntax :

  int* GetPtr() const;
  int GetPtrSize() const;
 

This method retrieves the pointer to the indices of the first element of each row.

Example :

  
// construction of a sparse matrix (m = number of rows)
Matrix<double, General, RowSparse> V(m, n, nnz, values, ptr, columns);
// we get indices of the first element of each row
int* ptr = V.GetPtr();
// and also the size of this array (should be number of rows + 1)
int size = V.GetPtrSize();
// you can use data as a normal C array
// here the number of elements of each row is computed
Vector<int> size_row(m);
for (int i = 0; i < m; i++)
  size_row(i) = ptr[i+1] - ptr[i];

Related topics :

SetData
GetData
GetInd

Location :

Class Matrix_Sparse
Class Matrix_SymSparse
Matrix_Sparse.hxx Matrix_Sparse.cxx
Matrix_SymSparse.hxx Matrix_SymSparse.cxx

GetInd, GetIndSize for RowSparse, RowSymSparse

Syntax :

  int* GetInd() const;
  int GetIndSize() const;
 

This method retrieves the pointer to the column numbers of each row.

Example :

  
// construction of a sparse matrix (m = number of rows)
Matrix<double, General, RowSparse> V(m, n, nnz, values, ptr, columns);
// we get column numbers
int* ind = V.GetInd();
// and also the size of this array (should be nnz)
int size = V.GetIndSize();
// you can use data as a normal C array
// here the number of elements of each column is computed
Vector<int> size_col(n);
size_col.Zero();
for (int i = 0; i < nnz; i++)
  size_col(ind[i])++;

Related topics :

SetData
GetData
GetPtr

Location :

Class Matrix_Sparse
Class Matrix_SymSparse
Matrix_Sparse.hxx Matrix_Sparse.cxx
Matrix_SymSparse.hxx Matrix_SymSparse.cxx

Index

Syntax :

  int Index(int, int) const;
  int& Index(int, int);
 

This method allows the user to modify directly the column numbers. The drawback of this method is that the user has to take care that the column numbers are sorted and unique (no duplicate).

Example :

  
// declaration of a sparse matrix
int m = 5;
int n = 4;
Matrix<double, General, ArrayRowSparse> V(m, n);

// we set the number of non-zero entries of first row
V.ReallocateRow(0, 2);
// you initialize column numbers with Index, and values with Value
V.Index(0, 0) = 1; V.Value(0, 0) = 0.5;
V.Index(0, 1) = 3; V.Value(0, 1) = -0.5;
// now matrix should be equal to [0 1 -0.5; 0 3 -0.5]
cout << " V = " << V << endl;

// if you add a new value on the first row
V.ResizeRow(0, 3);
V.Index(0, 2) = 0; V.Value(0, 2) = 1.1;
// you better be careful because the new entry is not at the correct
position
// one way to fix that problem is to call Assemble
V.AssembleRow(0);

Related topics :

ReallocateRow
ResizeRow
AssembleRow
AddInteractionRow
Value

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

Value

Syntax :

  const T& Value(int, int) const;
  T& Value(int, int);
 

This method allows the user to modify directly the values.

Example :

  
// declaration of a sparse matrix
int m = 5;
int n = 4;
Matrix<double, General, ArrayRowSparse> V(m, n);

// we set the number of non-zero entries of first row
V.ReallocateRow(0, 2);
// you initialize column numbers with Index, and values with Value
V.Index(0, 0) = 1; V.Value(0, 0) = 0.5;
V.Index(0, 1) = 3; V.Value(0, 1) = -0.5;
// now matrix should be equal to [0 1 -0.5; 0 3 -0.5]
cout << " V = " << V << endl;

Related topics :

ReallocateRow
Index

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

Zero

Syntax :

  void Zero();

This method sets to 0 all values contained in non-zero entries. This is useful when you want to keep the pattern of the sparse matrix, but initialize values to 0.

Example :

  
Matrix<double, General, ArrayRowSparse> V(5, 3);
// creation of pattern
V.AddInteraction(0, 3, 1.0);
V.AddInteraction(2, 4, -1.0);

// values are set to 0
V.Zero();

// then you can increment values of the pattern
V(0, 3) += 1.5;

Related topics :

Fill

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

SetIdentity

Syntax :

  void SetIdentity();

This method sets all elements to 0, except on the diagonal set to 1. This forms the so-called identity matrix.

Example :

  
Matrix<double, Symmetric, ArrayRowSymSparse> V(5, 5);
// initialization
V.SetIdentity();

Related topics :

Fill

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

Fill

Syntax :

  void Fill();
  void Fill(const T0& );

This method fills matrix with 0, 1, 2, etc or with a given value.

Example :

  
Matrix<double, General, ArrayRowSparse> A(5,5);
A(1,2) = 3;
A(2,4) = 5;
A(3,0) = 6;
A.Fill();
// A should contain [1 2 0.0, 2 4 1.0, 3 0 2.0]

A.Fill(2);
// A should contain [1 2 2.0, 2 4 2.0, 3 0 2.0]

Related topics :

Zero

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

FillRand

Syntax :

  void FillRand();

This method sets values of non-zero entries randomly.

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 3);
A(1,2) = 0.1;
A(2,4) = 0.1;
A.FillRand();
// A should contain 2 non-zero entries with random values

Related topics :

Fill

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

ReallocateRow / ReallocateColumn

Syntax :

  void ReallocateRow(int, int );

ReallocateColumn is the name used for storages ArrayColSparse and ArrayColSymSparse. This method creates n non-zero entries in specified row.

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 6);
// for second row, we create 3 non-zero entries
int irow = 1;
int n = 3;
A.ReallocateRow(irow, n);
// you need to initialize elements of the row
A.Index(irow, 0) = 0; A.Value(irow, 0) = 0.5;
A.Index(irow, 1) = 2; A.Value(irow, 1) = -0.4;
A.Index(irow, 2) = 3; A.Value(irow, 2) = 0.6;

Related topics :

Index
Value
ResizeRow

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

ResizeRow / ResizeColumn

Syntax :

  void ResizeRow(int, int );

ResizeColumn is the name used for storages ArrayColSparse and ArrayColSymSparse. This method creates n non-zero entries in specified row, and keeps previous elements.

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 6);
// for second row, we create 3 non-zero entries
int irow = 1;
int n = 3;
A.ReallocateRow(irow, n);
// you need to initialize elements of the row
A.Index(irow, 0) = 0; A.Value(irow, 0) = 0.5;
A.Index(irow, 1) = 2; A.Value(irow, 1) = -0.4;
A.Index(irow, 2) = 3; A.Value(irow, 2) = 0.6;

// you can change the size of the row
A.ResizeRow(irow, n+1);
// you need to initialize only the new entry
A.Index(irow, 3) = 5; A.Index(irow, 3) = 3.5;

Related topics :

Index
Value
ReallocateRow

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

ClearRow / ClearColumn

Syntax :

  void ClearRow(int,);

ClearColumn is the name used for storages ArrayColSparse and ArrayColSymSparse. This method removes non-zero entries in specified row.

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 6);
A(1, 3) = 4.0; // one non-zero entry in second row
// we clear second row
A.ClearRow(1);

Related topics :

Index
Value
ResizeRow

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

SwapRow / SwapColumn

Syntax :

  void ClearRow(int);

SwapColumn is the name used for storages ArrayColSparse and ArrayColSymSparse. This method swaps two rows. This method should be used very cautiously for symmetric matrices, since in that case only upper part of the matrix is stored.

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 6);
A(1, 3) = 4.0; // one non-zero entry in second row
A(2, 4) = -1.0;
// we swap third and second row
A.SwapRow(1, 2);

Related topics :

PermuteMatrix

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

ReplaceIndexRow / ReplaceIndexColumn

Syntax :

  void ReplaceIndexRow(int, Vector<int>&);

ReplaceIndexColumn is the name used for storages ArrayColSparse and ArrayColSymSparse. This method changes column numbers of the specified row.

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 6);
A(1, 3) = 4.0;
A(1, 4) = -1.0;
// we change column numbers of second row
IVect new_number(2);
new_number(0) = 0;
new_number(1) = 2;
A.ReplaceIndexRow(1, new_number);

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

GetRowSize / GetColumnSize

Syntax :

  void GetRowSize(int) const;

GetColumnSize is the name used for storages ArrayColSparse and ArrayColSymSparse. This method returns the number of non-zero entries in the specified row.

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 6);
A(1, 3) = 4.0; 
A(1, 4) = -1.0;
// two non-zero entries in second row
cout << "Number of elements in second row " << A.GetRowSize(1) << endl;

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

PrintRow / PrintColumn

Syntax :

  void PrintRow(int) const;

PrintColumn is the name used for storages ArrayColSparse and ArrayColSymSparse. This method returns the number of non-zero entries in the specified row.

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 6);
A(1, 3) = 4.0; 
A(1, 4) = -1.0;
// we print second row
cout << "Second row of A " << A.PrintRow(1) << endl;

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

AssembleRow / AssembleColumn

Syntax :

  void AssembleRow(int) const;

AssembleColumn is the name used for storages ArrayColSparse and ArrayColSymSparse. This method sorts and merges non-zero entries of a row. You don't need to call this method if you are using methods AddInteraction, AddInteractionRow or the access operator ().

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 5);
A.ReallocateRow(1, 3);
// we initialize non-zero entries but non-sorted and with duplicates
A.Index(1, 0) = 4; A.Value(1, 0) = 1.2;
A.Index(1, 1) = 2; A.Value(1, 1) = -0.7;
A.Index(1, 2) = 4; A.Value(1, 2) = 2.5;

// we sort column numbers of row 1
A.AssembleRow(1);
// now A should be equal to [1 2 -0.7, 1 4 3.7]

Related topics :

Index
Value
AddInteraction

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

AddInteraction

Syntax :

  void AddInteraction(int, int, T);

This methods adds/inserts a value in the matrix. This is equivalent to use the access operator ().

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 5);
// insertion
A.AddInteraction(1, 3, 2.6);
// we add
A.AddInteraction(1, 3, -1.0);
// now A should be equal to [1 3 1.6]

Related topics :

AddInteractionRow
AddInteractionColumn

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

AddInteractionRow

Syntax :

  void AddInteractionRow(int, int, Vector<int>, Vector<T>);

This methods adds/inserts several values in a row of the matrix. This is more efficient to use that method rather than calling AddInteraction several times.

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 5);
// insertion, you don't need to sort column numbers
int irow = 2;
int nb_values = 3;
IVect col(nb_values);
Vector<double> values(nb_values);
col(0) = 0; values(0) = 0.1;
col(1) = 3; values(1) = 0.6;
col(2) = 2; values(2) = -1.4;

A.AddInteractionRow(irow, nb_values, col, values);

Related topics :

AddInteraction
AddInteractionColumn

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

AddInteractionColumn

Syntax :

  void AddInteractionColumns(int, int, Vector<int>, Vector<T>);

This methods adds/inserts several values in a column of the matrix.

Example :

  
Matrix<double, General, ArrayRowSparse> A(5, 5);
// insertion, you don't need to sort row numbers
int icol = 2;
int nb_values = 3;
IVect row(nb_values);
Vector<double> values(nb_values);
row(0) = 0; values(0) = 0.1;
row(1) = 3; values(1) = 0.6;
row(2) = 2; values(2) = -1.4;

A.AddInteractionColumn(icol, nb_values, row, values);

Related topics :

AddInteraction
AddInteractionRow

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

Print

Syntax :

  void Print() const;

This method displays the matrix.

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

Write

Syntax :

  void Write(string) const;
  void Write(ofstream&) const;

This method writes the matrix on a file/stream in binary format. The file will contain the number of rows, columns, non-zero entries, then the array of values, indices of first elements of rows, and finals the array of column numbers.

Example :

  
// construction of a sparse matrix
Matrix<double, General, RowSparse> A(m, n, nnz, values, ptr, columns); 
// you can write directly in a file
A.Write("matrix.dat");

// or open a stream with other datas
ofstream file_out("matrix.dat");
int my_info = 3;
file_out.write(reinterpret_cast<char*>(>my_info), sizeof(int));
A.Write(file_out);
file_out.close();

Related topics :

Read
WriteText
ReadText

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

Read

Syntax :

  void Read(string);
  void Read(ifstream&);

This method sets the matrix from a file/stream in binary format. The file contains the number of rows, columns, non-zero entries, then the array of values, indices of first elements of each row, column numbers.

Example :

  
Matrix<double, General RowSparse> A; 
// you can read directly on a file
A.Read("matrix.dat");

// or read from a stream
ifstream file_in("matrix.dat");
int my_info;
file_in.read(reinterpret_cast<char*<(>my_info), sizeof(int));
A.Read(file_in);
file_in.close();

Related topics :

Write
WriteText
ReadText

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

WriteText

Syntax :

  void WriteText(string) const;
  void WriteText(ofstream&) const;

This method writes the matrix on a file/stream in text format. The file will contain on each line a non-zero entry, i.e. :

row_number col_number value

The row numbers and columns numbers used 1-index convention, i.e. the first row has its row number equal to 1. An exemple of output file is

1 1 0.123
1 3 0.254
1 4 -0.928
2 1 -0.2
2 5 -0.3

Example :

  
Matrix<double, General, ArrayRowSparse> A(4,4);
A(1,3) = -1.0;
A(2,2) = 2.5;
// you can write directly in a file
A.WriteText("matrix.dat");

// or open a stream with other datas
ofstream file_out("matrix.dat");
int my_info = 3;
file_out << my_info << '\n';
A.WriteText(file_out);
file_out.close();

Related topics :

Write
Read
ReadText

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx

ReadText

Syntax :

  void ReadText(string);
  void ReadText(ifstream&);

This method sets the matrix from a file/stream in text format. The format is detailed in method WriteText.

Example :

  
Matrix<double, Symmetric, ArrayRowSymSparse> V; 
// you can read directly on a file
V.ReadText("matrix.dat");

// or read from a stream
ifstream file_in("matrix.dat");
int my_info;
file_in >> my_info;
V.ReadText(file_in);
file_in.close();

Related topics :

Write
Read
WriteText

Location :

Class Matrix_ArraySparse
Matrix_ArraySparse.hxx Matrix_ArraySparse.cxx