Sparse output from CasAdi generated mex function

301 views
Skip to first unread message

Chen Yutao

unread,
Feb 23, 2017, 10:27:15 AM2/23/17
to CasADi
Hi all,

I'm using CasADi c-code generation functions in the Matlab interface and they works quite well.

The default output of a CasADi generated mex function is a sparse matrix. This is good if I use this standalone, however, it's not so conveninent if I want to link with other codes. For example, Lapack supports only full matrices so a frequent transfermation from sparse to full matrices must be performed, if I want to use Lapack to solve some linear systems. This is also a problem when I want to use Matlab Coder, which does not support sparse data. 

Is it possible to get a full matrix (in this case, a double array) from CasADi generated mex function? 

If not, can anyone give me a suggestion how to overcome the problems I mentioned?

Thanks in advance,

Yutao


Joel Andersson

unread,
Feb 23, 2017, 11:23:46 AM2/23/17
to CasADi
Hi,

There is a helper function for this: "returntypes". I think you do "f_dense = returntypes(f_mex, 'full');"

Joel

Chen Yutao

unread,
Feb 23, 2017, 1:41:34 PM2/23/17
to CasADi
Thank you Joel,

I tried to use "returntypes" and also " returntypes_call", they actually are anonymous functions in Matlab. Evaluating numerical inputs I get dense outputs, however, I cannot transfer them into Casadi function or Matlab function. 

Moreover, these two functions actually just perform "full" or "sparse" command in Matlab, but not set the output of a Casadi function to be dense or sparse.

So it seems that my problems mentioned above are not solved.

Any ideas?

Best,

Yutao

在 2017年2月23日星期四 UTC+1下午5:23:46,Joel Andersson写道:

Joel Andersson

unread,
Feb 23, 2017, 5:42:15 PM2/23/17
to CasADi
Is it a question about speed? I would think that MATLAB converts sparse to dense matrices very quickly.

Joel

Chen Yutao

unread,
Feb 24, 2017, 5:16:59 AM2/24/17
to CasADi
The problem is, if I use casadi's generated MEX function, which outputs sparse data only, I have to convert the sparse data to full one when I need to do some linear algebra in C or MEX. (In MEX or C, the support of sparse data is poor, i.e. no sparse matrix factorization or multiplication)

In Matlab, this is not a problem. The problem is in MEX or C.

Joel Andersson

unread,
Feb 24, 2017, 9:41:09 AM2/24/17
to CasADi
Why not use the generated C code without the mex entry point if you want to use it from C or Mex? The API is described in the user guide.

Joel

Chen Yutao

unread,
Feb 24, 2017, 10:16:49 AM2/24/17
to CasADi
Do you mean that the generated C code (if I turn mex option off), outputs dense data? 

If so, this can help me greatly. I thought it outputs sparse data as well.

I will try it.

Thank you

Joel Andersson

unread,
Feb 24, 2017, 10:43:27 AM2/24/17
to CasADi
Internally, CasADi only works with sparse data types, but often uses dense routines for the calculations, when there are no structural zeros in the matrices. Furthermore, the sparsity patterns is always fixed in the generated C code, so any calculations only operates on the nonzeros of the matrices.

You can ensure that the generated matrices are dense when you create the functions. For example, instead of:
f = Function('f', {x,y}, {f, g})
you can do
f = Function('f', {x,y}, {densify(f), densify(g)})
That will ensure that f and g don't have structural zeros in them.

In the C API, you can then simply treat them as dense matrices, stored column-major (same as LAPACK's dense routines).

Joel

Chen Yutao

unread,
Feb 25, 2017, 5:38:09 PM2/25/17
to CasADi
Thanks Joel, 

that's what I need. Although it's not perfect in Matlab (the output is still a sparse data but it treats zeros as nonzeros), I can use these function in Mex coding.

Yutao
Reply all
Reply to author
Forward
0 new messages