I am attempting to Compile
this function which is used to calculate a elastic collision in two dimensions. The two first coordinates of each list are the position and the last are the velocity of each colliding particle. It should take an argument like:
list={{1,2,3,4},{5,6,7,8}}
cF = Compile[{{list, _Real, 2}},
If[
(list[[2]] - list[[1]])[[{1, 2}]] != {0, 0},
Map[
Flatten[
{#[[1,1 ;; 2]]
+ (#[[1,3 ;; 4]].{(#[[1, 2]] - #[[2, 2]]), (#[[2, 1]] - #[[1,1]])}
/ Sqrt[(#[[1, 2]] - #[[2, 2]])^2 + (#[[2,1]] - #[[1, 1]])^2]
) {(#[[1, 2]] - #[[2,2]]), (#[[2, 1]] - #[[1, 1]])}
/Sqrt[(#[[1, 2]] - #[[2, 2]])^2 + (#[[2, 1]] - #[[1,1]])^2]
+ (#[[2,3 ;; 4]].(#[[2]] - #[[1]])[[{1, 2}]]
/ Sqrt[(#[[2]] - #[[1]])[[1]]^2 + (#[[2]] - #[[1]])[[2]]^2]
) (#[[2]] - #[[1]])[[{1, 2}]]
/Sqrt[(#[[2]] - #[[1]])[[1]]^2 + (#[[2]] - #[[1]])[[2]]^2]
, (#[[1, 3 ;; 4]].{(#[[1, 2]] - #[[2, 2]]), (#[[2, 1]] - #[[1, 1]])}
/ Sqrt[(#[[1, 2]] - #[[2, 2]])^2 + (#[[2, 1]] - #[[1,1]])^2]
) {(#[[1, 2]] - #[[2, 2]]), (#[[2, 1]] - #[[1, 1]])}
/ Sqrt[(#[[1, 2]] - #[[2, 2]])^2 + (#[[2, 1]] - #[[1,1]])^2]
+ (#[[2, 3 ;; 4]].(#[[2]] - #[[1]])[[{1, 2}]]
* Sqrt[(#[[2]] - #[[1]])[[1]]^2 + (#[[2]] - #[[1]])[[2]]^2]
) (#[[2]] - #[[1]])[[{1, 2}]]
/ Sqrt[(#[[2]] - #[[1]])[[1]]^2 + (#[[2]] - #[[1]])[[2]]^2]}
] &, (* End Flatten *)
{{list[[1]], list[[2]]}, {list[[2]], list[[1]]}}
], (* End Map; End True branch of If *)
Map[Flatten[{#[[{1, 2}]] + #[[{3, 4}]], #[[{3, 4}]]}] &, list]
] (* End If *)
]; (* End Compile*)
However, it throws an error:
Compile::cplist:"Compile`GetElement[System`Private`CompileSymbol[0],System`Private`CompileSymbol[1]][[1,Compile`$5]] should be a tensor of type Integer, Real, or Complex; evaluation will use the uncompiled function."
Comments
Post a Comment