Questions tagged [evaluation]

Questions about how Mathematica evaluates expressions, tracing evaluation (Trace), handling expressions in unevaluated form (Hold, Unevaluated), nonstandard evaluation, etc.

For questions about how and in what order Mathematica evaluates expressions, tracing evaluation (Trace family), handling expressions in unevaluated form (Hold, HoldForm, HoldPattern, Unevaluated, Defer), nonstandard or controlled evaluation (ReleaseHold, Evaluate), etc.

Questions about algebraic simplification of expressions should use . Questions about structural replacement/manipulation in/of held expressions should use .

Useful links

Example questions

1579 questions
40
votes
3 answers

Can Mathematica do two evaluations at the same time?

When one evaluation is running, if I want do another evaluation, Mathematica will put the second one in the queue and run it after the first evaluation finish. However, the first evaluation may take very long time and the second one may just a easy…
user10568
21
votes
1 answer

Did Fibonacci slow down?

For Fibonacci numbers we have a nice formula: $$ \frac{\left(1+\sqrt{5}\right)^n-\left(1-\sqrt{5}\right)^n}{2^{n} \sqrt{5}} $$ We can implement that in Mathematica: a[n_]:= ((1+Sqrt[5])^n-(1-Sqrt[5])^n)/(2^n Sqrt[5]) a[10] //Expand (* 55 *) We…
Fred Simons
  • 10,181
  • 18
  • 49
19
votes
2 answers

Understanding Villegas-Gayley

In this nice answer, Mr.Wizard writes mk : MakeBoxes[(Hold | HoldForm | HoldComplete | HoldPattern)[__], _] := Block[{$hldGfx = True, Graphics, Graphics3D}, mk] /; ! TrueQ[$hldGfx] which is an application of the Villegas-Gayley pattern Question:…
Jacob Akkerboom
  • 12,215
  • 45
  • 79
18
votes
5 answers

Is it possible to construct a fullform trace function

Is it possible to construct a FullFormTrace function that takes an arbitrary expression and prints a sequence (or returns a list) of the evaluation steps in full form. For instance FullFormTrace[(1+2*3)^2] would output Power[Plus[1,Times[2,3]],2]…
Bob Ueland
  • 1,059
  • 6
  • 16
17
votes
1 answer

Get the current iteration count

I would like to be able to get the current iteration count, the one that if exceeds $IterationLimit makes the evaluation stop. After not finding a magical variable that stores this value, or a magical built-in that retrieves it, I thought about…
Rojo
  • 42,601
  • 7
  • 96
  • 188
16
votes
3 answers

Arguments to If[] are not evaluated

I got bitten by the following: f[x_] := 3*x; g[x_] := If[Log[f[x]] < 0, f[x], 0]; g[x] Out[11]= If[Log[3 x] < 0, f[x], 0] where I thought the call to f in the second argument to If would be “expanded”, i.e. replaced by its expression. Once I…
F'x
  • 10,817
  • 3
  • 52
  • 92
14
votes
2 answers

Why the Block command does not forget the $ContextPath variable

I do not understand this behavior: method[args_] := (* forget the context path *) Block[{$ContextPath}, (* should given nothing, right? *) Print["cpath=", $ContextPath]; ... but when evaluated it gives the…
zorank
  • 829
  • 4
  • 14
12
votes
4 answers

Order of evaluation - does Mathematica evaluate variables before or after substitution?

Let's say I write y = x^2; x = 10; z = y + y^2 + y^3 Does Mathematica internally evaluate y before or after substituting it into z? I.e., does Mathematica internally evaluate this as y = x^2 = 100 z = 100 + 100^2 + 100^3 = 1010100 or as z =…
tparker
  • 1,856
  • 12
  • 22
11
votes
1 answer

Aborting from inside a ScheduledTask

If you have an Abort[] inside a ScheduledTask expression, what does it abort? The scheduled task evaluation, or any main kernel evaluation that is ongoing?
Joel Klein
  • 5,225
  • 1
  • 31
  • 44
11
votes
3 answers

Prevent Mathematica to automatically evaluate Manipulate at start

I'm using an initialization cell to import a lot of data: data = Import[myFiles]; and then I use a custom function to display this data with Manipulate: Manipulate[customFunction[data,variable],{variable,{0,1}}] I have to keep these two commands…
Sulli
  • 2,185
  • 14
  • 28
11
votes
4 answers

How do I evaluate the terms in an expression, but not the expression itself?

I am trying to write a set of homework questions. I would like the output to evaluate the constants but not the sum, so that I may present the homework question as something like this: $$\sum_{n=1}^{340}(-4n^2-n+3)$$ Here's what I have: c1 =…
userX
  • 233
  • 1
  • 5
11
votes
3 answers

Apply OwnValues of argument after DownValues of function

I have the Symbol e with some upvalues and ownvalues: f[e] = 1; g[e] = 2; e = 3; I would like the evaluator to evaluate the definitions in that order. Currently, I get f[e] (* f[3] *) I want f[e] (* 1 *) The evaluator applies the ownvalues…
Tanner Legvold
  • 529
  • 3
  • 13
11
votes
1 answer

Different behaviour of ValueQ inside and outside of a loop

I need to store a rather big number of variables and I tried to do that by storing each variable as Z[n] with varying n. I guess this was not my best idea. The first problem I encountered is when I tried to check whether Z[n] is set. As you can see…
tst
  • 953
  • 6
  • 13
11
votes
2 answers

How to reliably reload package after change?

I'm using IntelliJ Idea with a Mathematica plugin to develop Mathematica application. I keep IntelliJ and Mathematica opened at the same time. I would like to change something in the IDE, evaluate notebook in the Mathematica and see updated result,…
lambdas
  • 212
  • 1
  • 7
11
votes
2 answers

Is there a way to prevent N from being applied to all parts of an expression

For example. If I have N[x[1]] how do I prevent it from returning x[1.] since I want to keep the variable as x[1]. I am using indexed variables like this in NonlinearModelFit where the form has N[expression involving indexed variables], but since…
BioPhysicist
  • 1,124
  • 5
  • 16
1
2 3
9 10