# Skyline matrices

An implementation of skyline matrices (general or symmetric) is proposed in Montjoie. Symmetric skyline matrices are not easily modifiable (as RowSparse), you have to provide directly the pattern and values of the non-zero entries. General skyline matrices are easily modifiable (as ArrayRowSparse), with methods AddInteraction/AddInteractionRow. Unitary tests of these classes are present in file src/Program/Unit/skyline_matrix_test.cc.

## Basic use

```// General skyline matrices
Matrix<double, General, RowSkyLine> A;
int n = 2000;
A.Reallocate(n, n);

// and AddInteractionRow(i, nb_val, col_number, values);
IVect col_num(3);
Vector<double> values(3);
col_num(0) = 2; col_num(1) = 4; col_num(2) = 5;
values.FillRand();
int nb_val = 3;

// or with ReallocateRow(i, size_row, first_col_number) and Value
A.ReallocateRow(1, 2, 4); // adding a_14, a_15 terms
A.Value(1, 0) = 2.3; // setting a_14
A.Value(1, 1) = -3.5; // setting a_15

// once the matrix is constructed, you can perform LU factorisation
// (performed without pivoting)
GetLU(A);

// and solves linear systems
SolveLU(A, x, b);

```

## Methods for unsymmetric skyline matrices :

 Clear clears the matrix Reallocate changes the size of the matrix ReallocateRow changes the size of a row of the matrix AddInteraction adds a value to entry A(i, j) AddInteractionRow adds severals value on a row of the matrix GetRowSize returns the number of non-zero entries contained in a row GetFirstColNumber returns the column number of the first non-zero entry in the row i GetMemorySize returns size of the memory used by the object in bytes Value access to the j-th non-zero entry of the row i GetM returns the number of rows GetN returns the number of columns GetDataSize returns the number of elements effectively stored in the matrix Fill sets all non-zero entries to a given value FillRand sets randomly values of all non-zero entries Zero sets all non-zero entries to zero WriteText writes the matrix on the disk

## Methods for symmetric skyline matrices :

 Clear clears the matrix Reallocate changes the size of the matrix GetM returns the number of rows GetN returns the number of columns GetData returns the pointer to the array containing non-zero entries GetInd returns the pointer to the array containing beginning indexes for each row SetData sets directly the two arrays representing the structure GetMemorySize returns the memory used by the object in bytes GetDataSize returns the number of elements effectively stored in the matrix Fill sets all non-zero entries to a given value FillRand sets randomly values of all non-zero entries Zero sets all non-zero entries to zero WriteText writes the matrix on the disk

## Functions for skyline matrices :

 Copy Basic conversion from any matrix to skyline matrix MltAdd performs matrix vector products GetLU LU factorisation without pivoting SolveLU LU resolution without pivoting GetCholesky computes the cholesky factorisation of a symmetric skyline matrix SolveCholesky solves linear system L x = b or LT x = b, once GetCholesky has been called MltCholesky computes y = L x or t = LT x, once GetCholesky has been called

# Diagonal matrices

An implementation of diagonal matrices is proposed in Montjoie. They implement standard methods available for other matrices, and Mlt/MltAdd functions. Here an example how to use these matrices:

## Basic use

```  int n = 10;
// you can use the constructor with the number of rows and columns
Matrix<double, Symmetric, DiagonalRow> A(n, n);

// or the Reallocate method
A.Reallocate(2*n, 2*n);

// non-zero entries are modified with AddInteraction

// which is equivalent to Get(i, j) +=
A.Get(1, 1) += 0.7;

// access operator is used in read-only mode
cout << A(0, 3) << endl;

// the matrix can be written on a file
A.WriteText("A.dat");
```

## Methods for diagonal matrices :

 GetMemorySize returns the memory used by the object in bytes Reallocate reallocates the matrix with m rows Resize resizes the matrix with m rows Get returns access to element i, j of the matrix AddInteraction adds a value to the element i, j of the matrix Clear clears the matrix ClearRow clears a row of the matrix SetData sets the pointer storing the diagonal Nullify nullifies the pointer storing the diagonal WriteText writes the matrix in a file or a stream

### Clear

#### Syntax

``` void Clear();
```

This function clears the matrix.

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// then you can allocate the matrix with Reallocate
A.Reallocate(n, n);

// for general matrices, you can allocate each row separately
for (int i = 0; i < n; i++)
A.ReallocateRow(i, 4, max(0, i-2));

