Questions on Mathematica's scoping constructs (Block, Module, With, etc.)

learn more… | top users | synonyms (1)

5
votes
1answer
227 views

Parts of Module body evaluated in external scope?

I have an expression that suggests that some expressions in a module body are dragging in definitions from outside the scope in a surprising way. First, consider a symbolic constant, ...
1
vote
1answer
178 views

Using block or module to free cache

I'm writing some code at the moment that schematically looks like: (set initial conditions for some differential equation for given parameters (M,R,...)) ...
34
votes
3answers
1k views

Why modules with no variables?

I was reading some code, in particular, recipe 4.13 on unification pattern-matching in Sal Mangano's Mathematica Cookbook, and there were many instances of Modules with no variables in them, such as ...
10
votes
2answers
226 views

Avoiding MainEvaluate in a CompiledFunction to fetch global variables

I'm trying to write a simulation using Mathematica 8.0. Since I will most likely be doing the same operation over and over again, I'm trying to Compile whatever I ...
10
votes
2answers
464 views

Scoping in assigning a derivative

While answering another question, I stumbled upon a problem I cannot easily resolve. To assign the derivative of a function to another function, typically one can do this with a ...
3
votes
2answers
94 views

Is there a way to localize Off[some::message]?

Is there a way to temporarily suppress certain messages, so that I could write for example (with made-up syntax for that feature): ...
2
votes
2answers
478 views

Scoping inside of Module and Manipulate

This is driving me nuts: I'm trying to control the parameters for a relatively large system of ODEs using Manipulate. ...
4
votes
1answer
173 views

How to avoid conflicts between local variable names and symbolic arguments in Block constructs?

I am using Block inside the definition of some recursive functions. If the Block definitions use short variable names, like ...
8
votes
2answers
126 views

Question about scoping data in a multi-level Manipulate construction

Background: In a module containing an outer / inner manipulate I select the key of data with a dropdownlist in the outer manipulate. Data is read and displayed in the inner manipulate for edit ...
9
votes
3answers
806 views

How to use local variables with indices in a Mathematica Block environment?

I have the following code: Subscript[f, i_][x_] := Block[{a}, Subscript[a, i] = 3 x; Subscript[a, 1] ] If I evaluate for example ...
6
votes
1answer
185 views

Difficulty catching thrown errors in asynchronous tasks

In this Question, Leonid explained that module variables can get garbage collected from a chain of asynchronous tasks unless their Temporary attribute is cleared ...
6
votes
1answer
236 views

Module variable scoping in Scheduled Tasks?

I ran across a scoping puzzle while experimenting with ScheduledTasks, and I'd be grateful for an explanation from the sages here. I found a workaround by intuitive horse sense, but was unable to ...
9
votes
3answers
275 views

Find variable name from DumpSave on a scoped variable

I saved an InterpolationFunction in a ".mx" files using DumpSave on a variable that was scoped by a ...
5
votes
3answers
263 views

Notation for specifying transformation rules

I would like to have a compact notation for specifying conditions and results in transformation rules. Consider ReplaceAll: ...
3
votes
2answers
378 views

Local variables

I'm trying to use Modules together with functions. tmp2 = x^2 + 1; f[y_] := Module[{x = 1}, Evaluate[y tmp2]] This works when ...
8
votes
3answers
165 views

With/Function(/etc) replacement behaviour

Can anyone explain to me this behaviour? I've been having more than a couple of similar doubts these last weeks. For example f[_?NumericQ] := 8; Now, if I do ...
6
votes
4answers
455 views

Why do I see the error “only assignments to symbols are allowed” when using a Module and not otherwise?

When I type Remove["Global`*"] foo[] := Module[{Subscript[a, 1] = "x"}, 0]; foo[] I get the expected error "only assignments to symbols are allowed". I ...
6
votes
6answers
579 views

How can you give a Module a context and have its local variables and Modules belong to that context?

What would be the best way to give a module its own context and have its local variables and Modules belong to that context?
93
votes
7answers
3k views

What are the use cases for different scoping constructs?

Mathematica includes three functions that I know of which can be used to effectively (if not actually) define a variable and give it a value within a local scope: ...