is there a way to speed up the conversion of symbolic expression to function handle?

4 views (last 30 days)
I'm doing an optimization and the objective function is as follows:
if true
M=2;
N=2;
R=10;
A is a fully known complex matrix of dimension (M*N) by 2^R.
Pr is a fully known vector of length 2^R
Q1,Q2 are known matrice of dimension M*N by M*N
dB= sym('B%d', [1,M*N]);
B=diag(dB);
j = 1:M*N;
Ah=A';
temp=reshape(repmat(A,M*N,1).*transpose(Ah(:,j(ones(M*N, 1),:))),[M*N,M*N,2^R]);
temp2=temp.*repmat(reshape(Pr, [1 1 2^R]), [M*N,M*N]);
temp3=reshape(sum(reshape(temp2,M*N*M*N,[]),2),M*N,[]);
Qz2=2*Q2+eye(M*N)-B^(-1/2)*temp3*transpose(B^(-1/2));
objfun=trace(Q1)-trace(Q1*((Q1+Qz2)^-1*Q1));
end
And then I will do optimization based on this nolinear object function objun, so I use the matlabFunction to convert symbolic expression 'objfun' to function handle:
if true
objhandle=matlabFunction(objfun,'vars',{transpose(dB)});
res=fmincon(objhandle,......)
end
but due to the fact that 2^R is large (here 1024) the objfun is VERY complex in symbolic expression, thus this conversion is extremely slow. Is there any way to speed up the conversion? Thanks for your help!

Answers (1)

Walter Roberson
Walter Roberson on 25 Feb 2014
Sometimes it is faster to simplify() a symbolic expression before using matlabFunction(). Only when the simplification is sufficient to counter-balance the time required for the simplification.
You might also want to experiment with dropping into the symbolic engine and using generate::MATLAB . In particular you may wish to look at generate::optimize. generate::optimize will usually take more time than not using it: the advantage of generate::optimize is that the resulting MATLAB code will be more efficient.
You would need to use evalin(symengine) or feval(symengine) in order to access generate::MATLAB

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!