Prev: sinc interpolation along one dimension of a 4D array
Next: Continue Entering Statement - how to exit or quit
From: Michael on 26 Jul 2010 10:36 I encountered a today problem with the built-in ccode function of matlab (MatlabR2010a under linux 64bit). This problem can be demonstrated on an extended version of the third example of the documentation page of ccode: Starting with syms x z = exp(-exp(-x)); ccode(diff(z,3),'file','ccodetest'); the result in the file ccodetest looks as expected, since the first argument was an intermediate expression: t2 = exp(-x); t3 = exp(-t2); t0 = t3*exp(x*-2.0)*-3.0+t3*exp(x*-3.0)+t2*t3; If we make another call syms a; a = diff(z,3); ccode( a, 'file', 'ccodetest2' ) the contents of ccodetest2 looks almost the same: t5 = exp(-x); t6 = exp(-t5); t0 = t6*exp(x*-2.0)*-3.0+t6*exp(x*-3.0)+t5*t6; So here is my first question: Why is the last variable not named 'a'? This would be the same behavior as seen in the 2nd example of the ccode documentation. But the main problem appears if we try to do the following: syms c; c = [a; 2*a]; ccode( c, 'file', 'ccodetest3' ); The result in ccodetest3 looks like this: t3 = exp(-x); t4 = exp(-t3); t5 = exp(x*-2.0); t6 = exp(x*-3.0); t0 = MatrixWithNoName[0][0] = t3*t4-t4*t5*3.0+t4*t6; MatrixWithNoName[1][0] = t3*t4*2.0-t4*t5*6.0+t4*t6*2.0; Obviously ccode has some problem with the naming of the variable if its a vector of symbolic expressions. Am I doing sth wrong or missing sth? I know that there exist other functions like matlabFunction which have similar functionality, but I would like to use ccode. In the meanwhile I will employ some strrep on t0 and MatrixWithNoName. On a side note: It would be nice, if the string output of the call ccode(a) would also be optimized, right now I am using the ccode( a, 'file', ...) call to create a temporary file and read it with fileread. Any help is appreciated. Thank you, Michael
From: Walter Roberson on 26 Jul 2010 14:52
Michael wrote: > If we make another call > > syms a; a = diff(z,3); ccode( a, 'file', 'ccodetest2' ) > > the contents of ccodetest2 looks almost the same: > > t5 = exp(-x); > t6 = exp(-t5); > t0 = t6*exp(x*-2.0)*-3.0+t6*exp(x*-3.0)+t5*t6; > > So here is my first question: Why is the last variable not named 'a'? > This would be the same behavior as seen in the 2nd example of the ccode > documentation. If you examine the ccode documentation and the code generation guidelines http://www.mathworks.com/access/helpdesk/help/toolbox/symbolic/brt7nuv.html you will see that the only examine in which ccode generates a final symbol of the same name as the expression being generated from, is a case where a 2D array is being explicitly generated. In all other cases, a t* variable is the terminal value. Your above example does not generate an explicit matrix. > But the main problem appears if we try to do the following: > syms c; > c = [a; 2*a]; > ccode( c, 'file', 'ccodetest3' ); > > The result in ccodetest3 looks like this: > t3 = exp(-x); > t4 = exp(-t3); > t5 = exp(x*-2.0); > t6 = exp(x*-3.0); > t0 = MatrixWithNoName[0][0] = t3*t4-t4*t5*3.0+t4*t6; > MatrixWithNoName[1][0] = t3*t4*2.0-t4*t5*6.0+t4*t6*2.0; > > Obviously ccode has some problem with the naming of the variable if its > a vector of symbolic expressions. Am I doing sth wrong or missing sth? There is no documented naming convention for the intermediate or output variables for ccode, and thus no bug in generating unexpected names. Inconvenience, yes. |