Tag Info

New answers tagged

2

I replaced a[#] with Abs[#] and b[#] with Abs[#] in your fastest solution. It took 7.8 seconds to evaluate. The following takes 0.085 seconds to evaluate and gives the same result: m = Compile[{{p, _Real, 1}}, Outer[Which[ 2 < Abs[# - #2] < 3, Abs[# - #2], 5 < Abs[# - #2] < 6, Abs[# - #2], True, 0 ] &, p, p], ...


1

Not particularly memory efficient, but ~20X faster than your first, ~5X faster than your non-parallel map (which, btw, does not produce the same results as your first) on n=1000... oo = Flatten@Outer[Subtract, p, p]; aoo = Abs@oo; rl = (Length@p)^2; aa = Pick[Range@rl, Unitize@Clip[aoo, {2, 3}, {0, 0}], 1]; bb = Pick[Range@rl, Unitize@Clip[aoo, {5, 6}, {0, ...


4

If Table is part of your actual operation you will be served by learning Array: Array[#, {2, 2}] & /@ {Plus, Subtract, Times, Divide} { {{2, 3}, {3, 4}}, {{0, -1}, {1, 0}}, {{1, 2}, {2, 4}}, {{1, 1/2}, {2, 1}} }


10

This is really a natural fit for Outer: t = Table[{i, j}, {i, 1, 2}, {j, 1, 2}]; Outer[Apply, {Plus, Subtract, Times, Divide}, t, 2] (* ==> {{{2, 3}, {3, 4}}, {{0, -1}, {1, 0}}, {{1, 2}, {2, 4}}, {{1, 1/2}, {2, 1}}} *)


3

An alternative using Replace to do this: mytable = Table[{i, j}, {i, 1, 2}, {j, 1, 2}]; Replace[mytable, List[a_, b_] -> #[a, b], {-2}] & /@ {Plus, Subtract, Times, Divide} {{{2, 3}, {3, 4}}, {{0, -1}, {1, 0}}, {{1, 2}, {2, 4}}, {{1, 1/2}, {2, 1}}}


7

You very nearly had it. What you need, instead of Map[], is Apply[]. This can then be combined with Map[], like so: mat = Table[{i, j}, {i, 2}, {j, 2}]; Apply[#, mat, {2}] & /@ {Plus, Subtract, Times, Divide}


5

You might just map the Maps ops = {plus, subtract, times, divide} = Function[op, Map[op[#[[1]], #[[2]]] &, Table[{i, j}, {i, 1, 2}, {j, 1, 2}], {2}]] /@ {Plus, Subtract, Times, Divide} {{{2, 3}, {3, 4}}, {{0, -1}, {1, 0}}, {{1, 2}, {2, 4}}, {{1, 1/2}, {2, 1}}}


11

Map is automatically compiled. Yes, even with RandomChoice. Try it: f = Compile[{{p, _Real, 0}, {t, _Integer, 1}}, Map[# RandomChoice[{p, 1 - p} -> {1, 0}] &, t] ]; f // InputForm (* -> clean bytecode *) Check its performance: p = 0.1; t = Table[1, {10^6}]; SeedRandom[1000]; AbsoluteTiming[a = Map[# RandomChoice[{p, 1 - p} -> {1, 0}] ...


13

This is because of the compilation that kicks in automatically if the list in Map exceeds a certain number of elements. "MapCompileLength" /. ("CompileOptions" /. SystemOptions["CompileOptions"]) (* Out: 100 *) shows that the default setting is that if the list contains more than 100 elements then Map will be compiled. MapThread on the other hand does not ...


4

As mentioned in the comments, the construct you are probably looking for is Outer (see its documentation page). You could then define the following function to achieve what you indicated, even without using Map: Clear[func] func[list1_List, list2_List, factor_?NumberQ] := factor Outer[Times, list1, list2] func[{1, 2, 3}, {1, 2, 3}, 3] (* Out: {{3, 6, 9}, ...


4

As requested, I'm copying the above comment as an answer: g[x, ##] & @@@ Tuples[{X, Y}] seems an elegant way to me


4

f0 = EuclideanDistance @@@ Partition[#, 2, 1] &; (* Nasser's post*) f1 = Norm /@ Differences@# &; (* Guesswhoitis's comment *) f2 = Compose[EuclideanDistance @@@ # &, Partition[#, 2, 1] &, #] &; f3 = Composition[EuclideanDistance @@@ # &, Partition[#, 2, 1] &]; f4 = Developer`PartitionMap[EuclideanDistance @@ # &, #, 2, 1] ...



Top 50 recent answers are included