// the matrix is erased
A.Clear();
```

### Reallocate

#### Syntax

``` void Reallocate(int m, int n);
```

This function initializes the matrix with m rows and n columns.

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// then you can allocate the matrix with Reallocate
A.Reallocate(n, n);

// for general matrices, you can reallocate each row separately
for (int i = 0; i < n; i++)
A.ReallocateRow(i, 4, max(0, i-2));

// the matrix is erased
A.Clear();
```

### ReallocateRow

#### Syntax

``` void ReallocateRow(int irow, int size_row, int first_index);
```

This function sets the row irow with size_row elements, the column number of the first element being equal to first_index.

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);
```

#### Syntax

``` void AddInteraction(int i, int j, const T& val);
```

This function adds the value val to the non-zero entry located at row i and column j. If the non-zero entry does not exist at this place, the profile is increased in order to contain this non-zero entry.

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// once the pattern has been defined, you can set non-zero entries to zero
A.Zero();

```

#### Syntax

``` void AddInteractionRow(int i, int n, const IVect& col_num, const Vector<T>& val);
```

This function adds n values val to the non-zero entries located at row i and columns col_num. If a non-zero entry does not exist, the profile is increased in order to contain this non-zero entry.

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// once the pattern has been defined, you can set non-zero entries to zero
A.Zero();

IVect col_num(3);
Vector<double> val(3);
col_num(0) = 0; val(0) = 0.9;
col_num(1) = 1; val(1) = -2.4;
col_num(2) = 3; val(2) = 1.7;
```

### GetRowSize

#### Syntax

``` int GetRowSize(int i) const;
```

This function returns the number of non-zero entries contained in the row i.

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// at any moment, you can know the number of non-zero entries of a row (e.g. row 4)
int nnz_row = A.GetRowSize(4);
```

### GetFirstColNumber

#### Syntax

``` int GetFirstColNumber(int i) const;
```

This function returns the column number of the first non-zero entry contained in the row i.

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// at any moment, you can know the first column number on row i
int first_pos = A.GetFirstColNumber(i);
```

### Value

#### Syntax

``` T Value(int i, int j) const;
T& Value(int i, int j);
```

This function returns the j-th non-zero entry of the row i. This function can be used to modify the non-zero entry.

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// you can set the values with Value
A.Value(0, 0) = 2.3; A.Value(0, 1) = 2.7;
A.Value(1, 0) = 3.8; A.Value(1, 1) = -2.4; A.Value(1, 2) = 1.9; A.Value(1, 3) = -0.6;
A.Value(2, 0) = 4.4; A.Value(2, 1) = -3.7;
A.Value(3, 0) = 0.9; A.Value(3, 1) = 2.5;
A.Value(4, 0) = -3.5; A.Value(4, 1) = 1.2;
```

### GetM

#### Syntax

``` int GetM() const;
```

This function returns the number of rows in the matrix

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// at any moment you can retrieve the number of rows
int n = A.GetM();
```

### GetN

#### Syntax

``` int GetN() const;
```

This function returns the number of columns in the matrix

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// at any moment you can retrieve the number of columns
int n = A.GetN();
```

### GetMemorySize

#### Syntax

``` size_t GetMemorySize() const;
```

This function returns the memory used by the object in bytes.

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// at any moment you can retrieve the memory effectively used to store the matrix
size_t num_bytes = A.GetMemorySize();
```

### GetDataSize

#### Syntax

``` int GetDataSize() const;
```

This function returns the number of non-zero entries stored in the matrix.

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// at any moment you can retrieve the number of elements stored
int nnz = A.GetDataSize();
```

### Fill

#### Syntax

``` void Fill(const T& val);
```

This function sets all non-zero entries to a same value (val).

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// you can set all non-zero entries to a given value
A.Fill(-0.5);
```

### FillRand

#### Syntax

``` void FillRand();
```

This function sets all non-zero entries to random values.

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// you can set all non-zero entries to a random values
A.FillRand();
```

### Zero

#### Syntax

``` void Zero();
```

This function sets all non-zero entries to 0.

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// you can set all non-zero entries to 0
A.Zero();
```

### WriteText

#### Syntax

``` void WriteText(const string&);
void WriteText(ostream&);
```

This function writes the matrix in a file or an output stream. As usual, the file contains the matrix in the triplet form (i j val) with indexes beginning at 1 (instead of 0).

#### Example :

```// default constructor
Matrix<double, General, RowSkyLine> A;

