# How to make a compiled version of a function?

I'm trying to make a compiled version of this function

LN = Compile[{{a, _Real, 1}, {CMat, _Real}, {P, _Real,
2}, {OMG, _Real, 2}},
Module[{Mat, CoPart, CovT, root, Eigs},
Mat = DeveloperToPackedArray[CMat];
CoPart = ArrayFlatten[Mat[[a, a]]];
CovT = P[1, Length[a] - 1].CoPart.P[1, Length[a] - 1];
root = Eigenvalues[-OMG[Length[a]].CovT.OMG[Length[a]].CovT];
Eigs = Delete[Sqrt[root], Table[{2*i}, {i, 1, Length[root]/2}]];
Sum[-Log2[Min[1, Eigs[[i]]]], {i, 1, Length[Eigs]}]],
Parallelization -> True]


but I face some errors as follows

Compile::cplist: Compile$8 should be a tensor of type Integer, Real, or Complex; evaluation will use the uncompiled function. Compile::cplist: Compile$8.CoPart should be a tensor of type Integer, Real, or Complex; evaluation will use the uncompiled function.

Compile::cplist: Compile\$8 should be a tensor of type Integer, Real, or Complex; evaluation will use the uncompiled function.

General::stop: Further output of Compile::cplist will be suppressed during this calculation.


CMat is an n×n matrix of 2×2 blocks, and P and OMG are some 2n×2n matrices.

Any help would be appreciated.

• I suggest starting from here: mathematica.stackexchange.com/a/104031/1871 – xzczd Apr 25 at 11:34
• Here an incomplete list of issues: DeveloperToPackedArray[CMat] in code to be compiled does not make sense; whenever CMat is an input array of a compiled functions, it is already packed. Morever, Eigenvalues cannot be compiled (it is already essentially a library function) and indexing into OMG should be done with OMG[[Length[a]]] instead of OMG[Length[a]]. Parallelization -> True without RuntimeAttributes -> {Listable} does not do anything. The rank specification of CMat is certainly wrong; it should be something like {CMat, _Real, 4}. – Henrik Schumacher Apr 25 at 11:56
• Mat[[a, a]] is going to be troublesome if a is a list of reals. – Henrik Schumacher Apr 25 at 11:59
• Thank you. 'Forgot to say: OMG is a square matrix function and the argument is its dimension, P matrix as well. Unfortunately, the input for compile function is a list of integers e.g. LN[{1,2,3}] and maybe parallelization wouldn't make any sense. Unluckily, I don't know what the rank of a block matrix is. CMat is a MxM matrix of 2x2 blocks, by the way. Unfortunately, a` is a list of integers. – Ghaem Apr 25 at 13:15