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

learn more… | top users | synonyms (1)

12
votes
2answers
467 views

Syntax highlighter shows error for Manipulate when it's inside a Block or a Module

I'm putting my new copy of Mathematica 10 through its paces and I noticed a weird change in the colouring scheme that I find very annoying, and I'd like to get some insight into why it's happening. ...
0
votes
1answer
10 views

Module and Manipulate: Why the Red Highlighting? [duplicate]

If I define a function that returns a Manipulate object, and the Manipulate has a local variable inside of it, the local ...
0
votes
2answers
57 views

Use a Module to return multiple plots

A program I'm running requires me to generate multiple plots over the same domain, so I thought it would be fairly simple to use a module and bury all of the plot commands within that that way I only ...
14
votes
2answers
285 views

Messages leak out from Block

fixed in 10.1 When assigning message to a symbol localized by Block, I would expect that this message is not accessible outside of ...
0
votes
0answers
25 views

What is the right way to combine a Package, Module, ParallelTable and NIntegrate?

I am stuck with a curious problem. I would like to to have a package from which i can call a function test[path], which then performs a time consuming numerical ...
1
vote
1answer
140 views

Combining Mandelbrot and Monte Carlo

I'm trying to combine plotting a Mandelbrot set with Monte Carlo randomization to plot an equation using random points for complex number z, for the function $z^3-2z+2=0$. Below is the code I have ...
42
votes
4answers
952 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 ...
3
votes
2answers
68 views

Memory Problem with Modules and NDSolve

I'm using NDSolve inside a module, and I appear to have a memory leak. The relevant code is: ...
1
vote
2answers
98 views

Random walk using Module [closed]

I want to create a random walk using Module. The walk start at {0, 0}. At every step, x ...
4
votes
1answer
96 views

Unwanted interaction between instances of a Manipulate function

The function trajectory below is defined in terms of Manipulate and calls the auxiliary function ...
3
votes
0answers
58 views

Decrease computation time of module [closed]

I need to compute the exponential of a matrix (in this caseR) by the summation method where R is a square matrix with symbolic ...
111
votes
7answers
5k 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: ...
7
votes
1answer
122 views

Context of localised (dynamic) symbols

When you localise a symbol with Module, the created unique symbol is usually in the current context. There seems to be an exception to this which has further ...
3
votes
2answers
146 views

Strange interaction between Module and ListPlot

Why is the variable a ending up assigned to {1,2,3} after executing Module[{a}, a=Range[3]; ListPlot[a] ]
9
votes
2answers
135 views

How does $CellContext` work?

The only official info about $CellContext` I was able to find is: placeholder for the context of a symbol inside Dynamic ...
4
votes
3answers
500 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 ...
2
votes
1answer
37 views

Local function becomes global after Plot [duplicate]

In the code below, I define and plot a local function f. Clear[f] Module[{f}, f[x_] := 1; Plot[f[x], {x, 0, 1}]] However, ...
3
votes
0answers
84 views

Why is it not allowed to define vectors of variables in a scoping construct?

The following command assigns 1 to a, 2 to b and 3 to c: ...
2
votes
1answer
64 views

Writing a procedure that invokes Manipulate[]

I would like to write a procedure with the signature: Foo[vars, params] which produces a Manipulate object. The procedure ...
13
votes
2answers
385 views

I define a variable as local to a module BUT then the module uses its global value! Why?

I have a question about modules and local variables. Here's my example: h = 5; Module[{a, h}, a[h_]= h^2; a[4]] (*Out[2] = 25*) I expected the module to ...
2
votes
1answer
84 views

ControlType -> None Vs. Module inside Manipulate (i.e. making everything local)

I'm building a program on another one made available in the Wolfram Demonstration Project. I've noticed that in the original code (working flawlessly) many variables were made local inside the ...
15
votes
2answers
276 views

What is the purpose of Internal`LocalizedBlock?

I have come across the (internal) use of of the function: Internal`LocalizedBlock I am trying to determine its purpose. It seem to behave like ...
5
votes
1answer
93 views

Module does not remove a function definition

The following is a minor variant of the function uniqueTuples as given by Simon Woods in this answer. ...
8
votes
1answer
139 views

Temporary variable not removed in Mathematica 10

Bug introduced in 10.0.1 and fixed in 10.0.2 In Mathematica 10, the documentation still states that a symbol with attribute Temporary will automatically be removed when it is no longer needed. Now ...
5
votes
4answers
245 views

Replacing parts of a matrix

