Let's consider the following:
n = 1000; m=3;
p = RandomReal[{0, 9}, {n,m}];
f = Compile[{{ps, _Real, 2}},
Outer[
Which[
2 < Abs[#1[[1]] - #2[[1]]] < 3 && 2 < Abs[#1[[2]] - #2[[2]]] < 3,
Sin@Norm[#1 - #2],
2 < Abs[#1[[2]] - #2[[2]]] < 3 && 2 < Abs[#1[[3]] - #2[[3]]] < 3,
Cos@Norm[#1 - #2]
,True, 0]
&, ps, ps]];
f[p]
It gives the error message
Compile::part: Part specification Compile`FunctionVariable$41818[1] cannot be compiled since the argument is not a tensor of sufficient rank. Evaluation will use the uncompiled function. >>
because Outer
doesn't combine vectors like it does elements (see f.e. Outer[h, {{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}]
- the elements are not say h[{1,2},{5,6}]
as you might think, which is what I want). How can I solve this? I left the Compile
in because it is important to me that the solution is compilable.
The result should be a sparse array of numeric values.
This is basically an extension of my earlier question Faster use of Condition for a large array (see the example) which was answered by Pickett. As the code above shows, I don't know what to do when p
is a two-dimensional list instead of a one-dimensional list.
Sin
,Cos
and the new conditions, and we can help you from there. Mathematica code is strongly preferred, when possible, over the math formulas. $\endgroup$Which
andTable
. $\endgroup$Outer
which you may not have been able to write, this question would still be very readable. Anyone can see from the code what you are trying to do etc. the math formulas etc. were mere distractions because they weren't what you had a problem with. The way your first question was written you would have us type the formulas into Mathematica before we could even begin to think about the problem. $\endgroup$