# Tag Info

14

Roman's solution gets very slow as the value b or c increases. I use Memoization below to prevent recomputing values. We should also ensure b and c are positive integers to avoid infinite recursion. My improved version that does all that is here. Clear[f]; f[a_, b_] := a^b; f[a_, b_, 0] := a^b; f[a_, 1, c_Integer?Positive] := f[a,1,c] = f[a,a,c-1] f[a_, ...

13

You can write $(a,b(c))$ as f[a,b,c]: f[a_, b_] = a^b; f[a_, b_, 0] = f[a, b]; f[a_, 1, c_] := f[a, a, c - 1] f[a_, b_, c_] := f[a, b - 1, c]*f[a + 1, b, c - 1] examples: f[1, 2, 3] (* 15552 *) f[3, 2, 1] (* 432 *) f[4, 4, 4] (* ...

7

What you want is called "currying". Not involving new operators you may try: Clear[f, g] f[x_, y_] := x^2 + y^3 g[x_] = Evaluate[f[x, #]] &; f[3, 2] == 17 == g[3][2] ?? g However, MMA has a special operator CurryApplied for this: Clear[f, g] f[x_, y_] := x^2 + y^3 g = CurryApplied[f, 2]; f[3, 2] == 17 == g[3][2] ?? g You may want to also ...

6

You can do it even without slots: Map[Map[Flatten @* List] @* FoldList[List]] @ test {{{4}, {4, 2}, {4, 2, 2}}, {{9}, {9, 1}, {9, 1, 5}}, {{5}, {5, 2}, {5, 2, 9}, {5, 2, 9, 3}}, {{5}, {5, 2}, {5, 2, 7}, {5, 2, 7, 1}, {5, 2, 7, 1, 1}}} Also Map[Extract[#, Map[List] @ Range @ Range @ Length @ #] &] @ test {{{4}, {4, 2}, {4, 2, 2}}, {{...

6

To expand upon my comment, in order to pass by reference you can use Hold. So for instance: In[1]:= foo[Hold[x_], val_] := AppendTo[x, val]; In[5]:= x = {}; In[6]:= foo[Hold@x, 2] Out[6]= {2} In[7]:= x Out[7]= {2} If you pass a held variable, this has the effect of passing by reference as opposed to by value, so we have access to the variable x rather ...

5

5

According to AppendTo documentation: AppendTo[x,elem] is equivalent to x=Append[x,elem]. When you use AppendTo[{}, 1] Mathematica can't evaluate {}=Append[{},1]. The solution is to give the variable's name to AppendTo and it'll figure it out. That's why Mathematica has something like Hold_sth. The code below will raise error because when you write f[l1], ...

3

This is what immediate assignments are for: f[x_] = D[x^100*E^(2*x^5)*Cos[x^2], {x, 137}] (* long output (can be suppressed with semicolon) *) f[3.] (* -8.90666869434476*10^664 *) See here for a tutorial on the distinction between = and :=.

3

There is no direct notation for anonymous slot references to outer pure function arguments (named arguments to Function having been ruled out). But we can use the higher-order function OperatorApplied as a way to avoid explicit argument naming: OperatorApplied[Take, 2][#] /@ Range@Length[#] & /@ test (* {{{4}, {4,2}, {4,2,2}}, {{9}, {9,1}, {9,1,5}},...

3

Example 1 already addressed in the comments as position can already handle this. Example 2 is addressed below: With[{list = {3, 5, 1, 7, 2, 8, 9, 7, 5, 6}}, Select[14 == Plus @@ # &][Thread[{list, Range@Length@list}]] ][[All, 1]] (* returns : {8, 5} *) Another way using ResourceFunction["PositionCases"]: PositionCases = ResourceFunction[&...

3

Usually I try to avoid global variables that don't hold long living data. Especially in this function, which should in this implementation begin with Block[{list1={},list2={}},etcetc] instead of just [etcetc]... scoping. Your Do[i=1,n,1,stuff] should be more like Do[stuff,{i,1,n,1}] or just Do[stuff,{i,n}]. There's a million ways to do this in ...

3

First, Animate takes 2 arguments, a list of graphics and a variable specification. Therefore, you must wrap your graphics command into a list: {...}. Then, to get a general coordinate system, you must wrap all the graphics into one single Graphics. Otherwise, every item has its own coordinate system. Then you must adept the time range, otherwise your machine ...

2

Get the palette in RGB: RGBpalette = (List @@ ColorConvert[Hue[##], "RGB"]) & @@@ palette Get the convex hull in RGB, and pick only those colors on the boundary—we'll use these for interpolation: region = ConvexHullRegion[RGBpalette] generators = Select[(List @@@ RGBpalette), RegionMember[RegionBoundary@ConvexHullRegion[List @@@ ...

2

Clear["Global`*"] f1[x_] = D[x^100*E^(2*x^5)*Cos[x^2], {x, 137}]; f2[x_] = D[x^100*E^(2*x^5)*Cos[x^2], {x, 137}] // Simplify; While defining f2 takes much longer, LeafCount /@ {f1[x], f2[x]} (* {4274535, 6386} *) AbsoluteTiming[#[3.]] & /@ {f1, f2} (* {{1.07832, -8.90666869434476*10^664}, {0.002447, -8.906668694344757*10^664}} *)

2

tup4a = Join @@@ Tuples[{tup1, tup3}]; tup4a // Short {{0,-1,-1,0,-1,-1,d,0,0},{0,-1,-1,0,-1,-1,d,0,1},<<2912>>, {1,1,1,1,1,1,d,-1,1},{1,1,1,1,1,1,d,-1,-1}} tup4b = Distribute[{tup1, tup3}, List, List, List, Join]; tup4a == tup4b True "to get that directly from tup1 and tup2": tup4c = Join @@@ Tuples[{tup1, {{d}}, tup2}]; tup4a ...

2

You could use LinearModelFit (or NonlinearModelFit according to your needs): Clear[fitn] fitn[dataset_, i_] := LinearModelFit[dataset[[i]], {1, x}, x] fitn[data, 2] (* Out: FittedModel[0. + 1. x] *) When you want a specific calculated value, you can use: fitn[data, 1][2.5] (* Out: 5. *)

2

The problem you encountered is due to the evaluation algorithm used by Mathematica. In the documentation of SetDelayed is this statement SetDelayed has attribute HoldAll, rather than HoldFirst. What this means is that you do want the RHS to be evaluated in your case. As usual, you can force this by using f[x_]:=Evaluate[function] or else by using the ...

2

From the Functions Wolfram site we get EllipticTheta[2, z, q] == (q^(1/4) EllipticTheta[3, z - (Pi/2) τ, q])/ E^(I z) /; q == E^(I Pi τ) So define a new theta Th2 and test the identity with the code Th2[z_, tau_] := With[{q=E^(I Pi tau)}, E^(I Pi tau/4) EllipticTheta[3, z - (Pi/2) tau, q]/ E^(I z)]; test1[tau_] := With[{q=E^(I Pi tau)}, {Th2[0, 2tau]^...

2

σ = 9; L[s_, d_] = (1/(σ*Sqrt[2*π]))* E^(-(1/2)*((s - (-50 - 11*Log[d]))/σ)^2); The probability distribution is dist[s_] = ProbabilityDistribution[L[s, d], {d, 0, Infinity}, Method -> "Normalize"]; r[s, d] is the PDF of the distribution r[s_, d_] = PDF[dist[s], d] Verifying the normalization, Integrate[r[s, d], {d, 0, Infinity}] (* ...

1

If the $d$ takes on values from 0 to $\infty$, then the normalizing constant is given by Integrate[L[s, d], {d, 0, ∞}] (* 1/11 E^(-(1019/242) - s/11) *) So you could define the following function which is non-negative and integrates to 1 for a pdf associated with $d$: r[s_, d_] := L[s, d]/(1/11 E^(-(1019/242) - s/11)) You should avoid using capital letters ...

1

ClearAll[f,x] function = x^2; f[x_] = function; DownValues[f] (* {HoldPattern[f[x_]] :> x^2} *) ClearAll[f] f[x_] := x^2; DownValues[f] (* {HoldPattern[f[x_]] :> x^2} *)

1

Background: Level[{a, b}, {1}, f] is f[a, b], f[#] &[a, b] is f[a], and f[##] &[a, b] is f[a,b]. So in your case, you can use Level[{a, b, c}, {1}, {##}^2 &] or Level[{a, b, c}, {0}, #^2 &].

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