# Evaluation control inside Compile

I have the following function

cTestEvalControl =
With[{eq =
Unevaluated[{{Flatten[a + 5 b^2] d + Flatten[a^2*b + 3 b*a] c,
Flatten[a*b^2] c +
Flatten[a^2*b] d}, {Flatten[a*b^3 + 5 b^2] c +
Flatten[a^2*b^4 + 12 b*a^2] c,
Flatten[4 a*b^2 + 3 a] c + Flatten[a^2*b^2] d}}]},
Compile[{{a, _Real, 1}, {b, _Real, 2}, {c, _Real, 2}, {d, _Real,
2}}, eq, RuntimeAttributes -> {Listable},
Parallelization -> True]];


some small example with it

v1 = RandomReal[{0, 1}, {2}]; m1 =
RandomReal[{0, 1}, {2, 2}]; m2 = RandomReal[{0, 1}, {4, 4}]; m3 =
RandomReal[{0, 1}, {4, 4}];

cTestEvalControl[v1, m1, m2, m3]

{{{{2.27262, 2.56534, 2.07663, 1.60572}, {0.59353, 0.132561, 0.401489,
0.114946}, {0.657416, 0.388311, 0.43386, 0.414723}, {0.903638,
0.686363, 0.38627, 0.749547}}, {{0.169533, 0.236441, 0.178987,
0.206864}, {0.0235736, 0.00447615, 0.0158849,
0.00488492}, {0.019912, 0.0155357, 0.0189165,
0.0178772}, {0.0257693, 0.0501467, 0.0207543,
0.0225687}}}, {{{1.51258, 2.92861, 2.03442, 3.42816}, {0.293951,
0.197261, 0.209069, 0.00361905}, {0.220347, 0.293738, 0.39574,
0.3694}, {0.310412, 1.25446, 0.457183, 0.29725}}, {{0.871715,
1.62655, 1.13975, 1.85736}, {1.26437, 0.847259, 0.899176,
0.0160615}, {0.287964, 0.380763, 0.512416, 0.478373}, {0.211849,
0.832085, 0.304355, 0.201926}}}}


I really need the evaluation inside the Flatten to happen before the Flatten.

Is this the best way to do this?

In reallity eq will be a large matrix, say 100x100, of symbolic expressions, in which I want to plug in numerical values to evaluate it. In total there are 50 different arguments that I have to plug in to get a numerical value for the whole matrix eq. So each expression inside eq has the structure of Flatten[longAndNasty]*variable. Each longAndNasty (it is different for every expression) will be thousands of terms long, consisting of various combinations of these 50 arguments. Some of these arguments are scalars (like the constants 5, 3, 12 and so on in my example), some are vectors of length 'N' (like the a in the example) and some are matrices of size NxN (like b in my example). The variable thing is a an arguemnt that I plug in, like all the previous things I have described, but it is of size N^2xN^2, hence why I need to Flatten longAndNasty.

My non-compiled code uses an Association which I pass to a KeyValueMap that basically applies Flatten[Value/.{ReplacementRules}]*BigMatrix and then I Total over all the Keys. I am looking for a speed up through Compile.

• I really do not get the purpose of this question. I do not see any symbolic matrices here and the job that cTestEvalControl is doing can be performed faster with ordinary vectorized code like Flatten[v1 m1^2] m2 + Flatten[v1^2 m1] m3. – Henrik Schumacher Oct 16 at 14:41
• @HenrikSchumacher In reallity eq is a matrix of expressions, say 100x100, whereby each expression is something long and nasty that has the form of Flatten[longAndNasty]*c, whereby c is to be replaced with a big matrix and longAndNasty is a symbolic expression in which I want to plug in vectors and matrices. – ThunderBiggi Oct 16 at 14:47
• c is with dimensions (nx^2) by (nx^2), whereas the vectors and matrices in longAndNasty are with sizes (nx) and (nx) by (nx), respectively. Hence, flattening the longAndNasty bit gives you something that can multiply the c – ThunderBiggi Oct 16 at 14:53
• How are we supposed to understand it without a complete minimal example? – Henrik Schumacher Oct 16 at 15:28
• @HenrikSchumacher I have given the most minimal example? Is it too minimal? Do you want me to add more terms to the expressions? Or shall I make eq a matrix? The applied solution should work irrespective of the tensor rank of eq, shouldn't it? – ThunderBiggi Oct 16 at 15:49