How to get full submatrix of sparse jacobian?

Discussion in 'Priority support' started by Pedro Morais, Sep 16, 2019.

  1. Hello,

    I'd like to use the equality portion of the constraint jacobian to write an optimization that minimizes equality constraint violation. I'm only concerned about the submatrix of the constraint jacobian corresponding to equality constraints (which I believe is the first 3*neq x nv block in the dense jacobian) but I want it to work even when the jacobian is sparse. What's the best way to get a full submatrix from a sparse constraint jacobian? An equivalent alternative would be a way to multiply a vector by a submatrix of the jacobian.

    Thank you,
    Last edited: Sep 16, 2019
  2. Emo Todorov

    Emo Todorov Administrator Staff Member

    The equality constraints appear first in the list, but you cannot assume there are 3*neq rows in the Jacobian because different equality constraints have different dimensionality and can be enabled/disabled at runtime. So instead you should scan the vector efc_type and look for mjCNSTR_EQUALITY in it.

    As for sparse matrix-vector multiplication, the function is below. You can modify it to process a subset of rows (replace nr in the loop with the number of equality-constraint rows in J):

    // multiply sparse matrix and dense vector: res = mat * vec.
    void mju_mulMatVecSparse(mjtNum* res, const mjtNum* mat, const mjtNum* vec,
    int nr, const int* rownnz, const int* rowadr,
    const int* colind, const int* rowsuper)
    int r;
    for( r=0; r<nr; r++ )
    res[r] = mju_dotSparse(mat+rowadr[r], vec, rownnz[r], colind+rowadr[r]);

    // code using supernodes omitted