# Tag Info

0

You are right that the obj symbols are in the same Context. Note that because there is a Hold around the Block, the Block never gets evaluated. So Block gets no chance to do something like put symbols in another Context, but really Block never does so anyway. In particular, if there is a Hold wrapper around your entire expression, you can be sure nothing ...

9

The correct notation for a double derivative is Derivative[1][ Derivative[1][x] ][t], not Derivative[1][ Derivative[1][x][t] ][t]. Note the difference between a function f and its value at t: f[t]. Derivative[1][x] represents a function. Derivative[1][x][t] represents its value at t. Derivative needs to be used like this: Derivative[k][f] where k is a ...

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

In Mathematica 9.0 the second integral evaluates to 16/21...

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

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

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]

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

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

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

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

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

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

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

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

3

(1) Use Print, say, to see what exactly is happening under the hood. Something like the code below would have given a strong indication of what was happening. j = 0; While[Not[shouldStop] && j <= 5, j++; distances = g[xval] - zeroes; results = Map[Less[Abs[#], 0.00001] &, Abs[distances]]; xval = g[xval]; Print[N[xval]]; Print[results]; ...

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

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 ... 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] 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, ... 0 You are having a scoping problem here, because inside a Module your variable x will be renamed internally. Just replace your temporary definition of f with a ReplaceAll. I don't have your definitions for energies, effmass, etc, so I'm not 100% sure but normally this should work: getinter[a_, b_, u0_, k_, m_, hbar_, Nu_, Np_, up_] := Module[{ekp, ms, ... 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 ... 6 It is not very common, but sometimes,$Failed is used as a head, like f[x___]:= \$Failed[x] This makes it possible to have "return code" returned, rather than just a fact of failure. Basically, when this is used, it is usually in the error-reporting fall-back rule. In some cases, one may want to not evaluate the arguments x (e.g. if f is Hold*). I don't ...

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

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

0

A combination of Hold and Evaluate can achieve this: Hold[mytest[Evaluate[...],...]]. Illustrated with the example given in the question: parameters = {a -> 1} mytest[expr_, parameters_] := Module[{}, (* globpar = 2; *) NIntegrate[expr*a /. parameters, {var, 0, 1}] ] mytest1[parameters_] := Module[{}, globpar = 1; NIntegrate[ ...

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

0

nMax = 10; Block[{tab, boxes}, tab = Table[ {i, InputField[i]} , {i, nMax} ]; boxes = ToBoxes@TableForm[tab]; boxes[[2]] = Function[e, Print["replacement is updated"]; replacement = #[[1]] -> #[[2, 1]] & /@ e]; Cell[BoxData@boxes , "Input" ] // CellPrint ] Prints an input cell that looks like which evaluates to ...

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

3

You are almost there: Unprotect[Element]; Element[x_, Irrationals] := Element[x, Reals] && ! Element[x, Rationals]; Sqrt[2] \[Element] Irrationals True

0

The solution to this dilema seems to be that these sort of checks have to be done in the phraser that processes the code. Only there is the information sitting how the symbol is to be used. However, as it concerns the original question, a half way solution is like this: ClearAll[f] f /: Set[f, rhs___] := Print["fail1: ", ToString@rhs] f /: SetDelayed[f, ...

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

0

It looks like I should have used HoldForm instead of Hold. Here is my current solution that does the job: SetAttributes[fn,HoldAll]; fn[mappings_]:= ReleaseHold[Apply[Set, HoldForm[mappings]/.remoteData, {2}]]

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

Top 50 recent answers are included