You can replace MapThread[func,list,2]
with func/@Transpose[Flatten/@list]
.
s[{x_Real,y_Real,z_Real,w_Real}]:={{x+I y,-z+I w},{z+I w,x-I y}};
ssamp = Compile[{{n, _Integer}, {nd, _Integer}},
s /@
Transpose[
Flatten /@
Transpose[
Map[Normalize,
RandomVariate[NormalDistribution[0, 1], {n, nd, 4}], {2}], {3, 2, 1}]
]
]
When you evaluate this compiled function, first 3 times and only 3 times (it's very strange!) you will see such error message.
ssamp[2, 2]
CompiledFunction::cfex: Could not complete external evaluation at instruction 36; proceeding with uncompiled evaluation. >>
But CompilePrint
looks okay.
Needs["CompiledFunctionTools`"]
CompilePrint@ssamp
"
2 arguments
17 Integer registers
2 Real registers
7 Tensor registers
Underflow checking off
Overflow checking off
Integer overflow checking on
RuntimeAttributes -> {}
I0 = A1
I1 = A2
I3 = 0
I2 = 4
T(I1)6 = {3, 2, 1}
I14 = -1
I4 = 1
I16 = 3
Result = T(R2)0
1 T(I1)1 = {I0, I1, I2}
2 R0 = I3
3 R1 = I4
4 T(R3)3 = RandomNormal[ R0, R1, T(I1)1]]
5 I8 = Length[ T(R3)3]
6 T(R2)0 = Part[ T(R3)3, I4]
7 I11 = Length[ T(R2)0]
8 I15 = I14
9 T(R3)0 = Table[ I8, I11, I15]
10 I12 = I3
11 goto 18
12 I13 = I3
13 goto 17
14 T(R1)4 = GetElement[ T(R3)3, I12, I13]
15 T(R1)5 = MainEvaluate[ Hold[Normalize][ T(R1)4]]
16 Element[ T(R2)0, I15] = T(R1)5
17 if[ ++ I13 <= I11] goto 14
18 if[ ++ I12 <= I8] goto 12
19 T(R3)3 = Transpose[ T(R3)0, T(I1)6, I16]]
20 I6 = Length[ T(R3)3]
21 I9 = I14
22 T(R2)0 = Table[ I6, I9]
23 I7 = I3
24 goto 28
25 T(R2)5 = GetElement[ T(R3)3, I7]
26 T(R1)4 = Flatten[ T(R2)5, I4]]
27 Element[ T(R2)0, I9] = T(R1)4
28 if[ ++ I7 <= I6] goto 25
29 T(R2)3 = Transpose[ T(R2)0]]
30 I5 = Length[ T(R2)3]
31 I6 = I14
32 T(R2)0 = Table[ I5, I6]
33 I9 = I3
34 goto 38
35 T(R1)4 = GetElement[ T(R2)3, I9]
36 T(R1)5 = MainEvaluate[ Hold[s][ T(R1)4]]
37 Element[ T(R2)0, I6] = T(R1)5
38 if[ ++ I9 <= I5] goto 35
39 Return
"
I hope this helps.
Normalize
andNormalDistribution
$\endgroup$Normalize
andNormalDistribution
are not the problem since it compiles if I removeMapThread
$\endgroup$