TransWikia.com

Error on using Compile[]

Mathematica Asked on February 5, 2021

My code is below.

deltaX = 1/128;
W=256;
Mmax=40;

lPoly = ParallelTable[
   LegendreP[
    order, (-1.) + (deltaX/2.) + ((index - 1.)*deltaX)], 
{order, 0, Mmax}, {index, 1, W}];

XPoly = Compile[{{index, _Integer}}, 
   Block[{}, 
    polyMatrix = 
     PadRight[Table[lPoly[[m - n + 1, index]], {m, 0, Mmax}, {n, 0, m}]]; 
    polyMatrix], CompilationTarget -> "C",
   RuntimeAttributes -> {Listable}, Parallelization -> True, 
   RuntimeOptions -> {"CatchMachineIntegerOverflow" -> False}];

If I run XPoly[1], it will return:

CompiledFunction::cfse :  Compiled expression {{1.},{-0.996094,1.},<<48>>,<<71>>} should be a machine-size real number.

I have encountered this kinda error multiple times, sometimes it solved. But I dont know why.

One Answer

This should work better: It generates a rectangular array (filled with zeroes) first and than fills in the entries:

deltaX = 1./128;
W = 256;
Mmax = 40;
lPoly = Developer`ToPackedArray[
   Table[
     LegendreP[order, -1. + 0.5 deltaX + (index - 1.) deltaX], 
     {order, 0, Mmax}, {index, 1, W}]
   ];

XPoly = Compile[{{index, _Integer}, {lPoly, _Real, 2}},
   Block[{polyMatrix, Mmax},
    Mmax = Length[lPoly] - 1;
    polyMatrix = Table[0., {Mmax + 1}, {Mmax + 1}];
    Do[
     polyMatrix[[m + 1, n + 1]] = lPoly[[m - n + 1, index]]
     ,
     {m, 0, Mmax}, {n, 0, m}];
    
    polyMatrix
    ],
   CompilationTarget -> "C",
   RuntimeAttributes -> {Listable},
   Parallelization -> True
   ];

test = XPoly[{1, 2, 3}, lPoly];

Answered by Henrik Schumacher on February 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP