# Tag Info

## Hot answers tagged map

31

I did a bit of debugging to find the cause of the problem. After I found it, the problem no longer seems as outrageous as it looked at first sight. The root cause of the problem is the following property of Plus: Plus[x] (* x *) This means that unlike List, Plus cannot be used as a container that can be split into smaller parts and then put together ...

25

Here a bit of inspiration for you. The following code should be approximately as fast as your Python code. Preparation: nrealis = 100; ntemp = 2; nn = ntemp nrealis; tem = DeveloperToPackedArray[{0.05, 0.06}]; eps = 0.1; candmoves = DeveloperToPackedArray[{-eps, 0., eps}]; xInit = ConstantArray[-1./Sqrt[2.], {nn}]; tmp = (-1.)/DeveloperToPackedArray[Table[...

22

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

20

To my knowledge, there aren't built-in versions for comparison operators that would be automatically threaded over lists. One reason for that is that Mathematica is a symbolic system, and every auto-simplification has a cost, because there may be cases when this isn't desirable. It is relatively easy however to construct the behavior you want: ClearAll[l]; ...

17

The BoolEval package does exactly this. For example: BoolEval[{0.6, 1.2} > 1] (* Out: {0, 1} *) and BoolEval[{{0.6, 1.2}, {5, 0.1}} > 1] (* Out: {{0, 1}, {1, 0}} *) In order to return True and False instead of 0 and 1, you can append /. {0 -> False, 1 -> True}.

16

MapThread[#1^2 + #2^2 &, {x, y}]

16

A little "secret" of level specifications is that they can be negative. -1 refers to the atomic leaves, -2 refers to all Depth 2 subexpressions, generally -k refers to all depth k subexpressions. Thus the behaviour of negative levels is somewhat different from that of positive ones. You can read more here: Levels: how do they work? http://reference....

16

Edit Ok, so an incorrect answer producing arrays like: {{"0.", RGBColor["0.00000000000", "0.00000000000", "0.00000000000"]}} received +12 and an accept. Let's correct it though: Activate @ ReadList[ file , {Number, RGBColor[Inactive[ToExpression][Word], Number, Number]} , WordSeparators -> {"\t", " ", ","} ] {{0., RGBColor[0., 0., 0.]}, ...} ...

16

If the domain $\varOmega$ of the county is simply connect, one might use the Riemannian mapping theorem. For $z_0 \in \varOmega^\circ$, we make the ansatz for the holomorphic map $f \colon \varOmega \to D^2$ $$f(z) = (z-z_0) \, \operatorname{e}^{u(z) + \operatorname{i}\!v(z)}.$$ Then $|f(z)| = |z-z_0| \, \operatorname{e}^{u(z)}$ has to equal $1$ for all \$...

16

☺lookMaNoLetters☺ = 1 ## & @@@ # & /@ # &; ☺lookMaNoLetters☺ @ mylist {{y1 y2 y3, y3 y4 y5}, {w1 w2 w3, w4 w5 w6}} Further variations: ☺lookMaNoLettersOrNumbers☺ = # ##2 & @@@ # & /@ # &; ☺ApplyTimesAtLevel2☺ = # ##2 & @@ ## &[#, {2}] &; ☺InCaseYouLikeInfix☺ = # ~ (# ##2 & @@ ## &) ~ {2} &; ☺...

16

Many index-specific operations can be implemented via MapIndexed with a level specificaton. Your Power example can be written as: MapIndexed[#1^(#2[]*#2[]) &, test2D, {2}] If you want better readability of indices you can define an auxiliary function: myPower[x_, {n1_, n2_}] := x^(n1 n2); MapIndexed[myPower, test2D, {2}] Some index-specific ...

14

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

14

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

14

list = {5040, 1460, 280, 76, 16, 2, 1}; Ratios: 1 / Ratios @ list {252/73, 73/14, 70/19, 19/4, 8, 2} Reverse @ Ratios @ Reverse @ list {252/73, 73/14, 70/19, 19/4, 8, 2} Divide: Divide[Most @ #, Rest @ #]& @ list {252/73, 73/14, 70/19, 19/4, 8, 2} MovingMap: MovingMap[Divide @@ # &, list, 1] {252/73, 73/14, 70/19, 19/4, 8, 2} ...

13

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

13

There are many closely related topics but I've failed to find a duplicate. MapThread[Thread @* f, {First @ list1, list2}] MapThread[f, {list2, list3}] {{f[a, 1], f[a, 2]}, {f[b, 3], f[b, 4]}} {f[{1, 2}, {x, y}], f[{3, 4}, {z, w}]}

12

I like the following very much {x, y} = {{1, 2, 4}, {3, 4, 6}}; (#1^2 + #2^2) & @@@ Transpose[{x,y}] Another thing which is highly unused is to attach Attributes to pure Functions Function[{a, b}, a^2 + b^2, {Listable}][x, y] You can stick with the Slot notation too, but you have to tell then that the variables in Function is a Null list which might ...

12

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:

12

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; AbsoluteTiming[a = Map[# RandomChoice[{p, 1 - p} -> {1, 0}] &...

12

As noted in comments, the standard ways to evaluate such result is to use Composition or pure function. Using Composition: Map[Minus@*f, Range] {-f, -f, -f, -f, -f, -f, -f, -f, -f, -f} Using pure function: Map[(-f[#1])&, Range] {-f, -f, -f, -f, -f, -f, -f, -f, -f, -f}

11

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

11

This is an interesting puzzle. I think, though, you should restrict yourself to only words of length 4, to save processing time, as follows: words = DictionaryLookup[Repeated[CharacterRange["a", "z"], {4}]]; where I used CharacterRange to eliminate proper names and contractions. This has the side effect that all accent marks, umlauts, etc. are also removed....

11

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]

11

I feel this answer is as sensible as the question. First, binarise the image: m = Import["maz1.jpg"] bin = Opening[ColorNegate@Binarize[m], 6] Find the biggest white space, and thin it into a skeleton thin = Thinning[SelectComponents[bin, "Count", -1], Method -> "MedialAxis"] I used "MedialAxis" here to make it squarer, at the cost of extra bits ...

11

The difference between the parallelized and the serial version is not due to auto-compilation. Auto-compilation will be used on the subkernels too. You can easily check that turning it off there will slow the parallel evaluation down considerably. Not every program parallelizes well in Mathematica, and very often finding out where the bottleneck is is ...

10

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

10

molekyla777's answer can be very helpful but it is not technically correct. The question specifies "every element of a list" but using a levelspec of {-1} will apply the function to every atomic element regardless of its head: Map[f, 1 + 5 x + 10 x^2 + 10 x^3, {-1}] f + f f[x] + f f[x]^f + f f[x]^f Of course this can be very useful ...

10

Update Well I guess I should retire for the evening to a less brain-intensive activity as apparently I can't think clearly. One could of course use Outer: Outer[Compose, {f, g}, {a, b, c}] {{f[a], f[b], f[c]}, {g[a], g[b], g[c]}} However I recommend that you do not do this as you will not gain the auto-compilation of Map, meaning this method will often ...

10

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

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

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