Оптимизация длинных выражений в переполнении стека

У меня есть вопрос, касающийся длинных выражений в C / C ++. Поэтому я вычислил закрытую форму для матрицы, которая мне нужна в моем коде. Это симметричная матрица 6х6. Так что в основном я хочу сделать это

void calcMat(double * Mat, const double * S, const double * F, const double * tau, const double * D, const double * G)
{
//ReadIn all data from the given Arrays into const double's
const double G1 = G[0]; const double G2 = G[1]; const double G3 = G[2];

const double F11 = F[0]; const double F12 = F[1]; const double F13 = F[2];
const double F21 = F[3]; const double F22 = F[4]; const double F23 = F[5];
const double F31 = F[6]; const double F32 = F[7]; const double F33 = F[8];

const double sq2 = SQRT2 * 0.5;
const double S11 = S[0]; const double S12 = sq2 * S[3]; const double S13 = sq2 * S[5];
const double S22 = S[1]; const double S23 = sq2 * S[4];
const double S33 = S[2];

const double t11 = tau[0];
const double t22 = tau[4];
const double t33 = tau[8];
const double t12 = tau[1];
const double t23 = tau[5];
const double t13 = tau[2];

const double Sqr2I = 1. / sqrt(2.);
const double D1111 = D[0]; const double D1122 = D[1]; const double D1133 = D[2]; const double D1112 = Sqr2I * D[3]; const double D1123 = Sqr2I * D[4]; const double D1113 = Sqr2I * D[5];
const double D2222 = D[7]; const double D2233 = D[8]; const double D1222 = Sqr2I * D[9]; const double D2223 = Sqr2I*D[10]; const double D1322 = Sqr2I*D[11];
const double D3333 = D[14]; const double D1233 = Sqr2I * D[15];
const double D2333 = Sqr2I * D[16]; const double D1333 = Sqr2I * D[17];
const double D1212 = 0.5 * D[21]; const double D1223 = 0.5 * D[22];
const double D1213 = D[23];
const double D2323 = 0.5 * D[28]; const double D1323 = 0.5 * D[29];
const double D1313 = 0.5 * D[35];

//Build Matrix first entry
Mat[0] += (D1112*F11*G1*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1212*F12*G1*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1213*F13*G1*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1122*F11*G2*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1222*F12*G2*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1322*F13*G2*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1123*F11*G3*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1223*F12*G3*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1323*F13*G3*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1111*F11*G1*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1112*F12*G1*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1113*F13*G1*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1112*F11*G2*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1212*F12*G2*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1213*F13*G2*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1113*F11*G3*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1213*F12*G3*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1313*F13*G3*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1113*F11*G1*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1213*F12*G1*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1313*F13*G1*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1123*F11*G2*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1223*F12*G2*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1323*F13*G2*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1133*F11*G3*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1233*F12*G3*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1333*F13*G3*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)));
//other terms in a similar fashion
}

Я могу просто скопировать и вставить все оценки + =, и это прекрасно работает, когда я компилирую с g ++ и -O0. Но когда я включаю -O2, компиляция занимает очень много времени, и на некоторых машинах мне часто не хватает памяти. Мой вопрос: это поможет определить каждую запись матрицы в своей области видимости, а затем добавить, как

void calcMat(double * Mat, other InputArrays)
{
//ReadIn all data from the given Arrays into const double's
//as shown above
//Build Matrix
{
const double Mat0 = (D1112*F11*G1*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1212*F12*G1*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1213*F13*G1*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1122*F11*G2*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1222*F12*G2*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1322*F13*G2*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1123*F11*G3*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1223*F12*G3*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1323*F13*G3*(F12*F12*S12*t11 + F13*F22*S13*t12 + F13*F32*S13*t13 + F11*S11*(F12*t11 + F22*t12 + F32*t13) +
F12*(F13*S13*t11 + F21*S11*t12 + 2*F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + 2*F32*S12*t13 + F33*S13*t13) + F21*F22*S11*t22 + F22*F22*S12*t22 + F22*F23*S13*t22 + F22*F31*S11*t23 +
F21*F32*S11*t23 + 2*F22*F32*S12*t23 + F23*F32*S13*t23 + F22*F33*S13*t23 + F32*(F31*S11 + F32*S12 + F33*S13)*t33) +
D1111*F11*G1*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1112*F12*G1*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1113*F13*G1*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1112*F11*G2*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1212*F12*G2*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1213*F13*G2*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1113*F11*G3*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1213*F12*G3*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1313*F13*G3*(F11*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F21*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F31*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1113*F11*G1*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1213*F12*G1*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1313*F13*G1*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1123*F11*G2*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1223*F12*G2*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1323*F13*G2*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1133*F11*G3*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1233*F12*G3*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)) +
D1333*F13*G3*(F13*(F11*S11*t11 + F12*S12*t11 + F13*S13*t11 + F21*S11*t12 + F22*S12*t12 + F23*S13*t12 + F31*S11*t13 + F32*S12*t13 + F33*S13*t13) +
F23*(F11*S11*t12 + F12*S12*t12 + F13*S13*t12 + F21*S11*t22 + F22*S12*t22 + F23*S13*t22 + F31*S11*t23 + F32*S12*t23 + F33*S13*t23) +
F33*(F11*S11*t13 + F12*S12*t13 + F13*S13*t13 + F21*S11*t23 + F22*S12*t23 + F23*S13*t23 + F31*S11*t33 + F32*S12*t33 + F33*S13*t33)));
Mat[0] += Mat0;
}
...
}

