# Question:Math code generation in C/C++? Can Maple or Matlab symbolic do collecting the terms in C translation?Maple cannot take th

## Question:Math code generation in C/C++? Can Maple or Matlab symbolic do collecting the terms in C translation?Maple cannot take th

Maple
Hi all, I am asking this for my engineering friend. We've posted on Maple newsgroup and we just heard that here is another place where Maple experts appear, so we posted it again here to seek more advice -- because we have a struggling problem which annoyed us for several days already! Thank you! ---------------- Here is what I want to compute: t22 := Re((1/2*(a^2+2*sigma2*(c+g*(ee+i*v)))^(1/2)*exp(.5*a*t)/ (1/2*(a^2+2*sigma2*(c+g*(ee+i*v)))^(1/2)*cosh(1/2*(a^2+2*sigma2*(c +g*(ee+i*v)))^(1/2)*t)+.5*a*sinh(1/2*(a^2+2*sigma2*(c+g*(ee +i*v)))^(1/2)*t)))^(2*a*xbar/sigma2)) Assuming all variables are real and positive... My objective is to translate the complex-valued function into real and imaginary parts because in our engineering programming, the numerical library IMSL doesnot have a complex-valued integrator. Of course I can do every calculation in complex domain using a complex-number class in C/C++, then extract the real and imaginar parts and feed those values into the IMSL numerical integrator. But this is very slow -- because the computation done in complex domain is roughly 10-20x slower than real double precision calculations. As a real-world engineering project, we hope we could find closed-form solutions of the real and imaginary parts of the integrand, instead of the above approach, because that's more efficient. We want to explore the possibility of "semi-auto" code-generation in Maple and/or Matlab to convert code into C/C++, so in the future that when we make change, we just have to rerun the process. But the result generated by Maple or Matlab symbolic(using a Maple engine) is not promising at all: I cut a slice and show it below: as you can see, there are many redundant piece, and this is only one very small portion of our expression. If we directly convert this to C/C++, the code won't run efficiently. Our question is: is there any way that within the Maple and/or Matlab symbolic domain, we can manipulate the expression to do variable sustitution so the code can be executed in a much efficient way, using step by step variable defining and substituting methods? Math Within the C/C++ programming and compiler code optimization domain, is it possible that the compiler will do the job to do the redundancy reduction and code optimization job? -------------------- One more problem: I've posted this on the Maple newsgroup and got help from Walter. But then I found inside this code snippet, there is an "i" there, if you search, you will find "csgn(i*(t3+2.0*t31+2.0*t34+2.0*i*sigma2*g*v));" This is too bad, it shows that basically Maple's "evalc" and "Re" functions didnot work. If they really work, there should be no "i" in the result of the real part of the expression. Math/Maple/Matlab/C++ experts, please help me! Thank you very much! -------------------------------------------------- t22 := exp(a*xbar*(a*t+log(-1.*(a^4+4.*a^2*sigma2*c+4.*a^2*sigma2*g*ee +4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2)/ ((2.*(a^4+4.*a^2*sigma2*c+4.*a^2*sigma2*g*ee +4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2)+2.*a^2+4.*sigma2*c +4.*sigma2*g*ee)^(1/2)*sinh(. 25000000000000000000000000000000*(2.*(a^4+4.*a^2*sigma2*c +4.*a^2*sigma2*g*ee+4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2)+2.*a^2+4.*sigma2*c +4.*sigma2*g*ee)^(1/2)*t)*a*cosh(. 25000000000000000000000000000000*(2.*(a^4+4.*a^2*sigma2*c +4.*a^2*sigma2*g*ee+4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2)+2.*a^2+4.*sigma2*c +4.*sigma2*g*ee)^(1/2)*t)*cos(. 25000000000000000000000000000000*(2.*(a^4+4.*a^2*sigma2*c +4.*a^2*sigma2*g*ee+4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2)-2.*a^2-4.*sigma2*c-4.*sigma2*g*ee)^(1/2)*t)^2-1.*cosh(. 25000000000000000000000000000000*(2.*(a^4+4.*a^2*sigma2*c +4.*a^2*sigma2*g*ee+4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2)+2.*a^2+4.*sigma2*c +4.*sigma2*g*ee)^(1/2)*t)^2*cos(. 25000000000000000000000000000000*csgn(1.*i*(a^2+2.*sigma2*c +2.*sigma2*g*ee+2.*i*sigma2*g*v))*(2.*(a^4+4.*a^2*sigma2*c +4.*a^2*sigma2*g*ee+4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2)-2.*a^2-4.*sigma2*c-4.*sigma2*g*ee)^(1/2)*t)^2*(a^4+4.*a^2*sigma2*c +4.*a^2*sigma2*g*ee+4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2)-1.*cosh(. 25000000000000000000000000000000*(2.*(a^4+4.*a^2*sigma2*c +4.*a^2*sigma2*g*ee+4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2)+2.*a^2+4.*sigma2*c +4.*sigma2*g*ee)^(1/2)*t)^2*cos(. 25000000000000000000000000000000*csgn(1.*i*(a^2+2.*sigma2*c +2.*sigma2*g*ee+2.*i*sigma2*g*v))*(2.*(a^4+4.*a^2*sigma2*c +4.*a^2*sigma2*g*ee+4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2)-2.*a^2-4.*sigma2*c-4.*sigma2*g*ee)^(1/2)*t)^2*a^2-1.*(2.*(a^4+4.*a^2*sigma2*c +4.*a^2*sigma2*g*ee+4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2)+2.*a^2+4.*sigma2*c +4.*sigma2*g*ee)^(1/2)*sinh(. 25000000000000000000000000000000*(2.*(a^4+4.*a^2*sigma2*c +4.*a^2*sigma2*g*ee+4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2)+2.*a^2+4.*sigma2*c +4.*sigma2*g*ee)^(1/2)*t)*a*cosh(. 25000000000000000000000000000000*(2.*(a^4+4.*a^2*sigma2*c +4.*a^2*sigma2*g*ee+4.*sigma2^2*c^2+8.*sigma2^2*c*g*ee +4.*sigma2^2*g^2*ee^2+4.*sigma2^2*g^2*v^2)^(1/2) ﻿