Questions tagged [evaluation]
Questions about how Mathematica evaluates expressions, tracing evaluation (Trace*), handling expressions in unevaluated form (Hold*, Unevaluated), nonstandard evaluation, etc.
1,310
questions
129
votes
6answers
141k views
Get a “step-by-step” evaluation in Mathematica
Is it possible in Mathematica to get a step-by-step evaluation of some functions; that's to say, outputting not only the result but all the stages that have led to it? If so, how does one do it?
...
93
votes
4answers
5k views
Replacement inside held expression
I wish to make a replacement inside a held expression:
f[x_Real] := x^2;
Hold[{2., 3.}] /. n_Real :> f[n]
The desired output is ...
92
votes
4answers
13k views
Plot draws list of curves in same color when not using Evaluate
This example comes from
the Mathematica documentation for Plot
under Basic Examples.
Can someone please explain why these are each plotted as a different color in ...
78
votes
3answers
3k views
The clearest way to represent Mathematica's evaluation sequence
WReach has presented here a nice way to represent the Mathematica's evaluation sequence using OpenerView. It is much more clear way to go than using the standard <...
67
votes
10answers
3k views
Injecting a sequence of expressions into a held expression
Consider the following toy example:
Hold[{1, 2, x}] /. x -> Sequence[3, 4]
It will give
Hold[{1, 2, Sequence[3, 4]}]
...
61
votes
4answers
5k views
How do I evaluate only one step of an expression?
I am looking for a simple, robust way to evaluate an expression only one step, and return the result in a held form.
The definition of a single step is ambiguous, and this itself is probably worthy ...
49
votes
2answers
2k views
How do you set attributes on SubValues?
SubValues, as discussed in a previous question, are declared as follows
...
44
votes
5answers
3k views
How to create symbols from strings and set values for them?
I am trying to convert a list of string names into symbols, which will then be used to store data. I have 24 files (where the name of each file is a member of the list mentioned above) that I need to ...
41
votes
5answers
4k views
Why doesn't Mathematica abort evaluation when I tell it to?
This frequently happens to me. I'll have some code, execute it, and realize it's taking a long time. My PC isn't frozen, and Mathematica itself isn't even frozen: I can select stuff in the notebook, ...
41
votes
1answer
3k views
39
votes
3answers
8k views
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 ...
37
votes
3answers
2k views
Interrogating a running evaluation
Is it possible to find the values of variables of a running Mathematica evaluation, without interrupting it? For example, suppose I do:
Do[Pause[1]; a++, {50}]
...
37
votes
2answers
6k views
Difference between AbsoluteTiming and Timing
I need to evaluate the efficiency of my code. Therefore I would like to evaluate the time the code need for some calculations. Mathematica gives me two possibilities for this kind of evaluation:
<...
37
votes
4answers
1k views
What is the difference between Dynamic[x] and Dynamic[ h[x] ] for DynamicModule variables?
Bug introduced in 9.0 or earlier and persisting through 12.0
In 11.3 it is even worse. An example from the accepted answer with a Slider is now broken too:
...
36
votes
3answers
864 views
Can we intelligently control evaluation in Thread?
This is a bid at creating a canonical Q&A.
Many questions have been asked that come down to the fact that Thread evaluates its first argument before threading ...
33
votes
1answer
726 views
V11.1: Effective methods to use paid functions economically
In the newly released version 11.1, Mathematica introduces three new functions: WebSearch, WebImageSearch and TextTranslation that requires Service Credits. While I haven't found much information ...
32
votes
2answers
2k views
What are the cool kids talking about when they use ##&[]? [duplicate]
All the cool kids are apparently using
##&[]
for
Unevaluated @ Sequence[]
but I have no idea what either means.
...
31
votes
3answers
731 views
Why does removing a function and defining it on the same line not work?
Why do the two pieces of code below yield rather different results? The only difference is that in the first one the function definition is in the same line as the ...
31
votes
4answers
2k views
Asynchronous evaluation: Is it possible?
Currently we have a limited number of ways to perform asynchronous evaluation.
The most common is through Dynamic and Manipulate...
30
votes
8answers
1k views
Elegant high precision `log1p`?
Sometimes it is hard to understand how numerical expressions are evaluated. I remember reading claims by Wolfram on how smart the Kernel is to evaluate expressions trees numerically by recognizing ...
30
votes
6answers
4k views
How are parameters evaluated for a Plot in Manipulate
I am trying to get my head around how Manipulate evaluates functions in a Plot. I have read the introduction to Manipulate, and introduction to Dynamic, but I still ...
29
votes
4answers
8k views
Keyboard shortcut to evaluate notebook
(If this is entirely trivial, although I couldn't find an answer as much as I searched the archives, I will delete the question.)
How could I add a keyboard shortcut to evaluate the entire notebook ...
29
votes
1answer
3k views
A one line proof that one is zero using Mathematica 10
a = (Sqrt[2] + 1) (Sqrt[2] - 1) - 1;
{a/a, Simplify[a]/a, b/b == Simplify[b]/b}
{1, 0, True}
This one line "proof" that ...
29
votes
1answer
3k views
How does Return work?
It is not completely clear to me how Return[] works.
The documentation says:
Return[expr] returns the value expr from a ...
29
votes
2answers
1k views
Block attributes of Equal
I answered āEquating matrices (or higher order tensors) element-wiseā with:
...
27
votes
3answers
22k views
General::ivar is not a valid variable when plotting - what actually causes this and how to avoid it?
I was just evaluating a couple of expressions and started to get errors like this:
...
27
votes
2answers
845 views
Evaluation of OptionValue[]
I have a function that takes an OptionsPattern and I want to access several options, say a, and ...
27
votes
1answer
2k views
How can I set a keyboard shortcut to run a command?
I want to use this to add a keyboard shortcut to an undo function I wrote. The basic problem is the following:
I have a command, let's say x=1, and I want to run ...
25
votes
5answers
5k views
How to pass a symbol name to a function with any of the Hold attributes?
Given a function with the attribute HoldFirst, HoldAll or similar, and a variable, list, how ...
24
votes
4answers
1k views
Permanently extending the behaviour of functions (like decorators)
Suppose I have a function, e.g.
f[x_] := x^2
It works like this:
f[5]
(* 25 *)
I would like to permanently modify it so ...
24
votes
2answers
575 views
Why doesn't a Composition[] hold its arguments and what can be done about it?
Now that Composition (also RightComposition) has shorthand syntax I am more inclined to use it, however what I feel should be ...
24
votes
2answers
789 views
Implementing a safe ValueQ that does not evaluate its argument
Mathematica has a built-in function ValueQ. Quoting the docs,
ValueQ[expr] gives True if a ...
24
votes
2answers
1k views
Mathematica for Computer Scientists
Background
I've coded in C/Scheme for 10 years. I have a BS CS.
I've recently become very interested in pattern-matching (lisp's match macro), and term rewriting systems, which brought me to ...
24
votes
2answers
907 views
Wagner's trick to monitor FindRoot no longer works in Mathematica version 8
I recently learned that we can use Print as a monitoring tool. My favorite is Ted Ersek's example
...
23
votes
4answers
999 views
Making a symbol's new definitions be tried before all previously defined ones
Is there any way to "close" a package (or a symbol, or a context) in that if a user of the package adds definitions to the symbol they will be tried before the package defined ones, just like what ...
23
votes
3answers
2k views
When must I use the Return function?
In a comment to my answer to this question, QuantumDot asks, "Where can I find more information on when I must use the 'Return' function?". I found this too deep a question to be answered by leaving a ...
23
votes
2answers
760 views
Context unique to each group at a specified level
Are there any new tools in v10 that might be applicable to this problem?
Recent versions of Mathematica provide the option of having a unique $Context for each ...
22
votes
5answers
1k views
22
votes
2answers
1k views
How to inject an evaluated expression into a held expression? [duplicate]
I know that there are methods to structurally manipulate held expressions (discussed e.g. here), but I failed to apply those for this particular problem:
...
22
votes
3answers
519 views
Can't inject EncryptedObject
Bug present from 10.0 to 10.3.1 and fixed in 10.4.0
!!
Here are minimal examples:
...
22
votes
3answers
1k views
Comparing Mathematica expressions like diff
I am looking for a way to compare (or "diff") two Mathematica expressions, similarly to how to diff utility can compare two text files and report the differences.
Has anyone already written such a ...
22
votes
2answers
614 views
Shell Emulation inside Mathematica
I am trying to create a Mathematica/shell syntax hybrid.
I would like the following command
wget -qO- "http://google.com/space line
break\"" $var
to be ...
22
votes
3answers
2k views
How to find the name of the current function?
I would like to know the name of the current function from within that function. For example, consider the following code
...
22
votes
2answers
921 views
Restarting Mathematica automatically
I run a large computation in a loop. At each cycle of the loop
I produce large arrays that I save on a disk. At each cycle I would like to Quit Mathematica and ...
21
votes
6answers
4k views
All curves in plot have the same style. Cannot be fixed with Evaluate[]
Usually, when I plot multiple curves in Mathematica
Plot[{x,x^2,x^3},{x,0,1}]
they are given different colors. However, if I try to construct a list inside the <...
21
votes
4answers
1k views
Recovering intermediate results from a Table after an interruption
I am interested in knowing if it is possible to recover intermediate results from a Table after an interruption.
Let us say I want to produce a really useful table
...
21
votes
1answer
683 views
Why does the documentation call functions “pure”?
Clearly, functions in mathematica are not pure functions according to the definition on Wikipedia (no side effects - basically implementabe with table lookups):
...
21
votes
1answer
5k views
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:
...
21
votes
2answers
544 views
What does f_[whoCalled]^:=f really mean?
I came across the following post by John Doty in this Google Groups discussion:
On Friday, January 11, 2013 8:23:16 PM UTC-7, amzoti wrote:
It is clear the Mathematica uses Lisp as one ...
21
votes
1answer
1k views
Using Evaluate and Evaluated -> True in Plot
A few use cases:
(1) This, as expected, returns (almost) immediately:
f[x_?NumericQ] := Evaluate[(Pause[1]; x)]
Plot[f[x], {x, 0, 1}]
(2) This takes ...