Convert a sdp expression into a symbolic expression or a function handle?

111 views
Skip to first unread message

Pan Zhao

unread,
Sep 20, 2020, 5:20:16 PM9/20/20
to YALMIP
Dear Johan and others,

I was wondering whether it is possible to convert a sdp expression into a symbolic expression or a function handle? I want this conversion in order to compile the codes into C++.  The following code will not work. 

sdpvar x
syms y
z = replace(x^2+1, x, y)

If the conversion is impossible, would it be possible to compile the code with the Yalmip "replace" command there?

Thank you. 

Best
Pan

Johan Löfberg

unread,
Sep 21, 2020, 4:04:07 AM9/21/20
to YALMIP
You cannot mix sdpvar and syms

You have to go via strings

sdpvar x
p = x^2 + 1;

s = sdisplay(p);

syms x
eval(s{1})


Pan Zhao

unread,
Sep 21, 2020, 10:16:42 AM9/21/20
to YALMIP
Got it! This solution should be enough for my applications. Thank you. 

Best
Pan

Pan Zhao

unread,
Mar 6, 2021, 3:41:06 PM3/6/21
to YALMIP
Hi Johan, 

A follow-up question. Is it possible to convert a yalmip expression into a matlab function handle so that the evaluation is faster compared to using "replace"?

For example,  with 
sdpvar x
y = x^2+1;
Instead of doing replace(y,x,1), can I do
y_func(x) with y_func is the generated matlab function handle?

Thank you. 

Best
Pan

Pan Zhao

unread,
Mar 6, 2021, 6:42:34 PM3/6/21
to YALMIP
To be more precise, the x variable in my application is a vector, not a scalar. If it is a scalar, I can use the method you provided above to convert the expression to a matlab function. 

Best
Pan 



Johan Löfberg

unread,
Mar 7, 2021, 2:39:29 AM3/7/21
to YALMIP
sdisplay is the only way, but it is not guaranteed to work, as YALMIP does not know what the name is of your variables but has to guess by hacking around in the work-space

>> x = sdpvar(3,1);
>> sdisplay(sum(x.^4))
x(1)^4+x(2)^4+x(3)^4



Pan Zhao

unread,
Mar 7, 2021, 10:59:56 AM3/7/21
to YALMIP
 I was able to use the method you suggested to first convert the sdp expression to a symbolic expression, and then further convert the symbolic expression to a function handle. Thank you. 
Reply all
Reply to author
Forward
0 new messages