// for example, if you want to construct the following pattern :
// A = | x x 0 0 0 |
//     | x x x x 0 |
//     | 0 x x 0 0 |
//     | 0 0 0 x x |
//     | 0 x x x 0 |
A.Reallocate(5, 5);
A.ReallocateRow(0, 2, 0);
A.ReallocateRow(1, 4, 0);
A.ReallocateRow(2, 2, 1);
A.ReallocateRow(3, 2, 3);
A.ReallocateRow(4, 3, 1);

// you can set all non-zero entries to 0
A.Zero();

// and write the matrix on the disk
A.WriteText("matrix.dat");
```

### GetData

#### Syntax

``` T* GetData() const
```

This method returns the pointer storing the values of non-zero entries stored in a symmetric skyline matrix

#### Example :

```// default constructor
Matrix<double, Symmetric, SymColSkyLine> A;

// a basic way to construct a symmetric skyline matrix is
// to construct a sparse matrix, then convert it
Matrix<double, Symmetric, ArrayRowSymSparse> B(n, n);

Copy(B, A);

// pointer to non-zero entries stored in A :
double* data = A.GetData();

// number of non-zero entries
int nnz = A.GetDataSize();

// if you want to modify directly the values, you can use the pointer data
for (int i = 0; i < nnz; i++)
data[i] += 2.3;
```

### GetInd

#### Syntax

``` int* GetInd() const
```

This method returns the pointer storing the first index for each row of a symmetric skyline matrix

#### Example :

```// default constructor
Matrix<double, Symmetric, SymColSkyLine> A;

// a basic way to construct a symmetric skyline matrix is
// to construct a sparse matrix, then convert it
Matrix<double, Symmetric, ArrayRowSymSparse> B(n, n);

Copy(B, A);

// pointer to non-zero entries stored in A :
double* data = A.GetData();

// pointer to first indexes (for each row)
int* ind = A.GetInd();

// number of non-zero entries
int nnz = A.GetDataSize();

// if you want to modify directly the values, you can use the pointer data
// in this example, only even rows are modified
for (int i = 0; i < A.GetM(); i++)
if (i%2 == 0)
{
for (int j = ind[i]; j < ind[i+1]; j++)
data[j] += 2.3;
}
```

### SetData

#### Syntax

``` void SetData(IVect& ind, Vector<T>& val)
```

This method fills directly the structure with the arrays ind and val. The first array stores the first index for each row, the second array stores the non-zero entries. On output, the arrays ind and val are empty (they are nullified).

#### Example :

```// default constructor
Matrix<double, Symmetric, SymColSkyLine> A;

// a way to construct a symmetric sky-line matrix
// is to construct the arrays ind and val
// for example, if we consider the following matrix
// A = | 2.3  2.2  -1.4  0   0  |
//     | 2.2  4.2    0  2.8  0  |
//     |-1.4   0    3.1 5.2  0  |
//     |  0   2.8   5.2 7.5 1.0 |
//     |  0    0     0  1.0 4.6 |
// ind = (0, 3, 6, 8, 10, 11)
// val = (2.3, 2.2, -1.4, 4.2, 0, 2.8, 3.1, 5.2, 7.5, 1.0, 4.6)
IVect ind(6);
Vector<double> val(11);
ind(0) = 0; ind(1) = 3; ind(2) = 6; ind(3) = 8; ind(4) = 10; ind(5) = 11;

val(0) = 2.3; val(1) = 2.2; val(2) = -1.4; val(3) = 4.2; val(4) = 0;
val(5) = 2.8; val(6) = 3.1; val(7) = 5.2; val(8) = 7.5; val(9) = 1.0; val(10) = 4.6;

A.SetData(ind, val);

// then you can use the matrix
// for example compute the Cholesky factorization
GetCholesky(A);
```

### Copy

#### Syntax

``` void Copy(const Matrix1&, Matrix2&);
```

This function converts a sparse matrix into a skyline matrix.

#### Example :

```// you can construct a sparse matrix
Matrix<double, General, ArrayRowSparse> Asp;

Asp.Reallocate(5, 5);
// ...

// then you can convert this sparse matrix into a skyline matrix A
Matrix<double, General, RowSkyLine> A;

Copy(Asp, A);

// you can release the memory of Asp if you want
Asp.Clear();
```

#### Syntax

``` void MltAdd(const T& alpha, const Matrix& A, const Vector& X, const T& beta, Vector& Y);
```

This function performs a matrix vector product with a skyline matrix.

#### Example :

```// you can construct a sparse matrix
Matrix<double, General, ArrayRowSparse> Asp;

Asp.Reallocate(5, 5);
// ...

