Questions on Mathematica's scoping constructs (Block, Module, With, etc.)
3
votes
1answer
71 views
How can I set multiple values to local variables in a Module?
Multiple assignment is supported by Set.
How can do something similar in Module expressions?
If I could do it, whould there be ...
17
votes
1answer
106 views
What does Internal`InheritedBlock do?
What does the function Internal`InheritedBlock do? How is it different from the regular Block?
4
votes
3answers
95 views
Modify this code using Module and While
I have written a recursive function and would like to re-write the code using Module AND While to compare the timings.
Here is my recursive function for f[n], where 6 n f[n] = f[n-1] + n! for n>0 and ...
6
votes
2answers
104 views
Variable scoping problem when mapping over delayed replacement
This is something that got me curious while I was playing around with Mathematica.
Consider the following (contrived) example:
...
7
votes
0answers
84 views
Compile not correctly initializing a variable defined inside Module
In the following example, inside the minimumX's Module, x is initialized to 1, but the ...
0
votes
1answer
48 views
Don't pollute outside context when solving equation from the outside
This code works fine only if a is not defined in the outside context:
...
2
votes
1answer
52 views
How to make a code line dynamic in a Module?
The code line
If[StreamPosition>0,Read[str,Record]]
should be executed in a Module at first.
The Module consists of a Slider and a Pane.
The Slider ...
6
votes
2answers
94 views
How do I avoid scoping collisions of iterator variables?
Yesterday I had an issue that kept for quite a while. My code wasn't producing the right results. Now I know why and I am wondering how to avoid the issue in future. Consider the following code.
...
3
votes
3answers
104 views
Defining a new Function using RegionPlot
I'm trying to make images like this:
to illustrate 2D integrals to a calculus class.
I used the code
...
6
votes
1answer
64 views
Possible to scope an anonymous function within Module?
This is my first question. I'm fairly new to Mathematica and completely new to SE, so I apologize if this question is trivial.
I'm working on a problem that requires a function to be redefined ...
15
votes
4answers
240 views
Enforcing correct variable bindings and avoiding renamings for conflicting variables in nested scoping constructs
Using global variables the following turns an "expression" into a Function:
expr = 2 x;
Function[x, Evaluate[expr]]
Of course ...
4
votes
1answer
112 views
Module with “local functions”
I am trying to use a Module having "local functions", i.e., those which I need to define only inside this module.
So I tried this:
...
3
votes
1answer
112 views
Downvalues vs. Scoping for Functions
Regarding my recent question on using a default value for a function argument when a pattern was not met yielded some interesting answers, but the general consensus was "Yes this can be done, but ...
3
votes
2answers
126 views
Why does the syntax highlighter color a local symbol red inside DynamicModule?
I am adding a Module inside DynamicModule here is an example (thanks to @rm-rf for this simple example)
...
13
votes
1answer
182 views
Is the sharing of variables in Module/Block within Compile documented behavior?
Today I noticed something, I think for the first time.
When used inside Compile variable values within Module (and ...
6
votes
4answers
187 views
How to set Block local variables by code?
I need to create a user defined Block function where the Block variables values are defined by code. For example, imagine I have:
...
17
votes
1answer
161 views
“With” as part of the “Dynamic Interactivity Language”
I noticed that in Mathematica's Function Navigator With is listed as being part of the "Dynamic Interactivity Language".
It is not the first (or even the fourth, ...
6
votes
2answers
98 views
How to define UpValues in a different context from within a package
I'm trying to create a function (in a package) to import a lot of data and create a symbol with UpValues to return specific results about the data. I've got it working fine in a regular notebook, but ...
0
votes
0answers
60 views
What's the best practice for nested local constants? [duplicate]
Possible Duplicate:
How to avoid nested With[]?
I have many situations where I have a constant that is local to a function, with other constants that are computed from it. I see that I can ...
5
votes
2answers
181 views
Localizing variables within a Manipulate
I have a notebook with several examples of very similar computations, each involving the same variable/parameter names of the ingredients used in a final ...
0
votes
0answers
42 views
What is the difference between 'With' and 'Module' other than 'With' seems faster? [duplicate]
Possible Duplicate:
What are the use cases for different scoping constructs?
Timing[Do[Module[{x=5},x;],{10^6}]]
{4.306, NULL} (depending on your machine)
...
10
votes
1answer
154 views
Modules that initialize themselves on first call
I use a lot of functions that extract a specific data item from a file with many data items. I want these functions to load data (slow) and return the item (fast) on first call, but just return the ...
15
votes
2answers
454 views
What are some advanced uses for Block?
I read the answer to this question and this one.
According to those, Block is safer (if something aborts, it restores the values) and faster (perhaps something to ...
3
votes
2answers
168 views
LetL and Module efficiency
I was recently introduced to the LetL macro thanks to Leonid's answer to one of my prior questions. I was, needless to say, impressed by the simplicity of its ...
20
votes
3answers
395 views
How to avoid nested With[]?
With[
{v1 = #},
With[
{v2 = f[v1]},
g[v1, v2]
]
]
How to avoid nested With[] like the above? I'd like to use ...
0
votes
1answer
106 views
Trying to calculate a sum within a module - why is the iterator not creating integers?
Suppose I have the code:
...
0
votes
3answers
166 views
Constructing a list with a Module [closed]
I have build this simple module trying to construct a matrix via Table
...
9
votes
2answers
170 views
Strange results while nesting scope constructs?
I did a handful of tests with Block, With, and Module and had a couple of new surprises. ...
5
votes
1answer
179 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
126 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,...))
...
30
votes
3answers
668 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
...
9
votes
2answers
242 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
82 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
273 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
119 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
104 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
465 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
133 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
174 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 ...
8
votes
3answers
190 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
208 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
210 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
144 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
...
4
votes
4answers
202 views
why I see the error “only assignments to symbols are allowed” in Module context and not in global?
V 8.04, on windows.
When I type
Remove["Global`*"]
foo[] := Module[{Subscript[a, 1] = "x"}, 0];
foo[]
I get the expected error "only assignments to symbols ...
5
votes
6answers
492 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?
61
votes
7answers
2k 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: ...




