Tag Info

Hot answers tagged

48

Generally, you want the Trott-Strzebonski in-place evaluation technique: In[47]:= f[x_Real]:=x^2; Hold[{Hold[2.],Hold[3.]}]/.n_Real:>With[{eval = f[n]},eval/;True] Out[48]= Hold[{Hold[4.],Hold[9.]}] It will inject the evaluated r.h.s. into an arbitrarily deep location in the held expression, where the expression was found that matched the rule ...


40

RuleCondition provides an undocumented, but very convenient, way to make replacements in held expressions. For example, if we want to square the odd integers in a held list: In[3]:= Hold[{1, 2, 3, 4, 5}] /. n_Integer :> RuleCondition[n^2, OddQ[n]] Out[3]= Hold[{1, 2, 9, 4, 25}] RuleCondition differs Condition in that the replacement expression is ...


32

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


22

Here are a couple of alternatives to Trott-Strzebonski in @R.M's answer: Hold[{3,4,5|6}] /. Verbatim[Alternatives][x__] :> RuleCondition@RandomChoice@List@x Hold[{3, 4, 5}] Hold[{3,4,5|6}] /. Verbatim[Alternatives][x__] :> Block[{}, RandomChoice@List@x /; True] Hold[{3, 4, 6}] They operate on the same principle as Trott-Strzebonski ...


21

This is a case where the Trott-Strzebonski in-place evaluation trick is useful. You use With to inject inside your held expression as: (Hold[{3, 4, 5 | 6}] /. (Verbatim@Alternatives)[x__] :> With[{eval = RandomChoice@List@x}, eval /; True]) Out[1]= Hold[{3, 4, 5}] You should definitely read this post by Leonid, that gives you a good insight into ...


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


18

The way Mathematica works is that when it encounters a function with arguments it will try to evaluate the arguments first before proceeding to evaluate the function. This behavior can be modified by specifying the various HoldAll, HoldFirst, HoldRest, etc. attributes for a given function. So in your example f[x+1] will be immediately replaced by f[6] ...


16

Recall that the rendering of Graphics has nothing to do with evaluation. It is done entirely in typesetting. And therefore, a robust solution will treat this as a problem of typesetting, and not as a problem of evaluation. Once you frame the problem properly, the solution is fairly straightforward. What you want to do is to change the typesetting of Hold ...


15

