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

learn more… | top users | synonyms (1)

4
votes
1answer
143 views

Where does a package have to be loaded?

I'm trying to solve a very tricky bug in my program. It's tricky because it seems to be not working intermittently, and mostly not working when running with large data sets. The code itself is kind of ...
20
votes
1answer
510 views

Is the renaming mechanism of `With` flawed?

I'm confused about the renaming mechanism of With: ...
0
votes
2answers
42 views

Can you unpack a list of values into variables in a With expression?

For instance, if I am trying to draw a graph with 5 vertices labeled a through e, and I want to draw edges connecting a and b, a and c, a and d, a and e, b and c, b and d, and c and e, I would run the ...
0
votes
0answers
30 views

How to use a numerical integral as an input function

I am new to mathematica. I am trying to use a numerical indefinite integral as an input function of a module. I have a function $$r^* \equiv r^*(r)=\int_0^{1/r} B(z)dz$$ with $B(z)$ being a analytic ...
4
votes
1answer
67 views

Using previous result inside Module

I enjoy using "%" to capture the previous output as it allows me to split big tasks into chunks without having to introduce lots of intermediate variable names. Consider an overly simple example: <...
2
votes
0answers
59 views

How to use Compile? Bivariate Gibbs Sampler

I am trying to use compile to speed up my Gibbs sampler. I know I can use Reap and Sow to speed up tremendously. I have a more complicated Bayesian posterior that I want to speed up with compile so I ...
13
votes
3answers
199 views

Internal DynamicModule steals scope of external DynamicModule

I have two dynamic objects, one nested in the other. Both have their own functions defined, as follows. I have simplified them to the point where they don't make much sense, but can still illustrate ...
0
votes
1answer
33 views

How to turn a function's input argument into a new function

I want to write a function via a module that takes a user's input and creates a function from the input within the module. That is something like Input: ...
4
votes
1answer
78 views

Using delayed evaluation on module variables called through an association

I came up with a construct that works well enough for my current use case but has some flaws (at least I suspect it does). Say we have a function that calculates some stuff that takes a while to ...
8
votes
1answer
195 views

Why does the global variable exist here, where it was declared only in Module?

Why does Mathematica choose the mechanism that makes the global variable a here while parsing? In ...
11
votes
2answers
163 views

How to correctly implement in a new function the scoping behavior of Table, Sum and other commands that use Block to localize iterators?

It is documented that "Block is automatically used to localize values of iterators in iteration constructs such as Do, Sum, and Table." Therefore the dummy index (iterator) in a Sum is shielded ...
39
votes
1answer
1k views

Local variables in Module leak into the Global context

From the Mathematica documentation on Module: Module allows you to set up local variables with names that are local to the module. But after using ...
-3
votes
1answer
74 views

Animate Spring Pendelum

Is there ANYBODY who can solve this exercise??? Visualize the oscillation of a spring pendulum . Write to a function ( modules ) "spring mass" [ eg , nzigzag ] , which visualizes the spring ( Line ) ...
0
votes
1answer
46 views

Module, block or whatever to scope variables without explicitly listing them? [duplicate]

In larger notebooks I have often trouble with conflicting variable names. To combat this there are Modules and blocks. But with those you always have to list the variables you are using. I don't ...
0
votes
0answers
29 views

How to plot eigenvalues as a function of parameters in a dynamic module?

I am trying to make a fancy interactive plot that shows how the eigenvalues of a matrix mat change under variation of parameters ...
58
votes
6answers
2k 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 <...
7
votes
2answers
3k 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: ...
1
vote
1answer
55 views

Subscripted variables give errors in Module expression [closed]

I have following problem. I want the R not to calculate before the integration but substitute later as a function of z. My Mathematica codes look like: Codes like big, (sorry) but most of them ...
2
votes
2answers
80 views

Setting local variables with another function

I'm working on a package with a lot of functions using Module, which all begin by unpacking the arguments into local variables. I'd like to store this unpacking code in a separate function called ...
6
votes
2answers
314 views

What does empty {} mean in Module? [duplicate]

I have a question. I would like to know what an empty list ({}) means when used in Module[] as the code below shows? Why has a ...
4
votes
2answers
100 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? ...
1
vote
3answers
128 views

Multiple Plots inside a Manipulate with different dynamic variables on their x-axes

I have an expression with lots of variables, and I would like to make several plots of the expression, where the axis in each is a different variable. The idea is to put them side by side inside a <...
6
votes
1answer
162 views

Import another Notebook as if it were a Package

Is it possible to import the contents of another (existing) notebook into the current namespace in the same way that I can with a package? I know that it would be best to use a ...
2
votes
1answer
74 views

Module vs. DynamicModule in a custom plot function [closed]

