Questions tagged [evaluation]

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

Filter by
Sorted by
Tagged with
118
votes
6answers
121k 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? ...
90
votes
4answers
4k 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 ...
90
votes
4answers
12k 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 ...
74
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]}] ...
59
votes
4answers
4k 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 ...
47
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
1answer
2k views

How to compare power towers in Mathematica?

First I tried it directly, but it overflowed: ...
37
votes
4answers
947 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: ...
35
votes
3answers
1k 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}] ...
35
votes
2answers
5k 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: <...
35
votes
4answers
3k 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, ...
34
votes
3answers
773 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 ...
34
votes
3answers
6k 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 ...
32
votes
1answer
686 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 ...
31
votes
3answers
684 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...
31
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. ...
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 ...
29
votes
4answers
7k 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 ...
28
votes
6answers
3k 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 ...
28
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 ...
28
votes
2answers
988 views

Block attributes of Equal

I answered “Equating matrices (or higher order tensors) element-wise” with: ...
26
votes
2answers
767 views

Evaluation of OptionValue[]

I have a function that takes an OptionsPattern and I want to access several options, say a, and ...
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
537 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
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
883 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 ...
24
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 ...
23
votes
2answers
705 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
947 views

Reassign values to symbols

I've got a situation where I have, say 4 symbols, a, b, c and ...
22
votes
3answers
18k 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: ...
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
4answers
958 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 ...
22
votes
3answers
503 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
2answers
591 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
2answers
783 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 ...
22
votes
2answers
724 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 ...
21
votes
6answers
3k 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
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 ...
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
500 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
251 views

How to uncompress strings safely (without any evaluation)?

Compressed strings are often used to exchange Mathematica expressions on the Internet. It is, however, not easy to see what such expression will do after decompression. Resulting code can perform ...
20
votes
3answers
1k 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 ...
20
votes
4answers
533 views

Function that caches when it returns unevaluated

How would you go about implementing a function that caches not the result but the fact that it didn't match a pattern? So that it doesn't waste time recomputing a complex time-consuming pattern ...
20
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 ...
20
votes
1answer
278 views

How to get Cases to wrap results in an arbitrary head?

A number of functions provide for results to be wrapped in an arbitrary head. This is very important in cases where the results should not be evaluated. Take Level...