It is because, in version 9, the implementation of Plot is loaded from a dump file on its first usage, rather than loading when the kernel starts. One can see this by clearing the ReadProtected attribute: ClearAttributes[Plot, ReadProtected] Information[Plot] (* -> Plot := System`Dump`AutoLoad[ Hold[Plot], Hold[syms], Visualization`Proto` ...


15

How about this: list = {1, 2, 3}; ToExpression["list", InputForm, Hold] /. Hold[v_] :> AppendTo[v, 3] {1, 2, 3, 3} list {1, 2, 3, 3}


15

Although less magical, it can be done by ReplacePart expr = Hold[{2, 3, 4, 5}] pos = Position[expr, _Integer] newparts = Extract[expr, pos] /. n_Integer :> n^2 ReplacePart[expr, Thread[pos -> newparts]]


15

I think the problem might be related to a bug in FullForm when applied to a ByteArray object: ByteArray["aV+jpGtfd3BHhoSvOthJpQ=="] // FullForm (* List[105,95,163,164,107,95,119,112,71,134,132,175,58,216,73,165] *) The full form has lost information regarding the structure of the ByteArray. The box-form of the button is using this list form but the ...


14

Not to detract from the existing answers (particularly @WReach's suggestion, which was the same solution that came to my mind as I read your question, and which I will use here), but you may find it easier to define your own references rather than using strings. (In fact, I wouldn't necessarily recommend an approach based on building Mathematica expressions ...


13

#1 Trott-Strzebonski in-place evaluation: hf = HoldForm[1 - 1^2/2 + 1^3/3 - 1^4/4 + 1^5/5 - 1^6/6] hf /. x_Times :> With[{eval = x}, eval /; True] 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 Replace[hf, x_ :> With[{eval = x}, eval /; True], {2}] 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 One may simplify this method using the undocumented function ...


13

The Hold functions enable Mathematica's version of what some other languages call "macros." You can use them for a lot of things, but the essential point is that they preserve the structure of the input. The built-in functions are full of examples: x = 7; Plot[x^2, {x, -2, 2}] Type this in and you'll see that Plot draws the parabola even though "x" was ...


13

You are missing Unevaluated: SetAttributes[f, HoldFirst] f[x_] := {SymbolName[Unevaluated@x], x} because SymbolName does not hold its arguments, so you have to prevent evaluation also there. Generally, if you are passing some argument via a chain of function calls, and want to keep it unevaluated, you have to prevent it's evaluation at each stage ...


12

While I can't follow your code, I guess your problem is caused by the fact that you get evaluation in between individual replacements, and the fact that Listable functions of several arguments (which includes operators like + and *) have quite peculiar behaviour. The fact that you get a matrix instead of a vector, as well as the fact that you can avoid it ...


11

HoldForm[#1 + #2]&[RandomInteger[100], RandomInteger[100]] (* 77 + 84 *)


11

I propose: HoldForm[+##] & @@ RandomInteger[100, 2]


11

From prior comments I know that you are interested in forms such as: a - b - c - d a / b / c / d There is no simple short form for these as there is for Plus. To understand this you must understand how Mathematica parses and displays these expressions. Let's look at the first one: Subtract HoldForm[a - b - c - d] a - b - c - d No surprises. But ...


10

This is possible in the interactive session with $PreRead. I will adopt my solution to the same problem posted in this Mathgroup thread. To quote my explanation from there, the essence of the present solution is to delay the parsing of the code (body) that must be executed inside a given context until run-time, that is, replace code ...


10

I think that in general, for tasks like this one, tricks like Trott-Strzebonski technique are not the best way, and one really needs expression parsers, which are may be not shorter, but more readable and more extensible. Here is a possible one for your problem: ClearAll[convert]; SetAttributes[convert, {HoldAll}]; convert[x_List] := Map[convert, ...


10

One way to achieve this is to use a "vanishing" wrapper. The idea is to temporarily wrap the substituted expression with a holding symbolic head, and then remove that head in a second replacement: Module[{h} , SetAttributes[h, HoldAll] ; y /. bar[j_] :> RuleCondition[Extract[x, {j}, h]] /. h[x_] :> x ] (* Hold[foo[2+2]] *) Module is used to ensure ...


10

General I think that one can achieve the goal much easier if we reformulate the request. A variable is IMO not a proper object to store an iterator in the form of expression. What you really need is an environment, which would use certain iterator in code. Simple lexical / dynamic environment Here is how it may look: ClearAll[withIterator]; ...


9

I think your original method is fine, but perhaps this will be more to your liking: Table[With[{n = ni}, Plot[ρ[n, x], {x, 0, L}, PlotLabel -> Defer@ρ[n, x]] ], {ni, 1, 4}] Another, perhaps less fundamental way is Table[ Plot[ρ[n, x], {x, 0, L}, PlotLegends -> Placed["Expressions", Top]] ,{n, 1, 4}]


9

John Fultz alluded to using the Villegas-Gayley pattern. Since I believe that is the correct approach to this problem here is an implementation. mk : MakeBoxes[(Hold | HoldForm | HoldComplete | HoldPattern)[__], _] := Block[{$hldGfx = True, Graphics, Graphics3D}, mk] /; ! TrueQ[$hldGfx] I included HoldPattern to complete the Hold functions. This now ...


8

I think there is a case to be made for not using List at all. It seems to me that it is a needless complication. Why not instead use Hold in place of List? a = Hold[2 + 2]; b = Hold[4 + 4]; c = Join[a, b] Append[c, Unevaluated[6 + 6]] Hold[2 + 2, 4 + 4] Hold[2 + 2, 4 + 4, 6 + 6] Also: x = Hold @@ {a, b} Length[x] Dimensions[x] Hold[Hold[2 + ...


8

Not sure if this fits your needs: z2 := Sequence[{a, {1, 2, 3}}, {b, Complement[{1, 2, 3}, {a}]}, {c, Complement[{1, 2, 3}, {a, b}]}] Unevaluated @ Table[{a, b}, z2] /. OwnValues @ z2 {{{{1, 2}}, {{1, 3}}}, {{{2, 1}}, {{2, 3}}}, {{{3, 1}}, {{3, 2}}}} You can use z2 with Set here too: z2 = Unevaluated @ Sequence[{a, {1, 2, 3}}, {b, ...


7

You might want HoldForm[Plus[##]] & @@ (List @@ taylor /. x -> 1)


7

Injector pattern: list = {1, 2, 3}; MakeExpression["list"] /. _[sym_] :> AppendTo[sym, 4] Function (here using the Null syntax trick): Function[, AppendTo[#, 4], HoldAll] @@ MakeExpression["list"]



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