In my Mathematica work flow I move often used functions into a custom package (.m file) at some point. Since I like the features of the CustomTicks package, I have a collection of custom plot ...
1
vote
2answers
41 views

How do I Define a Module Which Displays Some Graphics and Returns Some Data?

I want to bundle some actions of a task in a module to call it a few times. The task shall display some Graphics and it shall return some data (as a list). But I can't do that (it would have been ...
2
votes
1answer
70 views

Code optimizations with functions inside Module

I wrote a small code by using functions inside Module: ...
3
votes
0answers
175 views

Is Mathematica's lexical scoping broken?

Why do these two lines output different things (b and 0, respectively)? Is it a bug? ...
1
vote
1answer
119 views

Is there any way to implement a “sequential” With[] in Mathematica? [duplicate]

I want the equivalent of Scheme's let*, or basically, a sequential With that works like this: ...
1
vote
1answer
40 views

How do I convert an expression that is a function of some variables into a functional?

If I have an arbitrary f[x, y, z], how do I convert it to f[#1, #2, #3] & without messing up scopes or causing scope leakage?...
8
votes
0answers
76 views

What is the equivalent of $ModuleNumber for DynamicModule

Mathematica provides access to $ModuleNumber which can be enormously useful. Unfortunately I can't find the equivalent for ...
149
votes
7answers
7k 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: ...
1
vote
1answer
49 views

How to write a function in the divide-and-conquer style [closed]

I'd like to vary a factor in a matrix calculation, so I have used Module to make it happened, but it doesn't work. Below is a code example. can someone tell me how ...
1
vote
0answers
50 views

ParallelTable gets slower when rerun in a module

Question: Why does the performance of ParallelTable within a Module drop so horribly when the module is RERUN several times? ...
0
votes
1answer
56 views

why URLFetch is slow when encapsulated by module and how to fix it [closed]

I'm using Mathematica 10.0.2 on Win7. I'm writing some program to crawl etymology of words on http://www.etymonline.com/. Below lies the example code: ...
1
vote
1answer
52 views

Declaring functions in NDSolve? [duplicate]

I have a simple enough series of coupled ODES, which is relatively easy to solve. It looks something like this. ...
11
votes
4answers
911 views

Subscripts - 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 ...
1
vote
2answers
81 views

Evaluating While Loops inside Modules [duplicate]

I am trying to compute some objects inside while loops within modules, but I keep getting the following error: ...
3
votes
2answers
102 views

Problem with PlotLegends in Manipulate

Please consider this simple code: Manipulate[ Plot[{a Sin[x], a Cos[x]}, {x, 0, 2 Pi}, PlotLegends -> "Expressions"], {a, 1, 2}] Mathematica returns this ...
3
votes
2answers
131 views

Plotting a function variable vs. an explicit function

Can someone tell me why this doesn't plot/animate? ...
1
vote
2answers
71 views

Dynamic Updating

I want to take some points, place them onto a graph and then use the locator pane. I then want to take the new locations that the user has moved the points too and remove the y coordinate and use just ...
1
vote
0answers
35 views

Using DynamicModule and Locator with an external Module [closed]

I wish to call an external Module, called PCA inside a DynamicModule. Essentially, this ...
3
votes
4answers
102 views

Working with ScheduledTasks inside DynamicModules

I have a question - is it possible to run ScheduleTask[] inside DynamicModule[]? What I have tried so far: ...
22
votes
3answers
486 views
19
votes
0answers
261 views

Compile not correctly initializing a variable defined inside Module

Bug introduced in 8.0 and persisting through 10.3.1 or later In the following example, inside the minimumX's Module, ...
3
votes
1answer
88 views

How to inject segments of code into functions defined by SetDelayed [duplicate]

I need to define a set of functions that are written in terms of common but lengthy expressions. To effect this, I want to inject abbreviations for such expressions into the RHS of ...
12
votes
0answers
127 views

Integrate - Why is the integration variable not local

I have noticed something odd: Let f[t_]:=Integrate[x, {x, 0, t}] h[t_]:=Sum[x, {x, 0, t}] Now, enter ...
14
votes
3answers
271 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 ...
5
votes
1answer
66 views

How to solve an equation system whose equations are based on Mod[x,y]

I'm trying to find the parameters of a specific linear congruential generator (pseudo random number generator) based on a list of the first five numbers generated by it. The list is this one: ...
4
votes
2answers
153 views

How to take derivative of a interpolated function inside the module

When handling derivatives of a interpolated function, I often use the following function makeDeriv[f_InterpolatingFunction] = D[ f[x], x] Then, I can define ...
4
votes
2answers
103 views

How can I pass arguments to Module without being reinterpreted each time they appear inside it?

I'm trying to understand how to save time when calling functions in Mathematica. I wrote this example, ...