Tag Info

Hot answers tagged

37

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


30

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


22

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


19

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


18

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


18

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


16

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


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}


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


12

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


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


12

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


11

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


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


9

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


9

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


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

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


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

Looks like StringForm can achieve this: Cp = 1.5; deltastar = 0.123; Then: StringForm["The value for `1` is `2` and the value for `3` is `4`.", HoldForm @ Subscript[C, p], Cp, HoldForm @ Superscript[\[Delta], "*"], deltastar]


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


7

You could do something like InputField[Dynamic[d, (d = HoldForm @@ #) &], Hold[Expression]] This will wrap the expression typed into the input field in HoldForm.


7

Here is yet another possibility (which, in a way, combines some of the suggestions given already): define a new scoping construct, most similar to Function, to perform the task you need: ClearAll[strFunction]; SetAttributes[strFunction, HoldAll]; SyntaxInformation[strFunction] = {"ArgumentsPattern" -> {_, _}, "LocalVariables" -> {"Solve", {1, ...


7

Note that the following works, but generates warning messages. f[y_, a_] := NIntegrate[y^2 + x, {x, 0, a}]; Plot[Table[f[y, a], {a, 1, 3}], {y, 0, 3}, Evaluated -> True] The issue is that Plot (which usually holds it's first argument until specific values of $x$ are plugged in) tries to evaluate the first argument symbolically (due to the Evaluated ...


7

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


7

It sounds like you're merely looking for Row: Cp = 1.5; deltastar = 0.123; Row[{ "The value for ", HoldForm[Subscript[C, p]], " is ", Cp, " and the value for ", HoldForm[Superscript[\[Delta], "*"]], " is ", deltastar, "." }] If this does not work for you please clearly state how it fails so that those issues can be directly addressed.


6

One easy way, which does not work for lists of different length, is a = Hold[{2 + 2}]; b = Hold[{4 + 4}]; Thread[{a, b} /. Hold[{a___}] :> Hold[a], Hold] What happens here is the following: first, you can use {a, b} /. Hold[{a___}] :> Hold[a] to get rid of the inner list braces without evaluating your expressions. Since we use {a,b} we will ...



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