# Tag Info

37

Updated Both Hold and Inactive block evaluation; the key difference is that Inactive is meant to be wrapped around heads rather than a whole expression. Inactivate does this. Inactivate[1 + 2 + 3 * 4 ^ 5 ] // FullForm Inactive[Plus][1, 2, Inactive[Times][3, Inactive[Power][4, 5]]] It is of course possible to use Inactive directly, and it will behave like ...

29

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

25

I thought that for any function f we could use "f" interchangeably with "f[#]&". That is only true if the function has no special attributes. Function effectively removes those attributes, and all other special behaviour. For example, Hold[1 + 1] (* Hold[1 + 1] *) Hold[#] &[1 + 1] (* Hold[2] *) Function[x, Hold[x], HoldAll][1 + 1] (* Hold[1 + 1]...

23

Try this: Map[If[#==1,Unevaluated@Sequence[],#]&,{1,2,3}] Note the output. The 1 is gone. That's because Unevaluated@Sequence[] puts the empty sequence there, that is, "nothing". ##&[] is a shorthand that can be used in most places for same - ## is the sequence of arguments, & makes it a function to apply to something, [] is that something - ...

21

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

20

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 := SystemDumpAutoLoad[ Hold[Plot], Hold[syms], VisualizationProto ...

18

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

18

One difference is that NDSolve directly supports Inactive. It can be used to specify operators such as divergence ($\nabla\cdot$) without automatically evaluating them to components. This is described here.

18

The two are not equivalent. Even if f lacks special attributes. Assuming no special attributes are present, a function's arguments are always evaluated before the function is called. This event occurs twice with f[#] &[x], but only once with f[x]. Now, if everything has been fully evaluated, then you won't see a difference, but if something is marked as &...

18

The action of the symbol Unevaluated is due to a special rule within the evaluator that occurs at a very specific time during the evaluation process. Unevaluated only suppresses the evaluation of an expression part if it appears explicitly as the head of that part prior to the part's evaluation. Once the part has been evaluated, it is too late to suppress ...

16

A response to this comment: I understand that there are workarounds. I am wondering whether I am misinterpreting the manual. Yes. The use of the word "function" in the manual may be confusing here. It is symbols that have attributes, not "functions". Attributes have an effect only at certain specific steps of the evaluation sequence, and only when the ...

16

This is definitely a bug. Here's a slightly simpler workaround than those suggested in the comments: Uncompress[str, HoldComplete] // InputForm Generally speaking, InputForm and OutputForm are safer than StandardForm. This is for a couple of reasons. Much more of those textual formats happens inside the raw kernel, where evaluation leaks are easier to ...

16

You could define a function similar to Echo with the difference that it won't evaluate its argument before printing. SetAttributes[echo, HoldAll] echo[expr_] := (CellPrint@ ExpressionCell[Defer[expr], CellDingbat -> ">"]; expr) Then just put it in front of anything you want printed: ex[eqns_, unks_, tmax_] := echo@NDSolve[eqns, unks, {t, 0, ...

16

The head Unevaluated is a symbol with the HoldAllComplete attribute. The head Unevaluated[#]& is not a symbol and thus has no attributes to suppress Mathematica's evaluation of its tail.

14

Jacob, allow me to suggest a presentation that I think you will find relevant and informative: Working with Unevaluated Expressions - Robby Villegas Some excerpts: Unevaluated is a wrapper on arguments that is simply a signal to the evaluator to avoid evaluating the argument. ... It is transparent to the function receiving the argument. You can ...

14

Thank you for bringing this function to my attention! It lets us use ReplaceAll without disturbing or even traversing held expressions. This is huge! I have little time at the moment and I intend to flesh out this answer later but I want to get some ink on the page lest I forget. Many times Hold or HoldForm is used as a container to prevent automatic ...

13

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

13

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]; SetAttributes[...

13

I am betting that this is almost certainly an optimization short-cut. If Orderless had to try every ordering it would be extremely slow when there are a moderate number of arguments, but it is not. Consider for example: f @@@ Hold @@ {RandomSample[Range@12]} Hold @@ {f @@ Range@12 /. {7 -> _}} MatchQ[%%, %] Hold[f[2, 6, 11, 7, 12, 10, 4, 1, 3, 5, 8, 9]...

13

HoldPattern is a persistent head that prevents evaluation of its argument(s) yet is transparent to pattern matching. Unevaluated is a temporary wrapper that prevents the evaluation of a single argument* by a single head at one point in the evaluation, and then is stripped. Once it is stripped it cannot have any further effect on the evaluation. You can ...

13

Summary The confusion we observe here is largely due to ambiguous use of the symbol Association as an expression head. On the one hand, Association can be used as a constructor function to build an association object. On the other hand, it serves as the symbolic head of a constructed association object. The difference between these two uses is normally ...

11

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

11

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

11

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

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

11

Quick fix is to use e.g. Inactivate and friends: Manipulate[ Column[{ Activate @ #, Panel @ ExpressionCell[ # /. Inactive -> Defer, "Notebook", "Input" ] }] & @ Inactivate @ Plot[Sin[x], {x, -3, 3}, AxesLabel -> {xlabel, ylabel}, Ticks -> {tics, tics}] , Control[{{tics, Automatic, ""}, {Automatic -&...

11

This issue is known and it is a defect of TreeForm like Leonid already said. Let me give an illustrative example that shows the same behavior. Let us assume you want a function that just returns its input unevaluated. Without thinking we put down the function f1[arg_] := HoldForm[arg] Now, what seems like a clever idea has one flaw: when you evaluate f1[1+1]...

11

You need to wrap the expression Unevaluated before passing it to Compress, which means defining your own function instead of just assigning Compress to com: ClearAll[com]; SetAttributes[com, HoldAllComplete]; com[expr_] := Compress[Unevaluated[expr]]; string = com[Range[10]] (* "1:eJxTTMoPSmNkYGAoZgUSQYl56amZXEAWADzBBIQ=" *) And then make sure to wrap ...

11

The problem is not related to Set in general but to how Set works with Part on a left hand side. Following Set::setps message documentation This message is generated when a part assignment is used for the value of an expression other than a symbol. Part assignments are implemented only for parts of the value of a symbol. Shortly Part[(*something*), (*spec*)...

11

may be Sum[With[{i=i},HoldForm[i^2]],{i,1,5}] But this uses a wrapper. I do not think there is a way without using Hold and friends.

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