# Tag Info

12

Here I offer the safe version of Get that can be used successively to collect all the source files and contexts of packages without polluting the memory (too much). What it does I have practically reverse-engineered all the necessary functions (Get, Needs, BeginPackage, Begin, EndPackage and End) so that I could inject the monitoring code for ...

11

There are some "special variables" in Mathematica that cannot be cleared. These are fundamentally necessary for its operation and while it will let you revert to a default value inside a Block, it will not allow you to clear it. You can verify this directly, without Block: Clear@$ContextPath Clear::spsym: Special symbol$ContextPath cannot be ...

9

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

Basically, you have at least two options. One is to use Trace, and there have been many discussions here on SE about how to make the representation of the output of Trace convenient. A complementary approach is to set up some code which would spy on what is being passed to a particular function. It may have an advantage over Trace in that it will only ...

8

If I try this, everything gets removed except for {System,Global}: In[1]:= $ContextPath Out[1]= {"PacletManager", "QuantityUnits", "WebServices", "System", "Global"} In[2]:= Block[{$ContextPath}, Print[$ContextPath]] During evaluation of In[2]:= {System,Global`} My guess is that this is a special exception which is implemented to make ... 8 The general way to construct functions with partially evaluated pieces is to use With, which is a general device for injecting evaluated pieces in otherwise unevaluated or held expressions. In your case, it would look like f[a_] := With[{sqa = a^2}, sqa * # &] The method based on Evaluate is generally less powerful, since evaluation of the entire body ... 7 You can watch the evaluations of a function using On: SetAttributes[zot, HoldFirst] zot[x_, y_] := {x, y} On[zot] In[11]:= zot[1+1, 2+2] During evaluation of In[11]:= zot::trace:zot[1+1,2+2] --> zot[1+1,4]. During evaluation of In[11]:=zot::trace: zot[1+1,4] --> {1+1,4}. Out[11]= {2,4} Here we can see the original unevaluated arguments to zot and also ... 5 I took a rather shotgun approach to the question and got a range of behaviors. The range is rather confusing, so I agree with Szabolcs's conclusion that using Hold this way is not supported. First, NMinimize[Hold[Print["hi"]; x^2], x] prints "hi" and then crashes the kernel, while NMinimize[Print["hi"]; x^2, x] prints "hi" and returns {0., {x -> 0.}}. ... 5 I'm not sure why you don't like your merge function. This is the same pattern matching approach but using ReplaceAll: {e1, e2} /. {Hold[x_], Hold[y_]} :> Hold[x; y] If you would rather avoid patterns here is one (ugly) option: Block[{CompoundExpression}, (e1; e2) ~Thread~ CompoundExpression ~Thread~ Hold] 5 Here is an alternative solution based on expression parsers: ClearAll[ev]; ev[expr_, rules : {__RuleDelayed}] := Block[{ev}, SetAttributes[ev, HoldAll]; Replace[rules, (lhs_ :> rhs_) :> (ev[lhs] := rhs), {1}]; ev[f_[args___]] := Join @@ Map[ev, Unevaluated[{args}]] /. Hold[x___] :> Hold[f[x]]; ev[x_] := Hold[x]; ... 5 If a function is HoldAll, that means that the arguments are not evaluated before the function sees them, i.e. they're passed unevaluated into the function body. But once they're passed to the function, the function is free to do whatever it wants with them. This way HoldAll give you complete freedom in evaluating things in the order you want (or not ... 4 Here's another way, using TagSetDelayed: Irrationals /: Element[x_, Irrationals] := Element[x, Reals] && ! Element[x, Rationals]; Element[Sqrt[2], Irrationals] (* True *) This should work for elementary calculation. However, I don't think there is way to fully incorporate Irrationals as a domain into Mathematica (e.g., into Reduce[expr, ... 4 General considerations I think your goal here is misguided. If you state what you are actually trying to accomplish we can probably recommend alternative approaches. Due to the standard evaluation sequence the heads of expressions are evaluated first, if f has a direct (OwnValues) assignment it will evaluate first in the expression f[1]. Edit: As Rojo ... 3 If I understood your requirements correctly, what you need is Defer, which is in the same class of functions as Hold and Unevaluated, but will evaluate if explicitly evaluated. Then, using Trott–Strzebonski: Clear@factorial factorial[n_Integer /; n >= 0] := With[{m = n - 1}, If[n >= 2, n*Defer@factorial[m], 1]] factorial[0] = 0; (In the lines ... 3 You can use Cauchy's theorem. Define the approximate zero of your function : zero = FindRoot[DirichletL[19, 10, s], {s, 0.5 + I}][[1, 2]] (* 0.5 + 1.51608 I *) Series will not consider this a pole of 1/DirichletL[19, 10, s] and I think this is why you get a zero residue. However, integrating on a small square around that pole one finds : Table[{eps, ... 3 Leonid already named the big one, With, but there are a few more approaches I'd like to outline. First of all you can use Evaluate if you wish to evaluate the entire body of the Function, and if the Function isn't part of some larger held expression. I fully agree with Leonid however that With is more general and less prone to surprises here. Nevertheless ... 2 How about this. Generate a table of length nmax nmax = 10; x = Table[{i, RandomInteger[]}, {i, 1, nmax}]; and use InputField to update the second column of the table Column[{ TableForm@ Table[With[{i = i}, {x[[i, 1]], InputField[Dynamic[x[[i, 2]]], FieldSize -> Tiny]}], {i, 1, nmax}], replacement = Dynamic@x; }] ( The column ... 2 This is not a complete answer, but here I will suggest a somewhat different way to extract the dependencies, than in Istvan's answer. I think my method is somewhat more economical. The idea is to load the package of interest in a dynamic environment where$Packages variable is reset to {} initially. This will automatically prompt all packages to be loaded ...

2

In this particular case there's an easy explanation: Count[{a, b}, 0] immediately evaluates to 0, so we end up with Probability[ 0 != 2, ...], then Probability[True, ...], which is 1. To be able to give a warning, Probability would need to be HoldAll and check the arguments before they're evaluated. I do think that this is a somewhat tricky point, but the ...

1

You can use Piecewise to define your piecewise functions. For example your second example could be defined as follows: f[x_] := Piecewise[{{1/(2 x^2), Abs[x] > 1}, {0, True}}] Integration: Integrate[f[x], {x, -3, 7}] yields 16/21

1

I think there is nothing wrong with the solutions you have. If for whatever reason you'd really need to join into one CompoundExression instead of nesting three of them, then here are two way to achieve that: CompoundExpression @@@ Join[ Hold @@@ Hold[a = x + x; b = y + y + y], Hold @@@ Hold[c = a + a + b + b + b; c^2], 2 ] Many functions which ...

1

The general case of simplifying a sum of products according to a predefined algebra can be solved by representing each product in the form prod[a1,a2,a3,...] — i.e. a general non-commutative product. Defining your own prod like this neatly avoids any of Mathematica's built-in rules for simplification of expressions. Define rules for expanding out any Plus ...

1

The only solution I could come up with was to use TraceScan to detect the first evaluation of the symbol, at which point I'd copy the symbol (including all its UpValues, DownValues, etc.) to a temporary place, unset the symbol to prevent further evaluation, and set \$Pre to restore the symbol immediately prior to evaluating the next input (as well as ...

1

Use pure functions as a return value : fOne[x_] :=(*just return a piecewise*) Piecewise[{{#^2, # < x}, {#^2 + (x - #)^3 Sin[3 #], # > x}}] & fTwo[y_, z_, w_] := Module[{vars}, Plot[fOne[y][x], {x, z, w}]] fTwo[3, -3, 8]

1

After your comment showed that I misunderstood your problem, I cannot reproduce the issue you have. Let me copy and evaluate your complete example and show you that I get the correct behavior: f[v_]:=Sqrt[(1-2 v+8 v^2+Sqrt[-3+12 v+4 v^2-32 v^3+64 v^4])/(-1+2 v)]/Sqrt[2] g[v_]:=3/8+1/8 Sqrt[25+16 v]+Sqrt[5+8 v-25/Sqrt[25+16 v]-(16 v)/Sqrt[25+16 v]]/(4 ...

1

The Mathematica tutorial on Numerical Precision gives a brief intro of how Mathematica handles certain numerical inputs. Under Documentation, Details for Sin guides that "certain special arguments" are automatically evaluated to exact values. But in your examples, Mathematica sees the input value of 2. as a machine precision number and, per the ...

1

If I understand the question correctly, this is what you are asking: You have a table with headings in a text file. You want to read this table in and assign the columns to certain variables. You want to do this based on some mapping of the form "heading-name" -> variable that your code takes as input. First of all, personally I feel uneasy about ...

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