coin-or / qpOASES

Open-source C++ implementation of the recently proposed online active set strategy
GNU Lesser General Public License v2.1
384 stars 129 forks source link

What do the arguments mean of SymSparseMat class? #105

Open AlBrP opened 4 years ago

AlBrP commented 4 years ago

Hi, I want to create symmetric sparse matrix by SymSparseMat class so I check the Matrices.hpp for arguments descriptions.

int_t nr,           /**< Number of rows. */
int_t nc,           /**< Number of columns. */
sparse_int_t* r,    /**< Row indices (length). */
sparse_int_t* c,    /**< Indices to first entry of columns (nCols+1). */
real_t* v           /**< Vector of entries (length). */

I don't understand these three agruments: sparse_int_t* r sparse_int_t* c real_t* v.

Before creating this issue, I have saw the qrecipe.cppqrecipe_data.hpp but I still can not understand whar the arguments mean. I also googled for answers about this question but I couldn't find simliar questions.

Could you please give me a simple example(like creating a 3*3 symmetric sparse matrix ) to explain what these three arguments mean?

Thanks a lot in advance!

jhallier commented 3 years ago

Hi, I had the same question, and eventually figured that the matrix is in a column-compressed storage format. There's a good example here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csc_matrix.html or here https://de.wikipedia.org/wiki/Harwell-Boeing-Format (only in German, the English version doesn't have the example).

So basically, you go through your matrix column-wise, then add each non-zero value to v and each row of that entry to r, and in c you save the position of the first element in v for each column, plus one past the last element (equaling the total number of entries) - length of c is (number of columns + 1).

Example: 0 3 4 0 1 0 0 0 0 0 1 0 1 0 0 0 v = { 1, 1, 3, 4, 1} r = { 1, 3, 0, 0, 2} c = {0, 2, 3, 3, 5}

Should this be added to the documentation? It may help to understand how to fill the sparse matrices.

Also note that after creating a SymSparseMat, other than a SparseMatrix, you need to call the method createDiagInfo() afterwards.