# Ublas :

Subject: [ublas] How to load a ublas::compressed_matrix<ScalarType> ublas_matrix;
From: Klaus Burkart (k_burkart_at_[hidden])
Date: 2017-08-21 13:17:28

Hello,
I want to export and convert a matrix and load the matrix in anÂ  ublas::compressed_matrix<ScalarType> ublas_matrix;
I have a function to convert the original matrix from LDU to CSR format but I have no idea about ho to load the ublas_matrix?! I am an absolute beginner.

Function to convert the original matrix:
void ldu2csr
(
Â Â Â  const Foam::lduMatrix & matrix,
Â Â Â  scalar* vals,
Â Â Â  uint* c_idx,
Â Â Â  uint* r_idxÂ Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â Â Â  Â
)
{
Â Â Â  int n_rows = matrix.diag().size();

Â Â Â  //
Â Â Â  //Â Â Â  Calculate each row size. Sizes are shifted, because in the next part
Â Â Â  //Â Â Â  array r_idx is modified twice.
Â Â Â  //
Â Â Â  for (int i=0; i < matrix.upper().size(); i++)
Â Â Â  {
Â Â Â Â Â Â Â  r_idx[ri1+2] ++;
Â Â Â  r_idx[ri2+2] ++;
Â Â Â  }

Â Â Â  //Â Â Â
Â Â Â  //Â Â Â  Compute row offsets. Offsets are shifted by one position,
Â Â Â  //Â  because they are used as START positions while filling values.
Â Â Â  //
Â Â Â  r_idx[0] = 0;
Â Â Â Â Â Â Â  r_idx[1] = 0;

Â Â Â  for (int i=1; i<n_rows; i++)
Â Â Â  {
Â Â Â Â Â Â Â  r_idx[i+1] += r_idx[i];
Â Â Â  }

Â Â Â  //
Â Â Â  //Â Â Â  Fill in CSR matrix.
Â Â Â  //Â Â Â  Order below is important to keep column indices sorted.
Â Â Â  //

Â Â Â  //Â  lower triangle
Â Â Â  for (int i=0; i < matrix.lower().size() ; i++)
Â Â Â  {
Â Â Â Â Â Â Â  int rowÂ Â Â  = matrix.lduAddr().upperAddr()[i] +1;

Â Â Â Â Â Â Â  int idx = r_idx[row];
Â Â Â Â Â Â Â  vals[idx] = matrix.lower()[i];
Â Â Â Â Â Â Â  c_idx[idx] = column;
Â Â Â Â Â Â Â  r_idx[row]++;
Â Â Â  }
Â Â Â  Â Â Â
Â Â Â  // diagonal
Â Â Â  for (int i=0; i<matrix.diag().size(); i++)
Â Â Â  {
Â Â Â Â Â Â Â  int idx = r_idx[i+1];
Â Â Â Â Â Â Â  vals[idx] = matrix.diag()[i];
Â Â Â Â Â Â Â  c_idx[idx] = i; // i is row and column index
Â Â Â Â Â Â Â  r_idx[i+1]++;
Â Â Â  Â Â Â  }

Â Â Â  // upper triangle
Â Â Â  for (int i=0; i < matrix.upper().size() ; i++)
Â Â Â  {

Â Â Â  int idx = r_idx[row];
Â Â Â  vals[idx] = matrix.upper()[i];
Â Â Â  c_idx[idx] = column;
Â Â Â  r_idx[row]++;
Â Â Â  }
}
My idea of a function to load the ublas_matrix:
void load_umatrix(Â Â Â  const Foam::lduMatrix & matrix, boost::numeric::ublas::compressed_matrix<scalar>& ublas_matrix){Â Â Â  uint N_DIAG = matrix.diag().size();
Â Â Â  uint lduMatrixSIZE = matrix.lower().size() + matrix.upper().size() + matrix.diag().size(); // nnz
Â Â Â  Â Â Â
Â Â Â  // allocat mem for CSR sparse matrix
Â Â Â  scalar * v = (scalar *)calloc(lduMatrixSIZE, sizeof(scalar));
Â Â Â  uint * c = (uint *)calloc(lduMatrixSIZE, sizeof(uint));
Â Â Â  uint * r = (uint *)calloc(N_DIAG + 2, sizeof(uint));

Â Â Â  ldu2csr(matrix,v,c,r);Â Â Â  ublas_matrix.set(r,c,v,N_DIAG,N_DIAG,lduMatrixSIZE); //???Â Â
Â Â Â  // free and release the matrix mem
Â Â Â  free(v); free(r); free(c);Â  //colloc()Â Â Â

}
I keep getting error messages related to "incomplete types" and that the matrix cannot be defined.
Any hint to fix the problem is highly appreciated.
Klaus