// then you can convert this sparse matrix into a skyline matrix A
Matrix<double, General, RowSkyLine> A;

Copy(Asp, A);

// you can release the memory of Asp if you want
Asp.Clear();

Vector<double> X(n), Y(n);
Y.FillRand(); X.FillRand();

// computes Y = beta Y + alpha A X
```

### GetLU

#### Syntax

``` void GetLU(Matrix& A);
```

This function replaces a skyline matrix A by its LU factorisation.

#### Example :

```// you can construct a sparse matrix
Matrix<double, General, ArrayRowSparse> Asp;

Asp.Reallocate(5, 5);
// ...

// then you can convert this sparse matrix into a skyline matrix A
Matrix<double, General, RowSkyLine> A;

Copy(Asp, A);

// you can release the memory of Asp if you want
Asp.Clear();

// and compute its LU factorisation
GetLU(A);

// then you can compute the solution of A X = B
Vector<double> X(n), B(n);
B.FillRand();

X = B; SolveLU(A, X);
```

### SolveLU

#### Syntax

``` void SolveLU(Matrix& A, Vector& X);
void SolveLU(SeldonTrans, Matrix& A, Vector& X);
```

This function computes the solution of A X = B or AT X = B, assuming that A has been replaced by its L and U factors by calling GetLU before.

#### Example :

```// you can construct a sparse matrix
Matrix<double, General, ArrayRowSparse> Asp;

Asp.Reallocate(5, 5);
// ...

// then you can convert this sparse matrix into a skyline matrix A
Matrix<double, General, RowSkyLine> A;

Copy(Asp, A);

// you can release the memory of Asp if you want
Asp.Clear();

// and compute its LU factorisation
GetLU(A);

// then you can compute the solution of A X = B
Vector<double> X(n), B(n);
B.FillRand();

X = B; SolveLU(A, X);
```

### GetCholesky

#### Syntax

``` void GetCholesky(Matrix& A);
```

This function replaces a skyline matrix A by its Cholesky factorisation. It is available only for symmetric skyline matrices.

#### Example :

```// you can construct a sparse matrix
Matrix<double, Symmetric, ArrayRowSymSparse> Asp;

Asp.Reallocate(5, 5);
// ...

// then you can convert this sparse matrix into a skyline matrix A
Matrix<double, Symmetric, SymColSkyLine> A;

Copy(Asp, A);

// you can release the memory of Asp if you want
Asp.Clear();

// and compute its Cholesky factorisation
GetCholesky(A);

// then you can compute the solution of L^T X = B for example
Vector<double> X(n), B(n);
B.FillRand();

X = B; SolveCholesky(SeldonTrans, A, X);

// or perform the matrix vector product B = L X
B = X; MltCholesky(SeldonNoTrans, A, B);
```

### SolveCholesky

#### Syntax

``` void SolveCholesky(Matrix& A, Vector& X);
```

This function solves the linear system L X = B or LT X = B where A has been replaced by its Cholesky factor L (A = L LT), by calling GetCholesky.

#### Example :

```// you can construct a sparse matrix
Matrix<double, Symmetric, ArrayRowSymSparse> Asp;

Asp.Reallocate(5, 5);
// ...

// then you can convert this sparse matrix into a skyline matrix A
Matrix<double, Symmetric, SymColSkyLine> A;

Copy(Asp, A);

// you can release the memory of Asp if you want
Asp.Clear();

// and compute its Cholesky factorisation
GetCholesky(A);

// then you can compute the solution of L^T X = B for example
Vector<double> X(n), B(n);
B.FillRand();

X = B; SolveCholesky(SeldonTrans, A, X);

// or perform the matrix vector product B = L X
B = X; MltCholesky(SeldonNoTrans, A, B);
```

### MltCholesky

#### Syntax

``` void MltCholesky(Matrix& A, Vector& X);
```

This function performs the matrix vector product Y = L X or Y = LT X where A has been replaced by its Cholesky factor L (A = L LT), by calling GetCholesky.

#### Example :

```// you can construct a sparse matrix
Matrix<double, Symmetric, ArrayRowSymSparse> Asp;

Asp.Reallocate(5, 5);
// ...

// then you can convert this sparse matrix into a skyline matrix A
Matrix<double, Symmetric, SymColSkyLine> A;

Copy(Asp, A);

// you can release the memory of Asp if you want
Asp.Clear();

// and compute its Cholesky factorisation
GetCholesky(A);

// then you can compute the solution of L^T X = B for example
Vector<double> X(n), B(n);
B.FillRand();

