# Tag Info

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 You can specify the evaluation of which construct should be stopped by Return by providing the second argument (undocumented?). For example, Scan[Function[x, Module[{}, Print[x]; Return[$Failed, Module]; Print[-x]]], {1, 2, 3}] or Scan[Function[x, Print[x]; Return[$Failed, CompoundExpression]; Print[-x]], {1, 2, 3}] 7 It's hard to reply without larger context, but if you are not restricted to use pure functions, then one option would be to use the pattern-defined overloaded function instead: ClearAll[fun]; fun[2] := Null; fun[x_] := ((*Do something useful*)Print[x]) Then, you just write: Scan[Function[x, Scan[fun, x]], {{1, 2, 3}, {4, 5, 6}}] In fact, you can as ... 7 Thanks to Michael E2's comment, the following approach is successful. The method sets up a scheduled task that (at certain resolution res) monitors the elapsed$time and compares it to the dynamic $max. If$time is more than allowed by $max, it calls the front-end "EvaluatorAbort". Attributes[dynamicTimeConstrained] = {HoldAll}; ... 7 Updated This happens because your DynamicModule returns a dynamic object of which x is passed on to the front-end before the scheduled task starts, so the front-end-x cannot be modified anymore by any process (more details at the end). The problem can be further simplified. This works: RemoveScheduledTask@ScheduledTasks[]; DynamicModule[{x = 0}, ... 6 As Leonid has explained the problem is that the symbols are created and get their context at parse time, so if you need to avoid generating them in the current (usually "Global") context, using$PreRead as he explained is the only possibility. If you don't care that the symbols you use are created in the current context AND the context you want to evaluate ...

6

ClearAll clears all definitions associated with the symbol. However, the symbol remains in the symbol table, so all references to that symbol from other symbols (their definitions) remain fully valid. The symbol can then acquire new rules or other global properties associated with it. Remove removes the symbol from the symbol table. More precisely, it ...

5

Besides destroying the symbol itself, the main side effect of Remove that is not shared by ClearAll is what happens to expressions containing a removed symbol. a = {x, y}; ClearAll[x, y];a {x, y} Remove[y]; a {x, Removed[y]}

5

Since Do (and Table) has attribute HoldAll, paramToVary won't be evaluated at the right time. Use Evaluate on the iterator specification to force the replacement of paramToVary -> a. ClearAll[a]; paramToVary = a; paramValues = Range[0, 1, .2]; Table[a, Evaluate@{paramToVary, paramValues}] {0., 0.2, 0.4, 0.6, 0.8, 1.}

4

Abort[] inside a scheduled task will abort the rest of the task expression at the given time, not any main evaluation. It will also repeate to evaluate the task (up till the Abort[]) if further time slots are scheduled. To show this, first start a scheduled task: RunScheduledTask[ Print[DateString[], " Scheduled task before Abort[]"]; Abort[]; ...

4

Maybe TimeConstraint is helpful: y = Gamma[1 - x] Gamma[x] Sin[Pi x] + Gamma[x] Gamma[1 - x] Sin[Pi (1 - x)]; FullSimplify[y, TimeConstraint -> 0.000001] FullSimplify[y, TimeConstraint -> 0.0001] FullSimplify[y, TimeConstraint -> 0.01] Gamma[1 - x] Gamma[x] Sin[π (1 - x)] + Gamma[1 - x] Gamma[x] Sin[π x] 2 Gamma[1 - x] Gamma[x] Sin[π x] 2 π

4

The only method I can think of that will use the built-in simplification routines is to snoop on transformations using either TransformationFunctions or ComplexityFunction. Unfortunately neither of these will be restricted to the entire expression therefore what is produced may not be usable. Nevertheless as an example: FullSimplify[Gamma[1 - x] Gamma[x] ...

3

Borrowing from an example of WhenEvent from the documentation in which a Button is used to stop the integration, I came up with this. ClearAll[ndsolveMemConstrained]; SetAttributes[ndsolveMemConstrained, HoldFirst]; ndsolveMemConstrained::mlim = "Memory used  exceeded limit ."; ndsolveMemConstrained[(nd_: NDSolve | NDSolveValue)[eqns_, rest___], bytes_] ...

3

With a few bells and whistles: Manipulate[ Module[{plts}, plts[a_, t_] = Table[Tooltip[ Sum[((a t)^k)/k! Exp[-a t],{k, 0, Infinity, n}] // Simplify, StringForm["n = ", n]], {n, 5}]; Plot[Evaluate[plts[a, t]], {t, 0, 5},PlotRange -> {0, 1}]], {{a, 1.5}, 0, 3, 0.05, Appearance -> "Labeled"}] Bob Hanlon

3

It can work with a=0 too, the problem is that a is applied before closed form of the sum is calculated. We can force this: DynamicModule[{t, k, tab, a}, Column[{ Dynamic@Plot[tab[a, t], {t, 0, 5}, ImageSize -> 400], Slider[Dynamic[a], {0, 11, 1}] }], Initialization :> {tab[a_, t_] = Table[Sum[((a t)^k)/k! Exp[-a t], {k, 0, Infinity, n}], ...

3

Since it was mr. Leonid Shifrin who provided a proper solution, I feel less guilty of my brute-force, very limited try: it adds the context to the first symbol in every Set and SetDelayed. a =.; b =.; c =. context2a =.; context2b =.; context2c =. InContextSetAndSetDelayed := Function[{context, code}, code /. SetDelayed -> f /. ...

3

I suppose what you ask about recalculating can be done with With or other scoping construct. Manipulate[ With[{mag = FL/OL, minmag = AD/OP}, Grid[ MapAt[ NumberForm[N@#, {\[Infinity], 4}] &, {{"Gathering Power: ", (minmag)^2}, {"Magnification: ", mag}, {"Min Magnification: ", minmag}, ...

2

May be I am missing something but Manipulate is designed to automatically re-evaluate when one variable in its expression changes. Manipulate[ N@Grid[{ {"Magnification: f/c = ", f/c}, {"Exit Pupil: a/m =", a/(f/c)}, {"Longest Useful FOV: f/e =", f/(a/(f/c))} }], {{a, 130, "Aperature: "}, 60, 508, 1,Appearance -> "Labeled"}, {{f, 650, "Focal ...

2

The problem is how to evaluate the sums without before a is set to 0. That can be done with With. If you put Dynamic around Plot, then only the Plot will be updated when the slider for a is moved. Manipulate[ With[{plots = Table[Sum[((a0 t)^k)/k! Exp[-a0 t], {k, 0, Infinity, n}], {n, 1, 5}]}, Dynamic @ Plot[Evaluate[plots /. a0 -> a], {t, 0, 5}, ...

2

I would use Catch and Throw if pressed to choose, without the 'exit' : Scan[Function[x, Scan[Function[y, Catch[If[y == 2, Throw[Null]]; (*Do something useful*) Print[y]] ], x]], {{1, 2, 3}, {4, 5, 6}}] or simply, Scan[Function[x, Scan[Function[y, If[y == 2, Null, (*Do something useful*) Print[y]] ], x]], {{1, 2, 3}, ...

1

Use a numerical derivative: Clear[band, en, w, fermi, k, T, S, a] << NumericalCalculus k = 86*10^-6; T = 4000; band[en_, w_] := 10000 Exp[-en^2/2 w^2]; fermi[en_, ef_] := 1/(Exp[(en - ef)/(k T)] + 1); S[ef_?NumericQ] := -k NIntegrate[(band[en, w] fermi[en, ef] Log[fermi[en, ef]]) /. w -> 1, {en, -Infinity, Infinity}] ...

1

You can't get an exact solution with Mathematica, but you may approximate it, for example with polynomials: s = NDSolve[{y'''[x] + y[x]^2 y''[x] - y'[x] == 0, y[0] == 0, y'[0] == 0, y''[1]== 1}, y, {x, 0, 1}]; data = Table[{x, y[x] /. s[[1]]}, {x, 0, 1, .01}]; Manipulate[ Column[{#, Show[ListPlot@data, Plot[#, {x, 0, 1}, PlotStyle -> {Thick, Red}], ...

1

For this specific case (units in Quantity) a solution is relatively simple: if you use the "standard" unit name Mathematica won't need to use the internet to try to interpret ist. Here that means using "Meters" should avoid the slow evaluation in the first place (I think that is documented and was already discussed once on this site)> m = ...

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