I'm a beginner to Mathematica and to writing scripts in general. I'm currently trying to write a code that will take any element in a matrix higher than a certain threshold value n and set it equal to ...
13
votes
0answers
189 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 ...
8
votes
1answer
131 views

How to assemble and disassemble a function into steps

I've watched many programmers (both novice and expert) write functions in Mathematica and have observed a common behavioral pattern. This pattern has many elements that would be automated and thus ...
2
votes
1answer
42 views

Substitution breaking the scope

A novice's problem, perhaps: it seems that /. substitution does weird things to the scope. ...
1
vote
2answers
84 views

Localised variable shows up as Global [duplicate]

Why does this local variable show up as Global? ?q Information::notfound: Symbol q not found. >> ...
-4
votes
2answers
90 views

Module function corrupts string? [closed]

I have the module who should replace some elements of a matrix with checkboxes ...
2
votes
1answer
55 views

How to protect function variables [duplicate]

I am sure this has been asked before, but I can't seem to find the right solution. I want a function to be the solution to a differential equation ...
4
votes
2answers
177 views

A better way of handling Module output?

I am sure this has been asked before in a way or another. I need to Save or DumpSave some symbols that contain the result of ...
1
vote
1answer
47 views

Global variales, Paralell computation and Limiting notebook's contex

If I limit the context of each notebook: and use parallel computation: ParallelTable[ToExpression["b" <> ToString[j]], {j, 1, 10}] then after ...
3
votes
1answer
60 views

Should “counters” used in Do, Table, … within Module be declared as local?

I have little to no understanding of how Mathematica handles scoping, so the question may seem a little bit funny. What is the preferred way: ...
0
votes
0answers
50 views

Where to put manipulate brackets for slider effect to refresh page

In order to move the plane of a best fit around to alter the sum of the residuals, I have decided to pick on one point and attach a slider to it, this adds a little variance and moves the table ...
0
votes
1answer
37 views

Fit inside a module [closed]

I have here a MMA NB which works fine outside the MODULE. BUt in the module routine there comes up an warning that the solution is not real. Can anyone here help please? ...
4
votes
2answers
280 views

Comparing 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 ...
22
votes
2answers
998 views

What are some advanced uses for Block?

I read the answers to this question (What are the use cases for different scoping constructs?) and this one (Condition, Block, Module - which way is the most memory and computationally efficient?). ...
1
vote
1answer
90 views

NDSolve inside Module with local variables

I want to use NDSolve within a Module and output the solution at some times. (Mathematica 10) All works fine with global variables (symbols) ...
3
votes
1answer
72 views

Defining a function that prints both the name and value of a variable defined locally within the calling function

To save time when debugging with Print statements, I'd like to define a function -- call it PrintVal -- that prints both the name and value of a variable defined locally within the calling function. ...
6
votes
1answer
233 views

A smarter nested With?

I often find myself writing code that looks a bit like this: ...
3
votes
1answer
81 views

Hiding body of a module or function in mathematica

Is it possible to hide the body of a module or function in MMA? For example I would like to turn this: myModule[]:=[{}, Body of module ] to: ...
1
vote
2answers
63 views

How do I localize an assignment to expression (instead of symbol) within scoping construct?

Is it possible to localize an assignment to an expression (defined via UpSet) instead of a Symbol within a scoping construct? ...
4
votes
3answers
160 views

Altering values in Manipulate with dynamically generated controls

I have a Manipulate whose controls are generated based on a parameter in a containing Manipulate: ...
1
vote
2answers
66 views

Assigning to the “local” version of a symbol

Compare Manipulate[c[1] = 1; c[1], {c[1], 0, 1}] with Manipulate[Do[c[i] = 1, {i, 1}]; c[1], {c[1], 0, 1}] The first case ...
13
votes
1answer
309 views

Is garbage collection broken in version 10.0.0?

Bug introduced in 10.0.0 and fixed in 10.0.1 In version 10.0.0 under Windows I am observing the following behavior: ...
8
votes
4answers
474 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 ...
4
votes
0answers
176 views

Exhaustive examples of variable replacement due to nested scoping constructs

Consider the following examples, where I have replaced dollar signs in the output by a D. ...
0
votes
2answers
69 views

Use conditional expression inside With

I want to assign a number to an internal variable, depending if a specific option has been given, but it seems to fail. Something like: ...
2
votes
1answer
111 views

Odd problem with Module

I've been having a strange problem with Module. Oddly it seems to be linked to my use of Import and then applying a function such as Flatten to my Import. I pasted images of my code and output ...