Tag Info

New answers tagged


In Mathematica version 10, you can also use Inactive to allow the Symbol to be created before doing the assignment. Here is an example: Clear["x"]; Activate[Inactive[Set][Symbol["x"], 3]] (* ==> 3 *) x (* ==> 3 *)


I'd use fs = Reap[Do[With[{n = n}, Sow[#^n &]], {n, 0, 10}]][[2, 1]] {#1^0 &, #1^1 &, #1^2 &, #1^3 &, #1^4 &, #1^5 &, #1^6 &, #1^7 &, #1^8 &, #1^9 &, #1^10 &} Your methods 2 & 3 don't work because Function has the attribute HoldAll and therefore m = 2; #^m & #1^m & With can be used "to ...


tree = Function[x, Defer @ FullForm @ x, HoldAll]; Now: 2 + 2 // tree Plus[2, 2] I used Defer to allow the output to be evaluated. If you do not prefer this replace it with HoldForm. For some explanation of the mechanics of this code see: Why doesn't "Defer" work with "TableForm"? See also my standard methods for ...


Another alternative: Clear[f, V] V = (a[1] + a[2]) b[1]; f[x_, y_, z_] := V /. Thread[Variables[V] :> {x, y, z}]; f[1, 2, 3] (* 9 *)


ClearAll[f,g]; f[a_[1], a_[2], b_[1]] := (a[1] + a[2]) b[1] f[a[1], a[2], b[1]] (* (a[1] + a[2]) b[1] *) f[z[1], z[2], w[1]] (* w[1] (z[1] + z[2]) *) f[z[1], z[2], w[2]] (* f[z[1], z[2], w[2]] --- f undefined for this input pattern *) Or, more generally, g[a_[x___], a_[y___], b_[z___]] := (a[x] + a[y]) b[z] g[a[1], a[3], b[5]] (* (a[1] + a[3]) b[5] *) ...


One way to do this, most likely not the most elegant, is to rename variables of the form a[n] temporarily. Suppose v = (a[1] + a[2]) b[1]. Then define f[a1_, a2_, b1_] := Evaluate[v /. {a_[n_] :> ToExpression[ToString[a] <> ToString[n]]}] With this definition you get the desired result if you evaluate f[a[1],a[2],b[1]].


Are you trying to nest functions? i.e. are 'a' and 'b' two functions that you apply the parameter values of '1' or '2' to them and then apply the results to f? Or in the case that a[1] is simply a name for a variable (something like 'x') Then maybe you're simply looking for f[x_,y_,z_]:=(x+y)z


If, in an input cell, you type the expression, x1 ~ f ~ x2 ~ f ~ x3 it will evaluate to f[f[x1,x2],x3] UNLESS the symbol f has the attribute Flat in which case it will evaluate to f[x1,x2,x3]


Between Versions 7 and 8 Hash now gives the hash of a raw sequence of characters when applied to Strings. In past versions the string characters (quotation marks) were included in the calculation of the hash. (Reference) Use "\"" <> string <> "\"" before hashing if you want output to match older versions. \[Dash], \[LongDash] and ...


Very nice answers. I wanted to add something else. One typical "Mathematica way" of coding involves overloading a function with several definitions, that do different things according to what arguments are passed (I actually abuse this). You can pattern match by head with things like f[x_Integer]:=... and f[x_Real]:=.... I see the Dataset/Query ...


For me the operator forms of Map and Apply will probably provide the most important benefits in terms of code readability. Often I need to apply a sequence of transformations to some data, and I am fond of infix notation for this purpose. For example I find a ~Position~ 0 ~SortBy~ Last more readable than the "conventional" SortBy[Position[a, 0], Last] ...


I would have liked to have more experience with the operator forms before this question was asked as I am short on examples, and I'm sure my opinion will evolve over time. Nevertheless I think I have enough familiarity with similar syntax to provide some useful comments. Taliesin Beynon provided some background for this functionality in Chat: Operator ...


I find the value of the new operator forms becomes critical when working with datasets. Consider titanic = ExampleData[{"Dataset", "Titanic"}]; titanic[Count[#], "survived"] & /@ {True, False, _Missing} {500, 809, 0} Derive a data set for analyzing the survival of very young passengers. cutoff = 8; youngest = titanic[All, {"age", ...


Here's a stab at a second pass: Syntactic sugar shouldn't be underestimated given its cumulative effects (also only a limited number of functions can have shortforms and sometimes for precedence reasons four symbols are needed in the pure form - (#)&) An example: Suppose it is desired to take keys/values "f" through to "h" and "p" through to "r" in ...

Top 50 recent answers are included