Tag Info

Hot answers tagged

31

Preamble This is a very good question, because answering it will make it very clear what immutability means, both in general and in the context of Associations. General A few general words on immutability Associations are immutable data structures. This means that they carry no state, and a copy of an Association is another completely independent ...


28

These three functions are similar (speaking commonly), and in some applications any of them could be used, yet they have very different special applications. Rudimentarily: Map wraps (sub)expressions in a given Head, and returns the modified input Apply replaces Heads in (sub)expressions, and returns the modified input Scan "visits" (sub)expressions, ...


26

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 /@ ...


20

I hesitate to add anything after @Leonid's comprehensive answer, but I'd like to point out that an easy way to achieve the stated goal is to define f like this: f[x_] := <| x, "isFirstValueTrue" -> x@"firstValue" |> ... which yields the desired result when mapped across the associations in x: f /@ x (* { <|"firstValue" -> True, ...


16

Please see the Utility function section for a concise summary. An arbitrary density plot for the example: den = DensityPlot[Sin[x] Sin[y], {x, -180, 180}, {y, -90, 90}] : Extract the graphics primitives from the density plot: prim = First @ Cases[den, Graphics[a_, ___] :> a, {0, -1}, 1]; Plot them directly with GeoGraphics while setting the ...


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 ...


13

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

Summary: undocumented HashTable is a bit faster (at least in version 9) both in storage and in retrieval than DownValues. DownValues list = RandomInteger[{-10^9, 10^9}, 10^6]; ret = RandomInteger[{-10^9, 10^9}, 10^6]; isGood[___] = False; Scan[(isGood[#] = True) &, list]; // AbsoluteTiming (* ==> {3.240005, Null} *) ClearAll[isGood]; isGood[___] ...


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 ...


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_, ...


12

Use Map with a levelspec of {-1}: Map[g, {a, b, {c, d}, {{e}}}, {-1}] {g[a],g[b],{g[c],g[d]},{{g[e]}}}


11

This perhaps: Function[{a, b}, a[#]/b[#] &] @@@ {{a, b}, {c, d}, {e, f}} (* Out: {a[#1]/b[#1] &, c[#1]/d[#1] &, e[#1]/f[#1] &} *) Mr.Wizard's way of writing it (see comment) looks like this in the frontend:


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}] ...


10

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 ...


10

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; ...


10

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]


10

This is a good example where Internal`PartitionRagged (IPR) can be used very effectively. First, observe the following: list = {10, 20, 30, 40, 50, 60, 70}; (* 3 continuous elements in the middle *) Internal`PartitionRagged[list, {2, 3, 2}] (* {{10, 20}, {30, 40, 50}, {60, 70}} *) (* 3 continuous elements from the start *) Internal`PartitionRagged[list, ...


10

I believe I have a solution for you, assuming we've worked out all the discrepancies in the original question. You will need my dynamicPartition function or one of its "core function" equivalents. process[data_List] := Module[{f, s1, s2}, f[_, {1, ___, 1}] = 1; f[_, {___, 0}] = 0; f[x_, _] := x; s1 = Split @ Rest @ FoldList[f, 0, data]; ...


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}}} *)


9

Here is another way: Transpose[{list, sel}] /. {{x_, True} :> f[x], {x_, _} :> x} Although I think that MapAt with Position seems the cleanest way.


9

Transpose followed by mapping Mean over the array should do the job. data = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Mean /@ Transpose[data] (* ==> {4, 5, 6} *)


9

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 ...


9

If I understand the statement, you wish to do this: Table[If[time[[i]] == 0, x[[i]] y[[i]], z[[i]]], {i, 1, Length[time]}] but you wish to do it without Table or Do. One way to accomplish this is quite straightforward: (1 - time) x y + time z which gives the same result as above, though this assumes that the time variable is either zero (when you wish ...


9

MapThread[Thread[{##}] &, {lst2, lst1}] Map[Thread, {lst2, lst1}\[Transpose]] ## is used so Thread gets called like Thread[{1, {a, b, c}}] As MapThread gives two arguments in this case it is equivalent to Thread[{#1, #2}]& and Composition[Thread, List]


9

I would prefer to use the "GeoImage" styling, because you can use other projections when using it. Let's say you have data for the whole world in a matrix: data = Table[ Sin[x Degree] Sin[y Degree], {y, -90, 90}, {x, -180, 180}] Then you use ListDensityPlot: den1 = ListDensityPlot[data, AspectRatio -> 1/2, Frame -> None, PlotRangePadding ...


8

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}}]


8

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] = ...


8

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 ...


8

Another one, similar to ruebenko's but a bit faster on my machine: simon[] := Block[{g}, g[True, x_] := f[x]; g[False, x_] := x; SetAttributes[g, Listable]; g[sel, list]]



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