или есть какой-то другой способ помочь компилятору разобрать огромные выражения?

0

Решение

Прежде чем закрыть это, позвольте мне дать вам старт.

Если у вас есть только ваше выражение, а не алгоритм, как он был построен, вы все равно можете провести некоторую оптимизацию. Я принимаю ваше большое выражение для Mat[0] и поместите его в переменную внутри Mathematica. Функция, которую мы используем для оптимизации общих выражений, здесь

Experimental`OptimizeExpression[{x^2 Sin[x^2]}]
(*
Experimental`OptimizedExpression[
Block[{Compile`$125},
Compile`$125 = x^2; {Compile`$125 Sin[Compile`$125]}]]
*)

Мы надеемся, что мы сможем (а) сделать ваше выражение меньше и (б) упростить компилятору, потому что он может оптимизировать операторы лучше, чем большое выражение.

Мы уже видим, что оптимизированное выражение (даже со всеми новыми временными переменными) будет в 6 раз меньше, чем у вас сейчас:

ByteCount[expr]
Experimental`OptimizeExpression[{expr}] // ByteCount

Out[560]= 64624

Out[561]= 11752

Давайте попробуем превратить оптимизированное выражение в C-код.

<< CCodeGenerator`
opt = Experimental`OptimizeExpression[{expr}];
vars = # -> ToString[#] & /@ DeleteDuplicates[Cases[expr, _Symbol, Infinity]]

vars список замены, который имеет правило для каждой переменной в большом выражении формы var -> "var", Это понадобится нам позже, чтобы создать строку c-кода. Если вы посмотрите на оптимизированное выражение, вы увидите, что оно содержит Block с определениями переменных и CompoundExpression в теле, где происходит все действие.

Сначала мы преобразуем все присваивания в допустимые c-объявления и создадим c-блок.

expr1 = opt /. CompoundExpression :> List /.
HoldPattern[Block[_, body_]] :> CBlock[body] /.
HoldPattern[Set[lhs_, rhs_]] :>
CDeclare["const double", CAssign[lhs, rhs]] /. {final_} :>
CAssign["Mat[0]", final] /. repl /. vars // First

После этого у нас есть много вещей вида

CDeclare["const double", CAssign["tmp55", "F22" "S12" "t23"]]

Важно отметить, что операции (как здесь, умножение «F22», «S12» и «t23») все еще там. Они также должны быть заменены действительными c-операциями.

expr2 = expr1 //. {(h : (Times | Plus))[a__] :> COperator[h, {a}],
Power[a_, b_] :> CCall["pow", {a, b}]}

Теперь, когда структура верна, вы можете превратить это в читаемый c-код

ToCCodeString @@ expr2

Обратите внимание, что я не проверил, что все, что я сделал, правильно. Я бы даже не реализовал это с этими заменами, но я просто попробовал и подумал, какого черта, я могу также опубликовать это. Созданный код по сути отражает то, что будет делать компилятор Mathematica. Может быть, это поможет:

const double tmp1 = pow(F12, 2);
const double tmp2 = S12 * t11 * tmp1;
const double tmp3 = F13 * F22 * S13 * t12;
const double tmp4 = F13 * F32 * S13 * t13;
const double tmp5 = F12 * t11;
const double tmp6 = F22 * t12;
const double tmp7 = F32 * t13;
const double tmp8 = tmp5 + tmp6 + tmp7;
const double tmp9 = F11 * S11 * tmp8;
const double tmp10 = F13 * S13 * t11;
const double tmp11 = F21 * S11 * t12;
const double tmp12 = 2 * F22 * S12 * t12;
const double tmp13 = F23 * S13 * t12;
const double tmp14 = F31 * S11 * t13;
const double tmp15 = 2 * F32 * S12 * t13;
const double tmp16 = F33 * S13 * t13;
const double tmp17 = tmp10 + tmp11 + tmp12 + tmp13 + tmp14 + tmp15 + tmp16;
const double tmp18 = F12 * tmp17;
const double tmp19 = F21 * F22 * S11 * t22;
const double tmp20 = pow(F22, 2);
const double tmp21 = S12 * t22 * tmp20;
const double tmp22 = F22 * F23 * S13 * t22;
const double tmp23 = F22 * F31 * S11 * t23;
const double tmp24 = F21 * F32 * S11 * t23;
const double tmp25 = 2 * F22 * F32 * S12 * t23;
const double tmp26 = F23 * F32 * S13 * t23;
const double tmp27 = F22 * F33 * S13 * t23;
const double tmp28 = F31 * S11;
const double tmp29 = F32 * S12;
const double tmp30 = F33 * S13;
const double tmp31 = tmp28 + tmp29 + tmp30;
const double tmp32 = F32 * t33 * tmp31;
const double tmp33 = tmp18 + tmp19 + tmp2 + tmp21 + tmp22 + tmp23 + tmp24 + tmp25 + tmp26 + tmp27 + tmp3 + tmp32 + tmp4 + tmp9;
const double tmp34 = F11 * S11 * t11;
const double tmp35 = F12 * S12 * t11;
const double tmp36 = F22 * S12 * t12;
const double tmp37 = F32 * S12 * t13;
const double tmp38 = tmp10 + tmp11 + tmp13 + tmp14 + tmp16 + tmp34 + tmp35 + tmp36 + tmp37;
const double tmp39 = F11 * tmp38;
const double tmp40 = F11 * S11 * t12;
const double tmp41 = F12 * S12 * t12;
const double tmp42 = F13 * S13 * t12;
const double tmp43 = F21 * S11 * t22;
const double tmp44 = F22 * S12 * t22;
const double tmp45 = F23 * S13 * t22;
const double tmp46 = F31 * S11 * t23;
const double tmp47 = F32 * S12 * t23;
const double tmp48 = F33 * S13 * t23;
const double tmp49 = tmp40 + tmp41 + tmp42 + tmp43 + tmp44 + tmp45 + tmp46 + tmp47 + tmp48;
const double tmp50 = F21 * tmp49;
const double tmp51 = F11 * S11 * t13;
const double tmp52 = F12 * S12 * t13;
const double tmp53 = F13 * S13 * t13;
const double tmp54 = F21 * S11 * t23;
const double tmp55 = F22 * S12 * t23;
const double tmp56 = F23 * S13 * t23;
const double tmp57 = F31 * S11 * t33;
const double tmp58 = F32 * S12 * t33;
const double tmp59 = F33 * S13 * t33;
const double tmp60 = tmp51 + tmp52 + tmp53 + tmp54 + tmp55 + tmp56 + tmp57 + tmp58 + tmp59;
const double tmp61 = F31 * tmp60;
const double tmp62 = tmp39 + tmp50 + tmp61;
const double tmp63 = F13 * tmp38;
const double tmp64 = F23 * tmp49;
const double tmp65 = F33 * tmp60;
const double tmp66 = tmp63 + tmp64 + tmp65;
Mat[0] = D1112 * F11 * G1 * tmp33 + D1212 * F12 * G1 * tmp33 + D1213 * F13 * G1 * tmp33 + D1122 * F11 * G2 * tmp33 + D1222 * F12 * G2 * tmp33 + D1322 * F13 * G2 * tmp33 + D1123 * F11 * G3 * tmp33 + D1223 * F12 * G3 * tmp33 + D1323 * F13 * G3 * tmp33 + D1111 * F11 * G1 * tmp62 + D1112 * F12 * G1 * tmp62 + D1113 * F13 * G1 * tmp62 + D1112 * F11 * G2 * tmp62 + D1212 * F12 * G2 * tmp62 + D1213 * F13 * G2 * tmp62 + D1113 * F11 * G3 * tmp62 + D1213 * F12 * G3 * tmp62 + D1313 * F13 * G3 * tmp62 + D1113 * F11 * G1 * tmp66 + D1213 * F12 * G1 * tmp66 + D1313 * F13 * G1 * tmp66 + D1123 * F11 * G2 * tmp66 + D1223 * F12 * G2 * tmp66 + D1323 * F13 * G2 * tmp66 + D1133 * F11 * G3 * tmp66 + D1233 * F12 * G3 * tmp66 + D1333 * F13 * G3 * tmp66;
2

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]