Question: CodeGeneration:-C Output variable

Hello,

I'm using CodeGeneration:-C in Maple to translate the coefficients of a dynamical model into C++ code. However, I'm running into an issue when trying to assign the output in a way that matches the format of the coef vector used in the main C++ file (which is set up to simulate any 3D quadratic model).

For context, ans is a Maple vector containing all 30 coefficients.
Taking ans[12] as an example:

ans := [0, -sigma, xi[1, 2], xi[1, 3], 0, 0, 0, 0, 0, 0, 0, -sqrt(-4*xi[1, 3]^2*xi[1, 2]^2 + 4*xi[1, 2]*xi[1, 3]*xi[2, 5] + 1)*rho*sigma/(2*xi[1, 2]) + sigma*rho/(2*xi[1, 2]), -sqrt(-4*xi[1, 3]^2*xi[1, 2]^2 + 4*xi[1, 2]*xi[1, 3]*xi[2, 5] + 1)*beta/2 + sqrt(-4*xi[1, 3]^2*xi[1, 2]^2 + 4*xi[1, 2]*xi[1, 3]*xi[2, 5] + 1)/2 - beta/2 - 1/2, -sqrt(-4*xi[1, 3]^2*xi[1, 2]^2 + 4*xi[1, 2]*xi[1, 3]*xi[2, 5] + 1)*beta*xi[1, 3]/(2*xi[1, 2]) + sqrt(-4*xi[1, 3]^2*xi[1, 2]^2 + 4*xi[1, 2]*xi[1, 3]*xi[2, 5] + 1)*xi[1, 3]/(2*xi[1, 2]) + beta*xi[1, 3]/(2*xi[1, 2]) - xi[1, 3]/(2*xi[1, 2]), 0, xi[2, 5], -sqrt(-4*xi[1, 3]^2*xi[1, 2]^2 + 4*xi[1, 2]*xi[1, 3]*xi[2, 5] + 1)/(2*xi[1, 2]^2) + xi[1, 3]*xi[2, 5]/xi[1, 2] + 1/(2*xi[1, 2]^2), 0, 0, 0, 0, sqrt(-4*xi[1, 3]^2*xi[1, 2]^2 + 4*xi[1, 2]*xi[1, 3]*xi[2, 5] + 1)*rho*sigma/(2*xi[1, 3]) + sigma*rho/(2*xi[1, 3]), sqrt(-4*xi[1, 3]^2*xi[1, 2]^2 + 4*xi[1, 2]*xi[1, 3]*xi[2, 5] + 1)*beta*xi[1, 2]/(2*xi[1, 3]) - sqrt(-4*xi[1, 3]^2*xi[1, 2]^2 + 4*xi[1, 2]*xi[1, 3]*xi[2, 5] + 1)*xi[1, 2]/(2*xi[1, 3]) + beta*xi[1, 2]/(2*xi[1, 3]) - xi[1, 2]/(2*xi[1, 3]), sqrt(-4*xi[1, 3]^2*xi[1, 2]^2 + 4*xi[1, 2]*xi[1, 3]*xi[2, 5] + 1)*beta/2 - sqrt(-4*xi[1, 3]^2*xi[1, 2]^2 + 4*xi[1, 2]*xi[1, 3]*xi[2, 5] + 1)/2 - beta/2 - 1/2, 0, -xi[1, 2]*xi[2, 5]/xi[1, 3] - sqrt(-4*xi[1, 3]^2*xi[1, 2]^2 + 4*xi[1, 2]*xi[1, 3]*xi[2, 5] + 1)/(2*xi[1, 3]^2) - 1/(2*xi[1, 3]^2), -xi[2, 5], 0, 0, 0]

When I run the command:

CodeGeneration:-C(ans[12],resultname="coef21");

the output is:

coef21 = -0.1e1 / (double) xi[0][1] * sqrt((double) (-4 * xi[0][2] * xi[0][2] * xi[0][1] * xi[0][1] + 4 * xi[0][1] * xi[0][2] * xi[1][4] + 1)) * rho * sigma / 0.2e1 + 0.1e1 / (double) xi[0][1] * sigma * rho / 0.2e1;

This is syntactically fine, but what I actually need is an assignment to coef[21] instead of coef21, to be consistent with the structure in my C++ code.

To clarify, the resultname must reflect an offset: ans[12] should become coef[12 + 9], ans[1] becomes coef[1 + 9], and so on. I realize this isn’t ideal, but the current C++ code has been working reliably for years, and adapting it would require unnecessary refactoring.

Do you have any suggestions on how to direct CodeGeneration:-C to generate the assignment with coef[21] as the left-hand side?

Many thanks in advance!

Ed

P.S. Using CodeGeneration:-C(ans[i], resultname = sprintf("coef%d", i + 9)) displays the coefficients on the screen as needed, but they still require some additional editing.

Please Wait...