Tag Info

Hot answers tagged

10

Interesting question. Here is my proposal: fn[f_, expr_] := Module[{h}, h[x_List, _] := x; h[o_[x__h], i_] := h[o @@ {x}[[All, 1]], i]; MapIndexed[h, expr, {1, -1}] /. h -> f ] Test: fn[h, {a, q Sqrt[r], {{{e + f g h}, c^d}, b}}] {h[a, {1}], h[q Sqrt[r], {2}], {{{h[e + f g h, {3, 1, 1, 1}]}, h[c^d, {3, 1, 2}]}, h[b, {3, 2}]}} I ...


9

May be this is not the smarter way to do it but here is what I got: l = {a, {b, {c^d, {e + f g}}}}; pos = ReplaceAll[(Position[l, List]), 0 -> 1]; h[l[[Sequence @@ #]], #] & /@ pos (*{h[a, {1}], h[b, {2, 1}], h[c^d, {2, 2, 1}], h[e + f g, {2, 2, 2, 1}]}*) If you want to keep the levels as they are then: rule = Rule[#, h[l[[Sequence @@ #]], #]] ...


9

What you have is a so called linked list, and such lists are usually traversed through recursion like this: applyFunc[f_, {el_, rest_}, level_: {1}] := {f[el, level], applyFunc[f, rest, Prepend[level, 2]]} applyFunc[f_, {el_}, level_] := {f[el, level]} applyFunc[h, expr] {h[a, {1}], {h[b, {2, 1}], {h[c^d, {2, 2, 1}], {h[e + f g, {2, 2, 2,1}]}}}} But ...


5

Another method with a single plot: ClearAll[myFunc2]; yvalues = {0.1`, 0.5`, 1.`, 2.`, 3.`}; myFunc2[x_?NumericQ, y_?NumericQ] := ConditionalExpression[Sin[x y], IntervalMemberQ[Interval[{0, Cos[y]}], x]]; {min, max} = Through[{Min, Max}[Cos[yvalues]]]; Plot[myFunc2[x, #] & /@ yvalues, {x, min, max}, Evaluated -> True, BaseStyle -> ...


5

A method using a single Plot expression: myFunc[x_, y_] := Sin[x y] yvalues = {0.1, 0.5, 1.0, 2.0, 3.0}; Block[{x}, Plot[#, {x, -5, 5}, PlotRange -> All, PlotLegends -> Automatic] &[ If[Cos[#] <= x <= 0 || 0 <= x <= Cos[#], myFunc[x, #]] & /@ yvalues ] ] Notes: I use Block to keep x localized. I did not programmatically ...


5

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


5

ReplacePart If one is willing to bend on the requirement to use MapIndexed, ReplacePart can generate the desired output directly: $list = {a, {b, {c^d, {e + f g}}}}; ReplacePart[$list, {i:PatternSequence[2..., 1]} :> h[$list[[i]], {i}]] (* {h[a, {1}], {h[b, {2, 1}], {h[c^d, {2, 2, 1}], {h[e + f g, {2, 2, 2, 1}]}}}} *) MapIndexed Alternatively, we ...


4

I don't think there's any built-in function to do this, but here is an alternative: myFunc[func_, data_] := Transpose[Through[func[#]] & /@ data] Then myFunc[{f, g, h}, {a, b, c}] {{f[a], f[b], f[c]}, {g[a], g[b], g[c]}, {h[a], h[b], h[c]}}


3

If eldo's plot is your desired outcome you could also do: Show[Plot[Sin[x #], {x, 0, Cos@#}, PlotStyle -> Hue[RandomReal[]]] & /@ yvalues, PlotRange-> All] Noting Cos2 and Cos[3] are negative. If you want legends: With[{col = Hue /@ RandomReal[{0, 1}, 5]}, Legended[ Show[MapThread[ Plot[Sin[x #1], {x, 0, Cos@#1}, PlotStyle -> ...


3

Is something like this your intention? myFunc[x_, y_] := Sin[x y] yvalues = {0.1, 0.5, 1.0, 2.0, 3.0}; fun = Map[myFunc[x, #] &, yvalues]; lim = Table[Cos[yvalues[[i]]], {i, 1, 5}]; plot = Table[Plot[fun[[j]], {x, 0, lim[[j]]}, PlotStyle -> {Red, Green, Blue, Orange, Brown}[[j]]], {j, 1, 5}] Show[plot, PlotRange -> All]


3

You are completely right: using Map is much nicer than an iterative approach. Here's one way you might do it: ReplacePart[M, # -> s]& /@ L Here /@ is a short-hand input form of Map, and the #& combination is what's known as a pure function. Welcome to Mathematica :).



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