Hi,
I have an expression DesignMatrixMult which is 2*2 matrix (in general it will be a n*n matrix). The expression is in terms of four symbolic variables q1/q2/x1/x2.
DesignMatrixMult = [ ((q1*(exp(-q1*x1) - exp(-q2*x1)))/(q1 - q2)^2 - (exp(-q1*x1) - exp(-q2*x1))/(q1 - q2) + (q1*x1*exp(-q1*x1))/(q1 - q2))^2 + ((q1*(exp(-q1*x2) - exp(-q2*x2)))/(q1 - q2)^2 - (exp(-q1*x2) - exp(-q2*x2))/(q1 - q2) + (q1*x2*exp(-q1*x2))/(q1 - q2))^2, - ((q1*(exp(-q1*x1) - exp(-q2*x1)))/(q1 - q2)^2 + (q1*x1*exp(-q2*x1))/(q1 - q2))*((q1*(exp(-q1*x1) - exp(-q2*x1)))/(q1 - q2)^2 - (exp(-q1*x1) - exp(-q2*x1))/(q1 - q2) + (q1*x1*exp(-q1*x1))/(q1 - q2)) - ((q1*(exp(-q1*x2) - exp(-q2*x2)))/(q1 - q2)^2 + (q1*x2*exp(-q2*x2))/(q1 - q2))*((q1*(exp(-q1*x2) - exp(-q2*x2)))/(q1 - q2)^2 - (exp(-q1*x2) - exp(-q2*x2))/(q1 - q2) + (q1*x2*exp(-q1*x2))/(q1 - q2))]
[ - ((q1*(exp(-q1*x1) - exp(-q2*x1)))/(q1 - q2)^2 + (q1*x1*exp(-q2*x1))/(q1 - q2))*((q1*(exp(-q1*x1) - exp(-q2*x1)))/(q1 - q2)^2 - (exp(-q1*x1) - exp(-q2*x1))/(q1 - q2) + (q1*x1*exp(-q1*x1))/(q1 - q2)) - ((q1*(exp(-q1*x2) - exp(-q2*x2)))/(q1 - q2)^2 + (q1*x2*exp(-q2*x2))/(q1 - q2))*((q1*(exp(-q1*x2) - exp(-q2*x2)))/(q1 - q2)^2 - (exp(-q1*x2) - exp(-q2*x2))/(q1 - q2) + (q1*x2*exp(-q1*x2))/(q1 - q2)), ((q1*(exp(-q1*x1) - exp(-q2*x1)))/(q1 - q2)^2 + (q1*x1*exp(-q2*x1))/(q1 - q2))^2 + ((q1*(exp(-q1*x2) - exp(-q2*x2)))/(q1 - q2)^2 + (q1*x2*exp(-q2*x2))/(q1 - q2))^2]
I want to evaluate RANK of the DesignMatrixMult for specific values of the four symbolic variables. I use SUBS function on DesignMatrixMult, then use DOUBLE followed by RANK and this whole process takes ~0.016 second per evaluation (average by checking on a FOR loop run 10^4 times) but this is not good enough as I need to do this evaluation 10^8 times. I want to understand ways to speed up the evaluation process while retaining the accuracy afforded by symbolic approach.
I thought by doing multiple evaluations together the process will be faster so tried various ways (see below) but not any headway till now.
EvalRankDesignMatrixMultMethod1 = rank(double(vpa(subs(DesignMatrixMult,{t1(1),t1(2),q(1),q(2)},{C.z26,C.z27,C.z21,C.z22}))));
But the above approach gives wrong answers. Specifically, DesignMatrixMult is a 2*2 matrix and in the output the rows corresponding to 10^8 instances are not arranged in correct order and subsequent rank is incorrect.
How to make the above subs approach work?
DesignMatrixMultF = symfun(DesignMatrixMult,[t1 q]);
RankDesignMatrixMultF = @(xsub1,xsub2,qsub1,qsub2)rank(double(DesignMatrixMultF(xsub1,xsub2,qsub1,qsub2)));
EvalRankDesignMatrixMultMethod2=cell2mat(cellfun(RankDesignMatrixMultF,num2cell(double(C.z26)),num2cell(double(C.z27)),num2cell(double(C.z21)),num2cell(double(C.z22)),'UniformOutput',false));
The above code starts executing but after a couple of minutes it issues an out of memory error and my computer had to be restarted.
StartCnter = 1;
EndCnter = 10000;
EvalRankDesignMatrixMultMethod3 = cell2mat(cellfun(RankDesignMatrixMultF,num2cell(double(C.z26(StartCnter:EndCnter))),num2cell(double(C.z27(StartCnter:EndCnter))),num2cell(double(C.z21(StartCnter:EndCnter))),num2cell(double(C.z22(StartCnter:EndCnter))),'UniformOutput',false));
The above approach works but takes ~160 seconds..so this means 0.016 seconds per evaluation...so for the complete dataset it will take impractical amount of time. So it seems cellfun approach (even by doing it in chunks) is not a worthy alternative.
PS: If I had used a matlabFunction approach (no symbolic) then per evaluation takes ~ 6E-5 so 10^8 evaluations wraps in 6000 seconds which is amazing...I understand symbolic can't achieve this peformance but how can I bridge the gap.