X = B; SolveCholesky(SeldonTrans, A, X);

// or perform the matrix vector product B = L X
B = X; MltCholesky(SeldonNoTrans, A, B);
```

### GetMemorySize

#### Syntax

``` size_t GetMemorySize();
```

This function returns the memory used by the matrix in bytes.

#### Example :

```// default constructor
Matrix<double, Symmetric, DiagonalRow> A;

// then you can allocate the matrix with Reallocate
A.Reallocate(n, n);

// size used to store A :
cout << A.GetMemorySize() << endl;
```

### Reallocate

#### Syntax

``` void Reallocate(int, int);
```

This function modifies the size of the matrix. Previous elements may be lost.

#### Example :

```// default constructor
Matrix<double, Symmetric, DiagonalRow> A;

// then you can allocate the matrix with Reallocate
A.Reallocate(n, n);

// you can enforce deallocation with Clear
A.Clear();
```

### Resize

#### Syntax

``` void Resize(int, int);
```

This function modifies the size of the matrix. Previous elements are kept.

#### Example :

```// default constructor
Matrix<double, Symmetric, DiagonalRow> A;

// then you can allocate the matrix with Reallocate
A.Reallocate(n, n);

// matrix can be modified with Get
A.Get(0, 0) = 3.0;
A.Get(1, 1) = -1.0;
A.Get(2, 2) = -2.5;

// Resize keeps previous elements
A.Resize(n+1, n+1);

```

### Get

#### Syntax

``` T& Get(int, int);
```

This function returns an access to element i, j of the matrix.

#### Example :

```// default constructor
Matrix<double, Symmetric, DiagonalRow> A;

// then you can allocate the matrix with Reallocate
A.Reallocate(n, n);

// matrix can be modified with Get
A.Get(0, 0) = 3.0;
A.Get(1, 1) = -1.0;
A.Get(2, 2) = -2.5;

```

#### Syntax

``` void AddInteraction(int, int, const T&);
```

This function adds a value to the element i, j of the matrix. If j is different from i, the element is not added since the matrix is diagonal.

#### Example :

```// default constructor
Matrix<double, Symmetric, DiagonalRow> A;

// then you can allocate the matrix with Reallocate
A.Reallocate(n, n);

// matrix can be modified with Get or AddInteraction
A.Get(0, 0) = 3.0;
A.Get(1, 1) = -1.0;
A.Get(2, 2) = -2.5;

// element 1, 1 should contain now -1+2.3 = 1.3

```

### Clear

#### Syntax

``` void Clear();
```

This function clears the matrix.

#### Example :

```// default constructor
Matrix<double, Symmetric, DiagonalRow> A;

// then you can allocate the matrix with Reallocate
A.Reallocate(n, n);

// the matrix is erased
A.Clear();
```

### ClearRow

#### Syntax

``` void ClearRow(int);
```

This function clears a row of the matrix. For a diagonal matrix, it consists of setting the diagonal element to zero.

#### Example :

```// default constructor
Matrix<double, Symmetric, DiagonalRow> A;

// then you can allocate the matrix with Reallocate
A.Reallocate(n, n);

// you can clear row 2 (the value returns to 0)
A.ClearRow(2);
```

### SetData

#### Syntax

``` void SetData(int, T*);
```

This function is a low-level function and should be used carefully, it sets the pointer storing the diagonal.

#### Example :

```Vector<double> diagA(10);

// default constructor
Matrix<double, Symmetric, DiagonalRow> A;

// then you can use a pointer stored in another vector
A.SetData(diagA.GetM(), diagA.GetData());

// to avoid a duplicate delete, Nullify should be called before calling the destructor of A:
A.Nullify();
```

### Nullify

#### Syntax

``` void Nullify();
```

This function is a low-level function and should be used carefully, it nullifies the pointer storing the diagonal.

#### Example :

```Vector<double> diagA(10);

// default constructor
Matrix<double, Symmetric, DiagonalRow> A;

// then you can use a pointer stored in another vector
A.SetData(diagA.GetM(), diagA.GetData());

// to avoid a duplicate delete, Nullify should be called before calling the destructor of A:
A.Nullify();
```

### WriteText

#### Syntax

```   void WriteText(const string&);
void WriteText(ostream&);
```

This function writes the matrix to a file or an output stream.

#### Example :

```// a diagonal matrix is constructed
Matrix<double, Symmetric, DiagonalRow> A(n, n);

// and filled
A.Get(2, 2) = 3.0;

// finally, the matrix can be written in a file
A.WriteText("A.dat");
```