Outer is slowing down considerably when the first argument is Abs[#1 - #2]/Max[#1, #2]&
Outer[List, Range[5000], Range[5000]]; // AbsoluteTiming
(* {0.120091, Null} *)
Outer[Abs[#1 - #2]/Max[#1, #2]&, Range[5000], Range[5000]]; // AbsoluteTiming
(* ~ 59 seconds *)
Is there a faster way to perform the above computation? In the real problem I have two lists that are different and not Range[5000] i.e. the lists can be of different lengths.
Example with two small lists:
list1 = {6576.13, 7504.5, 6964., 7645.63, 5297.5, 6897.75, 4944.13, 8184.13,
3426.75, 8722.75, 3683.63, 15344.4, 7026.25, 5677.63, 6872.88,
6050.5, 7948.63, 5095.13, 6335.25, 6024.25, 6508.88, 6961.63,
8262.13, 4560.38, 7113.75, 7011., 9070.13, 5625.88, 7801., 6855.38,
5973.25, 6164.75, 6115.75, 3886.13, 11967.4, 6606.13, 6223.5,
5576.38, 7855.88, 5616.38, 5946.88, 4750.25, 6162.25, 6539.88,
5563.75, 7723.63, 6241.5, 3794.13, 6854.88, 8154., 4241.};
(* length is 50 *)
list2 = {3762.13, 7272.75, 7923.25, 7882.38, 4407., 7110., 6468.5, 7565.88,
3117.25, 15918.8, 3753.5, 8801.25, 7120.13, 6643.63, 6565.5, 7537.,
6081.5, 6948.88, 7468.63, 6736., 7091.75, 7980., 5143.38, 7540.88,
6754., 5746.13, 9075.63, 4536.5, 7873.75, 7106.5, 5127., 3809.25,
5274.5, 6760.25, 7031.25, 7158.38, 7484.25, 5753.25, 6105.38,
7084.63, 4866.88, 5690., 7179., 5572.88, 6209.13, 7820.25, 5432.5,
2281.63, 3917.13, 4050.75};
(* length is 51 *)
Outer[Abs[#1 - #2]/Max[#1, #2]&,list1,list2];
This question is not a duplicate since it involves lists of different lengths.
DistanceMatrixperforms horribly with the customDistanceFunctionforRange[5000]– Ali Hashmi Jul 20 '17 at 14:24Compileversion in your response goes to machine real arithmetic, hence the speed gain. That at least is my guess for what causes the difference (I've not tried to verify it though). – Daniel Lichtblau Jul 20 '17 at 16:23DistanceMatrix[list1, list2]/DistanceMatrix[list1, list2, DistanceFunction -> Max]? – J. M.'s missing motivation Jul 26 '17 at 14:38