How does one use Outer
with a compiled function that accepts 3 or more arguments. Alternatively, how does one create a compiled function with 3 or more arguments that can be used with Outer
?
I am trying to use the following function:
minimumImagePD = Compile[{{x, _Real, 1}, {y, _Real, 1}, {z, _Real}},
Total[( (x - y) - z * Round [ (x - y) / z]) ^ 2]]
This function compiles just fine but you can't use it with Outer
. Trying the following doesn't work because Compile
does not "see" the argument 'z':
minimumImagePD2[z_Real] = Compile[{{x, _Real, 1}, {y, _Real, 1}},
Total[((x - y) - z * Round[ (x - y)/z] ) ^ 2]]
Outer[minimumImagePD2[3.2], {{2.1, 3.2, 4.3}}, {{3.2, 4.3, 2.8},
{1.1, 2.2, 3.3}, {3.4, 2.0, 6.5}}, 1].
Now, If I replace z
in minimumImagePD
with a number as follows:
minimumImagePD3 = Compile[{{x, _Real, 1}, {y, _Real, 1}},
Total[((x - y) - 3.25026*Round[(x - y)/3.25026])^2]]
The function compiles and works fine as with the following example:
Outer[minimumImagePD3, {{2.3, 4.3, 6.5}}, {{2.1, 4.8, 7.3}, {2.2,
1.1, 4.3}, {2.1, 3.3, 4.7}}, 1] // Flatten
Which gives:
{0.93, 1.11557, 3.14325}
As noted by Oleksandr R. below, using set-delayed in minimumImagePD2
solves the problem.
minimumImagePD = Compile[{{x, _Real, 1}, {y, _Real, 1}, {z, _Real}}, Total[List @ ((x - y) - z*Round[(x - y)/z])^2]]
– andre Feb 7 at 18:03Outer
as there's no room for that third argument. – RunnyKine Feb 7 at 18:17z
to go. That's why I tried the 2nd variation of the function.minimumImagePD
should acceptList
just like I showed withminimumImagePD2
. – RunnyKine Feb 7 at 18:28