Questions tagged [scoping]

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

Filter by
Sorted by
Tagged with
3
votes
1answer
181 views

Module Inside ToBoxes or MakeBoxes

Does someone mind explaining why Module doesn't seem to work while inside ToBoxes or MakeBoxes. ...
3
votes
1answer
385 views

Call Functions From File Without Modifying Context (Sandbox)

Let's say I have file named test.m containing test[arg1_] := ( Print[arg1];) What is the best technique for calling test like ...
1
vote
2answers
197 views

Confused about scoping, not sure what to ask

I'm grading for a class using Mathematica right now, and one of the students ran into a weird issue. When he ran code like this: ...
7
votes
1answer
319 views

Creating a Block from a list of rules

How can I create a function which works like Block but takes a list of rules as its first argument, rather than a list of assignments? The function needs to be ...
10
votes
0answers
895 views

Compile recursive function modifying global variables

How to compile recursive formula when it relies on more than a few global variables (global to the topmost compiled function)? It is unreasonable to pass on all such variables to each recursive ...
10
votes
2answers
522 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 ...
0
votes
1answer
220 views

Arbitrary number of variables in Module, Block, etc

How do I specify an arbitrary number of variable names and initialize them inside of a Module or Block? For example, I want to do something like: ...
0
votes
1answer
87 views

Scope of expression in a user-defined function

I have the following code: fbasic = A + R + Z*Z *Z*Z; f[A_, Z_, R_] := fbasic + R; f[0, 2, 3] The answer I am getting is ...
1
vote
0answers
316 views

Variable scope in nested NMaximize, NMaxValue

I'm trying to use NMaxValue inside other statements (FindRoot and NMinimize). Those statements should bind variable names, but I'm getting weird warnings which seem to indicate that the variables ...
16
votes
3answers
2k views

What is the right way to construct DynamicModule

When I'm creating DynamicModules for CDF purposes I'm usualy basing on my experience with that, particular goal and content rather than on deep insight. I want to ...
10
votes
1answer
172 views

What are the scoping rules for function parameters shadowing System` symbols?

Here are some very contrived code snippets, highly unlikely to appear in real code, but still I am curious why they behave like this: ...
5
votes
0answers
144 views

Are there any videos that show how different Mathematica expressions are evaluated?

Are there any videos that show how different Mathematica expressions are represented internally as object graphs and demonstrate how they are transformed during an evaluation? I am asking because I ...
8
votes
1answer
172 views

Is it possible to write a function immune to symbol redefinitions in any Block surrounding its invocation?

Normally, semantics of symbols used in a function definition can be changed by their redefinitions in Blocks surrounding the function invocation: ...
3
votes
1answer
196 views

Issue with scope of variables

For the definitions, t = 1; f[x_, y_] := t (x + y); v[3] := {1, 1}; g[u_] := f[v[u][[1]], v[u][[2]]]; the following two expressions yield different results, ...
10
votes
1answer
169 views

Is it possible to use the global definition of a symbol as part of its new local definition set in a Block?

Consider the following expression: Block[{Print = CopyToClipboard}, doSomething[]] Now all calls to Print from ...
3
votes
1answer
449 views

Effect of Block[] on iterators in Manipulate[]

The following is a toy example that hopefully shows the problem clearly enough. Why does the following code create a nice, manipulable output: ...
8
votes
1answer
329 views

FullDefinition and Module local variable

I have a question about FullDefinition and how it interacts with the local variables of Module. I've created a few functions (...
9
votes
3answers
860 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
1answer
143 views

Odd edge-case behavior of Block

In this example, Block is used to localize the variable cache as used in the function g when called from the function ...
8
votes
2answers
200 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 ...
3
votes
1answer
537 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 ...
47
votes
1answer
965 views

What does Internal`InheritedBlock do?

What does the function Internal`InheritedBlock do? How is it different from the regular Block?
7
votes
2answers
340 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: ...
17
votes
1answer
473 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 ...
4
votes
3answers
494 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 ...
0
votes
1answer
69 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
123 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 ...
5
votes
1answer
194 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 ...
5
votes
2answers
698 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) ...
21
votes
1answer
270 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, ...
5
votes
3answers
319 views

Notation for specifying transformation rules

I would like to have a compact notation for specifying conditions and results in transformation rules. Consider ReplaceAll: ...
1
vote
0answers
79 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 ...
11
votes
2answers
498 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 Manipulate....
5
votes
1answer
486 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 ...
0
votes
1answer
157 views

Trying to calculate a sum within a module - why is the iterator not creating integers?

Suppose I have the code: ...
41
votes
3answers
2k 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
615 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
356 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, ...
4
votes
2answers
1k 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. ...
9
votes
6answers
853 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?
8
votes
2answers
164 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
2k 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 ...
10
votes
1answer
267 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 ...
8
votes
1answer
331 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 ...
10
votes
3answers
642 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 ...
10
votes
3answers
227 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 <...

1
6 7 8 9
10