Tag Info

Hot answers tagged

22

Updated with new functions and additional timings Since this question inspired so many answers I think there is as need to compare them. I have included two of my own functions, freely borrowing from previous answers: wizard1[] := Inner[Compose, sel /. {True -> f, False -> Identity}, list, List] wizard2[] := Module[{x = list}, x[[#]] = f /@ ...


16

Perhaps something like this: list = {1, 10, 100}; sel = {True, False, True}; MapThread[f, {list, sel}] (* {f[1, True], f[10, False], f[100, True]} *) So, like: f[i_, True] := f[i] f[i_, False] := i MapThread[f, {list, sel}] (* {f[1], 10, f[100]} *)


14

If[ #[[2]], f[#[[1]]], #[[1]]] & /@ Transpose[{list, sel}] {f[1], 10, f[100]} this should be a bit faster : If[ Last @ #, f @ First @ #, First @ #] & /@ Transpose[{list, sel}] or using Inner : Inner[ If[#2, f, Identity][#1] &, list, sel, List]


13

For Mean you don't have to do any transformation to the input array data = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Mean[data] (* {4, 5, 6} *) because (from docs Mean >> More Information) that is, Mean "threads" over its input when it is fed an array. In general, in addition to func/@Transpose[data] (as in @m_goldberg's answer) you can also use ...


12

This version seems to be about twice faster than the fastest so far (generally, as much faster as small is a fraction of selected elements), and about an order of magintude faster when Listable functions are mapped on a numerical list - since it automatically utilizes Map auto-compilation in such cases: ClearAll[conditionalMap]; conditionalMap[f_, lst_, ...


11

How about something unconventional? Transpose[{list, sel}] /. {x_, y_} :> (f^Boole[y])[x] /. 1[x_] :> x (* {f[1], 10, f[100]} *) Again, another unconventional solution: Transpose[{list, sel}] /. {x_, y_} :> (y /. {True -> f, False -> Identity})[x]


9

There are two built-in functions to generate pairs, either with (Tuples) or without (Subsets) duplication. Since your question states the number of iterations as $n*(n-1)/2$ I believe you want the latter: set = {1, 2, 3, 4}; Subsets[set, {2}] {{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}} The short notation for Apply at level 1 is @@@, so this ...


9

Just playing around Normal@SparseArray[{i_ /; sel[[i]] :> f[list[[i]]], i_ :> list[[i]]}, Dimensions@list] Another playful one Total[{#~BitXor~1, #} &@Boole@sel {list, f /@ list}] An almost similar solution to @Artes and @ruebenko's that I find neater could be If[#1, f[#2], #2] & ~ MapThread ~ {sel, list} The function on the lhs of ...


9

As J. M. suggested: isGood[___] = False; list = RandomInteger[{-100000000, 100000000}, 1000000]; Scan[(isGood[#] = True) &, list]; // AbsoluteTiming (* ==> {3.1651810, Null} *) On my computer, this takes about 3 seconds for a million integers. Isn't this fast enough? Retrieval of the results is also quite quick: (*retrieve the results*) ret = ...


8

Are you sure you want to use UpValues? You can use Dispatch which is pretty fast when generating the lookup table and is equally fast when accessing values: n = 6; list = RandomInteger[{0, 10^(n + 1)}, {10^n}]; AbsoluteTiming[disp = Dispatch@Thread[list -> True];] {1.6220927, Null} Remove[isGood]; AbsoluteTiming[isGood[___] = False; ...


8

What you try to achieve here is called Currying which can be used in other languages like Haskell naturally. In Mathematica this does not work like that. But what about Outer[f, list1, list2] (* {{f[a, x], f[a, y], f[a, z]}, {f[b, x], f[b, y], f[b, z]}, {f[c, x], f[c, y], f[c, z]}} *) or Flatten@Outer[f, list1, list2] if you want a flat list? ...


8

First of all, let's clarify that if you define h as `h[{x_, y_}] := ...` then it takes a single argument which is a list of two items. If you define it as `h[x_, y_] := ...` then it takes two separate arguments. #n denotes the nth argument in a pure function. In the function call (#1^#2)& [{2,3}] you are passing the pure function a single ...


7

If your elements are in lists the fastest way is to use array operations. In the present case of an outer product one index, let's say "i", will not be expanded, on the other you want to thread. To operate on a list the function needs the attribute Listable. The Times function, as many other internal ones, is already listable, that is Times[{1,2,3},x] = ...


6

You may use map with a pure function: f[#,10,100]& /@ xVals {f[1, 10, 100], f[2, 10, 100], f[3, 10, 100], f[4, 10, 100]} Table will also work: Table[f[x, 10, 100], {x, xVals}] {f[1, 10, 100], f[2, 10, 100], f[3, 10, 100], f[4, 10, 100]} Multiple iterator form: Table[f[x, y, 100], {x, {1, 2, 3, 4}}, {y, {5, 6, 7, 8}}]


6

I quite like the following myself: list = {1, 10, 100}; sel = {True, False, True}; MapIndexed[If[sel[[Sequence @@ #2]], f[#1], #1] &, list] {f[1], 10, f[100]} Here, we leverage the fact that MapIndexed[] conveniently produces the position of the objects its first argument is being mapped at. For the positions to be usable by Part[], one has to ...


6

vertices = Range[10]; pairs = Tuples[vertices, 2]; func[x__] := First@x <= Last@x; edges = Pick[pairs, func[#] & /@ pairs]; (* or *) edges = Pick[pairs, Boole[func[#]] & /@ pairs, 1]; Graph[DirectedEdge @@@ edges, VertexLabels -> "Name", ImagePadding -> 20] Graph[DirectedEdge @@@ edges, VertexLabels -> "Name", ImagePadding -> 20] ...


6

For educational purposes, here's a couple other ways to do this: Power @@@ {{1, 2}, {2, 2}, {3, 2}} Power[Sequence @@ #] & /@ {{1, 2}, {2, 2}, {3, 2}} Cases[{{1, 2}, {2, 2}, {3, 2}}, List[x__] :> Power[x]] # /. List -> Power & /@ {{1, 2}, {2, 2}, {3, 2}} Replace[{{1, 2}, {2, 2}, {3, 2}}, List -> Power, {2}, Heads -> True] ...


5

Yes, there are definitely shorter, non-loop ways to do this. Define your link-defining function like this (with whatever test you need as the first argument to If: islinked[a_Integer?Positive, b_Integer?Positive] := If[Mod[a, 3] == 0 && Mod[b, 2] == 1, a -> b, {}] You can then Apply this at the level of each row using the @@@ shorthand. I ...


5

MapThread[(#2 f1[#1] + (1 - #2) #1) &, {list, Boole@ sel}] or (#2 f1[#1] + (1 - #2) #1) & @@@ Thread[{list, Boole@sel}] or Inner[(#2 f1[#1] + (1 - #2) #1) &, list, Boole@sel, List] or list /. Dispatch[Thread[# -> f1 /@ #] &@Pick[list, sel]]


5

For those that don't like the look of multiple nested brackets arising from the use of Part (eg [[1]]), I would point out that Artes' answer is equivalent to using Apply at level 1, which has the shorthand syntax @@@ If[#2, f[#1], #1] & @@@ Transpose[{list, sel}]


5

Or you could use Tuples, which appears a bit more natural to me. Tuples[{{a, b, c}, {x, y, z}}] creates {{a, x}, {a, y}, {a, z}, {b, x}, {b, y}, {b, z}, {c, x}, {c, y}, {c, z}} Afterwards Apply can be used to apply your function to the sublist Apply[f , Tuples[{{a, b, c}, {x, y, z}}], {1}] creates: {f[a, x], f[a, y], f[a, z], f[b, x], f[b, y], f[b, ...


5

Simply you could use: Thread @ h[{1, 2}, a, b] {h[1, a, b], h[2, a, b]} If you can demonstrate how that fails in your application I will give other methods. It was suggested that I use Sequence @@ {a, b} so as to keep {a, b} in the given form. I did not, because I was not clear as to the expected input format and because I felt that it would ...


5

One option is to separate the slots by using an explicit Function for the second argument Map[Function[arg, Apply[{h[arg, ##]} &, {a, b}]], {1, 2}] Regarding your updated question. The approach is the same Map[Function[arg, Apply[{h1[arg, ##], h2[arg, ##]} &, {RandomReal[], RandomReal[]}]], {1, 2}]


5

A few other alternatives: either you can make g itself a Listable function by executing SetAttributes[g,Listable] (assuming that g is a symbol), or you can do something like this: Function[Null, g[#], Listable][list] where I don't make any assumptions on g (which may be a symbol but may be something else). Note that there are subtle differences between ...


5

But it doesn't work. Why is that? It becomes visible when you inspect the inner Map only. I replace the slot for the outer function with 1, because we don't need it to see the error b = {1, 2}; c = {1, 2, 3}; Map[f[# &, 1], b] (* {f[#1 &, 1][1], f[#1 &, 1][2]} *) This is not what you expect and when you look a bit closer, you instantly ...


4

You've almost got it: list1 = {{"a", 1}, {"b", 2}, {"c", 3}}; list2 = {"A", "b"}; test = StringMatchQ[#, Alternatives @@ list2, IgnoreCase -> True] &; Cases[list1, {_?test, _}] {{"a", 1}, {"b", 2}} A key element is clearly Alternatives. I used Cases rather than DeleteCases as that seemed simpler to me. The first part of my post is in ...


4

Contrary to what the title claims, your example shows that do not want to map at the "maximum depth" of the list, but rather, merely onto the elements of a List that are not lists themselves. I think you're over complicating things with your definition of mapAtLeavesOfList. The solution is as simple as: Clear@g g[a_List] := g /@ a g@list (* {g[1], g[2], ...


4

As expressed in the comments, the Replace functions are not merely "syntactic sugar" for Map. The two are quite different. One primary difference is the order in which expressions are visited. See: How to perform a depth-first preorder traversal of an expression? Another is that Replace will go inside held expressions, while Map does not evaluate: Hold[1 ...


3

You can use Outer to get a nested output list : Outer[f[#1, #2, 100] &, {1, 2, 3, 4}, {5, 6, 7, 8}] (* {{500, 600, 700, 800}, {1000, 1200, 1400, 1600}, {1500, 1800, 2100, 2400}, {2000, 2400, 2800, 3200}} *) However I'm not sure you can take advantage of Parallelize.


3

My solution is ugly, but task-specific. It builds a bitmap out of machine-sized integers in imperative fashion and uses Compile. This works reasonably in memory usage for ranges that have at least couple percent of True values. A million integers: n = 6; list = RandomInteger[{0, 10^(n + 1)}, {10^n}]; Function itself: << Developer` isGood = With[ ...



Only top voted, non community-wiki answers of a minimum length are eligible